Skip to content

Commit 13f967f

Browse files
chore: add type annotations and resolve LSP warnings (#2555)
* chore: add type annotations to (almost) all functions * stylua * Add classes for symlink nodes * Replace deprecated `@vararg` * Move node classes to `node` module * Fix `Symlink*` classes * add vim and libuv runtime for luals, qualify libuv types * add scripts/luals-check, not quite ready for CI * additional nil checks for git/init.lua and git/runner.lua * additional nil checks for nvim-tree.lua * wrap vim.cmd-as-a-function calls inside functions * vim.tbl_filter predicate returns booleans * Revert "add scripts/luals-check, not quite ready for CI" This reverts commit c70229c. * Add `MinimalNode` class in `marks` module * Fix various LSP warnings * stylua * Fix `Explorer` class, update related annotations and add necessary checks * Add missing annotations to `live-filter` * Add temporary aliases for `uv.*` types * Resolve remaining LSP warnings * Revert changes not related to internal types * Minor adjustments * Update doc comments style * Minor adjustments (pt. 2) --------- Co-authored-by: Alexander Courtis <[email protected]>
1 parent 7d1760f commit 13f967f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+623
-162
lines changed

lua/nvim-tree.lua

+22-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ local M = {
2323
}
2424

2525
--- Update the tree root to a directory or the directory containing
26-
--- @param path string relative or absolute
27-
--- @param bufnr number|nil
26+
---@param path string relative or absolute
27+
---@param bufnr number|nil
2828
function M.change_root(path, bufnr)
2929
-- skip if current file is in ignore_list
3030
if type(bufnr) == "number" then
@@ -42,6 +42,10 @@ function M.change_root(path, bufnr)
4242
end
4343

4444
local cwd = core.get_cwd()
45+
if cwd == nil then
46+
return
47+
end
48+
4549
local vim_cwd = vim.fn.getcwd()
4650

4751
-- test if in vim_cwd
@@ -73,6 +77,7 @@ function M.change_root(path, bufnr)
7377
change_dir.fn(vim.fn.fnamemodify(path, ":p:h"))
7478
end
7579

80+
---@param cwd string|nil
7681
function M.open_replacing_current_buffer(cwd)
7782
if view.is_visible() then
7883
return
@@ -153,10 +158,13 @@ function M.place_cursor_on_node()
153158
end
154159
end
155160

161+
---@return table
156162
function M.get_config()
157163
return M.config
158164
end
159165

166+
---@param disable_netrw boolean
167+
---@param hijack_netrw boolean
160168
local function manage_netrw(disable_netrw, hijack_netrw)
161169
if hijack_netrw then
162170
vim.cmd "silent! autocmd! FileExplorer *"
@@ -168,14 +176,18 @@ local function manage_netrw(disable_netrw, hijack_netrw)
168176
end
169177
end
170178

179+
---@param name string|nil
171180
function M.change_dir(name)
172-
change_dir.fn(name)
181+
if name then
182+
change_dir.fn(name)
183+
end
173184

174185
if _config.update_focused_file.enable then
175186
find_file.fn()
176187
end
177188
end
178189

190+
---@param opts table
179191
local function setup_autocommands(opts)
180192
local augroup_id = vim.api.nvim_create_augroup("NvimTree", { clear = true })
181193
local function create_nvim_tree_autocmd(name, custom_opts)
@@ -671,9 +683,15 @@ local ACCEPTED_STRINGS = {
671683
},
672684
}
673685

686+
---@param conf table|nil
674687
local function validate_options(conf)
675688
local msg
676689

690+
---@param user any
691+
---@param def any
692+
---@param strs table
693+
---@param types table
694+
---@param prefix string
677695
local function validate(user, def, strs, types, prefix)
678696
-- if user's option is not a table there is nothing to do
679697
if type(user) ~= "table" then
@@ -756,6 +774,7 @@ function M.purge_all_state()
756774
end
757775
end
758776

777+
---@param conf table|nil
759778
function M.setup(conf)
760779
if vim.fn.has "nvim-0.8" == 0 then
761780
notify.warn "nvim-tree.lua requires Neovim 0.8 or higher"

lua/nvim-tree/actions/finders/search-node.lua

+5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ local find_file = require("nvim-tree.actions.finders.find-file").fn
44

55
local M = {}
66

7+
---@param search_dir string|nil
8+
---@param input_path string
9+
---@return string|nil
710
local function search(search_dir, input_path)
811
local realpaths_searched = {}
912

1013
if not search_dir then
1114
return
1215
end
1316

17+
---@param dir string
18+
---@return string|nil
1419
local function iter(dir)
1520
local realpath, path, name, stat, handle, _
1621

lua/nvim-tree/actions/fs/copy-paste.lua

+48-15
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ local clipboard = {
2020
copy = {},
2121
}
2222

23+
---@param source string
24+
---@param destination string
25+
---@return boolean
26+
---@return string|nil
2327
local function do_copy(source, destination)
2428
local source_stats, handle
2529
local success, errmsg
@@ -81,6 +85,12 @@ local function do_copy(source, destination)
8185
return true
8286
end
8387

88+
---@param source string
89+
---@param dest string
90+
---@param action_type string
91+
---@param action_fn fun(source: string, dest: string)
92+
---@return boolean|nil -- success
93+
---@return string|nil -- error message
8494
local function do_single_paste(source, dest, action_type, action_fn)
8595
local dest_stats
8696
local success, errmsg, errcode
@@ -140,14 +150,17 @@ local function do_single_paste(source, dest, action_type, action_fn)
140150
end
141151
end
142152

153+
---@param node Node
154+
---@param clip table
143155
local function toggle(node, clip)
144156
if node.name == ".." then
145157
return
146158
end
147159
local notify_node = notify.render_path(node.absolute_path)
148160

149161
if utils.array_remove(clip, node) then
150-
return notify.info(notify_node .. " removed from clipboard.")
162+
notify.info(notify_node .. " removed from clipboard.")
163+
return
151164
end
152165

153166
table.insert(clip, node)
@@ -161,22 +174,28 @@ function M.clear_clipboard()
161174
renderer.draw()
162175
end
163176

177+
---@param node Node
164178
function M.copy(node)
165179
utils.array_remove(clipboard.cut, node)
166180
toggle(node, clipboard.copy)
167181
renderer.draw()
168182
end
169183

184+
---@param node Node
170185
function M.cut(node)
171186
utils.array_remove(clipboard.copy, node)
172187
toggle(node, clipboard.cut)
173188
renderer.draw()
174189
end
175190

191+
---@param node Node
192+
---@param action_type string
193+
---@param action_fn fun(source: string, dest: string)
176194
local function do_paste(node, action_type, action_fn)
177195
node = lib.get_last_group_node(node)
178-
if node.name == ".." then
179-
node = core.get_explorer()
196+
local explorer = core.get_explorer()
197+
if node.name == ".." and explorer then
198+
node = explorer
180199
end
181200
local clip = clipboard[action_type]
182201
if #clip == 0 then
@@ -202,10 +221,14 @@ local function do_paste(node, action_type, action_fn)
202221

203222
clipboard[action_type] = {}
204223
if not M.config.filesystem_watchers.enable then
205-
return reloaders.reload_explorer()
224+
reloaders.reload_explorer()
206225
end
207226
end
208227

228+
---@param source string
229+
---@param destination string
230+
---@return boolean
231+
---@return string?
209232
local function do_cut(source, destination)
210233
log.line("copy_paste", "do_cut '%s' -> '%s'", source, destination)
211234

@@ -225,12 +248,13 @@ local function do_cut(source, destination)
225248
return true
226249
end
227250

251+
---@param node Node
228252
function M.paste(node)
229253
if clipboard.cut[1] ~= nil then
230-
return do_paste(node, "cut", do_cut)
254+
do_paste(node, "cut", do_cut)
255+
else
256+
do_paste(node, "copy", do_copy)
231257
end
232-
233-
return do_paste(node, "copy", do_copy)
234258
end
235259

236260
function M.print_clipboard()
@@ -248,9 +272,10 @@ function M.print_clipboard()
248272
end
249273
end
250274

251-
return notify.info(table.concat(content, "\n") .. "\n")
275+
notify.info(table.concat(content, "\n") .. "\n")
252276
end
253277

278+
---@param content string
254279
local function copy_to_clipboard(content)
255280
local clipboard_name
256281
if M.config.actions.use_system_clipboard == true then
@@ -264,28 +289,36 @@ local function copy_to_clipboard(content)
264289
end
265290

266291
vim.api.nvim_exec_autocmds("TextYankPost", {})
267-
return notify.info(string.format("Copied %s to %s clipboard!", content, clipboard_name))
292+
notify.info(string.format("Copied %s to %s clipboard!", content, clipboard_name))
268293
end
269294

295+
---@param node Node
270296
function M.copy_filename(node)
271-
return copy_to_clipboard(node.name)
297+
copy_to_clipboard(node.name)
272298
end
273299

300+
---@param node Node
274301
function M.copy_path(node)
275302
local absolute_path = node.absolute_path
276-
local relative_path = utils.path_relative(absolute_path, core.get_cwd())
303+
local cwd = core.get_cwd()
304+
if cwd == nil then
305+
return
306+
end
307+
308+
local relative_path = utils.path_relative(absolute_path, cwd)
277309
local content = node.nodes ~= nil and utils.path_add_trailing(relative_path) or relative_path
278-
return copy_to_clipboard(content)
310+
copy_to_clipboard(content)
279311
end
280312

313+
---@param node Node
281314
function M.copy_absolute_path(node)
282315
local absolute_path = node.absolute_path
283316
local content = node.nodes ~= nil and utils.path_add_trailing(absolute_path) or absolute_path
284-
return copy_to_clipboard(content)
317+
copy_to_clipboard(content)
285318
end
286319

287-
---Clipboard text highlight group and position when highlight_clipboard.
288-
---@param node table
320+
--- Clipboard text highlight group and position when highlight_clipboard.
321+
---@param node Node
289322
---@return HL_POSITION position none when clipboard empty
290323
---@return string|nil group only when node present in clipboard
291324
function M.get_highlight(node)

lua/nvim-tree/actions/fs/create-file.lua

+13-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local find_file = require("nvim-tree.actions.finders.find-file").fn
88

99
local M = {}
1010

11+
---@param file string
1112
local function create_and_notify(file)
1213
events._dispatch_will_create_file(file)
1314
local ok, fd = pcall(vim.loop.fs_open, file, "w", 420)
@@ -19,6 +20,8 @@ local function create_and_notify(file)
1920
events._dispatch_file_created(file)
2021
end
2122

23+
---@param iter function iterable
24+
---@return integer
2225
local function get_num_nodes(iter)
2326
local i = 0
2427
for _ in iter do
@@ -27,6 +30,8 @@ local function get_num_nodes(iter)
2730
return i
2831
end
2932

33+
---@param node Node
34+
---@return string
3035
local function get_containing_folder(node)
3136
if node.nodes ~= nil then
3237
return utils.path_add_trailing(node.absolute_path)
@@ -35,11 +40,18 @@ local function get_containing_folder(node)
3540
return node.absolute_path:sub(0, -node_name_size - 1)
3641
end
3742

43+
---@param node Node|nil
3844
function M.fn(node)
45+
local cwd = core.get_cwd()
46+
if cwd == nil then
47+
return
48+
end
49+
3950
node = node and lib.get_last_group_node(node)
4051
if not node or node.name == ".." then
4152
node = {
42-
absolute_path = core.get_cwd(),
53+
absolute_path = cwd,
54+
name = "",
4355
nodes = core.get_explorer().nodes,
4456
open = true,
4557
}

lua/nvim-tree/actions/fs/remove-file.lua

+12-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local M = {
88
config = {},
99
}
1010

11+
---@param windows integer[]
1112
local function close_windows(windows)
1213
-- Prevent from closing when the win count equals 1 or 2,
1314
-- where the win to remove could be the last opened.
@@ -23,6 +24,7 @@ local function close_windows(windows)
2324
end
2425
end
2526

27+
---@param absolute_path string
2628
local function clear_buffer(absolute_path)
2729
local bufs = vim.fn.getbufinfo { bufloaded = 1, buflisted = 1 }
2830
for _, buf in pairs(bufs) do
@@ -44,10 +46,13 @@ local function clear_buffer(absolute_path)
4446
end
4547
end
4648

49+
---@param cwd string
50+
---@return boolean|nil
4751
local function remove_dir(cwd)
4852
local handle = vim.loop.fs_scandir(cwd)
4953
if type(handle) == "string" then
50-
return notify.error(handle)
54+
notify.error(handle)
55+
return
5156
end
5257

5358
while true do
@@ -75,27 +80,30 @@ local function remove_dir(cwd)
7580
end
7681

7782
--- Remove a node, notify errors, dispatch events
78-
--- @param node table
83+
---@param node Node
7984
function M.remove(node)
8085
local notify_node = notify.render_path(node.absolute_path)
8186
if node.nodes ~= nil and not node.link_to then
8287
local success = remove_dir(node.absolute_path)
8388
if not success then
84-
return notify.error("Could not remove " .. notify_node)
89+
notify.error("Could not remove " .. notify_node)
90+
return
8591
end
8692
events._dispatch_folder_removed(node.absolute_path)
8793
else
8894
events._dispatch_will_remove_file(node.absolute_path)
8995
local success = vim.loop.fs_unlink(node.absolute_path)
9096
if not success then
91-
return notify.error("Could not remove " .. notify_node)
97+
notify.error("Could not remove " .. notify_node)
98+
return
9299
end
93100
events._dispatch_file_removed(node.absolute_path)
94101
clear_buffer(node.absolute_path)
95102
end
96103
notify.info(notify_node .. " was properly removed.")
97104
end
98105

106+
---@param node Node
99107
function M.fn(node)
100108
if node.name == ".." then
101109
return

0 commit comments

Comments
 (0)