mirror of
https://github.com/neovim/neovim.git
synced 2024-12-24 13:15:09 -07:00
perf(treesitter): filter out trees outside the visible range early
Problem: Treesitter highlighter's on_line was iterating all the parsed trees, which can be quite a lot when injection is used. This may slow down scrolling and cursor movement in big files with many comment injections (e.g., lsp/_meta/protocol.lua). Solution: In on_win, collect trees inside the visible range, and use them in on_line. NOTE: This optimization depends on the correctness of on_win's botline_guess parameter (i.e., it's always greater than or equal to the line numbers passed to on_line). The documentation does not guarantee this, but I have never noticed a problem so far.
This commit is contained in:
parent
f0eb3ca916
commit
c0cb1e8e94
@ -5,14 +5,16 @@ local Range = require('vim.treesitter._range')
|
|||||||
---@alias TSHlIter fun(end_line: integer|nil): integer, TSNode, TSMetadata
|
---@alias TSHlIter fun(end_line: integer|nil): integer, TSNode, TSMetadata
|
||||||
|
|
||||||
---@class TSHighlightState
|
---@class TSHighlightState
|
||||||
|
---@field tstree TSTree
|
||||||
---@field next_row integer
|
---@field next_row integer
|
||||||
---@field iter TSHlIter|nil
|
---@field iter TSHlIter|nil
|
||||||
|
---@field highlighter_query TSHighlighterQuery
|
||||||
|
|
||||||
---@class TSHighlighter
|
---@class TSHighlighter
|
||||||
---@field active table<integer,TSHighlighter>
|
---@field active table<integer,TSHighlighter>
|
||||||
---@field bufnr integer
|
---@field bufnr integer
|
||||||
---@field orig_spelloptions string
|
---@field orig_spelloptions string
|
||||||
---@field _highlight_states table<TSTree,TSHighlightState>
|
---@field _highlight_states TSHighlightState[]
|
||||||
---@field _queries table<string,TSHighlighterQuery>
|
---@field _queries table<string,TSHighlighterQuery>
|
||||||
---@field tree LanguageTree
|
---@field tree LanguageTree
|
||||||
---@field redraw_count integer
|
---@field redraw_count integer
|
||||||
@ -157,18 +159,47 @@ function TSHighlighter:destroy()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@package
|
---@param srow integer
|
||||||
---@param tstree TSTree
|
---@param erow integer exclusive
|
||||||
---@return TSHighlightState
|
---@private
|
||||||
function TSHighlighter:get_highlight_state(tstree)
|
function TSHighlighter:prepare_highlight_states(srow, erow)
|
||||||
if not self._highlight_states[tstree] then
|
self.tree:for_each_tree(function(tstree, tree)
|
||||||
self._highlight_states[tstree] = {
|
if not tstree then
|
||||||
next_row = 0,
|
return
|
||||||
iter = nil,
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return self._highlight_states[tstree]
|
local root_node = tstree:root()
|
||||||
|
local root_start_row, _, root_end_row, _ = root_node:range()
|
||||||
|
|
||||||
|
-- Only worry about trees within the visible range
|
||||||
|
if root_start_row > erow or root_end_row < srow then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local highlighter_query = self:get_query(tree:lang())
|
||||||
|
|
||||||
|
-- Some injected languages may not have highlight queries.
|
||||||
|
if not highlighter_query:query() then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
-- _highlight_states should be a list so that the highlights are added in the same order as
|
||||||
|
-- for_each_tree traversal. This ensures that parents' highlight don't override children's.
|
||||||
|
table.insert(self._highlight_states, {
|
||||||
|
tstree = tstree,
|
||||||
|
next_row = 0,
|
||||||
|
iter = nil,
|
||||||
|
highlighter_query = highlighter_query,
|
||||||
|
})
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param fn fun(state: TSHighlightState)
|
||||||
|
---@package
|
||||||
|
function TSHighlighter:for_each_highlight_state(fn)
|
||||||
|
for _, state in ipairs(self._highlight_states) do
|
||||||
|
fn(state)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@private
|
---@private
|
||||||
@ -214,12 +245,8 @@ end
|
|||||||
---@param line integer
|
---@param line integer
|
||||||
---@param is_spell_nav boolean
|
---@param is_spell_nav boolean
|
||||||
local function on_line_impl(self, buf, line, is_spell_nav)
|
local function on_line_impl(self, buf, line, is_spell_nav)
|
||||||
self.tree:for_each_tree(function(tstree, tree)
|
self:for_each_highlight_state(function(state)
|
||||||
if not tstree then
|
local root_node = state.tstree:root()
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local root_node = tstree:root()
|
|
||||||
local root_start_row, _, root_end_row, _ = root_node:range()
|
local root_start_row, _, root_end_row, _ = root_node:range()
|
||||||
|
|
||||||
-- Only worry about trees within the line range
|
-- Only worry about trees within the line range
|
||||||
@ -227,17 +254,9 @@ local function on_line_impl(self, buf, line, is_spell_nav)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local state = self:get_highlight_state(tstree)
|
|
||||||
local highlighter_query = self:get_query(tree:lang())
|
|
||||||
|
|
||||||
-- Some injected languages may not have highlight queries.
|
|
||||||
if not highlighter_query:query() then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if state.iter == nil or state.next_row < line then
|
if state.iter == nil or state.next_row < line then
|
||||||
state.iter =
|
state.iter =
|
||||||
highlighter_query:query():iter_captures(root_node, self.bufnr, line, root_end_row + 1)
|
state.highlighter_query:query():iter_captures(root_node, self.bufnr, line, root_end_row + 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
while line >= state.next_row do
|
while line >= state.next_row do
|
||||||
@ -250,9 +269,9 @@ local function on_line_impl(self, buf, line, is_spell_nav)
|
|||||||
local start_row, start_col, end_row, end_col = Range.unpack4(range)
|
local start_row, start_col, end_row, end_col = Range.unpack4(range)
|
||||||
|
|
||||||
if capture then
|
if capture then
|
||||||
local hl = highlighter_query.hl_cache[capture]
|
local hl = state.highlighter_query.hl_cache[capture]
|
||||||
|
|
||||||
local capture_name = highlighter_query:query().captures[capture]
|
local capture_name = state.highlighter_query:query().captures[capture]
|
||||||
local spell = nil ---@type boolean?
|
local spell = nil ---@type boolean?
|
||||||
if capture_name == 'spell' then
|
if capture_name == 'spell' then
|
||||||
spell = true
|
spell = true
|
||||||
@ -327,6 +346,7 @@ function TSHighlighter._on_win(_, _win, buf, topline, botline)
|
|||||||
end
|
end
|
||||||
self.tree:parse({ topline, botline + 1 })
|
self.tree:parse({ topline, botline + 1 })
|
||||||
self:reset_highlight_state()
|
self:reset_highlight_state()
|
||||||
|
self:prepare_highlight_states(topline, botline + 1)
|
||||||
self.redraw_count = self.redraw_count + 1
|
self.redraw_count = self.redraw_count + 1
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user