api - allow to provide a cancellation token to findFiles. Fixes #1553

This commit is contained in:
Johannes Rieken 2015-12-22 09:29:35 +01:00
parent 28e35df8b9
commit 4dfa345e91
4 changed files with 56 additions and 14 deletions

View file

@ -6,7 +6,7 @@
'use strict';
import * as assert from 'assert';
import {workspace, TextDocument, window, Position, Uri} from 'vscode';
import {workspace, TextDocument, window, Position, Uri, CancellationTokenSource} from 'vscode';
import {createRandomFile, deleteFile, cleanUp, pathEquals} from './utils';
import {join, basename} from 'path';
import * as fs from 'fs';
@ -125,4 +125,15 @@ suite('workspace-namespace', () => {
assert.equal(basename(workspace.asRelativePath(res[0])), 'far.js');
});
});
test('findFiles, cancellation', () => {
const source = new CancellationTokenSource();
const token = source.token; // just to get an instance first
source.cancel();
return workspace.findFiles('*.js', null, 100, token).then((res) => {
assert.equal(res, void 0);
});
});
});

6
src/vs/vscode.d.ts vendored
View file

@ -884,6 +884,9 @@ declare namespace vscode {
* A cancellation token is passed to an asynchronous or long running
* operation to request cancellation, like cancelling a request
* for completion items because the user continued to type.
*
* To get an instance of a `CancellationToken` use a
* [CancellationTokenSource](#CancellationTokenSource).
*/
export interface CancellationToken {
@ -2941,9 +2944,10 @@ declare namespace vscode {
* @param include A glob pattern that defines the files to search for.
* @param exclude A glob pattern that defines files and folders to exclude.
* @param maxResults An upper-bound for the result.
* @param token A token that can be used to signal cancellation to the underlying search engine.
* @return A thenable that resolves to an array of resource identifiers.
*/
export function findFiles(include: string, exclude: string, maxResults?: number): Thenable<Uri[]>;
export function findFiles(include: string, exclude: string, maxResults?: number, token?: CancellationToken): Thenable<Uri[]>;
/**
* Save all dirty files.

View file

@ -213,8 +213,8 @@ export class ExtHostAPIImplementation {
asRelativePath: (pathOrUri) => {
return pluginHostWorkspace.getRelativePath(pathOrUri);
},
findFiles: (include, exclude, maxResults?) => {
return pluginHostWorkspace.findFiles(include, exclude, maxResults);
findFiles: (include, exclude, maxResults?, token?) => {
return pluginHostWorkspace.findFiles(include, exclude, maxResults, token);
},
saveAll: (includeUntitled?) => {
return pluginHostWorkspace.saveAll(includeUntitled);

View file

@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/
'use strict';
import {isPromiseCanceledError} from 'vs/base/common/errors';
import URI from 'vs/base/common/uri';
import {ISearchService, QueryType} from 'vs/platform/search/common/search';
import {IWorkspaceContextService, IWorkspace} from 'vs/platform/workspace/common/workspace';
@ -11,14 +12,16 @@ import {Remotable, IThreadService} from 'vs/platform/thread/common/thread';
import {IEventService} from 'vs/platform/event/common/event';
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
import {ITextFileService} from 'vs/workbench/parts/files/common/files';
import {Uri} from 'vscode';
import {ICommonCodeEditor} from 'vs/editor/common/editorCommon';
import {bulkEdit, IResourceEdit} from 'vs/editor/common/services/bulkEdit';
import {TPromise} from 'vs/base/common/winjs.base';
import {fromRange} from 'vs/workbench/api/common/extHostTypeConverters';
import {Uri, CancellationToken} from 'vscode';
export class ExtHostWorkspace {
private static _requestIdPool = 0;
private _proxy: MainThreadWorkspace;
private _workspacePath: string;
@ -48,12 +51,17 @@ export class ExtHostWorkspace {
return path;
}
findFiles(include: string, exclude: string, maxResults?:number): Thenable<Uri[]> {
return this._proxy.findFiles(include, exclude, maxResults);
findFiles(include: string, exclude: string, maxResults?: number, token?: CancellationToken): Thenable<Uri[]> {
const requestId = ExtHostWorkspace._requestIdPool++;
const result = this._proxy.$startSearch(include, exclude, maxResults, requestId);
if (token) {
token.onCancellationRequested(() => this._proxy.$cancelSearch(requestId));
}
return result;
}
saveAll(includeUntitled?: boolean): Thenable<boolean> {
return this._proxy.saveAll(includeUntitled);
return this._proxy.$saveAll(includeUntitled);
}
appyEdit(edit: vscode.WorkspaceEdit): TPromise<boolean> {
@ -73,20 +81,20 @@ export class ExtHostWorkspace {
}
}
return this._proxy.applyWorkspaceEdit(resourceEdits);
return this._proxy.$applyWorkspaceEdit(resourceEdits);
}
}
@Remotable.MainContext('MainThreadWorkspace')
export class MainThreadWorkspace {
private _activeSearches: { [id: number]: TPromise<Uri[]> } = Object.create(null);
private _searchService: ISearchService;
private _workspace: IWorkspace;
private _textFileService: ITextFileService;
private _editorService:IWorkbenchEditorService;
private _eventService:IEventService;
constructor( @ISearchService searchService: ISearchService,
@IWorkspaceContextService contextService: IWorkspaceContextService,
@ITextFileService textFileService,
@ -100,13 +108,13 @@ export class MainThreadWorkspace {
this._eventService = eventService;
}
findFiles(include: string, exclude: string, maxResults: number): Thenable<Uri[]> {
$startSearch(include: string, exclude: string, maxResults: number, requestId: number): Thenable<Uri[]> {
if (!this._workspace) {
return;
}
return this._searchService.search({
const search = this._searchService.search({
rootResources: [this._workspace.resource],
type: QueryType.File,
maxResults,
@ -114,16 +122,35 @@ export class MainThreadWorkspace {
excludePattern: { [exclude]: true },
}).then(result => {
return result.results.map(m => m.resource);
}, err => {
if (!isPromiseCanceledError(err)) {
return TPromise.wrapError(err);
}
});
this._activeSearches[requestId] = search;
const onDone = () => delete this._activeSearches[requestId];
search.done(onDone, onDone);
return search;
}
saveAll(includeUntitled?: boolean): Thenable<boolean> {
$cancelSearch(requestId: number): Thenable<boolean> {
const search = this._activeSearches[requestId];
if (search) {
delete this._activeSearches[requestId];
search.cancel();
return TPromise.as(true);
}
}
$saveAll(includeUntitled?: boolean): Thenable<boolean> {
return this._textFileService.saveAll(includeUntitled).then(result => {
return result.results.every(each => each.success === true);
});
}
applyWorkspaceEdit(edits: IResourceEdit[]): TPromise<boolean> {
$applyWorkspaceEdit(edits: IResourceEdit[]): TPromise<boolean> {
let codeEditor: ICommonCodeEditor;
let editor = this._editorService.getActiveEditor();