Skip to content

fix(grep): align preview highlight stripping with Rust query parser#343

Merged
dmtrKovalenko merged 2 commits intodmtrKovalenko:mainfrom
mvanhorn:fix/331-grep-highlight-alignment
Apr 8, 2026
Merged

fix(grep): align preview highlight stripping with Rust query parser#343
dmtrKovalenko merged 2 commits intodmtrKovalenko:mainfrom
mvanhorn:fix/331-grep-highlight-alignment

Conversation

@mvanhorn
Copy link
Copy Markdown
Contributor

@mvanhorn mvanhorn commented Apr 6, 2026

The Lua heuristic in highlight_grep_matches (location_utils.lua) used a simple prefix check to strip constraint tokens before highlighting. This diverged from the Rust GrepConfig parser in several ways:

  • Multi-word queries like foo bar *.rs only highlighted foo (first text part), not the full search text
  • type:rust and other key:value constraints weren't stripped
  • Tokens starting with . were incorrectly treated as constraints
  • Backslash-escaped constraints (e.g. \*.config as literal search text) weren't handled

This adds _is_grep_constraint() that matches the Rust parser's actual GrepConfig rules: extensions (*.rs), path segments (/src/), exclusions (!test), type filters (type:rust), and path-oriented globs. Text parts are now joined with space for highlighting, matching grep_text() on the Rust side.

Fixes #331

This contribution was developed with AI assistance (Claude Code).

@dmtrKovalenko
Copy link
Copy Markdown
Owner

I think we should simply expose the parsed query to the lua results to avoid this double reparse which will break once we add a new token type.

What are you trying to do ?

@mvanhorn
Copy link
Copy Markdown
Contributor Author

mvanhorn commented Apr 6, 2026

The highlight stripping in the preview was using a regex that didn't account for all the query syntax the Rust grep engine supports (like negation tokens). When the query had special tokens, the preview highlights would be misaligned.

Exposing the parsed query to lua makes sense - that would keep the Rust parser as the single source of truth and avoid any drift. Happy to take that approach instead if you can point me to where the results are serialized for lua.

@dmtrKovalenko
Copy link
Copy Markdown
Owner

There is lua_types.rs file. But we probably should only do this by request: add a new function that returns a breakdown of a query by string

mvanhorn and others added 2 commits April 6, 2026 19:29
The Lua heuristic in highlight_grep_matches used a simple prefix check
(^[*!/] or ^.) to strip constraints. This diverged from the Rust
GrepConfig parser in several ways:

- Multi-word queries like 'foo bar *.rs' only highlighted 'foo'
- Constraint prefixes like type:rust were not stripped
- Tokens starting with '.' were incorrectly treated as constraints
- Escaped constraint tokens (e.g. \*.config) were not handled

Replace the heuristic with _is_grep_constraint() that matches the
Rust parser's actual GrepConfig rules: extensions (*.rs), path segments
(/src/), exclusions (!test), type filters (type:rust), and path-oriented
globs. Use all text parts joined with space for highlighting, matching
grep_text() on the Rust side.

Fixes dmtrKovalenko#331
Replace the Lua-side constraint detection (_is_grep_constraint) with a
new parse_grep_query() function that delegates to the Rust GrepConfig
parser. This keeps the Rust parser as the single source of truth for
query parsing, avoiding drift when new token types are added.

The new function is exposed to Lua as fff.parse_grep_query(query) and
returns a table with the grep_text field (the search text with all
constraints stripped).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@mvanhorn mvanhorn force-pushed the fix/331-grep-highlight-alignment branch from 4586839 to d02642b Compare April 7, 2026 02:33
@mvanhorn
Copy link
Copy Markdown
Contributor Author

mvanhorn commented Apr 7, 2026

Replaced the Lua-side constraint detection with a new parse_grep_query() function in Rust that delegates to GrepConfig. The Lua code now calls fff.parse_grep_query(query).grep_text to get the search text with constraints stripped.

This removes _is_grep_constraint() entirely and keeps the Rust parser as the single source of truth. Also rebased on main.

@dmtrKovalenko dmtrKovalenko merged commit 477a8a2 into dmtrKovalenko:main Apr 8, 2026
41 checks passed
@mvanhorn
Copy link
Copy Markdown
Contributor Author

Thanks for the merge, @dmtrKovalenko!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

grep preview highlight logic diverges from core query parser semantics

2 participants