mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-05 13:00:08 -04:00
refactor(lua): fix some luals warnings
This commit is contained in:
parent
bdc2e01958
commit
53d7118483
12 changed files with 61 additions and 61 deletions
|
|
@ -13,7 +13,7 @@ local config = {
|
|||
}
|
||||
|
||||
---Setup call for users to override configuration configurations.
|
||||
---@param user_data TSConfig|nil user configuration table
|
||||
---@param user_data TSConfig? user configuration table
|
||||
function M.setup(user_data)
|
||||
if user_data then
|
||||
if user_data.install_dir then
|
||||
|
|
|
|||
|
|
@ -93,7 +93,8 @@ local function install_health()
|
|||
end
|
||||
|
||||
health.start('OS Info')
|
||||
for k, v in pairs(vim.uv.os_uname()) do
|
||||
local osinfo = vim.uv.os_uname() ---@type table<string,string>
|
||||
for k, v in pairs(osinfo) do
|
||||
health.info(k .. ': ' .. v)
|
||||
end
|
||||
|
||||
|
|
@ -107,9 +108,7 @@ local function install_health()
|
|||
end
|
||||
if
|
||||
vim.iter(vim.api.nvim_list_runtime_paths()):any(function(p)
|
||||
if installdir == vim.fs.normalize(p) .. '/' then
|
||||
return true
|
||||
end
|
||||
return installdir == vim.fs.normalize(p) .. '/'
|
||||
end)
|
||||
then
|
||||
health.ok('is in runtimepath.')
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ end
|
|||
---@param root TSNode
|
||||
---@param lnum integer
|
||||
---@param col? integer
|
||||
---@return TSNode
|
||||
---@return TSNode?
|
||||
local function get_first_node_at_line(root, lnum, col)
|
||||
col = col or get_indentcols_at_line(lnum)
|
||||
return root:descendant_for_range(lnum - 1, col, lnum - 1, col + 1)
|
||||
|
|
@ -35,7 +35,7 @@ end
|
|||
---@param root TSNode
|
||||
---@param lnum integer
|
||||
---@param col? integer
|
||||
---@return TSNode
|
||||
---@return TSNode?
|
||||
local function get_last_node_at_line(root, lnum, col)
|
||||
col = col or (#getline(lnum) - 1)
|
||||
return root:descendant_for_range(lnum - 1, col, lnum - 1, col + 1)
|
||||
|
|
@ -52,8 +52,8 @@ end
|
|||
---@param bufnr integer
|
||||
---@param node TSNode
|
||||
---@param delimiter string
|
||||
---@return TSNode|nil child
|
||||
---@return boolean|nil is_end
|
||||
---@return TSNode? child
|
||||
---@return boolean? is_end
|
||||
local function find_delimiter(bufnr, node, delimiter)
|
||||
for child, _ in node:iter_children() do
|
||||
if child:type() == delimiter then
|
||||
|
|
@ -89,6 +89,7 @@ local function memoize(fn, hash_fn)
|
|||
end
|
||||
|
||||
local get_indents = memoize(function(bufnr, root, lang)
|
||||
---@type table<string,table<string,table>>
|
||||
local map = {
|
||||
['indent.auto'] = {},
|
||||
['indent.begin'] = {},
|
||||
|
|
@ -154,19 +155,19 @@ function M.get_indent(lnum)
|
|||
end
|
||||
|
||||
local q = get_indents(vim.api.nvim_get_current_buf(), root, lang_tree:lang())
|
||||
local node ---@type TSNode
|
||||
local node ---@type TSNode?
|
||||
if getline(lnum):find('^%s*$') then
|
||||
local prevlnum = vim.fn.prevnonblank(lnum)
|
||||
local indentcols = get_indentcols_at_line(prevlnum)
|
||||
local prevline = vim.trim(getline(prevlnum))
|
||||
-- The final position can be trailing spaces, which should not affect indentation
|
||||
node = get_last_node_at_line(root, prevlnum, indentcols + #prevline - 1)
|
||||
if node:type():match('comment') then
|
||||
if node and node:type():match('comment') then
|
||||
-- The final node we capture of the previous line can be a comment node, which should also be ignored
|
||||
-- Unless the last line is an entire line of comment, ignore the comment range and find the last node again
|
||||
local first_node = get_first_node_at_line(root, prevlnum, indentcols)
|
||||
local _, scol, _, _ = node:range()
|
||||
if first_node:id() ~= node:id() then
|
||||
if first_node and first_node:id() ~= node:id() then
|
||||
-- In case the last captured node is a trailing comment node, re-trim the string
|
||||
prevline = vim.trim(prevline:sub(1, scol - indentcols))
|
||||
-- Add back indent as indent of prevline was trimmed away
|
||||
|
|
@ -174,7 +175,7 @@ function M.get_indent(lnum)
|
|||
node = get_last_node_at_line(root, prevlnum, col)
|
||||
end
|
||||
end
|
||||
if q['indent.end'][node:id()] then
|
||||
if node and q['indent.end'][node:id()] then
|
||||
node = get_first_node_at_line(root, lnum)
|
||||
end
|
||||
else
|
||||
|
|
@ -192,7 +193,7 @@ function M.get_indent(lnum)
|
|||
-- tracks to ensure multiple indent levels are not applied for same line
|
||||
local is_processed_by_row = {} --- @type table<integer,boolean>
|
||||
|
||||
if q['indent.zero'][node:id()] then
|
||||
if node and q['indent.zero'][node:id()] then
|
||||
return 0
|
||||
end
|
||||
|
||||
|
|
@ -240,7 +241,7 @@ function M.get_indent(lnum)
|
|||
local is_in_err = false
|
||||
if should_process then
|
||||
local parent = node:parent()
|
||||
is_in_err = parent and parent:has_error()
|
||||
is_in_err = parent and parent:has_error() or false
|
||||
end
|
||||
if
|
||||
should_process
|
||||
|
|
@ -276,8 +277,8 @@ function M.get_indent(lnum)
|
|||
and (srow ~= lnum - 1)
|
||||
then
|
||||
local metadata = q['indent.align'][node:id()]
|
||||
local o_delim_node, o_is_last_in_line ---@type TSNode|nil, boolean|nil
|
||||
local c_delim_node, c_is_last_in_line ---@type TSNode|nil, boolean|nil, boolean|nil
|
||||
local o_delim_node, o_is_last_in_line ---@type TSNode?, boolean?
|
||||
local c_delim_node, c_is_last_in_line ---@type TSNode?, boolean?, boolean?
|
||||
local indent_is_absolute = false
|
||||
if metadata['indent.open_delimiter'] then
|
||||
o_delim_node, o_is_last_in_line =
|
||||
|
|
|
|||
|
|
@ -434,7 +434,7 @@ local function install(languages, options, callback)
|
|||
end
|
||||
end
|
||||
|
||||
---@param languages string[]
|
||||
---@param languages string[]|string
|
||||
---@param options? InstallOptions
|
||||
---@param callback? fun(boolean)
|
||||
M.install = a.sync(function(languages, options, callback)
|
||||
|
|
@ -443,10 +443,8 @@ M.install = a.sync(function(languages, options, callback)
|
|||
install(languages, options, callback)
|
||||
end, 3)
|
||||
|
||||
---@class UpdateOptions
|
||||
|
||||
---@param languages? string[]|string
|
||||
---@param _options? UpdateOptions
|
||||
---@param _options? table
|
||||
---@param callback? function
|
||||
M.update = a.sync(function(languages, _options, callback)
|
||||
reload_parsers()
|
||||
|
|
@ -501,7 +499,7 @@ local function uninstall_lang(logger, lang, parser, queries)
|
|||
end
|
||||
|
||||
---@param languages string[]|string
|
||||
---@param _options? UpdateOptions
|
||||
---@param _options? table
|
||||
---@param _callback? fun()
|
||||
M.uninstall = a.sync(function(languages, _options, _callback)
|
||||
languages = config.norm_languages(languages or 'all', { missing = true, dependencies = true })
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ end
|
|||
---@param node TSNode
|
||||
---@return TSNode result
|
||||
local function get_root_for_node(node)
|
||||
local parent = node
|
||||
local parent = node ---@type TSNode?
|
||||
local result = node
|
||||
|
||||
while parent ~= nil do
|
||||
|
|
@ -56,9 +56,9 @@ end
|
|||
-- Iterates over a nodes scopes moving from the bottom up
|
||||
---@param node TSNode
|
||||
---@param bufnr integer
|
||||
---@return fun(): TSNode|nil
|
||||
---@return fun(): TSNode?
|
||||
function M.iter_scope_tree(node, bufnr)
|
||||
local last_node = node
|
||||
local last_node = node ---@type TSNode?
|
||||
return function()
|
||||
if not last_node then
|
||||
return
|
||||
|
|
@ -247,7 +247,7 @@ M.get_definitions_lookup_table = memoize(function(bufnr)
|
|||
return {}
|
||||
end
|
||||
|
||||
local result = {}
|
||||
local result = {} ---@type TSLocal[]
|
||||
for _, definition in ipairs(definitions) do
|
||||
for _, node_entry in ipairs(M.get_local_nodes(definition)) do
|
||||
local scopes = M.get_definition_scopes(node_entry.node, bufnr, node_entry.scope)
|
||||
|
|
@ -279,9 +279,10 @@ end)
|
|||
---@param node TSNode: the definition node
|
||||
---@param bufnr integer: the buffer
|
||||
---@param scope_type TSScope: the scope type
|
||||
---@return TSNode[]
|
||||
function M.get_definition_scopes(node, bufnr, scope_type)
|
||||
local scopes = {}
|
||||
local scope_count = 1 ---@type integer|nil
|
||||
local scope_count = 1 ---@type integer?
|
||||
|
||||
-- Definition is valid for the containing scope
|
||||
-- and the containing scope of that scope
|
||||
|
|
@ -365,7 +366,7 @@ end
|
|||
---@param node TSNode
|
||||
---@param bufnr? integer
|
||||
---@param allow_scope? boolean
|
||||
---@return TSNode|nil
|
||||
---@return TSNode?
|
||||
function M.containing_scope(node, bufnr, allow_scope)
|
||||
bufnr = bufnr or api.nvim_get_current_buf()
|
||||
allow_scope = allow_scope == nil or allow_scope == true
|
||||
|
|
@ -375,7 +376,7 @@ function M.containing_scope(node, bufnr, allow_scope)
|
|||
return
|
||||
end
|
||||
|
||||
local iter_node = node
|
||||
local iter_node = node ---@type TSNode?
|
||||
|
||||
while iter_node ~= nil and not vim.tbl_contains(scopes, iter_node) do
|
||||
iter_node = iter_node:parent()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue