From 8186062aa9d427b7d92947cf44497f561e565536 Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Fri, 15 Jul 2016 13:32:35 +0300 Subject: [PATCH] Fixes #9120: Seed search scope on find field focus gain if the editor selection was not set by the find model --- .../contrib/find/browser/findWidget.css | 2 +- .../editor/contrib/find/browser/findWidget.ts | 14 ++++++++++ .../editor/contrib/find/common/findModel.ts | 28 +++++++++++++------ .../editor/contrib/find/common/findState.ts | 20 +++++++++++-- 4 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/vs/editor/contrib/find/browser/findWidget.css b/src/vs/editor/contrib/find/browser/findWidget.css index 6ce452dcec3..3865e84c359 100644 --- a/src/vs/editor/contrib/find/browser/findWidget.css +++ b/src/vs/editor/contrib/find/browser/findWidget.css @@ -265,7 +265,7 @@ } .monaco-editor .findScope { - background-color: rgba(239, 239, 242, 0.4); + background-color: rgba(180, 180, 180, 0.3); } /* Theming */ diff --git a/src/vs/editor/contrib/find/browser/findWidget.ts b/src/vs/editor/contrib/find/browser/findWidget.ts index 6f565b0f162..11f46a02584 100644 --- a/src/vs/editor/contrib/find/browser/findWidget.ts +++ b/src/vs/editor/contrib/find/browser/findWidget.ts @@ -21,6 +21,7 @@ import {IConfigurationChangedEvent} from 'vs/editor/common/editorCommon'; import {ICodeEditor, IOverlayWidget, IOverlayWidgetPosition, OverlayWidgetPositionPreference} from 'vs/editor/browser/editorBrowser'; import {FIND_IDS, MATCHES_LIMIT} from 'vs/editor/contrib/find/common/findModel'; import {FindReplaceState, FindReplaceStateChangedEvent} from 'vs/editor/contrib/find/common/findState'; +import {Range} from 'vs/editor/common/core/range'; export interface IFindController { replace(): void; @@ -71,6 +72,8 @@ export class FindWidget extends Widget implements IOverlayWidget { private _isVisible: boolean; private _isReplaceVisible: boolean; + private _focusTracker: dom.IFocusTracker; + constructor( codeEditor: ICodeEditor, controller: IFindController, @@ -107,6 +110,17 @@ export class FindWidget extends Widget implements IOverlayWidget { this._updateToggleSelectionFindButton(); } })); + this._focusTracker = this._register(dom.trackFocus(this._findInput.inputBox.inputElement)); + this._focusTracker.addFocusListener(() => { + let selection = this._codeEditor.getSelection(); + let currentMatch = this._state.currentMatch; + if (selection.startLineNumber !== selection.endLineNumber) { + if (!Range.equalsRange(selection, currentMatch)) { + // Reseed find scope + this._state.change({ searchScope: selection }, true); + } + } + }); this._codeEditor.addOverlayWidget(this); } diff --git a/src/vs/editor/contrib/find/common/findModel.ts b/src/vs/editor/contrib/find/common/findModel.ts index 79cdbacafd6..bc583699396 100644 --- a/src/vs/editor/contrib/find/common/findModel.ts +++ b/src/vs/editor/contrib/find/common/findModel.ts @@ -140,7 +140,11 @@ export class FindModelBoundToEditorModel { let findMatches = this._findMatches(findScope, MATCHES_LIMIT); this._decorations.set(findMatches, findScope); - this._state.changeMatchInfo(this._decorations.getCurrentMatchesPosition(this._editor.getSelection()), this._decorations.getCount()); + this._state.changeMatchInfo( + this._decorations.getCurrentMatchesPosition(this._editor.getSelection()), + this._decorations.getCount(), + undefined + ); if (moveCursor) { this._moveToNextMatch(this._decorations.getStartPosition()); @@ -163,6 +167,18 @@ export class FindModelBoundToEditorModel { return false; } + private _setCurrentFindMatch(match:Range): void { + let matchesPosition = this._decorations.setCurrentFindMatch(match); + this._state.changeMatchInfo( + matchesPosition, + this._decorations.getCount(), + match + ); + + this._editor.setSelection(match); + this._editor.revealRangeInCenterIfOutsideViewport(match); + } + private _moveToPrevMatch(before:Position, isRecursed:boolean = false): void { if (this._cannotFind()) { return; @@ -220,10 +236,7 @@ export class FindModelBoundToEditorModel { return this._moveToPrevMatch(prevMatch.getStartPosition(), true); } - let matchesPosition = this._decorations.setCurrentFindMatch(prevMatch); - this._state.changeMatchInfo(matchesPosition, this._decorations.getCount()); - this._editor.setSelection(prevMatch); - this._editor.revealRangeInCenterIfOutsideViewport(prevMatch); + this._setCurrentFindMatch(prevMatch); } public moveToPrevMatch(): void { @@ -287,10 +300,7 @@ export class FindModelBoundToEditorModel { return this._moveToNextMatch(nextMatch.getEndPosition(), true); } - let matchesPosition = this._decorations.setCurrentFindMatch(nextMatch); - this._state.changeMatchInfo(matchesPosition, this._decorations.getCount()); - this._editor.setSelection(nextMatch); - this._editor.revealRangeInCenterIfOutsideViewport(nextMatch); + this._setCurrentFindMatch(nextMatch); } public moveToNextMatch(): void { diff --git a/src/vs/editor/contrib/find/common/findState.ts b/src/vs/editor/contrib/find/common/findState.ts index cec26c5c479..9929e8993f5 100644 --- a/src/vs/editor/contrib/find/common/findState.ts +++ b/src/vs/editor/contrib/find/common/findState.ts @@ -21,6 +21,7 @@ export interface FindReplaceStateChangedEvent { searchScope: boolean; matchesPosition: boolean; matchesCount: boolean; + currentMatch: boolean; } export interface INewFindReplaceState { @@ -48,6 +49,7 @@ export class FindReplaceState implements IDisposable { private _searchScope: Range; private _matchesPosition: number; private _matchesCount: number; + private _currentMatch: Range; private _eventEmitter: EventEmitter; public get searchString(): string { return this._searchString; } @@ -60,6 +62,7 @@ export class FindReplaceState implements IDisposable { public get searchScope(): Range { return this._searchScope; } public get matchesPosition(): number { return this._matchesPosition; } public get matchesCount(): number { return this._matchesCount; } + public get currentMatch(): Range { return this._currentMatch; } constructor() { this._searchString = ''; @@ -72,6 +75,7 @@ export class FindReplaceState implements IDisposable { this._searchScope = null; this._matchesPosition = 0; this._matchesCount = 0; + this._currentMatch = null; this._eventEmitter = new EventEmitter(); } @@ -83,7 +87,7 @@ export class FindReplaceState implements IDisposable { return this._eventEmitter.addListener2(FindReplaceState._CHANGED_EVENT, listener); } - public changeMatchInfo(matchesPosition:number, matchesCount:number): void { + public changeMatchInfo(matchesPosition:number, matchesCount:number, currentMatch:Range): void { let changeEvent:FindReplaceStateChangedEvent = { moveCursor: false, searchString: false, @@ -95,7 +99,8 @@ export class FindReplaceState implements IDisposable { matchCase: false, searchScope: false, matchesPosition: false, - matchesCount: false + matchesCount: false, + currentMatch: false }; let somethingChanged = false; @@ -117,6 +122,14 @@ export class FindReplaceState implements IDisposable { somethingChanged = true; } + if (typeof currentMatch !== 'undefined') { + if (!Range.equalsRange(this._currentMatch, currentMatch)) { + this._currentMatch = currentMatch; + changeEvent.currentMatch = true; + somethingChanged = true; + } + } + if (somethingChanged) { this._eventEmitter.emit(FindReplaceState._CHANGED_EVENT, changeEvent); } @@ -134,7 +147,8 @@ export class FindReplaceState implements IDisposable { matchCase: false, searchScope: false, matchesPosition: false, - matchesCount: false + matchesCount: false, + currentMatch: false }; let somethingChanged = false; -- GitLab