Skip to content

Commit

Permalink
feat: highlight range in grep buffer previewer (#2611)
Browse files Browse the repository at this point in the history
  • Loading branch information
luismeyer95 authored Jul 27, 2023
1 parent 1228f3b commit 2273594
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
4 changes: 3 additions & 1 deletion doc/telescope.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3724,7 +3724,9 @@ previewers.vim_buffer_vimgrep() *telescope.previewers.vim_buffer_vimgrep()*
It uses the `buffer_previewer` interface. To integrate this one into your
own picker make sure that the field `path` or `filename` and `lnum` is set
in each entry. If the latter is not present, it will default to the first
line. The preferred way of using this previewer is like this
line. Additionally, `lnend`, `col` and `colend` can be set to highlight a
text range instead of a single line. All line/column values are 1-indexed.
The preferred way of using this previewer is like this
`require('telescope.config').values.grep_previewer` This will respect user
configuration and will use `termopen_previewer` in case it's configured
that way.
Expand Down
35 changes: 29 additions & 6 deletions lua/telescope/previewers/buffer_previewer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,34 @@ previewers.vimgrep = defaulter(function(opts)
opts = opts or {}
local cwd = opts.cwd or vim.loop.cwd()

local jump_to_line = function(self, bufnr, lnum)
local jump_to_line = function(self, bufnr, entry)
pcall(vim.api.nvim_buf_clear_namespace, bufnr, ns_previewer, 0, -1)
if lnum and lnum > 0 then
pcall(vim.api.nvim_buf_add_highlight, bufnr, ns_previewer, "TelescopePreviewLine", lnum - 1, 0, -1)
pcall(vim.api.nvim_win_set_cursor, self.state.winid, { lnum, 0 })

if entry.lnum and entry.lnum > 0 then
local lnum, lnend = entry.lnum - 1, (entry.lnend or entry.lnum) - 1

local col, colend = 0, -1
-- Both col delimiters should be provided for them to take effect.
-- This is to ensure that column range highlighting was opted in, as `col`
-- is already used to determine the buffer jump position elsewhere.
if entry.col and entry.colend then
col, colend = entry.col - 1, entry.colend - 1
end

for i = lnum, lnend do
pcall(
vim.api.nvim_buf_add_highlight,
bufnr,
ns_previewer,
"TelescopePreviewLine",
i,
i == lnum and col or 0,
i == lnend and colend or -1
)
end

local middle_ln = math.floor(lnum + (lnend - lnum) / 2)
pcall(vim.api.nvim_win_set_cursor, self.state.winid, { middle_ln + 1, 0 })
vim.api.nvim_buf_call(bufnr, function()
vim.cmd "norm! zz"
end)
Expand Down Expand Up @@ -547,14 +570,14 @@ previewers.vimgrep = defaulter(function(opts)
if entry.bufnr and (p == "[No Name]" or has_buftype) then
local lines = vim.api.nvim_buf_get_lines(entry.bufnr, 0, -1, false)
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, lines)
jump_to_line(self, self.state.bufnr, entry.lnum)
jump_to_line(self, self.state.bufnr, entry)
else
conf.buffer_previewer_maker(p, self.state.bufnr, {
bufname = self.state.bufname,
winid = self.state.winid,
preview = opts.preview,
callback = function(bufnr)
jump_to_line(self, bufnr, entry.lnum)
jump_to_line(self, bufnr, entry)
end,
file_encoding = opts.file_encoding,
})
Expand Down
3 changes: 2 additions & 1 deletion lua/telescope/previewers/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ previewers.vim_buffer_cat = buffer_previewer.cat
--- It uses the `buffer_previewer` interface. To integrate this one into your
--- own picker make sure that the field `path` or `filename` and `lnum` is set
--- in each entry. If the latter is not present, it will default to the first
--- line.
--- line. Additionally, `lnend`, `col` and `colend` can be set to highlight a
--- text range instead of a single line. All line/column values are 1-indexed.
--- The preferred way of using this previewer is like this
--- `require('telescope.config').values.grep_previewer`
--- This will respect user configuration and will use `termopen_previewer` in
Expand Down

0 comments on commit 2273594

Please sign in to comment.