Skip to content

Commit efd53de

Browse files
committed
chore: Fix some EmmyLua annotations.
1 parent aaa04b9 commit efd53de

File tree

2 files changed

+47
-58
lines changed

2 files changed

+47
-58
lines changed

lua/winshift/lib.lua

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ local api = vim.api
44
local M = {}
55
local win_option_store = {}
66

7-
---@class Node
8-
---@type table<integer, Node>
7+
---@class Node : { [integer]: Node }
98
---@field type '"leaf"'|'"row"'|'"col"'
109
---@field parent Node
1110
---@field index integer
@@ -142,7 +141,7 @@ end
142141
---Move a row into a target window, replacing the target.
143142
---@param row Node
144143
---@param target integer Window id
145-
---@param ignore table<integer, boolean>
144+
---@param ignore? table<integer, boolean>
146145
function M.move_row(row, target, ignore)
147146
ignore = ignore or {}
148147
local opt = { vertical = true, rightbelow = false }
@@ -164,7 +163,7 @@ end
164163
---Move a column into a target window, replacing the target.
165164
---@param col Node
166165
---@param target integer Window id
167-
---@param ignore table<integer, boolean>
166+
---@param ignore? table<integer, boolean>
168167
function M.move_col(col, target, ignore)
169168
ignore = ignore or {}
170169
local opt = { vertical = false, rightbelow = false }
@@ -391,7 +390,7 @@ function M.pick_window(ignore)
391390
end
392391

393392
---@param leaf Node
394-
---@param flatten boolean
393+
---@param flatten? boolean
395394
---@return VirtualNode|nil
396395
function M.create_virtual_set(leaf, flatten)
397396
if not leaf.parent then
@@ -628,7 +627,7 @@ end
628627
function M.restore_win_options(winid)
629628
for option, _ in pairs(config.get_config().moving_win_options) do
630629
if win_option_store[winid][option] then
631-
utils.set_local(winid, option, win_option_store[winid][option])
630+
utils.set_local(winid, { [option] = win_option_store[winid][option] })
632631
else
633632
utils.unset_local(winid, option)
634633
end

lua/winshift/utils.lua

Lines changed: 42 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,17 @@ function M.err(msg, schedule)
4343
M._echo_multiline("[WinShift.nvim] " .. msg, "ErrorMsg", schedule)
4444
end
4545

46-
function M.no_win_event_call(cb)
47-
local last = vim.opt.eventignore._value
48-
vim.opt.eventignore = (
49-
"WinEnter,WinLeave,WinNew,WinClosed,BufEnter,BufLeave"
50-
.. (last ~= "" and "," .. last or "")
51-
)
52-
local ok, err = pcall(cb)
46+
---Call the function `f`, ignoring most of the window and buffer related
47+
---events. The function is called in protected mode.
48+
---@param f function
49+
---@return boolean success
50+
---@return any result Return value
51+
function M.no_win_event_call(f)
52+
local last = vim.o.eventignore
53+
vim.opt.eventignore:prepend(
54+
"WinEnter,WinLeave,WinNew,WinClosed,BufWinEnter,BufWinLeave,BufEnter,BufLeave"
55+
)
56+
local ok, err = pcall(f)
5357
vim.opt.eventignore = last
5458
return ok, err
5559
end
@@ -259,59 +263,47 @@ function M.pause(msg)
259263
)
260264
end
261265

266+
---@class SetLocalSpec
267+
---@field method '"set"'|'"remove"'|'"append"'|'"prepend"' Assignment method. (default: "set")
268+
269+
---@class SetLocalListSpec : string[]
270+
---@field opt SetLocalSpec
271+
262272
---HACK: workaround for inconsistent behavior from `vim.opt_local`.
263273
---@see [Neovim issue](https://github.com/neovim/neovim/issues/14670)
264274
---@param winids number[]|number Either a list of winids, or a single winid (0 for current window).
265-
---`opt` fields:
266-
--- @tfield method '"set"'|'"remove"'|'"append"'|'"prepend"' Assignment method. (default: "set")
267-
---@overload fun(winids: number[]|number, option: string, value: string[]|string|boolean, opt?: any)
268-
---@overload fun(winids: number[]|number, map: table<string, string[]|string|boolean>, opt?: table)
269-
function M.set_local(winids, x, y, z)
275+
---@param option_map table<string, SetLocalListSpec|string|boolean>
276+
---@param opt? SetLocalSpec
277+
function M.set_local(winids, option_map, opt)
270278
if type(winids) ~= "table" then
271279
winids = { winids }
272280
end
273281

274-
local map, opt
275-
if y == nil or type(y) == "table" then
276-
map = x
277-
opt = y
278-
else
279-
map = { [x] = y }
280-
opt = z
281-
end
282-
283282
opt = vim.tbl_extend("keep", opt or {}, { method = "set" })
284283

285284
local cmd
286-
local ok, err = M.no_win_event_call(function()
287-
for _, id in ipairs(winids) do
288-
api.nvim_win_call(id, function()
289-
for option, value in pairs(map) do
285+
for _, id in ipairs(winids) do
286+
api.nvim_win_call(id, function()
287+
for option, value in pairs(option_map) do
288+
if type(value) == "boolean" then
289+
cmd = string.format("setl %s%s", value and "" or "no", option)
290+
else
291+
---@type SetLocalSpec
290292
local o = opt
291-
292-
if type(value) == "boolean" then
293-
cmd = string.format("setl %s%s", value and "" or "no", option)
294-
else
295-
if type(value) == "table" then
296-
---@diagnostic disable-next-line: undefined-field
297-
o = vim.tbl_extend("force", opt, value.opt or {})
298-
value = table.concat(value, ",")
299-
end
300-
301-
cmd = M.str_template(
302-
setlocal_opr_templates[o.method],
303-
{ option = option, value = tostring(value):gsub("'", "''") }
304-
)
293+
if type(value) == "table" then
294+
o = vim.tbl_extend("force", opt, value.opt or {})
295+
value = table.concat(value, ",")
305296
end
306297

307-
vim.cmd(cmd)
298+
cmd = M.str_template(
299+
setlocal_opr_templates[o.method],
300+
{ option = option, value = tostring(value):gsub("'", "''") }
301+
)
308302
end
309-
end)
310-
end
311-
end)
312303

313-
if not ok then
314-
error(err)
304+
vim.cmd(cmd)
305+
end
306+
end)
315307
end
316308
end
317309

@@ -322,13 +314,11 @@ function M.unset_local(winids, option)
322314
winids = { winids }
323315
end
324316

325-
M.no_win_event_call(function()
326-
for _, id in ipairs(winids) do
327-
api.nvim_win_call(id, function()
328-
vim.cmd(string.format("set %s<", option))
329-
end)
330-
end
331-
end)
317+
for _, id in ipairs(winids) do
318+
api.nvim_win_call(id, function()
319+
vim.cmd(string.format("set %s<", option))
320+
end)
321+
end
332322
end
333323

334324
return M

0 commit comments

Comments
 (0)