mirror of
https://github.com/folke/snacks.nvim
synced 2025-12-23 08:47:57 +00:00
feat(picker): add exact match position highlighting for grep results
Uses ripgrep's --replace feature to mark exact match positions with
separators (__snacks__${0}__snacks__), then parses these positions
for precise highlighting.
Benefits:
- Exact match highlighting in list, preview, and file formatter
- Works with any grep pattern (regex, fixed-string, case-insensitive)
- No need for pattern parsing or vim.regex workarounds
- Positions are provided directly by ripgrep, guaranteed accurate
Implementation:
- Added item.positions field to track match character indices
- New highlight.matches() helper for creating match extmarks
- Modified grep source to parse and extract positions from rg output
- Updated list, preview, and format to use positions when available
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
2aee35d059
commit
3b54c8d3d1
6 changed files with 85 additions and 21 deletions
|
|
@ -43,6 +43,7 @@ local M = {}
|
|||
---@field highlights? snacks.picker.Highlight[][]
|
||||
---@field preview? snacks.picker.Item.preview
|
||||
---@field resolve? fun(item:snacks.picker.Item)
|
||||
---@field positions? number[] indices of matched characters in `text`
|
||||
|
||||
---@class snacks.picker.finder.Item: snacks.picker.Item
|
||||
---@field idx? number
|
||||
|
|
|
|||
|
|
@ -499,27 +499,17 @@ function M:format(item)
|
|||
}
|
||||
it[field] = text:sub(extmark.col + 1, extmark.end_col)
|
||||
local positions = self.matcher:positions(it)
|
||||
for _, pos in ipairs(positions[field] or {}) do
|
||||
table.insert(extmarks, {
|
||||
col = pos - 1 + extmark.col,
|
||||
end_col = pos + extmark.col,
|
||||
hl_group = "SnacksPickerMatch",
|
||||
})
|
||||
end
|
||||
Snacks.picker.highlight.matches(extmarks, positions[field] or {}, extmark.col)
|
||||
end
|
||||
end
|
||||
|
||||
-- Highlight match positions for text
|
||||
local it = { text = text:gsub("%s*$", ""), idx = 1, score = 0, file = item.file }
|
||||
local positions = self.matcher:positions(it).text or {}
|
||||
vim.list_extend(positions, self.matcher_regex:positions(it).text or {})
|
||||
for _, pos in ipairs(positions) do
|
||||
table.insert(extmarks, {
|
||||
col = pos - 1,
|
||||
end_col = pos,
|
||||
hl_group = "SnacksPickerMatch",
|
||||
})
|
||||
if not item.positions then
|
||||
vim.list_extend(positions, self.matcher_regex:positions(it).text or {})
|
||||
end
|
||||
Snacks.picker.highlight.matches(extmarks, positions)
|
||||
return text, extmarks
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -318,7 +318,16 @@ function M:loc()
|
|||
|
||||
if self.item.pos and self.item.pos[1] > 0 and self.item.pos[1] <= line_count then
|
||||
show(self.item.pos)
|
||||
if self.item.end_pos then
|
||||
if self.item.positions then
|
||||
for _, extmark in ipairs(Snacks.picker.highlight.matches({}, self.item.positions)) do
|
||||
local col, row = extmark.col, self.item.pos[1]
|
||||
extmark.col = nil
|
||||
extmark.row = nil
|
||||
extmark.field = nil
|
||||
extmark.hl_group = "SnacksPickerSearch"
|
||||
pcall(vim.api.nvim_buf_set_extmark, self.win.buf, ns_loc, row - 1, col, extmark)
|
||||
end
|
||||
elseif self.item.end_pos then
|
||||
vim.api.nvim_buf_set_extmark(self.win.buf, ns_loc, self.item.pos[1] - 1, self.item.pos[2], {
|
||||
end_row = self.item.end_pos[1] - 1,
|
||||
end_col = self.item.end_pos[2],
|
||||
|
|
|
|||
|
|
@ -167,6 +167,10 @@ function M.file(item, picker)
|
|||
end
|
||||
|
||||
if item.line then
|
||||
if item.positions then
|
||||
local offset = Snacks.picker.highlight.offset(ret)
|
||||
Snacks.picker.highlight.matches(ret, item.positions, offset)
|
||||
end
|
||||
Snacks.picker.highlight.format(item, item.line, ret)
|
||||
table.insert(ret, { " " })
|
||||
end
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@ local M = {}
|
|||
|
||||
local uv = vim.uv or vim.loop
|
||||
|
||||
local MATCH_SEP = "__snacks__"
|
||||
|
||||
---@param opts snacks.picker.grep.Config
|
||||
---@param filter snacks.picker.Filter
|
||||
local function get_cmd(opts, filter)
|
||||
|
|
@ -11,12 +13,15 @@ local function get_cmd(opts, filter)
|
|||
"--no-heading",
|
||||
"--with-filename",
|
||||
"--line-number",
|
||||
"--replace",
|
||||
("%s${0}%s"):format(MATCH_SEP, MATCH_SEP),
|
||||
"--column",
|
||||
"--smart-case",
|
||||
"--max-columns=500",
|
||||
"--max-columns-preview",
|
||||
"--glob=!.bare",
|
||||
"--glob=!.git",
|
||||
"-0",
|
||||
}
|
||||
|
||||
args = vim.deepcopy(args)
|
||||
|
|
@ -116,17 +121,57 @@ function M.grep(opts, ctx)
|
|||
---@param item snacks.picker.finder.Item
|
||||
transform = function(item)
|
||||
item.cwd = cwd
|
||||
local file, line, col, text = item.text:match("^(.-):(%d+):(%d+):(.*)$")
|
||||
if not file then
|
||||
-- Split on NUL byte (which comes from rg's -0 flag)
|
||||
local file_sep = item.text:find("\0")
|
||||
if not file_sep then
|
||||
if not item.text:match("WARNING") then
|
||||
Snacks.notify.error("invalid grep output:\n" .. item.text)
|
||||
end
|
||||
return false
|
||||
else
|
||||
item.line = text
|
||||
item.file = file
|
||||
item.pos = { tonumber(line), tonumber(col) - 1 }
|
||||
end
|
||||
local file = item.text:sub(1, file_sep - 1)
|
||||
local rest = item.text:sub(file_sep + 1)
|
||||
item.text = file .. ":" .. rest
|
||||
---@type string?, string?, string?
|
||||
local line, col, text = rest:match("^(%d+):(%d+):(.*)$")
|
||||
if not (line and col and text) then
|
||||
if not item.text:match("WARNING") then
|
||||
Snacks.notify.error("invalid grep output:\n" .. item.text)
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- indices of matches
|
||||
local positions = {} ---@type number[]
|
||||
local from = tonumber(col)
|
||||
item.pos = { tonumber(line), from - 1 }
|
||||
|
||||
local offset = 0
|
||||
local in_match = false
|
||||
while from < #text do
|
||||
local idx = text:find(MATCH_SEP, from, true)
|
||||
if not idx then
|
||||
break
|
||||
end
|
||||
|
||||
if in_match then
|
||||
for i = from, idx - 1 do
|
||||
positions[#positions + 1] = i - offset
|
||||
end
|
||||
item.end_pos = item.end_pos or { item.pos[1], idx - offset - 1 }
|
||||
end
|
||||
in_match = not in_match
|
||||
|
||||
offset = offset + #MATCH_SEP
|
||||
from = idx + #MATCH_SEP
|
||||
end
|
||||
|
||||
item.file = file
|
||||
if #positions > 0 then
|
||||
text = text:gsub(MATCH_SEP, "")
|
||||
item.positions = positions
|
||||
end
|
||||
item.line = text
|
||||
end,
|
||||
},
|
||||
}, ctx)
|
||||
|
|
|
|||
|
|
@ -111,6 +111,21 @@ function M.offset(line, opts)
|
|||
return offset
|
||||
end
|
||||
|
||||
---@param line snacks.picker.Highlight[]
|
||||
---@param positions number[]
|
||||
---@param offset? number
|
||||
function M.matches(line, positions, offset)
|
||||
offset = offset or 0
|
||||
for _, pos in ipairs(positions) do
|
||||
table.insert(line, {
|
||||
col = pos - 1 + offset,
|
||||
end_col = pos + offset,
|
||||
hl_group = "SnacksPickerMatch",
|
||||
})
|
||||
end
|
||||
return line
|
||||
end
|
||||
|
||||
---@param line snacks.picker.Highlight[]
|
||||
---@param item snacks.picker.Item
|
||||
---@param text string
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue