aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/treesitter/languagetree.lua
diff options
context:
space:
mode:
authorLewis Russell <lewis6991@gmail.com>2024-03-16 17:11:42 +0000
committerLewis Russell <me@lewisr.dev>2024-03-16 19:26:10 +0000
commit14e4b6bbd8640675d7393bdeb3e93d74ab875ff1 (patch)
tree3e48d63a51e5ff90b9d47deccc86b22c43fc5fcf /runtime/lua/vim/treesitter/languagetree.lua
parent924a7ef8bb3b74eccbffd48bc1a283d3867b8119 (diff)
downloadrneovim-14e4b6bbd8640675d7393bdeb3e93d74ab875ff1.tar.gz
rneovim-14e4b6bbd8640675d7393bdeb3e93d74ab875ff1.tar.bz2
rneovim-14e4b6bbd8640675d7393bdeb3e93d74ab875ff1.zip
refactor(lua): type annotations
Diffstat (limited to 'runtime/lua/vim/treesitter/languagetree.lua')
-rw-r--r--runtime/lua/vim/treesitter/languagetree.lua38
1 files changed, 22 insertions, 16 deletions
diff --git a/runtime/lua/vim/treesitter/languagetree.lua b/runtime/lua/vim/treesitter/languagetree.lua
index ec933f5194..3b5d8953c9 100644
--- a/runtime/lua/vim/treesitter/languagetree.lua
+++ b/runtime/lua/vim/treesitter/languagetree.lua
@@ -156,8 +156,10 @@ function LanguageTree:_set_logger()
local lang = self:lang()
- vim.fn.mkdir(vim.fn.stdpath('log'), 'p')
- local logfilename = vim.fs.joinpath(vim.fn.stdpath('log'), 'treesitter.log')
+ local logdir = vim.fn.stdpath('log') --[[@as string]]
+
+ vim.fn.mkdir(logdir, 'p')
+ local logfilename = vim.fs.joinpath(logdir, 'treesitter.log')
local logfile, openerr = io.open(logfilename, 'a+')
@@ -463,7 +465,7 @@ end
--- Invokes the callback for each |LanguageTree| and its children recursively
---
---@param fn fun(tree: vim.treesitter.LanguageTree, lang: string)
----@param include_self boolean|nil Whether to include the invoking tree in the results
+---@param include_self? boolean Whether to include the invoking tree in the results
function LanguageTree:for_each_child(fn, include_self)
vim.deprecate('LanguageTree:for_each_child()', 'LanguageTree:children()', '0.11')
if include_self then
@@ -796,7 +798,7 @@ function LanguageTree:_get_injection(match, metadata)
local combined = metadata['injection.combined'] ~= nil
local injection_lang = metadata['injection.language'] --[[@as string?]]
local lang = metadata['injection.self'] ~= nil and self:lang()
- or metadata['injection.parent'] ~= nil and self._parent
+ or metadata['injection.parent'] ~= nil and self._parent:lang()
or (injection_lang and resolve_lang(injection_lang))
local include_children = metadata['injection.include-children'] ~= nil
@@ -1058,20 +1060,19 @@ function LanguageTree:_on_detach(...)
end
end
---- Registers callbacks for the |LanguageTree|.
----@param cbs table 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.
+--- 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_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.
--- - `on_child_added` : emitted when a child is added to the tree.
--- - `on_child_removed` : emitted when a child is removed from the tree.
---- - `on_detach` : emitted when the buffer is detached, see |nvim_buf_detach_event|.
+--- - `on_detach` : emitted when the buffer is detached, see [nvim_buf_detach_event].
--- Takes one argument, the number of the buffer.
--- @param recursive? boolean Apply callbacks recursively for all children. Any new children will
--- also inherit the callbacks.
function LanguageTree:register_cbs(cbs, recursive)
- ---@cast cbs table<TSCallbackNameOn,function>
if not cbs then
return
end
@@ -1112,12 +1113,18 @@ function LanguageTree:contains(range)
return false
end
+--- @class vim.treesitter.LanguageTree.tree_for_range.Opts
+--- @inlinedoc
+---
+--- Ignore injected languages
+--- (default: `true`)
+--- @field ignore_injections? boolean
+
--- Gets the tree that contains {range}.
---
---@param range Range4 `{ start_line, start_col, end_line, end_col }`
----@param opts table|nil Optional keyword arguments:
---- - ignore_injections boolean Ignore injected languages (default true)
----@return TSTree|nil
+---@param opts? vim.treesitter.LanguageTree.tree_for_range.Opts
+---@return TSTree?
function LanguageTree:tree_for_range(range, opts)
opts = opts or {}
local ignore = vim.F.if_nil(opts.ignore_injections, true)
@@ -1143,9 +1150,8 @@ end
--- Gets the smallest named node that contains {range}.
---
---@param range Range4 `{ start_line, start_col, end_line, end_col }`
----@param opts table|nil Optional keyword arguments:
---- - ignore_injections boolean Ignore injected languages (default true)
----@return TSNode | nil Found node
+---@param opts? vim.treesitter.LanguageTree.tree_for_range.Opts
+---@return TSNode?
function LanguageTree:named_node_for_range(range, opts)
local tree = self:tree_for_range(range, opts)
if tree then
@@ -1156,7 +1162,7 @@ end
--- Gets the appropriate language that contains {range}.
---
---@param range Range4 `{ start_line, start_col, end_line, end_col }`
----@return vim.treesitter.LanguageTree Managing {range}
+---@return vim.treesitter.LanguageTree tree Managing {range}
function LanguageTree:language_for_range(range)
for _, child in pairs(self._children) do
if child:contains(range) then