Skip to content

Commit

Permalink
chore: add type annotations and resolve LSP warnings (#2555)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
Akmadan23 and alex-courtis authored Dec 9, 2023
1 parent 7d1760f commit 13f967f
Show file tree
Hide file tree
Showing 51 changed files with 623 additions and 162 deletions.
25 changes: 22 additions & 3 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ local M = {
}

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

local cwd = core.get_cwd()
if cwd == nil then
return
end

local vim_cwd = vim.fn.getcwd()

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

---@param cwd string|nil
function M.open_replacing_current_buffer(cwd)
if view.is_visible() then
return
Expand Down Expand Up @@ -153,10 +158,13 @@ function M.place_cursor_on_node()
end
end

---@return table
function M.get_config()
return M.config
end

---@param disable_netrw boolean
---@param hijack_netrw boolean
local function manage_netrw(disable_netrw, hijack_netrw)
if hijack_netrw then
vim.cmd "silent! autocmd! FileExplorer *"
Expand All @@ -168,14 +176,18 @@ local function manage_netrw(disable_netrw, hijack_netrw)
end
end

---@param name string|nil
function M.change_dir(name)
change_dir.fn(name)
if name then
change_dir.fn(name)
end

if _config.update_focused_file.enable then
find_file.fn()
end
end

---@param opts table
local function setup_autocommands(opts)
local augroup_id = vim.api.nvim_create_augroup("NvimTree", { clear = true })
local function create_nvim_tree_autocmd(name, custom_opts)
Expand Down Expand Up @@ -671,9 +683,15 @@ local ACCEPTED_STRINGS = {
},
}

---@param conf table|nil
local function validate_options(conf)
local msg

---@param user any
---@param def any
---@param strs table
---@param types table
---@param prefix string
local function validate(user, def, strs, types, prefix)
-- if user's option is not a table there is nothing to do
if type(user) ~= "table" then
Expand Down Expand Up @@ -756,6 +774,7 @@ function M.purge_all_state()
end
end

---@param conf table|nil
function M.setup(conf)
if vim.fn.has "nvim-0.8" == 0 then
notify.warn "nvim-tree.lua requires Neovim 0.8 or higher"
Expand Down
5 changes: 5 additions & 0 deletions lua/nvim-tree/actions/finders/search-node.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ local find_file = require("nvim-tree.actions.finders.find-file").fn

local M = {}

---@param search_dir string|nil
---@param input_path string
---@return string|nil
local function search(search_dir, input_path)
local realpaths_searched = {}

if not search_dir then
return
end

---@param dir string
---@return string|nil
local function iter(dir)
local realpath, path, name, stat, handle, _

Expand Down
63 changes: 48 additions & 15 deletions lua/nvim-tree/actions/fs/copy-paste.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ local clipboard = {
copy = {},
}

---@param source string
---@param destination string
---@return boolean
---@return string|nil
local function do_copy(source, destination)
local source_stats, handle
local success, errmsg
Expand Down Expand Up @@ -81,6 +85,12 @@ local function do_copy(source, destination)
return true
end

---@param source string
---@param dest string
---@param action_type string
---@param action_fn fun(source: string, dest: string)
---@return boolean|nil -- success
---@return string|nil -- error message
local function do_single_paste(source, dest, action_type, action_fn)
local dest_stats
local success, errmsg, errcode
Expand Down Expand Up @@ -140,14 +150,17 @@ local function do_single_paste(source, dest, action_type, action_fn)
end
end

---@param node Node
---@param clip table
local function toggle(node, clip)
if node.name == ".." then
return
end
local notify_node = notify.render_path(node.absolute_path)

if utils.array_remove(clip, node) then
return notify.info(notify_node .. " removed from clipboard.")
notify.info(notify_node .. " removed from clipboard.")
return
end

table.insert(clip, node)
Expand All @@ -161,22 +174,28 @@ function M.clear_clipboard()
renderer.draw()
end

---@param node Node
function M.copy(node)
utils.array_remove(clipboard.cut, node)
toggle(node, clipboard.copy)
renderer.draw()
end

---@param node Node
function M.cut(node)
utils.array_remove(clipboard.copy, node)
toggle(node, clipboard.cut)
renderer.draw()
end

---@param node Node
---@param action_type string
---@param action_fn fun(source: string, dest: string)
local function do_paste(node, action_type, action_fn)
node = lib.get_last_group_node(node)
if node.name == ".." then
node = core.get_explorer()
local explorer = core.get_explorer()
if node.name == ".." and explorer then
node = explorer
end
local clip = clipboard[action_type]
if #clip == 0 then
Expand All @@ -202,10 +221,14 @@ local function do_paste(node, action_type, action_fn)

clipboard[action_type] = {}
if not M.config.filesystem_watchers.enable then
return reloaders.reload_explorer()
reloaders.reload_explorer()
end
end

---@param source string
---@param destination string
---@return boolean
---@return string?
local function do_cut(source, destination)
log.line("copy_paste", "do_cut '%s' -> '%s'", source, destination)

Expand All @@ -225,12 +248,13 @@ local function do_cut(source, destination)
return true
end

---@param node Node
function M.paste(node)
if clipboard.cut[1] ~= nil then
return do_paste(node, "cut", do_cut)
do_paste(node, "cut", do_cut)
else
do_paste(node, "copy", do_copy)
end

return do_paste(node, "copy", do_copy)
end

function M.print_clipboard()
Expand All @@ -248,9 +272,10 @@ function M.print_clipboard()
end
end

return notify.info(table.concat(content, "\n") .. "\n")
notify.info(table.concat(content, "\n") .. "\n")
end

---@param content string
local function copy_to_clipboard(content)
local clipboard_name
if M.config.actions.use_system_clipboard == true then
Expand All @@ -264,28 +289,36 @@ local function copy_to_clipboard(content)
end

vim.api.nvim_exec_autocmds("TextYankPost", {})
return notify.info(string.format("Copied %s to %s clipboard!", content, clipboard_name))
notify.info(string.format("Copied %s to %s clipboard!", content, clipboard_name))
end

---@param node Node
function M.copy_filename(node)
return copy_to_clipboard(node.name)
copy_to_clipboard(node.name)
end

---@param node Node
function M.copy_path(node)
local absolute_path = node.absolute_path
local relative_path = utils.path_relative(absolute_path, core.get_cwd())
local cwd = core.get_cwd()
if cwd == nil then
return
end

local relative_path = utils.path_relative(absolute_path, cwd)
local content = node.nodes ~= nil and utils.path_add_trailing(relative_path) or relative_path
return copy_to_clipboard(content)
copy_to_clipboard(content)
end

---@param node Node
function M.copy_absolute_path(node)
local absolute_path = node.absolute_path
local content = node.nodes ~= nil and utils.path_add_trailing(absolute_path) or absolute_path
return copy_to_clipboard(content)
copy_to_clipboard(content)
end

---Clipboard text highlight group and position when highlight_clipboard.
---@param node table
--- Clipboard text highlight group and position when highlight_clipboard.
---@param node Node
---@return HL_POSITION position none when clipboard empty
---@return string|nil group only when node present in clipboard
function M.get_highlight(node)
Expand Down
14 changes: 13 additions & 1 deletion lua/nvim-tree/actions/fs/create-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local find_file = require("nvim-tree.actions.finders.find-file").fn

local M = {}

---@param file string
local function create_and_notify(file)
events._dispatch_will_create_file(file)
local ok, fd = pcall(vim.loop.fs_open, file, "w", 420)
Expand All @@ -19,6 +20,8 @@ local function create_and_notify(file)
events._dispatch_file_created(file)
end

---@param iter function iterable
---@return integer
local function get_num_nodes(iter)
local i = 0
for _ in iter do
Expand All @@ -27,6 +30,8 @@ local function get_num_nodes(iter)
return i
end

---@param node Node
---@return string
local function get_containing_folder(node)
if node.nodes ~= nil then
return utils.path_add_trailing(node.absolute_path)
Expand All @@ -35,11 +40,18 @@ local function get_containing_folder(node)
return node.absolute_path:sub(0, -node_name_size - 1)
end

---@param node Node|nil
function M.fn(node)
local cwd = core.get_cwd()
if cwd == nil then
return
end

node = node and lib.get_last_group_node(node)
if not node or node.name == ".." then
node = {
absolute_path = core.get_cwd(),
absolute_path = cwd,
name = "",
nodes = core.get_explorer().nodes,
open = true,
}
Expand Down
16 changes: 12 additions & 4 deletions lua/nvim-tree/actions/fs/remove-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local M = {
config = {},
}

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

---@param absolute_path string
local function clear_buffer(absolute_path)
local bufs = vim.fn.getbufinfo { bufloaded = 1, buflisted = 1 }
for _, buf in pairs(bufs) do
Expand All @@ -44,10 +46,13 @@ local function clear_buffer(absolute_path)
end
end

---@param cwd string
---@return boolean|nil
local function remove_dir(cwd)
local handle = vim.loop.fs_scandir(cwd)
if type(handle) == "string" then
return notify.error(handle)
notify.error(handle)
return
end

while true do
Expand Down Expand Up @@ -75,27 +80,30 @@ local function remove_dir(cwd)
end

--- Remove a node, notify errors, dispatch events
--- @param node table
---@param node Node
function M.remove(node)
local notify_node = notify.render_path(node.absolute_path)
if node.nodes ~= nil and not node.link_to then
local success = remove_dir(node.absolute_path)
if not success then
return notify.error("Could not remove " .. notify_node)
notify.error("Could not remove " .. notify_node)
return
end
events._dispatch_folder_removed(node.absolute_path)
else
events._dispatch_will_remove_file(node.absolute_path)
local success = vim.loop.fs_unlink(node.absolute_path)
if not success then
return notify.error("Could not remove " .. notify_node)
notify.error("Could not remove " .. notify_node)
return
end
events._dispatch_file_removed(node.absolute_path)
clear_buffer(node.absolute_path)
end
notify.info(notify_node .. " was properly removed.")
end

---@param node Node
function M.fn(node)
if node.name == ".." then
return
Expand Down
Loading

0 comments on commit 13f967f

Please sign in to comment.