diff options
| -rw-r--r-- | .gitattributes | 3 | ||||
| -rw-r--r-- | lua/nvim-treesitter/config.lua | 2 | ||||
| -rw-r--r-- | lua/nvim-treesitter/health.lua | 7 | ||||
| -rw-r--r-- | lua/nvim-treesitter/indent.lua | 25 | ||||
| -rw-r--r-- | lua/nvim-treesitter/install.lua | 8 | ||||
| -rw-r--r-- | lua/nvim-treesitter/locals.lua | 15 | ||||
| -rw-r--r-- | plugin/query_predicates.lua | 16 | ||||
| -rwxr-xr-x | scripts/check-queries.lua | 8 | ||||
| -rwxr-xr-x | scripts/format-queries.lua | 26 | ||||
| -rwxr-xr-x | scripts/install-parsers.lua | 2 | ||||
| -rwxr-xr-x | scripts/update-parsers.lua | 5 | ||||
| -rwxr-xr-x | scripts/update-readme.lua | 5 |
12 files changed, 61 insertions, 61 deletions
diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000..3375d5ce2 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +runtime/queries/**/*.scm linguist-language=Tree-sitter-Query +doc/*.txt linguist-documentation +SUPPORTED_LANGUAGES.md linguist-generated diff --git a/lua/nvim-treesitter/config.lua b/lua/nvim-treesitter/config.lua index 44c9b09c5..b7ffbf47c 100644 --- a/lua/nvim-treesitter/config.lua +++ b/lua/nvim-treesitter/config.lua @@ -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 diff --git a/lua/nvim-treesitter/health.lua b/lua/nvim-treesitter/health.lua index 68b01bb35..8ca51814e 100644 --- a/lua/nvim-treesitter/health.lua +++ b/lua/nvim-treesitter/health.lua @@ -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.') diff --git a/lua/nvim-treesitter/indent.lua b/lua/nvim-treesitter/indent.lua index 03489f22e..00f85dea2 100644 --- a/lua/nvim-treesitter/indent.lua +++ b/lua/nvim-treesitter/indent.lua @@ -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 = diff --git a/lua/nvim-treesitter/install.lua b/lua/nvim-treesitter/install.lua index 75a18d5a8..b36a7ddee 100644 --- a/lua/nvim-treesitter/install.lua +++ b/lua/nvim-treesitter/install.lua @@ -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 }) diff --git a/lua/nvim-treesitter/locals.lua b/lua/nvim-treesitter/locals.lua index 63b3d458c..b6d9c25b2 100644 --- a/lua/nvim-treesitter/locals.lua +++ b/lua/nvim-treesitter/locals.lua @@ -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() diff --git a/plugin/query_predicates.lua b/plugin/query_predicates.lua index 1a500f591..6511d3104 100644 --- a/plugin/query_predicates.lua +++ b/plugin/query_predicates.lua @@ -1,8 +1,8 @@ local query = vim.treesitter.query local predicates = { - ---@param match TSQueryMatch - ---@param pred string[] + ---@param match table<integer,TSNode[]> + ---@param pred any[] ---@param any boolean ---@return boolean ['kind-eq'] = function(match, pred, any) @@ -26,16 +26,16 @@ local predicates = { -- register custom predicates (overwrite existing; needed for CI) ----@param match TSQueryMatch ----@param pred string[] ----@return boolean|nil +---@param match table<integer,TSNode[]> +---@param pred any[] +---@return boolean query.add_predicate('kind-eq?', function(match, _, _, pred) return predicates['kind-eq'](match, pred, false) end, { force = true }) ----@param match TSQueryMatch ----@param pred string[] ----@return boolean|nil +---@param match table<integer,TSNode[]> +---@param pred any[] +---@return boolean query.add_predicate('any-kind-eq?', function(match, _, _, pred) return predicates['kind-eq'](match, pred, true) end, { force = true }) diff --git a/scripts/check-queries.lua b/scripts/check-queries.lua index dddaf80d7..66b947914 100755 --- a/scripts/check-queries.lua +++ b/scripts/check-queries.lua @@ -7,13 +7,13 @@ local parsers = #_G.arg > 0 and { unpack(_G.arg) } or require('nvim-treesitter.config').installed_parsers() -- Extract captures from documentation for validation -local captures = {} +local captures = {} ---@type table[] do local current_query ---@type string for line in io.lines('CONTRIBUTING.md') do if vim.startswith(line, '### ') then - current_query = line:sub(5):lower() + current_query = line:sub(5):lower() ---@type string elseif vim.startswith(line, '@') and current_query then if not captures[current_query] then captures[current_query] = {} @@ -24,14 +24,14 @@ do end -- Complete captures for injections. - for _, lang in pairs(vim.tbl_keys(configs)) do + for lang, _ in pairs(configs) do table.insert(captures['injections'], lang) end end -- Check queries for each installed parser in parsers local errors = {} ---@type string[] -local timings = {} ---@type number[][] +local timings = {} ---@type { duration: number, lang: string, query_type: string }[] do print('::group::Check parsers') diff --git a/scripts/format-queries.lua b/scripts/format-queries.lua index c7362687e..bc2cf70ce 100755 --- a/scripts/format-queries.lua +++ b/scripts/format-queries.lua @@ -351,7 +351,7 @@ end ---@param bufnr integer ---@param node TSNode ---@param lines string[] ----@param q table<string, vim.treesitter.query.TSMetadata> +---@param q table<string, table[]> ---@param level integer local function iter(bufnr, node, lines, q, level) --- Sometimes 2 queries apply append twice. This is to prevent the case from happening @@ -430,19 +430,19 @@ end ---@param queries string local function format(bufnr, queries) local lines = { '' } - -- stylua: ignore + ---@type table<string,table<string,table>> local map = { - ['format.ignore'] = {}, -- Ignore the node and its children - ['format.indent.begin'] = {}, -- +1 shiftwidth for all nodes after this - ['format.indent.dedent'] = {}, -- -1 shiftwidth for this line only - ['format.prepend-space'] = {}, -- Prepend a space before inserting the node - ['format.prepend-newline'] = {}, -- Prepend a \n before inserting the node - ['format.append-space'] = {}, -- Append a space after inserting the node - ['format.append-newline'] = {}, -- Append a newline after inserting the node - ['format.cancel-append'] = {}, -- Cancel any `@format.append-*` applied to the node - ['format.cancel-prepend'] = {}, -- Cancel any `@format.prepend-*` applied to the node - ['format.replace'] = {}, -- Dedicated capture used to store results of `(#gsub!)` - ['format.remove'] = {}, -- Do not add the syntax node to the result, i.e. brackets [], parens () + ['format.ignore'] = {}, -- Ignore the node and its children + ['format.indent.begin'] = {}, -- +1 shiftwidth for all nodes after this + ['format.indent.dedent'] = {}, -- -1 shiftwidth for this line only + ['format.prepend-space'] = {}, -- Prepend a space before inserting the node + ['format.prepend-newline'] = {}, -- Prepend a \n before inserting the node + ['format.append-space'] = {}, -- Append a space after inserting the node + ['format.append-newline'] = {}, -- Append a newline after inserting the node + ['format.cancel-append'] = {}, -- Cancel any `@format.append-*` applied to the node + ['format.cancel-prepend'] = {}, -- Cancel any `@format.prepend-*` applied to the node + ['format.replace'] = {}, -- Dedicated capture used to store results of `(#gsub!)` + ['format.remove'] = {}, -- Do not add the syntax node to the result, i.e. brackets [], parens () } local root = ts.get_parser(bufnr, 'query'):parse(true)[1]:root() local query = ts.query.parse('query', queries) diff --git a/scripts/install-parsers.lua b/scripts/install-parsers.lua index cb7389e19..1d9448ca7 100755 --- a/scripts/install-parsers.lua +++ b/scripts/install-parsers.lua @@ -12,7 +12,7 @@ for i = 1, #_G.arg do elseif _G.arg[i]:find('^%-%-max%-jobs') then max_jobs = _G.arg[i]:match('=(%d+)') else - parsers[#parsers + 1] = _G.arg[i] + parsers[#parsers + 1] = _G.arg[i] ---@type string end end diff --git a/scripts/update-parsers.lua b/scripts/update-parsers.lua index 83f54e9da..70055c81d 100755 --- a/scripts/update-parsers.lua +++ b/scripts/update-parsers.lua @@ -55,8 +55,9 @@ if #updates > 0 then local update_list = table.concat(updates, ', ') print(string.format('\nUpdated parsers: %s', update_list)) -- pass list to workflow - if os.getenv('GITHUB_ENV') then - local env = io.open(os.getenv('GITHUB_ENV'), 'a') + local gh_env = os.getenv('GITHUB_ENV') + if gh_env then + local env = assert(io.open(gh_env, 'a')) env:write(string.format('UPDATED_PARSERS=%s\n', update_list)) env:close() end diff --git a/scripts/update-readme.lua b/scripts/update-readme.lua index 2505f845b..37eae6826 100755 --- a/scripts/update-readme.lua +++ b/scripts/update-readme.lua @@ -3,11 +3,8 @@ vim.opt.runtimepath:append('.') local util = require('nvim-treesitter.util') local parsers = require('nvim-treesitter.parsers') local tiers = require('nvim-treesitter.config').tiers ----@class Parser ----@field name string ----@field parser ParserInfo -local sorted_parsers = {} +local sorted_parsers = {} ---@type { name: string, parser: ParserInfo }[] for k, v in pairs(parsers) do table.insert(sorted_parsers, { name = k, parser = v }) end |
