Skip to content

Fix whole-word literal search skipping overlapping valid matches - #344811

Draft
cloudspeedapps-coder wants to merge 10 commits into
microsoft:mainfrom
cloudspeedapps-coder:codex/fix-291591-whole-word-search
Draft

cloudspeedapps-coder wants to merge 10 commits into
microsoft:mainfrom
cloudspeedapps-coder:codex/fix-291591-whole-word-search

Conversation

@cloudspeedapps-coder

@cloudspeedapps-coder cloudspeedapps-coder commented Oct 28, 2026 •

Copy link
Copy Markdown

Fixes #291591.

Searching for aa-aa with Match Whole Word enabled and regex mode off misses the trailing aa-aa in xaa-aa-aa. The first candidate begins inside a word and is rejected, but search resumes after that entire candidate, skipping a valid overlapping start. Resume at the next possible word boundary after rejected literal candidates in both literal fast paths and the regex engine used internally for case-insensitive literal search and capture results. Accepted matches retain their existing non-overlapping behavior.

SearchData.isLiteralSearch carries the user's search mode into each Searcher. Overlap retries apply only to literal-text searches. User-entered regular expressions retain their prior behavior of advancing past rejected candidates. This avoids introducing repeated suffix scans for patterns such as a.*b on 'a '.repeat(8000) + 'bx'. This PR does not add overlap support for regex mode or promise bounded execution of arbitrary JavaScript regexes.

Non-global, non-sticky regexes terminate after their first match because they ignore lastIndex. This includes both rejected and accepted empty matches; global and sticky empty matches continue to advance normally.

Related work: #326447 is an existing open proposal for this issue. I reviewed that proposal during validation, including its performance and non-global-regex considerations. This draft focuses on literal-text search and does not supersede that contributor's work.

Validation

  • Reproduced both Copilot findings with bounded tests before the correction: rejected non-global empty matches repeated, and the 8,000-word regex case executed 8,001 scans.
  • The corrected performance regression asserts at most two scans. Termination tests cover several zero-width patterns plus accepted non-global, global, and sticky matches.
  • Retains coverage for the original issue across case modes, capture results, chunked piece-tree buffers, forward/backward search, LF/CRLF, and Unicode surrogate pairs. Regex-mode expectations preserve previous behavior.
  • 186 tests pass across TextModelSearch, PieceTreeTextBuffer, and FindModel. Targeted ESLint passes for all four changed files; git diff --check passes.
  • Compiled with npm run build-fast -- --client-only.
node test/unit/browser/index.js --browser chromium-chrome --run src/vs/editor/test/common/model/textModelSearch.test.ts --run src/vs/editor/test/common/model/pieceTreeTextBuffer/pieceTreeTextBuffer.test.ts --run src/vs/editor/contrib/find/test/browser/findModel.test.ts --reporter dot
npm run eslint -- src/vs/editor/common/model.ts src/vs/editor/common/model/textModelSearch.ts src/vs/editor/common/model/pieceTreeTextBuffer/pieceTreeBase.ts src/vs/editor/test/common/model/textModelSearch.test.ts

Environment: Windows ARM64, Node 24.19.0, Chrome browser test runner. The local test base requests Node 24.22.1. Full Electron/native validation and a full TypeScript typecheck were not run. The original issue was reproduced manually in installed VS Code 1.144.0; the patched desktop application has not been manually retested. Browser tests ran on the local checkout; published file contents are verified against that tested checkout while preserving the PR's upstream merges.

Refactor search logic to improve match handling and add findNextWordStart function for better word boundary detection.
Refactor search logic to improve match finding efficiency.
Added tests for whole-word search functionality, including regex and edge cases.
Copilot AI balanced review requested due to automatic review settings October 28, 2026 18:37

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 search retry logic can loop forever on rejected non-global empty matches and perform quadratic rescanning after whole-word boundary failures, potentially hanging Find.

2 open issues
What changed in this PR

Fixes whole-word search missing valid matches that overlap rejected candidates, addressing #291591 across VS Code’s literal and regex search paths.

Changes:

  • Retry rejected candidates at the next possible word boundary while preserving non-overlapping accepted matches.
  • Share Unicode-aware advancement logic between text-model and piece-tree searches.
  • Add regression and control tests for overlaps, end-of-text matches, surrogate pairs, and termination.
File Description
src/​vs/​editor/​test/​common/​model/​textModelSearch.test.ts Adds regression and control coverage across search modes and buffer paths.
src/​vs/​editor/​common/​model/​textModelSearch.ts Adds the shared boundary helper and updates literal and regex retries.
src/​vs/​editor/​common/​model/​pieceTreeTextBuffer/​pieceTreeBase.ts Applies boundary-aware retries to the piece-tree literal-search fast path.

Review effort: Balanced


// Retry inside rejected candidates, including those that end at the end of the text.
// Skip to a possible word boundary to avoid repeatedly scanning the same long word.
this._searchRegex.lastIndex = findNextWordStart(this._wordSeparators, text, textLength, matchStartIndex);

// Retry inside rejected candidates, including those that end at the end of the text.
// Skip to a possible word boundary to avoid repeatedly scanning the same long word.
this._searchRegex.lastIndex = findNextWordStart(this._wordSeparators, text, textLength, matchStartIndex);

@cloudspeedapps-coder cloudspeedapps-coder left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Re-produced this bug and here are the fixes.

@cloudspeedapps-coder

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree company="CloudSpeed LLC"

@cloudspeedapps-coder cloudspeedapps-coder changed the title Fix whole-word search skipping overlapping valid matches Fix whole-word literal search skipping overlapping valid matches Oct 29, 2026

This branch has not been deployed

No deployments
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.

Find with "Match Whole Word" misses valid occurrences

3 participants