aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/treesitter/highlighter.lua
diff options
context:
space:
mode:
authorLewis Russell <lewis6991@gmail.com>2024-03-13 14:56:11 +0000
committerLewis Russell <me@lewisr.dev>2024-03-14 06:55:19 +0000
commit00c4962cd241044c9f02de39b34ca24b2711de43 (patch)
tree62830e7ccbd13878f40e046c03b153408848e6ac /runtime/lua/vim/treesitter/highlighter.lua
parent12faaf40f487132b9397d9f3e59e44840985612c (diff)
downloadrneovim-00c4962cd241044c9f02de39b34ca24b2711de43.tar.gz
rneovim-00c4962cd241044c9f02de39b34ca24b2711de43.tar.bz2
rneovim-00c4962cd241044c9f02de39b34ca24b2711de43.zip
refactor(treesitter): move some logic into functions
Diffstat (limited to 'runtime/lua/vim/treesitter/highlighter.lua')
-rw-r--r--runtime/lua/vim/treesitter/highlighter.lua62
1 files changed, 40 insertions, 22 deletions
diff --git a/runtime/lua/vim/treesitter/highlighter.lua b/runtime/lua/vim/treesitter/highlighter.lua
index 6175977b49..7bc6e5c019 100644
--- a/runtime/lua/vim/treesitter/highlighter.lua
+++ b/runtime/lua/vim/treesitter/highlighter.lua
@@ -252,6 +252,44 @@ function TSHighlighter:get_query(lang)
return self._queries[lang]
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 buf 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
local capture_name = state.highlighter_query:query().captures[capture]
- local spell = nil ---@type boolean?
- if capture_name == 'spell' then
- spell = true
- elseif capture_name == 'nospell' then
- spell = false
- end
+ local spell, spell_pri_offset = get_spell(capture_name)
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
local priority = (
tonumber(metadata.priority or metadata[capture] and metadata[capture].priority)
or vim.highlight.priorities.treesitter
) + spell_pri_offset
- local url = metadata[capture] and metadata[capture].url ---@type string|number|nil
- 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
+ local url = get_url(match, buf, capture, metadata)
-- 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