Skip to content

Commit b2bf9bd

Browse files
committed
feat(previewer): end_line, end_col
1 parent f2e39e6 commit b2bf9bd

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

lua/fzf-lua/previewer/builtin.lua

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,18 +1319,17 @@ function Previewer.buffer_or_file:set_cursor_hl(entry)
13191319
return
13201320
end
13211321

1322-
self.orig_pos = { lnum, math.max(0, col - 1) }
1322+
self.orig_pos = { lnum, math.max(1, col) - 1 }
13231323
api.nvim_win_set_cursor(win, cached_pos or self.orig_pos)
13241324
self:maybe_set_cursorline(win, self.orig_pos)
1325-
-- fn.clearmatches() is slow for bigfile
1326-
if self.match_id then
1327-
pcall(fn.matchdelete, self.match_id)
1328-
self.match_id = nil
1329-
end
1325+
1326+
self.ns_previewer = self.ns_previewer or api.nvim_create_namespace("fzf-lua.preview.hl")
1327+
api.nvim_buf_clear_namespace(buf, self.ns_previewer, 0, -1)
1328+
local extmark
13301329

13311330
-- If regex is available (grep/lgrep), match on current line
1332-
local regex_start, regex_end = 0, nil
13331331
if regex and hls.search then
1332+
local regex_start, regex_end = 0, nil
13341333
-- vim.regex is always magic, see `:help vim.regex`
13351334
local ok, reg = pcall(vim.regex, regex)
13361335
if ok then
@@ -1348,13 +1347,19 @@ function Previewer.buffer_or_file:set_cursor_hl(entry)
13481347
regex, reg)
13491348
end
13501349
if regex_start > 0 then
1351-
self.match_id = fn.matchaddpos(hls.search, { { lnum, regex_start, regex_end } }, 11) ---@as integer
1350+
extmark = api.nvim_buf_set_extmark(buf, self.ns_previewer, lnum - 1, regex_start - 1, {
1351+
end_line = lnum - 1,
1352+
end_col = regex_start - 1 + regex_end,
1353+
hl_group = hls.search,
1354+
})
13521355
end
13531356
end
13541357

13551358
-- Fallback to cursor hl, only if column exists
1356-
if regex_start <= 0 and hls.cursor and col > 0 then
1357-
self.match_id = fn.matchaddpos(hls.cursor, { { lnum, math.max(1, col) } }, 11) ---@as integer
1359+
if not extmark and hls.cursor and col > 0 then
1360+
local end_lnum, end_col = entry.end_line or lnum, entry.end_col or col + 1
1361+
api.nvim_buf_set_extmark(buf, self.ns_previewer, lnum - 1, math.max(1, col) - 1,
1362+
{ end_line = end_lnum - 1, end_col = math.max(1, end_col) - 1, hl_group = hls.cursor })
13581363
end
13591364

13601365
utils.zz()

lua/fzf-lua/types.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ local FzfLua = require("fzf-lua")
88
---@field stripped? string
99
---@field path? string
1010
---@field bufnr? integer
11-
---@field line? integer
12-
---@field col? integer
11+
---@field line? integer 1-based
12+
---@field col? integer 1-based
1313
---@field bufname? string
1414
---@field terminal? boolean
1515
---@field ctag? string
@@ -25,6 +25,8 @@ local FzfLua = require("fzf-lua")
2525
---@field cached? fzf-lua.buffer_or_file.Bcache
2626
---@field filetype? string
2727
---@field content? string[]
28+
---@field end_line? integer 1-based
29+
---@field end_col? integer 1-based
2830

2931
---@class fzf-lua.keymap.Entry
3032
---@field vmap string?

0 commit comments

Comments
 (0)