diff --git a/src/vs/workbench/api/electron-browser/mainThreadSearch.ts b/src/vs/workbench/api/electron-browser/mainThreadSearch.ts index d2fa937424f75bceb7edf84fb6f2820dde9ea8cb..caffd57767b87abe86ffadfaba7ef7e1baf3d192 100644 --- a/src/vs/workbench/api/electron-browser/mainThreadSearch.ts +++ b/src/vs/workbench/api/electron-browser/mainThreadSearch.ts @@ -5,14 +5,14 @@ 'use strict'; import { isFalsyOrEmpty } from 'vs/base/common/arrays'; -import { IDisposable, dispose } from 'vs/base/common/lifecycle'; +import { dispose, IDisposable } from 'vs/base/common/lifecycle'; import { values } from 'vs/base/common/map'; import URI, { UriComponents } from 'vs/base/common/uri'; -import { PPromise, TPromise } from 'vs/base/common/winjs.base'; -import { IFileMatch, ISearchComplete, ISearchProgressItem, ISearchQuery, ISearchResultProvider, ISearchService, QueryType, IRawFileMatch2, ISearchCompleteStats } from 'vs/platform/search/common/search'; +import { TPromise } from 'vs/base/common/winjs.base'; +import { IFileMatch, IRawFileMatch2, ISearchComplete, ISearchCompleteStats, ISearchProgressItem, ISearchQuery, ISearchResultProvider, ISearchService, QueryType } from 'vs/platform/search/common/search'; +import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry'; import { extHostNamedCustomer } from 'vs/workbench/api/electron-browser/extHostCustomers'; import { ExtHostContext, ExtHostSearchShape, IExtHostContext, MainContext, MainThreadSearchShape } from '../node/extHost.protocol'; -import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry'; @extHostNamedCustomer(MainContext.MainThreadSearch) export class MainThreadSearch implements MainThreadSearchShape { @@ -100,7 +100,7 @@ class RemoteSearchProvider implements ISearchResultProvider, IDisposable { search(query: ISearchQuery, onProgress?: (p: ISearchProgressItem) => void): TPromise { if (isFalsyOrEmpty(query.folderQueries)) { - return PPromise.as(undefined); + return TPromise.as(undefined); } const folderQueriesForScheme = query.folderQueries.filter(fq => fq.folder.scheme === this._scheme); diff --git a/src/vs/workbench/api/node/extHostSearch.ts b/src/vs/workbench/api/node/extHostSearch.ts index 35f121e588cb7074915932d204fa261767cebb9b..e7bcb39550d41f561a14b5aee61c9034d27296fc 100644 --- a/src/vs/workbench/api/node/extHostSearch.ts +++ b/src/vs/workbench/api/node/extHostSearch.ts @@ -10,7 +10,7 @@ import { toErrorMessage } from 'vs/base/common/errorMessage'; import * as glob from 'vs/base/common/glob'; import * as resources from 'vs/base/common/resources'; import URI, { UriComponents } from 'vs/base/common/uri'; -import { PPromise, TPromise } from 'vs/base/common/winjs.base'; +import { TPromise } from 'vs/base/common/winjs.base'; import * as extfs from 'vs/base/node/extfs'; import { IFileMatch, IFolderQuery, IPatternInfo, IRawSearchQuery, ISearchCompleteStats, ISearchQuery } from 'vs/platform/search/common/search'; import * as vscode from 'vscode'; @@ -58,12 +58,9 @@ export class ExtHostSearch implements ExtHostSearchShape { } const query = reviveQuery(rawQuery); - return this._fileSearchManager.fileSearch(query, provider).then( - null, - null, - progress => { - this._proxy.$handleFileMatch(handle, session, progress.map(p => p.resource)); - }); + return this._fileSearchManager.fileSearch(query, provider, progress => { + this._proxy.$handleFileMatch(handle, session, progress.map(p => p.resource)); + }); } $clearCache(handle: number, cacheKey: string): TPromise { @@ -84,12 +81,7 @@ export class ExtHostSearch implements ExtHostSearchShape { const query = reviveQuery(rawQuery); const engine = new TextSearchEngine(pattern, query, provider, this._extfs); - return engine.search().then( - null, - null, - progress => { - this._proxy.$handleTextMatch(handle, session, progress); - }); + return engine.search(progress => this._proxy.$handleTextMatch(handle, session, progress)); } } @@ -380,11 +372,11 @@ class TextSearchEngine { this.activeCancellationTokens = new Set(); } - public search(): PPromise<{ limitHit: boolean }, IFileMatch[]> { + public search(onProgress: (matches: IFileMatch[]) => void): TPromise<{ limitHit: boolean }> { const folderQueries = this.config.folderQueries; - return new PPromise<{ limitHit: boolean }, IFileMatch[]>((resolve, reject, _onResult) => { - this.collector = new TextSearchResultsCollector(_onResult); + return new TPromise<{ limitHit: boolean }>((resolve, reject) => { + this.collector = new TextSearchResultsCollector(onProgress); const onResult = (match: vscode.TextSearchResult, folderIdx: number) => { if (this.isCanceled) { @@ -403,8 +395,8 @@ class TextSearchEngine { }; // For each root folder - PPromise.join(folderQueries.map((fq, i) => { - return this.searchInFolder(fq).then(null, null, r => onResult(r, i)); + TPromise.join(folderQueries.map((fq, i) => { + return this.searchInFolder(fq, r => onResult(r, i)); })).then(() => { this.collector.flush(); resolve({ limitHit: this.isLimitHit }); @@ -418,9 +410,9 @@ class TextSearchEngine { }); } - private searchInFolder(folderQuery: IFolderQuery): PPromise { + private searchInFolder(folderQuery: IFolderQuery, onResult: (result: vscode.TextSearchResult) => void): TPromise { let cancellation = new CancellationTokenSource(); - return new PPromise((resolve, reject, onResult) => { + return new TPromise((resolve, reject) => { const queryTester = new QueryGlobTester(this.config, folderQuery); const testingPs = []; @@ -532,10 +524,10 @@ class FileSearchEngine { this.activeCancellationTokens = new Set(); } - public search(): PPromise { + public search(_onResult: (match: IInternalFileMatch) => void): TPromise { const folderQueries = this.config.folderQueries; - return new PPromise((resolve, reject, _onResult) => { + return new TPromise((resolve, reject) => { const onResult = (match: IInternalFileMatch) => { this.resultCount++; _onResult(match); @@ -562,8 +554,8 @@ class FileSearchEngine { } // For each root folder - PPromise.join(folderQueries.map(fq => { - return this.searchInFolder(fq).then(null, null, onResult); + TPromise.join(folderQueries.map(fq => { + return this.searchInFolder(fq, onResult); })).then(cacheKeys => { resolve({ limitHit: this.isLimitHit, cacheKeys }); }, (errs: Error[]) => { @@ -576,9 +568,9 @@ class FileSearchEngine { }); } - private searchInFolder(fq: IFolderQuery): PPromise { + private searchInFolder(fq: IFolderQuery, onResult: (match: IInternalFileMatch) => void): TPromise { let cancellation = new CancellationTokenSource(); - return new PPromise((resolve, reject, onResult) => { + return new TPromise((resolve, reject) => { const options = this.getSearchOptionsForFolder(fq); const tree = this.initDirectoryTree(); @@ -748,12 +740,16 @@ class FileSearchManager { private readonly expandedCacheKeys = new Map(); - fileSearch(config: ISearchQuery, provider: vscode.SearchProvider): PPromise { - let searchP: PPromise; - return new PPromise((c, e, p) => { + fileSearch(config: ISearchQuery, provider: vscode.SearchProvider, onResult: (matches: IFileMatch[]) => void): TPromise { + let searchP: TPromise; + return new TPromise((c, e) => { const engine = new FileSearchEngine(config, provider); - searchP = this.doSearch(engine, FileSearchManager.BATCH_SIZE).then( + const onInternalResult = (progress: IInternalFileMatch[]) => { + onResult(progress.map(m => this.rawMatchToSearchItem(m))); + }; + + searchP = this.doSearch(engine, FileSearchManager.BATCH_SIZE, onInternalResult).then( result => { if (config.cacheKey) { this.expandedCacheKeys.set(config.cacheKey, result.cacheKeys); @@ -763,10 +759,7 @@ class FileSearchManager { limitHit: result.limitHit }); }, - e, - progress => { - p(progress.map(m => this.rawMatchToSearchItem(m))); - }); + e); }, () => { if (searchP) { searchP.cancel(); @@ -789,29 +782,31 @@ class FileSearchManager { }; } - private doSearch(engine: FileSearchEngine, batchSize: number): PPromise { - return new PPromise((c, e, p) => { + private doSearch(engine: FileSearchEngine, batchSize: number, onResultBatch: (matches: IInternalFileMatch[]) => void): TPromise { + return new TPromise((c, e) => { + const _onResult = match => { + if (match) { + batch.push(match); + if (batchSize > 0 && batch.length >= batchSize) { + onResultBatch(batch); + batch = []; + } + } + }; + let batch: IInternalFileMatch[] = []; - engine.search().then(result => { + engine.search(_onResult).then(result => { if (batch.length) { - p(batch); + onResultBatch(batch); } c(result); }, error => { if (batch.length) { - p(batch); + onResultBatch(batch); } e(error); - }, match => { - if (match) { - batch.push(match); - if (batchSize > 0 && batch.length >= batchSize) { - p(batch); - batch = []; - } - } }); }, () => { engine.cancel();