aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/treesitter
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/lua/vim/treesitter')
-rw-r--r--runtime/lua/vim/treesitter/_meta/tsnode.lua18
-rw-r--r--runtime/lua/vim/treesitter/_query_linter.lua8
-rw-r--r--runtime/lua/vim/treesitter/dev.lua19
-rw-r--r--runtime/lua/vim/treesitter/health.lua18
-rw-r--r--runtime/lua/vim/treesitter/highlighter.lua14
-rw-r--r--runtime/lua/vim/treesitter/language.lua21
-rw-r--r--runtime/lua/vim/treesitter/languagetree.lua2
-rw-r--r--runtime/lua/vim/treesitter/query.lua2
8 files changed, 63 insertions, 39 deletions
diff --git a/runtime/lua/vim/treesitter/_meta/tsnode.lua b/runtime/lua/vim/treesitter/_meta/tsnode.lua
index acc9f8d24e..d982b6a505 100644
--- a/runtime/lua/vim/treesitter/_meta/tsnode.lua
+++ b/runtime/lua/vim/treesitter/_meta/tsnode.lua
@@ -15,7 +15,7 @@ error('Cannot require a meta file')
local TSNode = {} -- luacheck: no unused
--- Get the node's immediate parent.
---- Prefer |TSNode:child_containing_descendant()|
+--- Prefer |TSNode:child_with_descendant()|
--- for iterating over the node's ancestors.
--- @return TSNode?
function TSNode:parent() end
@@ -71,8 +71,24 @@ function TSNode:named_child(index) end
--- Get the node's child that contains {descendant}.
--- @param descendant TSNode
--- @return TSNode?
+--- @deprecated
function TSNode:child_containing_descendant(descendant) end
+--- Get the node's child that contains {descendant} (includes {descendant}).
+---
+--- For example, with the following node hierarchy:
+---
+--- ```
+--- a -> b -> c
+---
+--- a:child_with_descendant(c) == b
+--- a:child_with_descendant(b) == b
+--- a:child_with_descendant(a) == nil
+--- ```
+--- @param descendant TSNode
+--- @return TSNode?
+function TSNode:child_with_descendant(descendant) end
+
--- Get the node's start position. Return three values: the row, column and
--- total byte count (all zero-based).
--- @return integer, integer, integer
diff --git a/runtime/lua/vim/treesitter/_query_linter.lua b/runtime/lua/vim/treesitter/_query_linter.lua
index c5e4b86e1e..a825505378 100644
--- a/runtime/lua/vim/treesitter/_query_linter.lua
+++ b/runtime/lua/vim/treesitter/_query_linter.lua
@@ -240,8 +240,12 @@ function M.omnifunc(findstart, base)
table.insert(items, text)
end
end
- for _, s in pairs(parser_info.symbols) do
- local text = s[2] and s[1] or string.format('%q', s[1]):gsub('\n', 'n') ---@type string
+ for text, named in
+ pairs(parser_info.symbols --[[@as table<string, boolean>]])
+ do
+ if not named then
+ text = string.format('%q', text:sub(2, -2)):gsub('\n', 'n') ---@type string
+ end
if text:find(base, 1, true) then
table.insert(items, text)
end
diff --git a/runtime/lua/vim/treesitter/dev.lua b/runtime/lua/vim/treesitter/dev.lua
index 90c3720b80..26817cdba5 100644
--- a/runtime/lua/vim/treesitter/dev.lua
+++ b/runtime/lua/vim/treesitter/dev.lua
@@ -330,9 +330,7 @@ end
---
--- @param opts vim.treesitter.dev.inspect_tree.Opts?
function M.inspect_tree(opts)
- vim.validate({
- opts = { opts, 't', true },
- })
+ vim.validate('opts', opts, 'table', true)
opts = opts or {}
@@ -529,15 +527,22 @@ function M.inspect_tree(opts)
end,
})
- api.nvim_create_autocmd('BufHidden', {
+ api.nvim_create_autocmd({ 'BufHidden', 'BufUnload', 'QuitPre' }, {
group = group,
buffer = buf,
- once = true,
callback = function()
+ -- don't close inpector window if source buffer
+ -- has more than one open window
+ if #vim.fn.win_findbuf(buf) > 1 then
+ return
+ end
+
-- close all tree windows
for _, window in pairs(vim.fn.win_findbuf(b)) do
close_win(window)
end
+
+ return true
end,
})
end
@@ -667,10 +672,10 @@ function M.edit_query(lang)
api.nvim_buf_clear_namespace(query_buf, edit_ns, 0, -1)
end,
})
- api.nvim_create_autocmd('BufHidden', {
+ api.nvim_create_autocmd({ 'BufHidden', 'BufUnload' }, {
group = group,
buffer = buf,
- desc = 'Close the editor window when the source buffer is hidden',
+ desc = 'Close the editor window when the source buffer is hidden or unloaded',
once = true,
callback = function()
close_win(query_win)
diff --git a/runtime/lua/vim/treesitter/health.lua b/runtime/lua/vim/treesitter/health.lua
index 637f9ea543..53b64d1dec 100644
--- a/runtime/lua/vim/treesitter/health.lua
+++ b/runtime/lua/vim/treesitter/health.lua
@@ -4,10 +4,21 @@ local health = vim.health
--- Performs a healthcheck for treesitter integration
function M.check()
- local parsers = vim.api.nvim_get_runtime_file('parser/*', true)
+ health.start('Treesitter features')
+
+ health.info(
+ string.format(
+ 'Treesitter ABI support: min %d, max %d',
+ vim.treesitter.minimum_language_version,
+ ts.language_version
+ )
+ )
- health.info(string.format('Nvim runtime ABI version: %d', ts.language_version))
+ local can_wasm = vim._ts_add_language_from_wasm ~= nil
+ health.info(string.format('WASM parser support: %s', tostring(can_wasm)))
+ health.start('Treesitter parsers')
+ local parsers = vim.api.nvim_get_runtime_file('parser/*', true)
for _, parser in pairs(parsers) do
local parsername = vim.fn.fnamemodify(parser, ':t:r')
local is_loadable, err_or_nil = pcall(ts.language.add, parsername)
@@ -28,9 +39,6 @@ function M.check()
)
end
end
-
- local can_wasm = vim._ts_add_language_from_wasm ~= nil
- health.info(string.format('Can load WASM parsers: %s', tostring(can_wasm)))
end
return M
diff --git a/runtime/lua/vim/treesitter/highlighter.lua b/runtime/lua/vim/treesitter/highlighter.lua
index a94c408f4e..8ce8652f7d 100644
--- a/runtime/lua/vim/treesitter/highlighter.lua
+++ b/runtime/lua/vim/treesitter/highlighter.lua
@@ -93,9 +93,6 @@ function TSHighlighter.new(tree, opts)
opts = opts or {} ---@type { queries: table<string,string> }
self.tree = tree
tree:register_cbs({
- on_bytes = function(...)
- self:on_bytes(...)
- end,
on_detach = function()
self:on_detach()
end,
@@ -215,13 +212,6 @@ function TSHighlighter:for_each_highlight_state(fn)
end
---@package
----@param start_row integer
----@param new_end integer
-function TSHighlighter:on_bytes(_, _, start_row, _, _, _, _, _, new_end)
- api.nvim__redraw({ buf = self.bufnr, range = { start_row, start_row + new_end + 1 } })
-end
-
----@package
function TSHighlighter:on_detach()
self:destroy()
end
@@ -230,7 +220,7 @@ end
---@param changes Range6[]
function TSHighlighter:on_changedtree(changes)
for _, ch in ipairs(changes) do
- api.nvim__redraw({ buf = self.bufnr, range = { ch[1], ch[4] + 1 } })
+ api.nvim__redraw({ buf = self.bufnr, range = { ch[1], ch[4] + 1 }, flush = false })
end
end
@@ -328,7 +318,7 @@ local function on_line_impl(self, buf, line, is_spell_nav)
-- The "priority" attribute can be set at the pattern level or on a particular capture
local priority = (
tonumber(metadata.priority or metadata[capture] and metadata[capture].priority)
- or vim.highlight.priorities.treesitter
+ or vim.hl.priorities.treesitter
) + spell_pri_offset
-- The "conceal" attribute can be set at the pattern level or on a particular capture
diff --git a/runtime/lua/vim/treesitter/language.lua b/runtime/lua/vim/treesitter/language.lua
index 9f7807e036..446051dfd7 100644
--- a/runtime/lua/vim/treesitter/language.lua
+++ b/runtime/lua/vim/treesitter/language.lua
@@ -108,11 +108,9 @@ function M.add(lang, opts)
local path = opts.path
local symbol_name = opts.symbol_name
- vim.validate({
- lang = { lang, 'string' },
- path = { path, 'string', true },
- symbol_name = { symbol_name, 'string', true },
- })
+ vim.validate('lang', lang, 'string')
+ vim.validate('path', path, 'string', true)
+ vim.validate('symbol_name', symbol_name, 'string', true)
-- parser names are assumed to be lowercase (consistent behavior on case-insensitive file systems)
lang = lang:lower()
@@ -156,10 +154,8 @@ end
--- @param lang string Name of parser
--- @param filetype string|string[] Filetype(s) to associate with lang
function M.register(lang, filetype)
- vim.validate({
- lang = { lang, 'string' },
- filetype = { filetype, { 'string', 'table' } },
- })
+ vim.validate('lang', lang, 'string')
+ vim.validate('filetype', filetype, { 'string', 'table' })
for _, f in ipairs(ensure_list(filetype)) do
if f ~= '' then
@@ -170,7 +166,12 @@ end
--- Inspects the provided language.
---
---- Inspecting provides some useful information on the language like node names, ...
+--- Inspecting provides some useful information on the language like node and field names, ABI
+--- version, and whether the language came from a WASM module.
+---
+--- Node names are returned in a table mapping each node name to a `boolean` indicating whether or
+--- not the node is named (i.e., not anonymous). Anonymous nodes are surrounded with double quotes
+--- (`"`).
---
---@param lang string Language
---@return table
diff --git a/runtime/lua/vim/treesitter/languagetree.lua b/runtime/lua/vim/treesitter/languagetree.lua
index fd68c2b910..4b42164dc8 100644
--- a/runtime/lua/vim/treesitter/languagetree.lua
+++ b/runtime/lua/vim/treesitter/languagetree.lua
@@ -1037,7 +1037,7 @@ end
--- Registers callbacks for the [LanguageTree].
---@param cbs table<TSCallbackNameOn,function> An [nvim_buf_attach()]-like table argument with the following handlers:
---- - `on_bytes` : see [nvim_buf_attach()], but this will be called _after_ the parsers callback.
+--- - `on_bytes` : see [nvim_buf_attach()].
--- - `on_changedtree` : a callback that will be called every time the tree has syntactical changes.
--- It will be passed two arguments: a table of the ranges (as node ranges) that
--- changed and the changed tree.
diff --git a/runtime/lua/vim/treesitter/query.lua b/runtime/lua/vim/treesitter/query.lua
index 4614967799..1677e8d364 100644
--- a/runtime/lua/vim/treesitter/query.lua
+++ b/runtime/lua/vim/treesitter/query.lua
@@ -626,7 +626,7 @@ local directive_handlers = {
--- Adds a new predicate to be used in queries
---
---@param name string Name of the predicate, without leading #
----@param handler fun(match: table<integer,TSNode[]>, pattern: integer, source: integer|string, predicate: any[], metadata: vim.treesitter.query.TSMetadata)
+---@param handler fun(match: table<integer,TSNode[]>, pattern: integer, source: integer|string, predicate: any[], metadata: vim.treesitter.query.TSMetadata): boolean?
--- - see |vim.treesitter.query.add_directive()| for argument meanings
---@param opts? vim.treesitter.query.add_predicate.Opts
function M.add_predicate(name, handler, opts)