Skip to content

Add --match-context N for same-line excerpts - #228

Merged
Shengyu Fu (shengyfu) merged 7 commits into
microsoft:mainfrom
kubasur:match-context
Nov 25, 2026
Merged

Shengyu Fu (shengyfu) merged 7 commits into
microsoft:mainfrom
kubasur:match-context

Conversation

@kubasur

@kubasur Kubas (kubasur) commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: 0fa212690b6a04fae7962414afaaa8810b2729e3

Dimension Assessment Rationale
Change confidence 9/10 ↑ Added a regression test for replacement-introduced CRLF and verified the expected excerpt boundary.
↑ All three match-context integration tests and workspace Clippy with warnings denied pass on Windows.
↑ Formatting and whitespace checks pass.
↓ CI for this pushed revision has not yet started or been inspected.
Diagnosis confidence 10/10 ↑ Reproduced the reported hit-hit/X\r\nY case before the change; the right-context scan admitted CR before LF.
↑ The regression test fails before the fix and passes after excluding CR when followed by LF.
Evidence fit 10/10 ↑ The reproducer uses the exact input, match-context radius, and CRLF replacement from the review comment; output now omits the CR at the excerpt boundary.

Coding agents need to see context around matches. On a long line of minified JavaScript, a few characters around a function name can show how it’s called. --match-context N extends -o to print up to N Unicode characters on either side of a match.

tgrep --match-context 30 'PresShell::Destroy|nsGlobalWindowInner::FreeInnerObjects' .

The flag implies -o. The matcher searches for the original expression and the output code extends the excerpt to the requested width or the line boundary. Highlighting, columns, and byte offsets still identify the match. A radius of zero behaves like -o. The CLI and server use the same excerpt logic.

Firefox benchmark

On Firefox, plain -o and --match-context 30 returned the same 10 match locations from 51 candidate files. The regex workaround, .{0,30}(?:pattern).{0,30}, produced a MatchAll plan and searched 469,879 of 470,722 indexed files. Firefox revision

Indexed search Median of three runs
Plain -o, patched build 0.43 s
--match-context 30, patched build 0.31 s
Regex workaround, released tgrep 1.2.8 105 s

Testing

Tested on Apple Silicon with macOS 26.6.2 and Rust 1.98.0.

Check Result
cargo fmt --all --check Passed.
cargo test --locked -p tgrep-cli --test ripgrep_compat match_context Both tests passed. They check Unicode excerpts, match positions, highlighting, replacements, JSON output, and indexed candidate counts.
Clippy across all workspace targets with managed-test-hooks Completed with one needless_return warning in tgrep-core/src/managed/authentication_native.rs:89. The documented -D warnings check failed on that warning. The same failure reproduced on unmodified main.
Workspace tests with default features 1,532 passed, 1 failed, 4 ignored. A daemon polling test called testing.reconcile-timer, which requires test hooks. The same failure reproduced on unmodified main.
Workspace tests with managed-test-hooks 1,598 passed, 1 failed, 7 ignored. The managed collection crash-recovery test failed during the suite and passed when rerun in isolation.

Maintainer follow-ups

  • Clippy fixes for Rust 1.100 (be752d8).
  • --match-context N > 0 now prints context around an empty -U --replace replacement; plain -o still prints nothing (1fa1a9c).
  • Version bump to 1.2.9 (f004148).

Closes #227

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Direct searches with match_context: 0 and detail: false retain only the first match per line, incorrectly omitting additional matching excerpts.

1 open issue
What changed in this PR

Adds --match-context N to show same-line Unicode context around each match without changing the search expression or indexed query plan, addressing #227.

Changes:

  • Adds the CLI option and shared excerpt rendering for local and server searches.
  • Extends server protocols with capability checks for compatible fallback.
  • Documents the flag and adds coverage for excerpts, positions, replacements, JSON, and indexed candidate counts.
File Description
tgrep-cli/​tests/​shared_daemon.rs Adds shared-daemon excerpt coverage.
tgrep-cli/​tests/​ripgrep_compat.rs Tests rendering, backend consistency, and capability fallback.
tgrep-cli/​src/​serve/​shared/​server.rs Validates the option and advertises shared-v1 support.
tgrep-cli/​src/​serve/​shared/​managed.rs Advertises managed shared-v2 support.
tgrep-cli/​src/​serve.rs Parses the radius and configures server matching.
tgrep-cli/​src/​search.rs Passes rendering options and checks server support.
tgrep-cli/​src/​matching.rs Implements Unicode-aware excerpts and highlighting spans.
tgrep-cli/​src/​main.rs Exposes the flag and makes it imply -o.
README.md Documents excerpt and output semantics.

Review effort: Balanced

Comment thread tgrep-cli/src/serve.rs Resolved
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Kubas <jakub.surga@aisle.com>
Copilot AI balanced review requested due to automatic review settings November 20, 2026 14:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Empty replacements are dropped with positive match context, causing valid surrounding text to disappear from output.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Show zero-width replacements when context radius is positive

tgrep-cli/​src/​matching.rs:1468

With --match-context=1 -U -r '' 'ab\ncd', searching xab\ncdz\n produces no visible output instead of xz. piece drops the empty replacement before this excerpt code runs, even though surrounding text is available. Admit zero-width replacements on their rewritten line when the radius is positive, while preserving radius-zero behavior.

1 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Empty-string replacements of multiline matches incorrectly drop the context anchor, causing positive-radius match context to print nothing instead of the expected surrounding text.

1 open issue

Review effort: Balanced

Comment thread tgrep-cli/src/matching.rs Resolved
Return platform capacity checks and stale merge results directly without changing their behavior.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 21, 2026 04:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Empty multiline replacements suppress surrounding context when the context radius is positive, making their behavior inconsistent with single-line replacements.

1 open issue

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Empty multiline replacements are rejected before context extraction, causing --match-context=1 to emit no excerpt instead of the expected surrounding text.

2 open issues

Review effort: Balanced

Comment thread tgrep-cli/src/matching.rs Resolved
With --match-context N > 0, an empty -U --replace replacement now anchors an
excerpt on the rewritten line that contains its position, so
`--match-context=1 -U -r '' 'ab\ncd'` on "xab\ncdz" prints "xz" instead of
nothing. Plain -o (radius 0) still prints nothing for empty replacements.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 01:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed to evaluate potentially large, unnecessary span allocations in filename/count modes that return without rendering excerpts.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

…lper

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 02:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The right-context loop includes the CR from a replacement-introduced CRLF, causing excerpts to copy part of a line terminator instead of stopping before it.

1 open issue

Review effort: Balanced

Comment thread tgrep-cli/src/matching.rs Resolved
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 06:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human review is needed to assess the time and memory regression from collecting every match span for count and files-only requests with match context, even though those modes never render excerpts.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@shengyfu
Shengyu Fu (shengyfu) merged commit a5e5648 into microsoft:main Nov 25, 2026
9 of 10 checks passed
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.

Add character-level context around matches without losing indexed search performance

3 participants