fix(previewer): rg errmsg is mistakenly taken as ctags pattern#2687
fix(previewer): rg errmsg is mistakenly taken as ctags pattern#2687
Conversation
Problem: When rg cmd fail with errmsg e.g. `rg: PCRE2: error` This will be parsed and treated as an entry(with lnum=0) `matchadd` will remain even buf changed in window, so we have to clear the matches on each preview. To reproduce: `FzfLua live_grep search=\] no_esc=true` then type `<bs>`, `]`, `<bs>`, `]`... (with rg --pcre2) Maybe this can also happened in `FzfLua global`.
📝 WalkthroughWalkthroughA single conditional check in the cursor highlight handler was modified to require Changes
Possibly related PRs
Poem
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
lua/fzf-lua/previewer/builtin.lua (1)
1268-1283: Narrow fix works, but stalematchaddhighlights can still linger across previews.Adding
entry.ctagto the guard correctly prevents rg error text from being treated as a ctag pattern, and matches the convention used inactions.lua(context snippet 1). However,fn.clearmatches()now lives inside this branch — so when a subsequent entry has noctag/lnum(e.g., an rg error entry, or an entry that falls through to thelnum < 1path at line 1285), any previousmatchaddhighlight applied during an earlier ctag-based preview in the same preview window will persist. Given the PR's "always-clear-match" framing, you may want to hoist the clear out of the conditional so it runs on everyset_cursor_hlinvocation.♻️ Optional refinement
local cached_pos = self.bcache:get_pos(buf) if type(cached_pos) ~= "table" then cached_pos = nil end local lnum, col = entry.line, math.max(1, entry.col or 1) + pcall(api.nvim_win_call, win, function() fn.clearmatches() end) + if (not lnum or lnum == 0) and entry.ctag and regex then - -- pcall(fn.clearmatches, self.win.preview_winid) pcall(api.nvim_win_call, win, function() -- start searching at line 1 in case we -- didn't reload the buffer (same file) api.nvim_win_set_cursor(0, { 1, 0 }) - fn.clearmatches() -- test the regex so we can alert the user of the search fail if not utils.vim_regex(regex, { silent = true }) then return end🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lua/fzf-lua/previewer/builtin.lua` around lines 1268 - 1283, The highlight-clearing call fn.clearmatches() is currently only run inside the ctag/lnum branch, allowing previous matchadd highlights to persist for later previews; move the clearmatches call so it always runs at the start of the preview-window callback. Specifically, invoke pcall(api.nvim_win_call, win, function() fn.clearmatches() ... end) (or place fn.clearmatches() at the top of the existing api.nvim_win_call callback) before any conditional logic that checks entry.ctag/lnum/regex, remove the duplicate inside that branch if present, and keep the existing matchadd(fn.matchadd or hls.search) behavior only when entry.ctag and regex are valid so highlights are applied but always cleared first.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@lua/fzf-lua/previewer/builtin.lua`:
- Around line 1268-1283: The highlight-clearing call fn.clearmatches() is
currently only run inside the ctag/lnum branch, allowing previous matchadd
highlights to persist for later previews; move the clearmatches call so it
always runs at the start of the preview-window callback. Specifically, invoke
pcall(api.nvim_win_call, win, function() fn.clearmatches() ... end) (or place
fn.clearmatches() at the top of the existing api.nvim_win_call callback) before
any conditional logic that checks entry.ctag/lnum/regex, remove the duplicate
inside that branch if present, and keep the existing matchadd(fn.matchadd or
hls.search) behavior only when entry.ctag and regex are valid so highlights are
applied but always cleared first.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: d51632cb-f4ef-4baa-a432-e196ee714540
📒 Files selected for processing (1)
lua/fzf-lua/previewer/builtin.lua
If it happened, maybe if anyone mix ctags with lsp in |
Problem:
When rg cmd fail with errmsg e.g.
rg: PCRE2: errorThis will be parsed and treated as an entry(with lnum=0)
matchaddwill remain even buf changed in window, sowe have to clear the matches on each preview.
To reproduce:
FzfLua live_grep search=\] no_esc=truethen type
<bs>,],<bs>,]... (with rg --pcre2)Maybe this can also happened in
FzfLua global.Summary by CodeRabbit