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 c70229cad9.

* 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 <alex@courtis.org>
This commit is contained in:
Azad
2023-12-09 01:34:35 +01:00
committed by GitHub
parent 7d1760f892
commit 13f967f8e7
51 changed files with 622 additions and 161 deletions

View File

@@ -29,6 +29,10 @@ local WATCHED_FILES = {
"index", -- staging area
}
---@param toplevel string|nil
---@param path string|nil
---@param project table
---@param git_status table|nil
local function reload_git_status(toplevel, path, project, git_status)
if path then
for p in pairs(project.files) do
@@ -45,9 +49,9 @@ local function reload_git_status(toplevel, path, project, git_status)
end
--- Is this path in a known ignored directory?
--- @param path string
--- @param project table git status
--- @return boolean
---@param path string
---@param project table git status
---@return boolean
local function path_ignored_in_project(path, project)
if not path or not project then
return false
@@ -64,7 +68,7 @@ local function path_ignored_in_project(path, project)
end
--- Reload all projects
--- @return table projects maybe empty
---@return table projects maybe empty
function M.reload()
if not M.config.git.enable then
return {}
@@ -78,9 +82,9 @@ function M.reload()
end
--- Reload one project. Does nothing when no project or path is ignored
--- @param toplevel string|nil
--- @param path string|nil optional path to update only
--- @param callback function|nil
---@param toplevel string|nil
---@param path string|nil optional path to update only
---@param callback function|nil
function M.reload_project(toplevel, path, callback)
local project = M._projects_by_toplevel[toplevel]
if not toplevel or not project or not M.config.git.enable then
@@ -118,7 +122,8 @@ function M.reload_project(toplevel, path, callback)
end
--- Retrieve a known project
--- @return table|nil project
---@param toplevel string|nil
---@return table|nil project
function M.get_project(toplevel)
return M._projects_by_toplevel[toplevel]
end
@@ -128,8 +133,8 @@ end
--- not part of a project
--- not a directory
--- path in git.disable_for_dirs
--- @param path string absolute
--- @return string|nil
---@param path string absolute
---@return string|nil
function M.get_toplevel(path)
if not M.config.git.enable then
return nil
@@ -207,8 +212,8 @@ end
--- Load the project status for a path. Does nothing when no toplevel for path.
--- Only fetches project status when unknown, otherwise returns existing.
--- @param path string absolute
--- @return table project maybe empty
---@param path string absolute
---@return table project maybe empty
function M.load_project_status(path)
if not M.config.git.enable then
return {}
@@ -252,12 +257,17 @@ function M.load_project_status(path)
})
end
M._projects_by_toplevel[toplevel] = {
files = git_status,
dirs = git_utils.file_status_to_dir_status(git_status, toplevel),
watcher = watcher,
}
return M._projects_by_toplevel[toplevel]
if git_status then
M._projects_by_toplevel[toplevel] = {
files = git_status,
dirs = git_utils.file_status_to_dir_status(git_status, toplevel),
watcher = watcher,
}
return M._projects_by_toplevel[toplevel]
else
M._toplevels_by_path[path] = false
return {}
end
end
function M.purge_state()

View File

@@ -2,13 +2,26 @@ local log = require "nvim-tree.log"
local utils = require "nvim-tree.utils"
local notify = require "nvim-tree.notify"
-- TODO add "${3rd}/luv/library" to "workspace.library"
---@class uv.uv_handle_t: table
---@class uv.uv_stream_t: uv.uv_handle_t
---@class uv.uv_pipe_t: uv.uv_stream_t
---@class Runner
local Runner = {}
Runner.__index = Runner
local timeouts = 0
local MAX_TIMEOUTS = 5
---@private
---@param status string
---@param path string|nil
function Runner:_parse_status_output(status, path)
if not path then
return
end
-- replacing slashes if on windows
if vim.fn.has "win32" == 1 then
path = path:gsub("/", "\\")
@@ -18,6 +31,10 @@ function Runner:_parse_status_output(status, path)
end
end
---@private
---@param prev_output string
---@param incoming string
---@return string
function Runner:_handle_incoming_data(prev_output, incoming)
if incoming and utils.str_find(incoming, "\n") then
local prev = prev_output .. incoming
@@ -46,12 +63,15 @@ function Runner:_handle_incoming_data(prev_output, incoming)
end
for line in prev_output:gmatch "[^\n]*\n" do
self._parse_status_output(line)
self:_parse_status_output(line)
end
return ""
end
---@param stdout_handle uv.uv_pipe_t
---@param stderr_handle uv.uv_pipe_t
---@return table
function Runner:_getopts(stdout_handle, stderr_handle)
local untracked = self.list_untracked and "-u" or nil
local ignored = (self.list_untracked and self.list_ignored) and "--ignored=matching" or "--ignored=no"
@@ -62,6 +82,7 @@ function Runner:_getopts(stdout_handle, stderr_handle)
}
end
---@param output string
function Runner:_log_raw_output(output)
if log.enabled "git" and output and type(output) == "string" then
log.raw("git", "%s", output)
@@ -69,12 +90,17 @@ function Runner:_log_raw_output(output)
end
end
---@param callback function|nil
function Runner:_run_git_job(callback)
local handle, pid
local stdout = vim.loop.new_pipe(false)
local stderr = vim.loop.new_pipe(false)
local timer = vim.loop.new_timer()
if stdout == nil or stderr == nil or timer == nil then
return
end
local function on_finish(rc)
self.rc = rc or 0
if timer:is_closing() or stdout:is_closing() or stderr:is_closing() or (handle and handle:is_closing()) then
@@ -151,6 +177,7 @@ function Runner:_wait()
end
end
---@param opts table
function Runner:_finalise(opts)
if self.rc == -1 then
log.line("git", "job timed out %s %s", opts.toplevel, opts.path)
@@ -167,9 +194,9 @@ function Runner:_finalise(opts)
end
--- Runs a git process, which will be killed if it takes more than timeout which defaults to 400ms
--- @param opts table
--- @param callback function|nil executed passing return when complete
--- @return table|nil status by absolute path, nil if callback present
---@param opts table
---@param callback function|nil executed passing return when complete
---@return table|nil status by absolute path, nil if callback present
function Runner.run(opts, callback)
local self = setmetatable({
toplevel = opts.toplevel,

View File

@@ -6,9 +6,9 @@ local M = {
}
--- Retrieve the git toplevel directory
--- @param cwd string path
--- @return string|nil toplevel absolute path
--- @return string|nil git_dir absolute path
---@param cwd string path
---@return string|nil toplevel absolute path
---@return string|nil git_dir absolute path
function M.get_toplevel(cwd)
local profile = log.profile_start("git toplevel git_dir %s", cwd)
@@ -60,6 +60,8 @@ end
local untracked = {}
---@param cwd string
---@return string|nil
function M.should_show_untracked(cwd)
if untracked[cwd] ~= nil then
return untracked[cwd]
@@ -79,12 +81,18 @@ function M.should_show_untracked(cwd)
return untracked[cwd]
end
---@param t table|nil
---@param k string
---@return table
local function nil_insert(t, k)
t = t or {}
t[k] = true
return t
end
---@param status table
---@param cwd string|nil
---@return table
function M.file_status_to_dir_status(status, cwd)
local direct = {}
for p, s in pairs(status) do