Skip to content

Commit

Permalink
perf: cache highlighter queries
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 21, 2023
1 parent c2a745a commit b4eb215
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lua/noice/text/treesitter.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---@diagnostic disable: invisible
local M = {}
M.queries = {}

---@param buf buffer
---@param injections table<string, number[][][]>
Expand All @@ -19,6 +20,13 @@ function M.highlight_markdown(buf, injections)
parser:parse()
end

function M.get_query(lang)
if not M.queries[lang] then
M.queries[lang] = vim.treesitter.query.get(lang, "highlights") or vim.treesitter.query.get_query(lang, "highlights")
end
return M.queries[lang]
end

function M.get_lang(ft)
return vim.treesitter.language.get_lang and vim.treesitter.language.get_lang(ft) or ft
end
Expand Down Expand Up @@ -56,10 +64,7 @@ function M.highlight(buf, ns, range, lang)
if not tstree then
return
end

local highlighter_query = vim.treesitter.query.get and vim.treesitter.query.get(tree:lang(), "highlights")
---@diagnostic disable-next-line: deprecated
or vim.treesitter.query.get_query(tree:lang(), "highlights")
local highlighter_query = M.get_query(tree:lang())

-- Some injected languages may not have highlight queries.
if not highlighter_query then
Expand Down

0 comments on commit b4eb215

Please sign in to comment.