Merge pull request #57957 from Microsoft/joh/search-cancel

Use CancellationToken for search
This commit is contained in:
Rob Lourens
2018-09-05 15:16:51 -07:00
committed by GitHub
4 changed files with 23 additions and 31 deletions
@@ -107,7 +107,7 @@ class RemoteSearchProvider implements ISearchResultProvider, IDisposable {
dispose(this._registrations);
}
search(query: ISearchQuery, onProgress?: (p: ISearchProgressItem) => void, token?: CancellationToken): TPromise<ISearchComplete> {
search(query: ISearchQuery, onProgress?: (p: ISearchProgressItem) => void, token: CancellationToken = CancellationToken.None): TPromise<ISearchComplete> {
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<void> {
return this._proxy.$clearCache(cacheKey);
return TPromise.wrap(this._proxy.$clearCache(cacheKey));
}
handleFindMatch(session: number, dataOrUri: (UriComponents | IRawFileMatch2)[]): void {
@@ -697,9 +697,9 @@ export interface ExtHostFileSystemShape {
}
export interface ExtHostSearchShape {
$provideFileSearchResults(handle: number, session: number, query: IRawSearchQuery): TPromise<ISearchCompleteStats>;
$clearCache(cacheKey: string): TPromise<void>;
$provideTextSearchResults(handle: number, session: number, pattern: IPatternInfo, query: IRawSearchQuery): TPromise<ISearchCompleteStats>;
$provideFileSearchResults(handle: number, session: number, query: IRawSearchQuery, token: CancellationToken): Thenable<ISearchCompleteStats>;
$clearCache(cacheKey: string): Thenable<void>;
$provideTextSearchResults(handle: number, session: number, pattern: IPatternInfo, query: IRawSearchQuery, token: CancellationToken): Thenable<ISearchCompleteStats>;
}
export interface ExtHostExtensionServiceShape {
+8 -15
View File
@@ -78,23 +78,18 @@ export class ExtHostSearch implements ExtHostSearchShape {
});
}
$provideFileSearchResults(handle: number, session: number, rawQuery: IRawSearchQuery): TPromise<ISearchCompleteStats> {
$provideFileSearchResults(handle: number, session: number, rawQuery: IRawSearchQuery, token: CancellationToken): Thenable<ISearchCompleteStats> {
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<void> {
$clearCache(cacheKey: string): Thenable<void> {
// 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<ISearchCompleteStats> {
$provideTextSearchResults(handle: number, session: number, pattern: IPatternInfo, rawQuery: IRawSearchQuery, token: CancellationToken): Thenable<ISearchCompleteStats> {
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<ISearchCompleteStats> {
public search(onProgress: (matches: IFileMatch[]) => void, token: CancellationToken): TPromise<ISearchCompleteStats> {
const folderQueries = this.config.folderQueries;
const tokenSource = new CancellationTokenSource();
token.onCancellationRequested(() => tokenSource.cancel());
return new TPromise<ISearchCompleteStats>((resolve, reject) => {
this.collector = new TextSearchResultsCollector(onProgress);
@@ -349,9 +345,6 @@ class TextSearchEngine {
reject(new Error(errMsg));
});
}, () => {
// From IPC
tokenSource.cancel();
});
}
@@ -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;