fix finder highlight in grep

This commit is contained in:
2026-02-03 11:12:59 +02:00
parent b84e46bda1
commit 2c8524df9d
4 changed files with 72 additions and 43 deletions

View File

@@ -16,11 +16,11 @@ local M = {}
--
function M.get()
return {
{
ft = 'yaml.helm-values',
ts = 'helm',
lsp = 'helm-ls',
},
-- {
-- ft = 'helm',
-- ts = 'helm',
-- lsp = 'helm-ls',
-- },
{
ft = 'yaml',
ts = 'yaml',

View File

@@ -152,13 +152,17 @@ local function save_cache(root, files)
end
local function project_root()
local obj = vim.system({ 'git', 'rev-parse', '--show-toplevel' }, { text = true }):wait()
if obj.code == 0 and obj.stdout and obj.stdout ~= '' then
return vim.trim(obj.stdout)
end
return vim.fn.getcwd(0, 0)
end
-- local function project_root()
-- local obj = vim.system({ 'git', 'rev-parse', '--show-toplevel' }, { text = true }):wait()
-- if obj.code == 0 and obj.stdout and obj.stdout ~= '' then
-- return vim.trim(obj.stdout)
-- end
-- return vim.fn.getcwd(0, 0)
-- end
local function normalize_rel(root, p)
if not p or p == '' then
return ''
@@ -244,53 +248,49 @@ local function render()
-- For grep mode, reformat lines with fixed-width path column
local offset_map = {} -- maps display line -> character offset for highlighting
-- In render(), replace the grep reformatting section:
-- In render(), replace the grep reformatting section:
if S.mode == 'grep' and total > 0 then
local path_width = M.config.grep_path_width or 40
for i = 1, #view do
local line = view[i]
if line then
-- Parse original vimgrep format: file:line:col:content
local file, lnum, col = line:match('^(.-):(%d+):(%d+):')
if file and lnum and col then
-- Pad line number to 3 chars with underscores
local file, lnum, col, content_start_pos = line:match('^(.-):(%d+):(%d+):()')
if file and lnum and col and content_start_pos then
local padded_lnum = lnum
while #padded_lnum < 3 do
padded_lnum = ' ' .. padded_lnum
end
local lineinfo = ':' .. padded_lnum .. '|'
local content_start = #file + 1 + #lnum + 1 + #col + 2 -- file + :lnum: + col:
local content_part = line:sub(content_start)
local content_part = line:sub(content_start_pos)
-- Calculate how much space we need for file + lineinfo
local prefix_len = #file + #lineinfo
local formatted_line
local new_content_start -- 1-based position where content starts in formatted line
if prefix_len > path_width then
-- Need to truncate the filepath part only
local available_for_file = path_width - #lineinfo
if available_for_file > 1 then
local truncated_file = '' .. file:sub(-(available_for_file - 1))
formatted_line = truncated_file .. lineinfo .. content_part
-- Offset is: original_file_length - truncated_file_length
offset_map[i] = #file - #truncated_file
new_content_start = #truncated_file + #lineinfo + 1
else
-- Extreme case: line info itself is too long, just show what we can
formatted_line = ('' .. file):sub(1, path_width) .. lineinfo .. content_part
offset_map[i] = #file - (path_width - #lineinfo)
new_content_start = path_width + #lineinfo + 1
end
elseif prefix_len < path_width then
-- Right-align by padding before the filepath
local padding = string.rep(' ', path_width - prefix_len)
formatted_line = padding .. file .. lineinfo .. content_part
offset_map[i] = -(path_width - prefix_len) -- negative for padding added
new_content_start = #padding + #file + #lineinfo + 1
else
formatted_line = file .. lineinfo .. content_part
offset_map[i] = 0
new_content_start = #file + #lineinfo + 1
end
view[i] = formatted_line
offset_map[i] = content_start_pos - new_content_start
end
end
end
@@ -397,26 +397,32 @@ local function compute_positions_grep(lines, q)
if q == '' then
return lines, {}
end
-- Match rg's --smart-case: case-insensitive unless query has uppercase
local case_insensitive = not q:match('[A-Z]')
local qmatch = case_insensitive and q:lower() or q
local positions = {}
for i, line in ipairs(lines) do
local lmatch = case_insensitive and tostring(line or ''):lower() or tostring(line or '')
local spans = {}
local sidx = 1
while true do
local s, e = lmatch:find(qmatch, sidx, true)
if not s then
break
end
table.insert(spans, { s - 1, e })
sidx = e + 1
if #spans > 64 then
break
local file, lnum, col, content_start_pos = line:match('^(.-):(%d+):(%d+):()')
if not content_start_pos then
positions[i] = nil
else
local content = line:sub(content_start_pos)
local lmatch = case_insensitive and content:lower() or content
local spans = {}
local sidx = 1
while true do
local s, e = lmatch:find(qmatch, sidx, true)
if not s then
break
end
-- Use content_start_pos from the pattern match, not manual calculation
table.insert(spans, { content_start_pos - 1 + s - 1, content_start_pos - 1 + e })
sidx = e + 1
if #spans > 64 then
break
end
end
positions[i] = (#spans > 0) and spans or nil
end
positions[i] = (#spans > 0) and spans or nil
end
return lines, positions
end
@@ -715,7 +721,6 @@ local function grep_async(query, cb)
'/',
}
-- Add excludes before the pattern
for _, p in ipairs(M.config.exclude_patterns or {}) do
table.insert(args, '--glob')
table.insert(args, '!' .. p)
@@ -730,9 +735,17 @@ local function grep_async(query, cb)
return
end
local list = {}
local seen = {} -- track file:line to deduplicate
if obj.stdout and #obj.stdout > 0 then
for line in obj.stdout:gmatch('[^\n]+') do
list[#list + 1] = line
local file, lnum = line:match('^(.-):(%d+):%d+:')
if file and lnum then
local key = file .. ':' .. lnum
if not seen[key] then
seen[key] = true
list[#list + 1] = line
end
end
end
end
if #list > M.config.max_items then