perf(picker): cache treesitter line highlights

This commit is contained in:
Folke Lemaitre 2025-02-05 20:32:30 +01:00
parent 1bbc8f90fd
commit af31c31287
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
2 changed files with 12 additions and 5 deletions

View file

@ -5,7 +5,7 @@ local M = {}
function M.lines(opts)
local buf = opts.buf or 0
buf = buf == 0 and vim.api.nvim_get_current_buf() or buf
local extmarks = require("snacks.picker.util.highlight").get_highlights({ buf = buf })
local extmarks = require("snacks.picker.util.highlight").get_highlights({ buf = buf, extmarks = true })
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
local items = {} ---@type snacks.picker.finder.Item[]
for l, line in ipairs(lines) do

View file

@ -1,7 +1,7 @@
---@class snacks.picker.highlight
local M = {}
---@param opts? {buf?:number, code?:string, ft?:string, lang?:string, file?:string}
---@param opts? {buf?:number, code?:string, ft?:string, lang?:string, file?:string, extmarks?:boolean}
function M.get_highlights(opts)
opts = opts or {}
local source = assert(opts.buf or opts.code, "buf or code is required")
@ -65,7 +65,7 @@ function M.get_highlights(opts)
end
--- Add buffer extmarks
if opts.buf then
if opts.buf and opts.extmarks then
local extmarks = vim.api.nvim_buf_get_extmarks(opts.buf, -1, 0, -1, { details = true })
for _, extmark in pairs(extmarks) do
local row = extmark[2] + 1
@ -115,8 +115,15 @@ end
function M.format(item, text, line, opts)
opts = opts or {}
local offset = M.offset(line)
local highlights = M.get_highlights({ code = text, ft = item.ft, lang = opts.lang or item.lang, file = item.file })[1]
or {}
item._ = item._ or {}
item._.ts = item._.ts or {}
local highlights = item._.ts[text] ---@type table<number, snacks.picker.Extmark[]>?
if not highlights then
highlights = M.get_highlights({ code = text, ft = item.ft, lang = opts.lang or item.lang, file = item.file })[1]
or {}
item._.ts[text] = highlights
end
highlights = vim.deepcopy(highlights)
for _, extmark in ipairs(highlights) do
extmark.col = extmark.col + offset
extmark.end_col = extmark.end_col + offset