Skip to content

Commit 0b2c801

Browse files
authored
feat: buf highlights for current buffer fuzzy find (#732)
* feat: Add buffer highlights from treesitter * fix: Handle not having tree sitter in some buffers * fixup * fixup * fixup: move back to old node
1 parent d0cf646 commit 0b2c801

5 files changed

Lines changed: 104 additions & 3 deletions

File tree

lua/telescope/builtin/files.lua

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ files.current_buffer_fuzzy_find = function(opts)
332332
-- All actions are on the current buffer
333333
local bufnr = vim.api.nvim_get_current_buf()
334334
local filename = vim.fn.expand(vim.api.nvim_buf_get_name(bufnr))
335+
local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype")
335336

336337
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
337338
local lines_with_numbers = {}
@@ -345,6 +346,53 @@ files.current_buffer_fuzzy_find = function(opts)
345346
})
346347
end
347348

349+
local ok, parser = pcall(vim.treesitter.get_parser, bufnr, filetype)
350+
if ok then
351+
local query = vim.treesitter.get_query(filetype, "highlights")
352+
353+
local root = parser:parse()[1]:root()
354+
355+
local highlighter = vim.treesitter.highlighter.new(parser)
356+
local highlighter_query = highlighter:get_query(filetype)
357+
358+
local line_highlights = setmetatable({}, {
359+
__index = function(t, k)
360+
local obj = {}
361+
rawset(t, k, obj)
362+
return obj
363+
end,
364+
})
365+
for id, node in query:iter_captures(root, bufnr, 0, -1) do
366+
local hl = highlighter_query.hl_cache[id]
367+
if hl then
368+
local row1, col1, row2, col2 = node:range()
369+
370+
if row1 == row2 then
371+
local row = row1 + 1
372+
373+
for index = col1, col2 do
374+
line_highlights[row][index] = hl
375+
end
376+
else
377+
local row = row1 + 1
378+
for index = col1, #lines[row] do
379+
line_highlights[row][index] = hl
380+
end
381+
382+
while row < row2 + 1 do
383+
row = row + 1
384+
385+
for index = 0, #lines[row] do
386+
line_highlights[row][index] = hl
387+
end
388+
end
389+
end
390+
end
391+
end
392+
393+
opts.line_highlights = line_highlights
394+
end
395+
348396
pickers.new(opts, {
349397
prompt_title = 'Current Buffer Fuzzy',
350398
finder = finders.new_table {

lua/telescope/make_entry.lua

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,14 +685,30 @@ function make_entry.gen_from_buffer_lines(opts)
685685
separator = '',
686686
items = {
687687
{ width = 5 },
688-
{ remaining = true },
688+
{ remaining = true, },
689689
},
690690
}
691691

692692
local make_display = function(entry)
693+
693694
return displayer {
694695
{ entry.lnum, opts.lnum_highlight_group or 'TelescopeResultsSpecialComment' },
695-
entry.line
696+
{
697+
entry.line, function()
698+
if not opts.line_highlights then return {} end
699+
700+
local line_hl = opts.line_highlights[entry.lnum] or {}
701+
-- TODO: We could probably squash these together if the are the same...
702+
-- But I don't think that it's worth it at the moment.
703+
local result = {}
704+
705+
for col, hl in pairs(line_hl) do
706+
table.insert(result, { {col, col+1}, hl })
707+
end
708+
709+
return result
710+
end
711+
},
696712
}
697713
end
698714

lua/telescope/pickers/entry_display.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,16 @@ entry_display.create = function(configuration)
5959
hl_start = hl_start + #results[j] + (#configuration.separator or 1)
6060
end
6161
local hl_end = hl_start + #str:gsub('%s*$', '')
62-
table.insert(highlights, { { hl_start, hl_end }, hl })
62+
63+
if type(hl) == "function" then
64+
for _, hl_res in ipairs(hl()) do
65+
table.insert(highlights, { { hl_res[1][1] + hl_start, hl_res[1][2] + hl_start }, hl_res[2] })
66+
end
67+
else
68+
table.insert(highlights, { { hl_start, hl_end }, hl })
69+
end
6370
end
71+
6472
table.insert(results, str)
6573
end
6674
end
@@ -75,6 +83,7 @@ entry_display.create = function(configuration)
7583
table.insert(highlights, { { hl_start, hl_end }, configuration.separator_hl })
7684
end
7785
end
86+
7887
local final_str = table.concat(results, configuration.separator or "")
7988
if configuration.hl_chars then
8089
for i = 1, #final_str do

scratch/buffer_highlights.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
local a = vim.api
2+
3+
local ns = a.nvim_create_namespace("treesitter/highlighter")
4+
print(ns)
5+
local bufnr = 0
6+
7+
-- P(a.nvim_buf_get_extmarks(bufnr, ns, 0, -1, { details = true }))
8+
9+
local parser = vim.treesitter.get_parser(bufnr, "lua")
10+
local query = vim.treesitter.get_query("lua", "highlights")
11+
P(query)
12+
13+
local root = parser:parse()[1]:root()
14+
print("root", root)
15+
16+
local highlighter = vim.treesitter.highlighter.new(parser)
17+
local highlighter_query = highlighter:get_query("lua")
18+
19+
for id, node, metadata in query:iter_captures(root, bufnr, 0, -1) do
20+
local row1, col1, row2, col2 = node:range()
21+
print(highlighter_query.hl_cache[id])
22+
-- print(id, node, metadata, vim.treesitter.get_node_text(node, bufnr))
23+
-- print(">>>>", row1, col1, row2, col2)
24+
end

scratch/short_buf.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
local x = {}
2+
print(x); print("wow");
3+
4+
local function other() end

0 commit comments

Comments
 (0)