feat: highlight range in grep buffer previewer (#2611)

This commit is contained in:
Luis
2023-07-27 09:24:50 +02:00
committed by GitHub
parent 1228f3b15c
commit 22735947d8
3 changed files with 34 additions and 8 deletions

View File

@@ -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 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 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 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 `require('telescope.config').values.grep_previewer` This will respect user
configuration and will use `termopen_previewer` in case it's configured configuration and will use `termopen_previewer` in case it's configured
that way. that way.

View File

@@ -511,11 +511,34 @@ previewers.vimgrep = defaulter(function(opts)
opts = opts or {} opts = opts or {}
local cwd = opts.cwd or vim.loop.cwd() 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) 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) if entry.lnum and entry.lnum > 0 then
pcall(vim.api.nvim_win_set_cursor, self.state.winid, { lnum, 0 }) 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.api.nvim_buf_call(bufnr, function()
vim.cmd "norm! zz" vim.cmd "norm! zz"
end) end)
@@ -547,14 +570,14 @@ previewers.vimgrep = defaulter(function(opts)
if entry.bufnr and (p == "[No Name]" or has_buftype) then if entry.bufnr and (p == "[No Name]" or has_buftype) then
local lines = vim.api.nvim_buf_get_lines(entry.bufnr, 0, -1, false) 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) 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 else
conf.buffer_previewer_maker(p, self.state.bufnr, { conf.buffer_previewer_maker(p, self.state.bufnr, {
bufname = self.state.bufname, bufname = self.state.bufname,
winid = self.state.winid, winid = self.state.winid,
preview = opts.preview, preview = opts.preview,
callback = function(bufnr) callback = function(bufnr)
jump_to_line(self, bufnr, entry.lnum) jump_to_line(self, bufnr, entry)
end, end,
file_encoding = opts.file_encoding, file_encoding = opts.file_encoding,
}) })

View File

@@ -254,7 +254,8 @@ previewers.vim_buffer_cat = buffer_previewer.cat
--- It uses the `buffer_previewer` interface. To integrate this one into your --- 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 --- 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 --- 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 --- The preferred way of using this previewer is like this
--- `require('telescope.config').values.grep_previewer` --- `require('telescope.config').values.grep_previewer`
--- This will respect user configuration and will use `termopen_previewer` in --- This will respect user configuration and will use `termopen_previewer` in