diff --git a/src/vs/workbench/parts/search/browser/searchViewlet.ts b/src/vs/workbench/parts/search/browser/searchViewlet.ts index 0578f5f7b87f1aefd80d6e355e70abbe5dd05788..771ddf3308bfd108dc032a142c8c04df3dfabbdf 100644 --- a/src/vs/workbench/parts/search/browser/searchViewlet.ts +++ b/src/vs/workbench/parts/search/browser/searchViewlet.ts @@ -7,7 +7,7 @@ import 'vs/css!./media/searchviewlet'; import nls = require('vs/nls'); -import {TPromise, PPromise} from 'vs/base/common/winjs.base'; +import {TPromise} from 'vs/base/common/winjs.base'; import {EditorType} from 'vs/editor/common/editorCommon'; import {IDiffEditor} from 'vs/editor/browser/editorBrowser'; import lifecycle = require('vs/base/common/lifecycle'); @@ -64,7 +64,6 @@ export class SearchViewlet extends Viewlet { private isDisposed: boolean; private toDispose: lifecycle.IDisposable[]; - private currentRequest: PPromise; private loading: boolean; private queryBuilder: QueryBuilder; private viewModel: SearchModel; @@ -257,7 +256,7 @@ export class SearchViewlet extends Viewlet { this.updateGlobalPatternExclusions(this.configurationService.getConfiguration()); - this.registerListeners(); + this.toUnbind.push(this.viewModel.searchResult.onChange((event) => this.onSearchResultsChanged(event))); return TPromise.as(null); } @@ -315,26 +314,30 @@ export class SearchViewlet extends Viewlet { this.storageService.store(SearchViewlet.SHOW_REPLACE_STORAGE_KEY, this.searchAndReplaceWidget.isReplaceShown(), StorageScope.WORKSPACE); } - private registerListeners(): void { - this.toUnbind.push(this.viewModel.searchResult.onChange((event) => { - this.refreshTree(event); + private onSearchResultsChanged(event?: IChangeEvent): TPromise { + return this.refreshTree(event).then(() => { this.searchWidget.setReplaceAllActionState(!this.viewModel.searchResult.isEmpty()); - })); + }); } - private refreshTree(event: IChangeEvent): void { + private refreshTree(event?: IChangeEvent): TPromise { + if (!event) { + return this.tree.refresh(); + } + if (event.added || event.removed) { - this.tree.refresh(this.viewModel.searchResult); - if (event.added) { - event.elements.forEach(element => { - this.autoExpandFileMatch(element, true); - }); - } + return this.tree.refresh(this.viewModel.searchResult).then(() => { + if (event.added) { + event.elements.forEach(element => { + this.autoExpandFileMatch(element, true); + }); + } + }); } else { if (event.elements.length === 1) { - this.tree.refresh(event.elements[0]); + return this.tree.refresh(event.elements[0]); } else { - this.tree.refresh(event.elements); + return this.tree.refresh(event.elements); } } } @@ -526,22 +529,14 @@ export class SearchViewlet extends Viewlet { this.viewModel.searchResult.clear(); this.showEmptyStage(); this.searchWidget.clear(); - if (this.currentRequest) { - this.currentRequest.cancel(); - this.currentRequest = null; - } + this.viewModel.cancelSearch(); } public cancelSearch(): boolean { - if (this.currentRequest) { + if (this.viewModel.cancelSearch()) { this.searchWidget.focus(); - - this.currentRequest.cancel(); - this.currentRequest = null; - return true; } - return false; } @@ -697,6 +692,8 @@ export class SearchViewlet extends Viewlet { } private onQueryTriggered(query: ISearchQuery, excludePattern: string, includePattern: string): void { + this.viewModel.cancelSearch(); + // Progress total is 100% let progressTotal = 100; let progressRunner = this.progressService.show(progressTotal); @@ -733,10 +730,9 @@ export class SearchViewlet extends Viewlet { progressRunner.done(); } + this.onSearchResultsChanged().then(() => autoExpand(true)); this.viewModel.replaceText= this.searchWidget.getReplaceValue(); - autoExpand(true); - let hasResults = !this.viewModel.searchResult.isEmpty(); this.loading = false; @@ -882,8 +878,7 @@ export class SearchViewlet extends Viewlet { this.searchWidget.setReplaceAllActionState(false); this.replaceService.disposeAllInputs(); - this.currentRequest = this.viewModel.search(query); - this.currentRequest.done(onComplete, onError, onProgress); + this.viewModel.search(query).done(onComplete, onError, onProgress); } private showEmptyStage(): void { diff --git a/src/vs/workbench/parts/search/common/searchModel.ts b/src/vs/workbench/parts/search/common/searchModel.ts index 503d9232d7eb76d31f78d48b25c51b7d7ab69af8..484e0c8b3d5b9552e73092602e99205a640ef3b2 100644 --- a/src/vs/workbench/parts/search/common/searchModel.ts +++ b/src/vs/workbench/parts/search/common/searchModel.ts @@ -438,7 +438,7 @@ export class SearchModel extends Disposable { return this.isReplaceActive() && !!this.replaceText; } - public search(query: ISearchQuery, silent: boolean= true): PPromise { + public search(query: ISearchQuery): PPromise { this.cancelSearch(); this.searchResult.clear(); @@ -450,21 +450,23 @@ export class SearchModel extends Disposable { this.currentRequest = this.searchService.search(this._searchQuery); this.currentRequest.then(value => this.onSearchCompleted(value), - e => this.onSearchError(e, silent), - p => this.onSearchProgress(p, silent)); + e => this.onSearchError(e), + p => this.onSearchProgress(p)); return this.currentRequest; } private onSearchCompleted(completed: ISearchComplete): ISearchComplete { this.timerEvent.stop(); - this._searchResult.add(completed.results); this.telemetryService.publicLog('searchResultsShown', { count: this._searchResult.count(), fileCount: this._searchResult.fileCount() }); this.doneTimer.stop(); + if (completed) { + this._searchResult.add(completed.results, false); + } return completed; } - private onSearchError(e: any, silent: boolean): void { + private onSearchError(e: any): void { if (errors.isPromiseCanceledError(e)) { this.onSearchCompleted(null); } else { @@ -473,18 +475,20 @@ export class SearchModel extends Disposable { } } - private onSearchProgress(p: ISearchProgressItem, silent: boolean): void { + private onSearchProgress(p: ISearchProgressItem): void { if (p.resource) { - this._searchResult.add([p], silent); + this._searchResult.add([p], true); this.progressTimer.stop(); } } - public cancelSearch(): void{ + public cancelSearch(): boolean { if (this.currentRequest) { this.currentRequest.cancel(); this.currentRequest = null; + return true; } + return false; } public dispose(): void {