diff --git a/src/vs/workbench/api/electron-browser/mainThreadSearch.ts b/src/vs/workbench/api/electron-browser/mainThreadSearch.ts index 3b54bbecb24..e2f5950e12e 100644 --- a/src/vs/workbench/api/electron-browser/mainThreadSearch.ts +++ b/src/vs/workbench/api/electron-browser/mainThreadSearch.ts @@ -107,7 +107,7 @@ class RemoteSearchProvider implements ISearchResultProvider, IDisposable { dispose(this._registrations); } - search(query: ISearchQuery, onProgress?: (p: ISearchProgressItem) => void, token?: CancellationToken): TPromise { + search(query: ISearchQuery, onProgress?: (p: ISearchProgressItem) => void, token: CancellationToken = CancellationToken.None): TPromise { if (isFalsyOrEmpty(query.folderQueries)) { return TPromise.as(undefined); } @@ -116,14 +116,10 @@ class RemoteSearchProvider implements ISearchResultProvider, IDisposable { this._searches.set(search.id, search); const searchP = query.type === QueryType.File - ? this._proxy.$provideFileSearchResults(this._handle, search.id, query) - : this._proxy.$provideTextSearchResults(this._handle, search.id, query.contentPattern, query); + ? this._proxy.$provideFileSearchResults(this._handle, search.id, query, token) + : this._proxy.$provideTextSearchResults(this._handle, search.id, query.contentPattern, query, token); - if (token) { - token.onCancellationRequested(() => searchP.cancel()); - } - - return searchP.then((result: ISearchCompleteStats) => { + return TPromise.wrap(searchP).then((result: ISearchCompleteStats) => { this._searches.delete(search.id); return { results: values(search.matches), stats: result.stats, limitHit: result.limitHit }; }, err => { @@ -133,7 +129,7 @@ class RemoteSearchProvider implements ISearchResultProvider, IDisposable { } clearCache(cacheKey: string): TPromise { - return this._proxy.$clearCache(cacheKey); + return TPromise.wrap(this._proxy.$clearCache(cacheKey)); } handleFindMatch(session: number, dataOrUri: (UriComponents | IRawFileMatch2)[]): void { diff --git a/src/vs/workbench/api/node/extHost.protocol.ts b/src/vs/workbench/api/node/extHost.protocol.ts index 3262559aae5..c9c12d075df 100644 --- a/src/vs/workbench/api/node/extHost.protocol.ts +++ b/src/vs/workbench/api/node/extHost.protocol.ts @@ -697,9 +697,9 @@ export interface ExtHostFileSystemShape { } export interface ExtHostSearchShape { - $provideFileSearchResults(handle: number, session: number, query: IRawSearchQuery): TPromise; - $clearCache(cacheKey: string): TPromise; - $provideTextSearchResults(handle: number, session: number, pattern: IPatternInfo, query: IRawSearchQuery): TPromise; + $provideFileSearchResults(handle: number, session: number, query: IRawSearchQuery, token: CancellationToken): Thenable; + $clearCache(cacheKey: string): Thenable; + $provideTextSearchResults(handle: number, session: number, pattern: IPatternInfo, query: IRawSearchQuery, token: CancellationToken): Thenable; } export interface ExtHostExtensionServiceShape { diff --git a/src/vs/workbench/api/node/extHostSearch.ts b/src/vs/workbench/api/node/extHostSearch.ts index 7f9300b8f1b..2188ae2eb64 100644 --- a/src/vs/workbench/api/node/extHostSearch.ts +++ b/src/vs/workbench/api/node/extHostSearch.ts @@ -78,23 +78,18 @@ export class ExtHostSearch implements ExtHostSearchShape { }); } - $provideFileSearchResults(handle: number, session: number, rawQuery: IRawSearchQuery): TPromise { + $provideFileSearchResults(handle: number, session: number, rawQuery: IRawSearchQuery, token: CancellationToken): Thenable { const provider = this._fileSearchProvider.get(handle); const query = reviveQuery(rawQuery); if (provider) { - let cancelSource = new CancellationTokenSource(); - return new TPromise((c, e) => { + return new Promise((c, e) => { this._fileSearchManager.fileSearch(query, provider, batch => { this._proxy.$handleFileMatch(handle, session, batch.map(p => p.resource)); - }, cancelSource.token).then(c, err => { + }, token).then(c, err => { if (!isPromiseCanceledError(err)) { e(err); } }); - }, () => { - // TODO IPC promise cancellation #53526 - cancelSource.cancel(); - cancelSource.dispose(); }); } else { const indexProvider = this._fileIndexProvider.get(handle); @@ -110,13 +105,13 @@ export class ExtHostSearch implements ExtHostSearchShape { } } - $clearCache(cacheKey: string): TPromise { + $clearCache(cacheKey: string): Thenable { // Actually called once per provider. // Only relevant to file index search. return this._fileIndexSearchManager.clearCache(cacheKey); } - $provideTextSearchResults(handle: number, session: number, pattern: IPatternInfo, rawQuery: IRawSearchQuery): TPromise { + $provideTextSearchResults(handle: number, session: number, pattern: IPatternInfo, rawQuery: IRawSearchQuery, token: CancellationToken): Thenable { const provider = this._textSearchProvider.get(handle); if (!provider.provideTextSearchResults) { return TPromise.as(undefined); @@ -124,7 +119,7 @@ export class ExtHostSearch implements ExtHostSearchShape { const query = reviveQuery(rawQuery); const engine = new TextSearchEngine(pattern, query, provider, this._extfs); - return engine.search(progress => this._proxy.$handleTextMatch(handle, session, progress)); + return engine.search(progress => this._proxy.$handleTextMatch(handle, session, progress), token); } } @@ -304,9 +299,10 @@ class TextSearchEngine { constructor(private pattern: IPatternInfo, private config: ISearchQuery, private provider: vscode.TextSearchProvider, private _extfs: typeof extfs) { } - public search(onProgress: (matches: IFileMatch[]) => void): TPromise { + public search(onProgress: (matches: IFileMatch[]) => void, token: CancellationToken): TPromise { const folderQueries = this.config.folderQueries; const tokenSource = new CancellationTokenSource(); + token.onCancellationRequested(() => tokenSource.cancel()); return new TPromise((resolve, reject) => { this.collector = new TextSearchResultsCollector(onProgress); @@ -349,9 +345,6 @@ class TextSearchEngine { reject(new Error(errMsg)); }); - }, () => { - // From IPC - tokenSource.cancel(); }); } diff --git a/src/vs/workbench/test/electron-browser/api/extHostSearch.test.ts b/src/vs/workbench/test/electron-browser/api/extHostSearch.test.ts index c0781d01361..138d41fa2f7 100644 --- a/src/vs/workbench/test/electron-browser/api/extHostSearch.test.ts +++ b/src/vs/workbench/test/electron-browser/api/extHostSearch.test.ts @@ -17,6 +17,7 @@ import { ExtHostSearch } from 'vs/workbench/api/node/extHostSearch'; import { Range } from 'vs/workbench/api/node/extHostTypes'; import { TestRPCProtocol } from 'vs/workbench/test/electron-browser/api/testRPCProtocol'; import * as vscode from 'vscode'; +import { CancellationTokenSource } from 'vs/base/common/cancellation'; let rpcProtocol: TestRPCProtocol; let extHostSearch: ExtHostSearch; @@ -74,10 +75,11 @@ suite('ExtHostSearch', () => { async function runFileSearch(query: IRawSearchQuery, cancel = false): Promise<{ results: URI[]; stats: ISearchCompleteStats }> { let stats: ISearchCompleteStats; try { - const p = extHostSearch.$provideFileSearchResults(mockMainThreadSearch.lastHandle, 0, query); + const cancellation = new CancellationTokenSource(); + const p = extHostSearch.$provideFileSearchResults(mockMainThreadSearch.lastHandle, 0, query, cancellation.token); if (cancel) { await new TPromise(resolve => process.nextTick(resolve)); - p.cancel(); + cancellation.cancel(); } stats = await p; @@ -98,10 +100,11 @@ suite('ExtHostSearch', () => { async function runTextSearch(pattern: IPatternInfo, query: IRawSearchQuery, cancel = false): Promise<{ results: IFileMatch[], stats: ISearchCompleteStats }> { let stats: ISearchCompleteStats; try { - const p = extHostSearch.$provideTextSearchResults(mockMainThreadSearch.lastHandle, 0, pattern, query); + const cancellation = new CancellationTokenSource(); + const p = extHostSearch.$provideTextSearchResults(mockMainThreadSearch.lastHandle, 0, pattern, query, cancellation.token); if (cancel) { await new TPromise(resolve => process.nextTick(resolve)); - p.cancel(); + cancellation.cancel(); } stats = await p;