Skip to content

Commit fbdc028

Browse files
committed
Fix microsoft#97681. Use neareast find match position.
1 parent eb09996 commit fbdc028

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/vs/editor/contrib/find/findModel.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { IActiveCodeEditor } from 'vs/editor/browser/editorBrowser';
1010
import { ReplaceCommand, ReplaceCommandThatPreservesSelection } from 'vs/editor/common/commands/replaceCommand';
1111
import { CursorChangeReason, ICursorPositionChangedEvent } from 'vs/editor/common/controller/cursorEvents';
1212
import { Position } from 'vs/editor/common/core/position';
13-
import { Range } from 'vs/editor/common/core/range';
13+
import { Range, IRange } from 'vs/editor/common/core/range';
1414
import { Selection } from 'vs/editor/common/core/selection';
1515
import { Constants } from 'vs/base/common/uint';
1616
import { ScrollType, ICommand } from 'vs/editor/common/editorCommon';
@@ -23,6 +23,7 @@ import { ReplacePattern, parseReplaceString } from 'vs/editor/contrib/find/repla
2323
import { RawContextKey } from 'vs/platform/contextkey/common/contextkey';
2424
import { IKeybindings } from 'vs/platform/keybinding/common/keybindingsRegistry';
2525
import { EditorOption } from 'vs/editor/common/config/editorOptions';
26+
import { binarySearch, findFirstInSorted } from 'vs/base/common/arrays';
2627

2728
export const CONTEXT_FIND_WIDGET_VISIBLE = new RawContextKey<boolean>('findWidgetVisible', false);
2829
export const CONTEXT_FIND_WIDGET_NOT_VISIBLE = CONTEXT_FIND_WIDGET_VISIBLE.toNegated();
@@ -189,8 +190,17 @@ export class FindModelBoundToEditorModel {
189190
let findMatches = this._findMatches(findScope, false, MATCHES_LIMIT);
190191
this._decorations.set(findMatches, findScope);
191192

193+
const editorSelection = this._editor.getSelection();
194+
let currentMatchesPosition = this._decorations.getCurrentMatchesPosition(editorSelection);
195+
if (currentMatchesPosition === 0 && findMatches.length > 0) {
196+
// current selection is not on top of a match
197+
// try to find its nearest result from the top of the document
198+
const matchAfterSelection = findFirstInSorted(findMatches.map(match => match.range), range => Range.compareRangesUsingStarts(range, editorSelection) >= 0);
199+
currentMatchesPosition = matchAfterSelection > 0 ? matchAfterSelection - 1 + 1 /** match position is one based */ : currentMatchesPosition;
200+
}
201+
192202
this._state.changeMatchInfo(
193-
this._decorations.getCurrentMatchesPosition(this._editor.getSelection()),
203+
currentMatchesPosition,
194204
this._decorations.getCount(),
195205
undefined
196206
);

0 commit comments

Comments
 (0)