aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/treesitter/highlighter.lua
diff options
context:
space:
mode:
authorGregory Anders <8965202+gpanders@users.noreply.github.com>2024-03-12 09:32:17 -0500
committerGitHub <noreply@github.com>2024-03-12 09:32:17 -0500
commitcb46f6e467268edf917cc3617b4c024a66b256de (patch)
tree32b5fea91d770d404b9b7aab69f9e2030fce3ac7 /runtime/lua/vim/treesitter/highlighter.lua
parent41fb98d6fab5aa02ef370d1b2b283b078517ffa4 (diff)
downloadrneovim-cb46f6e467268edf917cc3617b4c024a66b256de.tar.gz
rneovim-cb46f6e467268edf917cc3617b4c024a66b256de.tar.bz2
rneovim-cb46f6e467268edf917cc3617b4c024a66b256de.zip
feat(treesitter): support URLs (#27132)
Tree-sitter queries can add URLs to a capture using the `#set!` directive, e.g. (inline_link (link_text) @text.reference (link_destination) @text.uri (#set! @text.reference "url" @text.uri)) The pattern above is included by default in the `markdown_inline` highlight query so that users with supporting terminals will see hyperlinks. For now, this creates a hyperlink for *all* Markdown URLs of the pattern [link text](link url), even if `link url` does not contain a valid protocol (e.g. if `link url` is a path to a file). We may wish to change this in the future to only linkify when the URL has a valid protocol scheme, but for now we delegate handling this to the terminal emulator. In order to support directives which reference other nodes, the highlighter must be updated to use `iter_matches` rather than `iter_captures`. The former provides the `match` table which maps capture IDs to nodes. However, this has its own challenges: - `iter_matches` does not guarantee the order in which patterns are iterated matches the order in the query file. So we must enforce ordering manually using "subpriorities" (#27131). The pattern index of each match dictates the extmark's subpriority. - When injections are used, the highlighter contains multiple trees. The pattern indices of each tree must be offset relative to the maximum pattern index from all previous trees to ensure that extmarks appear in the correct order. - The `iter_captures` implementation currently has a bug where the "match" table is only returned for the first capture within a pattern (see #27274). This bug means that `#set!` directives in a query apply only to the first capture within a pattern. Unfortunately, many queries in the wild have come to depend on this behavior. `iter_matches` does not share this flaw, so switching to `iter_matches` exposed bugs in existing highlight queries. These queries have been updated in this repo, but may still need to be updated by users. The `#set!` directive applies to the _entire_ query pattern when used without a capture argument. To make `#set!` apply only to a single capture, the capture must be given as an argument.
Diffstat (limited to 'runtime/lua/vim/treesitter/highlighter.lua')
-rw-r--r--runtime/lua/vim/treesitter/highlighter.lua91
1 files changed, 66 insertions, 25 deletions
diff --git a/runtime/lua/vim/treesitter/highlighter.lua b/runtime/lua/vim/treesitter/highlighter.lua
index 388680259a..cc5e11d632 100644
--- a/runtime/lua/vim/treesitter/highlighter.lua
+++ b/runtime/lua/vim/treesitter/highlighter.lua
@@ -4,7 +4,7 @@ local Range = require('vim.treesitter._range')
local ns = api.nvim_create_namespace('treesitter/highlighter')
----@alias vim.treesitter.highlighter.Iter fun(end_line: integer|nil): integer, TSNode, vim.treesitter.query.TSMetadata
+---@alias vim.treesitter.highlighter.Iter fun(): integer, table<integer, TSNode[]>, vim.treesitter.query.TSMetadata
---@class (private) vim.treesitter.highlighter.Query
---@field private _query vim.treesitter.Query?
@@ -248,6 +248,13 @@ end
---@param line integer
---@param is_spell_nav boolean
local function on_line_impl(self, buf, line, is_spell_nav)
+ -- Track the maximum pattern index encountered in each tree. For subsequent
+ -- trees, the subpriority passed to nvim_buf_set_extmark is offset by the
+ -- largest pattern index from the prior tree. This ensures that extmarks
+ -- from subsequent trees always appear "on top of" extmarks from previous
+ -- trees (e.g. injections should always appear over base highlights).
+ local pattern_offset = 0
+
self:for_each_highlight_state(function(state)
local root_node = state.tstree:root()
local root_start_row, _, root_end_row, _ = root_node:range()
@@ -258,22 +265,24 @@ local function on_line_impl(self, buf, line, is_spell_nav)
end
if state.iter == nil or state.next_row < line then
- state.iter =
- state.highlighter_query:query():iter_captures(root_node, self.bufnr, line, root_end_row + 1)
+ state.iter = state.highlighter_query
+ :query()
+ :iter_matches(root_node, self.bufnr, line, root_end_row + 1, { all = true })
end
+ local max_pattern_index = -1
while line >= state.next_row do
- local capture, node, metadata = state.iter(line)
+ local pattern, match, metadata = state.iter()
- local range = { root_end_row + 1, 0, root_end_row + 1, 0 }
- if node then
- range = vim.treesitter.get_range(node, buf, metadata and metadata[capture])
+ if pattern and pattern > max_pattern_index then
+ max_pattern_index = pattern
end
- local start_row, start_col, end_row, end_col = Range.unpack4(range)
- if capture then
- local hl = state.highlighter_query:get_hl_from_capture(capture)
+ if not match then
+ state.next_row = root_end_row + 1
+ end
+ 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
@@ -282,28 +291,60 @@ local function on_line_impl(self, buf, line, is_spell_nav)
spell = false
end
+ 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
- if hl and end_row >= line and (not is_spell_nav or spell ~= nil) then
- local priority = (tonumber(metadata.priority) or vim.highlight.priorities.treesitter)
- + spell_pri_offset
- api.nvim_buf_set_extmark(buf, ns, start_row, start_col, {
- end_line = end_row,
- end_col = end_col,
- hl_group = hl,
- ephemeral = true,
- priority = priority,
- conceal = metadata.conceal,
- spell = spell,
- })
+ -- 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
- end
- if start_row > line then
- state.next_row = start_row
+ -- 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
+
+ for _, node in ipairs(nodes) do
+ local range = vim.treesitter.get_range(node, buf, metadata[capture])
+ local start_row, start_col, end_row, end_col = Range.unpack4(range)
+
+ if hl and end_row >= line and (not is_spell_nav or spell ~= nil) then
+ api.nvim_buf_set_extmark(buf, ns, start_row, start_col, {
+ end_line = end_row,
+ end_col = end_col,
+ hl_group = hl,
+ ephemeral = true,
+ priority = priority,
+ _subpriority = pattern_offset + pattern,
+ conceal = conceal,
+ spell = spell,
+ url = url,
+ })
+ end
+
+ if start_row > line then
+ state.next_row = start_row
+ end
+ end
end
end
+
+ pattern_offset = pattern_offset + max_pattern_index
end)
end