Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: highlight range in previewer #2611

Merged
merged 2 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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