mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
refactor(treesitter): move some logic into functions
This commit is contained in:
parent
12faaf40f4
commit
00c4962cd2
@ -252,6 +252,44 @@ function TSHighlighter:get_query(lang)
|
|||||||
return self._queries[lang]
|
return self._queries[lang]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- @param match table<integer,TSNode[]>
|
||||||
|
--- @param bufnr integer
|
||||||
|
--- @param capture integer
|
||||||
|
--- @param metadata vim.treesitter.query.TSMetadata
|
||||||
|
--- @return string?
|
||||||
|
local function get_url(match, bufnr, capture, metadata)
|
||||||
|
---@type string|number|nil
|
||||||
|
local url = metadata[capture] and metadata[capture].url
|
||||||
|
|
||||||
|
if not url or type(url) == 'string' then
|
||||||
|
return url
|
||||||
|
end
|
||||||
|
|
||||||
|
if not match or not match[url] then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Assume there is only one matching node. If there is more than one, take the URL
|
||||||
|
-- from the first.
|
||||||
|
local other_node = match[url][1]
|
||||||
|
|
||||||
|
return vim.treesitter.get_node_text(other_node, bufnr, {
|
||||||
|
metadata = metadata[url],
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
--- @param capture_name string
|
||||||
|
--- @return boolean?, integer
|
||||||
|
local function get_spell(capture_name)
|
||||||
|
if capture_name == 'spell' then
|
||||||
|
return true, 0
|
||||||
|
elseif capture_name == 'nospell' then
|
||||||
|
-- Give nospell a higher priority so it always overrides spell captures.
|
||||||
|
return false, 1
|
||||||
|
end
|
||||||
|
return nil, 0
|
||||||
|
end
|
||||||
|
|
||||||
---@param self vim.treesitter.highlighter
|
---@param self vim.treesitter.highlighter
|
||||||
---@param buf integer
|
---@param buf integer
|
||||||
---@param line integer
|
---@param line integer
|
||||||
@ -284,37 +322,17 @@ local function on_line_impl(self, buf, line, is_spell_nav)
|
|||||||
|
|
||||||
for capture, nodes in pairs(match or {}) do
|
for capture, nodes in pairs(match or {}) do
|
||||||
local capture_name = state.highlighter_query:query().captures[capture]
|
local capture_name = state.highlighter_query:query().captures[capture]
|
||||||
local spell = nil ---@type boolean?
|
local spell, spell_pri_offset = get_spell(capture_name)
|
||||||
if capture_name == 'spell' then
|
|
||||||
spell = true
|
|
||||||
elseif capture_name == 'nospell' then
|
|
||||||
spell = false
|
|
||||||
end
|
|
||||||
|
|
||||||
local hl = state.highlighter_query:get_hl_from_capture(capture)
|
local hl = state.highlighter_query:get_hl_from_capture(capture)
|
||||||
|
|
||||||
-- Give nospell a higher priority so it always overrides spell captures.
|
|
||||||
local spell_pri_offset = capture_name == 'nospell' and 1 or 0
|
|
||||||
|
|
||||||
-- The "priority" attribute can be set at the pattern level or on a particular capture
|
-- The "priority" attribute can be set at the pattern level or on a particular capture
|
||||||
local priority = (
|
local priority = (
|
||||||
tonumber(metadata.priority or metadata[capture] and metadata[capture].priority)
|
tonumber(metadata.priority or metadata[capture] and metadata[capture].priority)
|
||||||
or vim.highlight.priorities.treesitter
|
or vim.highlight.priorities.treesitter
|
||||||
) + spell_pri_offset
|
) + spell_pri_offset
|
||||||
|
|
||||||
local url = metadata[capture] and metadata[capture].url ---@type string|number|nil
|
local url = get_url(match, buf, capture, metadata)
|
||||||
if type(url) == 'number' then
|
|
||||||
if match and match[url] then
|
|
||||||
-- Assume there is only one matching node. If there is more than one, take the URL
|
|
||||||
-- from the first.
|
|
||||||
local other_node = match[url][1]
|
|
||||||
url = vim.treesitter.get_node_text(other_node, buf, {
|
|
||||||
metadata = metadata[url],
|
|
||||||
})
|
|
||||||
else
|
|
||||||
url = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- The "conceal" attribute can be set at the pattern level or on a particular capture
|
-- The "conceal" attribute can be set at the pattern level or on a particular capture
|
||||||
local conceal = metadata.conceal or metadata[capture] and metadata[capture].conceal
|
local conceal = metadata.conceal or metadata[capture] and metadata[capture].conceal
|
||||||
|
Loading…
Reference in New Issue
Block a user