@@ -10,7 +10,7 @@ import { IActiveCodeEditor } from 'vs/editor/browser/editorBrowser';
1010import { ReplaceCommand , ReplaceCommandThatPreservesSelection } from 'vs/editor/common/commands/replaceCommand' ;
1111import { CursorChangeReason , ICursorPositionChangedEvent } from 'vs/editor/common/controller/cursorEvents' ;
1212import { 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' ;
1414import { Selection } from 'vs/editor/common/core/selection' ;
1515import { Constants } from 'vs/base/common/uint' ;
1616import { ScrollType , ICommand } from 'vs/editor/common/editorCommon' ;
@@ -23,6 +23,7 @@ import { ReplacePattern, parseReplaceString } from 'vs/editor/contrib/find/repla
2323import { RawContextKey } from 'vs/platform/contextkey/common/contextkey' ;
2424import { IKeybindings } from 'vs/platform/keybinding/common/keybindingsRegistry' ;
2525import { EditorOption } from 'vs/editor/common/config/editorOptions' ;
26+ import { binarySearch , findFirstInSorted } from 'vs/base/common/arrays' ;
2627
2728export const CONTEXT_FIND_WIDGET_VISIBLE = new RawContextKey < boolean > ( 'findWidgetVisible' , false ) ;
2829export 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