Skip to content

fix: update find searchScope when the selection changes - #331038

Open
aidaodedjl wants to merge 2 commits into
microsoft:mainfrom
aidaodedjl:fix/find-in-selection-scope-stale-state
Open

fix: update find searchScope when the selection changes#331038
aidaodedjl wants to merge 2 commits into
microsoft:mainfrom
aidaodedjl:fix/find-in-selection-scope-stale-state

Conversation

@aidaodedjl

Copy link
Copy Markdown

Description

The searchScope change detection in FindReplaceState.change() has inverted comparison logic since the multi-selection refactor in b9efdab (2020): a new scope was considered unchanged whenever any existing scope range differed from one of the new ranges. As a result, moving or resizing the selection while Find in Selection is active kept the stale scope, and the match count never updated.

This is the root cause of the symptoms in #237774:

  1. Select two lines (3 matches of the search term) → widget reports 1 result
  2. Toggling Find in Selection off → highlights disappear unexpectedly
  3. Changing the selection after a search → count stays at the old value

The fix

Replace the inverted per-range comparison with a proper set-equality check (searchScopesEqual): same length, and every new range must be equal to some existing range. The scope is now updated whenever the selection actually changes, and no redundant change event fires when the scope is identical.

Testing

Fixes #237774

The searchScope change detection in FindReplaceState.change() had
inverted comparison logic since the multi-selection refactor (b9efdab):
a new scope was treated as unchanged whenever any existing scope range
differed from it, so moving or resizing the selection kept the stale
scope and the match count never updated.

Compare scopes with a proper set-equality check instead.

Fixes microsoft#237774
Copilot AI balanced review requested due to automatic review settings August 15, 2026 19:15

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.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds regression coverage for search-scope updates in FindController and fixes searchScope change detection so identical scopes don’t emit state-change events.

Changes:

  • Added a new test covering searchScope updates and “no event on identical scope”.
  • Introduced searchScopesEqual helper and replaced the previous scope-change comparison logic.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/vs/editor/contrib/find/test/browser/findController.test.ts Adds regression test for searchScope updates and event emission behavior.
src/vs/editor/contrib/find/browser/findState.ts Adds searchScopesEqual helper and uses it to avoid emitting changes for equivalent scopes.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

if (!a || !b || a.length !== b.length) {
return false;
}
return a.every(rangeA => b.some(rangeB => Range.equalsRange(rangeA, rangeB)));
Comment on lines +681 to +686
test('issue #237774: Update searchScope when the selection changes', async () => {
await withAsyncTestCodeEditor([
'var x = (3 * 5)',
'var y = (3 * 5)',
'var z = (3 * 5)',
], { serviceCollection: serviceCollection }, async (editor) => {
findState.change({ searchScope: [new Selection(1, 1, 2, 1)] }, false);
listener.dispose();
assert.strictEqual(changeEventFired, false);

@aidaodedjl

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Track matched indices so a multiset comparison is performed: [A, A]
must not be considered equal to [A, B]. Also add a regression
assertion for duplicate ranges and rename the test to reflect that it
exercises scope changes directly.
@aidaodedjl

Copy link
Copy Markdown
Author

Addressed all three review comments in b88d231:

  1. Duplicate ranges false positivesearchScopesEqual now performs a proper multiset comparison by tracking matched indices, so [A, A] is no longer considered equal to [A, B].
  2. Test title — renamed to "Update searchScope when the scope changes" since the test drives findState.change() directly rather than editor selections.
  3. Duplicate-range regression assertion — added: a scope of two identical selections is stored as-is, and re-setting the identical duplicated scope fires no change event.

All 22 tests in findController.test.ts pass locally.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Find in Selection" toggle in text search is broken

3 participants