-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathinit.lua
370 lines (294 loc) · 9.24 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
local Object = require("nui.object")
local buf_storage = require("nui.utils.buf_storage")
local autocmd = require("nui.utils.autocmd")
local keymap = require("nui.utils.keymap")
local utils = require("nui.utils")
local split_utils = require("nui.split.utils")
local u = {
clear_namespace = utils._.clear_namespace,
get_next_id = utils._.get_next_id,
normalize_namespace_id = utils._.normalize_namespace_id,
split = split_utils,
}
local split_direction_command_map = {
editor = {
top = "topleft",
right = "vertical botright",
bottom = "botright",
left = "vertical topleft",
},
win = {
top = "aboveleft",
right = "vertical rightbelow",
bottom = "belowright",
left = "vertical leftabove",
},
}
---@param winid integer
---@param win_config _nui_split_internal_win_config
local function move_split_window(winid, win_config)
if win_config.relative == "editor" then
vim.api.nvim_win_call(winid, function()
vim.cmd("wincmd " .. ({ top = "K", right = "L", bottom = "J", left = "H" })[win_config.position])
end)
elseif win_config.relative == "win" then
local move_options = {
vertical = win_config.position == "left" or win_config.position == "right",
rightbelow = win_config.position == "bottom" or win_config.position == "right",
}
vim.cmd(
string.format(
"noautocmd call win_splitmove(%s, %s, #{ vertical: %s, rightbelow: %s })",
winid,
win_config.win,
move_options.vertical and 1 or 0,
move_options.rightbelow and 1 or 0
)
)
end
end
---@param winid integer
---@param win_config _nui_split_internal_win_config
local function set_win_config(winid, win_config)
if win_config.pending_changes.position then
move_split_window(winid, win_config)
end
if win_config.pending_changes.size then
if win_config.width then
vim.api.nvim_win_set_width(winid, win_config.width)
elseif win_config.height then
vim.api.nvim_win_set_height(winid, win_config.height)
end
end
win_config.pending_changes = {}
end
--luacheck: push no max line length
---@alias nui_split_option_relative_type 'editor'|'win'
---@alias nui_split_option_relative { type: nui_split_option_relative_type, win?: number }
---@alias nui_split_option_position "'top'"|"'right'"|"'bottom'"|"'left'"
---@alias nui_split_option_size { height?: number|string }|{ width?: number|string }
---@alias _nui_split_internal_win_config { height?: number, width?: number, position: nui_split_option_position, relative: nui_split_option_relative, win?: integer, pending_changes: table<'position'|'size', boolean> }
--luacheck: pop
---@class nui_split_internal
---@field enter? boolean
---@field loading boolean
---@field mounted boolean
---@field buf_options table<string, any>
---@field win_options table<string, any>
---@field position nui_split_option_position
---@field relative nui_split_option_relative
---@field size { height?: number }|{ width?: number }
---@field win_config _nui_split_internal_win_config
---@field pending_quit? boolean
---@field augroup table<'hide'|'unmount', string>
---@class nui_split_options
---@field ns_id? string|integer
---@field relative? nui_split_option_relative_type|nui_split_option_relative
---@field position? nui_split_option_position
---@field size? number|string|nui_split_option_size
---@field enter? boolean
---@field buf_options? table<string, any>
---@field win_options? table<string, any>
---@class NuiSplit
---@field private _ nui_split_internal
---@field bufnr integer
---@field ns_id integer
---@field winid number
local Split = Object("NuiSplit")
---@param options nui_split_options
function Split:init(options)
local id = u.get_next_id()
options = u.split.merge_default_options(options)
options = u.split.normalize_options(options)
self._ = {
id = id,
enter = options.enter,
buf_options = options.buf_options,
loading = false,
mounted = false,
layout = {},
position = options.position,
size = {},
win_options = options.win_options,
win_config = {
pending_changes = {},
},
augroup = {
hide = string.format("%s_hide", id),
unmount = string.format("%s_unmount", id),
},
}
self.ns_id = u.normalize_namespace_id(options.ns_id)
self:_buf_create()
self:update_layout(options)
end
--luacheck: push no max line length
---@param config { relative?: nui_split_option_relative_type|nui_split_option_relative, position?: nui_split_option_position, size?: number|string|nui_split_option_size }
function Split:update_layout(config)
config = config or {}
u.split.update_layout_config(self._, config)
if self.winid then
set_win_config(self.winid, self._.win_config)
end
end
--luacheck: pop
function Split:_open_window()
if self.winid or not self.bufnr then
return
end
self.winid = vim.api.nvim_win_call(self._.relative.win, function()
vim.api.nvim_command(
string.format(
"silent noswapfile %s %ssplit",
split_direction_command_map[self._.relative.type][self._.position],
self._.size.width or self._.size.height or ""
)
)
return vim.api.nvim_get_current_win()
end)
vim.api.nvim_win_set_buf(self.winid, self.bufnr)
if self._.enter then
vim.api.nvim_set_current_win(self.winid)
end
self._.win_config.pending_changes = { size = true }
set_win_config(self.winid, self._.win_config)
utils._.set_win_options(self.winid, self._.win_options)
end
function Split:_close_window()
if not self.winid then
return
end
if vim.api.nvim_win_is_valid(self.winid) and not self._.pending_quit then
vim.api.nvim_win_close(self.winid, true)
end
self.winid = nil
end
function Split:_buf_create()
if not self.bufnr then
self.bufnr = vim.api.nvim_create_buf(false, true)
assert(self.bufnr, "failed to create buffer")
end
end
function Split:mount()
if self._.loading or self._.mounted then
return
end
self._.loading = true
autocmd.create_group(self._.augroup.hide, { clear = true })
autocmd.create_group(self._.augroup.unmount, { clear = true })
autocmd.create("QuitPre", {
group = self._.augroup.unmount,
buffer = self.bufnr,
callback = function()
self._.pending_quit = true
vim.schedule(function()
self:unmount()
self._.pending_quit = nil
end)
end,
}, self.bufnr)
autocmd.create("BufWinEnter", {
group = self._.augroup.unmount,
buffer = self.bufnr,
callback = function()
autocmd.create("WinClosed", {
group = self._.augroup.hide,
pattern = tostring(self.winid),
callback = function()
self:hide()
end,
}, self.bufnr)
end,
}, self.bufnr)
self:_buf_create()
utils._.set_buf_options(self.bufnr, self._.buf_options)
self:_open_window()
self._.loading = false
self._.mounted = true
end
function Split:hide()
if self._.loading or not self._.mounted then
return
end
self._.loading = true
pcall(autocmd.delete_group, self._.augroup.hide)
self:_close_window()
self._.loading = false
end
function Split:show()
if self._.loading then
return
end
if not self._.mounted then
return self:mount()
end
self._.loading = true
autocmd.create_group(self._.augroup.hide, { clear = true })
self:_open_window()
self._.loading = false
end
function Split:_buf_destroy()
if self.bufnr then
if vim.api.nvim_buf_is_valid(self.bufnr) then
u.clear_namespace(self.bufnr, self.ns_id)
if not self._.pending_quit then
vim.api.nvim_buf_delete(self.bufnr, { force = true })
end
end
buf_storage.cleanup(self.bufnr)
self.bufnr = nil
end
end
function Split:unmount()
if self._.loading or not self._.mounted then
return
end
self._.loading = true
pcall(autocmd.delete_group, self._.augroup.hide)
pcall(autocmd.delete_group, self._.augroup.unmount)
self:_buf_destroy()
self:_close_window()
self._.loading = false
self._.mounted = false
end
-- set keymap for this split
---@param mode string check `:h :map-modes`
---@param key string|string[] key for the mapping
---@param handler string | fun(): nil handler for the mapping
---@param opts? table<"'expr'"|"'noremap'"|"'nowait'"|"'remap'"|"'script'"|"'silent'"|"'unique'", boolean>
---@return nil
function Split:map(mode, key, handler, opts, ___force___)
if not self.bufnr then
error("split buffer not found.")
end
return keymap.set(self.bufnr, mode, key, handler, opts, ___force___)
end
---@param mode string check `:h :map-modes`
---@param key string|string[] key for the mapping
---@return nil
function Split:unmap(mode, key)
if not self.bufnr then
error("split buffer not found.")
end
return keymap._del(self.bufnr, mode, key)
end
---@param event string | string[]
---@param handler string | function
---@param options? table<"'once'" | "'nested'", boolean>
function Split:on(event, handler, options)
if not self.bufnr then
error("split buffer not found.")
end
autocmd.buf.define(self.bufnr, event, handler, options)
end
---@param event? string | string[]
function Split:off(event)
if not self.bufnr then
error("split buffer not found.")
end
autocmd.buf.remove(self.bufnr, nil, event)
end
---@alias NuiSplit.constructor fun(options: nui_split_options): NuiSplit
---@type NuiSplit|NuiSplit.constructor
local NuiSplit = Split
return NuiSplit