提交 af195c96 编写于 作者: J Johannes Rieken

api-ify search provider, needs query logic...

上级 fe207183
......@@ -39,10 +39,10 @@ export const emptyProgress: IProgress<any> = Object.freeze({ report() { } });
export class Progress<T> implements IProgress<T> {
private _callback: () => void;
private _callback: (data: T) => void;
private _value: T;
constructor(callback: () => void) {
constructor(callback: (data: T) => void) {
this._callback = callback;
}
......@@ -52,7 +52,7 @@ export class Progress<T> implements IProgress<T> {
report(item: T) {
this._value = item;
this._callback();
this._callback(this._value);
}
}
......
......@@ -16,6 +16,10 @@ declare module 'vscode' {
writeContents(resource: Uri, contents: string): void | Thenable<void>;
}
export interface SearchProvider {
provideSearchResults(query: any, progress: Progress<Uri>, token?: CancellationToken): Thenable<void>;
}
export namespace workspace {
export function registerFileSystemProvider(authority: string, provider: FileSystemProvider): Disposable;
......
......@@ -12,7 +12,7 @@ import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/edi
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { ICommonCodeEditor, isCommonCodeEditor } from 'vs/editor/common/editorCommon';
import { bulkEdit, IResourceEdit } from 'vs/editor/common/services/bulkEdit';
import { TPromise } from 'vs/base/common/winjs.base';
import { TPromise, PPromise } from 'vs/base/common/winjs.base';
import { MainThreadWorkspaceShape, ExtHostWorkspaceShape, ExtHostContext, MainContext, IExtHostContext } from '../node/extHost.protocol';
import { ITextModelService } from 'vs/editor/common/services/resolverService';
import { IFileService } from 'vs/platform/files/common/files';
......@@ -101,6 +101,53 @@ export class MainThreadWorkspace implements MainThreadWorkspaceShape {
// --- save & edit resources ---
private readonly _searchProvider = new Map<number, IDisposable>();
private readonly _searchSession = new Map<number, { resolve: Function, reject: Function, progress: Function }>();
private _searchSessionIdPool: number = 0;
$registerSearchProvider(handle: number, type: number): void {
this._searchProvider.set(handle, this._searchService.registerSearchResultProvider({
search: (query) => {
if (query.type !== type) {
return null;
}
const session = ++this._searchSessionIdPool;
return new PPromise<any, any>((resolve, reject, progress) => {
this._searchSession.set(session, { resolve, reject, progress });
this._proxy.$startSearch(handle, session, query);
}, () => {
this._proxy.$cancelSearch(handle, session);
});
}
}));
}
$unregisterSearchProvider(handle: number): void {
const registration = this._searchProvider.get(handle);
if (registration) {
registration.dispose();
this._searchProvider.delete(handle);
}
}
$updateSearchSession(session: number, data): void {
if (this._searchSession.has(session)) {
this._searchSession.get(session).progress(data);
}
}
$finishSearchSession(session: number, err?: any): void {
if (this._searchSession.has(session)) {
const { resolve, reject } = this._searchSession.get(session);
this._searchSession.delete(session);
if (err) {
reject(err);
} else {
resolve();
}
}
}
$saveAll(includeUntitled?: boolean): Thenable<boolean> {
return this._textFileService.saveAll(includeUntitled).then(result => {
return result.results.every(each => each.success === true);
......
......@@ -280,6 +280,11 @@ export interface MainThreadWorkspaceShape extends IDisposable {
$applyWorkspaceEdit(edits: IResourceEdit[]): TPromise<boolean>;
$registerFileSystemProvider(handle: number, authority: string): void;
$onFileSystemChange(handle: number, resource: URI): void;
$registerSearchProvider(handle: number, type: number): void;
$unregisterSearchProvider(handle: number): void;
$updateSearchSession(session: number, data): void;
$finishSearchSession(session: number, err?: any): void;
}
export interface MainThreadTaskShape extends IDisposable {
......@@ -418,8 +423,11 @@ export interface ExtHostTreeViewsShape {
export interface ExtHostWorkspaceShape {
$acceptWorkspaceData(workspace: IWorkspaceData): void;
$resolveFile(handle: number, resource: URI): TPromise<string>;
$storeFile(handle: number, resource: URI, content: string): TPromise<any>;
$startSearch(handle: number, session: number, query): void;
$cancelSearch(handle: number, session: number): void;
}
export interface ExtHostExtensionServiceShape {
......
......@@ -19,6 +19,8 @@ import { compare } from "vs/base/common/strings";
import { asWinJsPromise } from 'vs/base/common/async';
import { Disposable } from 'vs/workbench/api/node/extHostTypes';
import { TrieMap } from 'vs/base/common/map';
import { CancellationTokenSource } from 'vs/base/common/cancellation';
import { Progress } from 'vs/platform/progress/common/progress';
class Workspace2 extends Workspace {
......@@ -207,27 +209,56 @@ export class ExtHostWorkspace implements ExtHostWorkspaceShape {
// --- EXPERIMENT: workspace resolver
private readonly _provider = new Map<number, vscode.FileSystemProvider>();
public registerFileSystemProvider(authority: string, provider: vscode.FileSystemProvider): vscode.Disposable {
private _handlePool = 0;
private readonly _fsProvider = new Map<number, vscode.FileSystemProvider>();
private readonly _searchProvider = new Map<number, vscode.SearchProvider>();
private readonly _searchSession = new Map<number, CancellationTokenSource>();
const handle = this._provider.size;
this._provider.set(handle, provider);
registerFileSystemProvider(authority: string, provider: vscode.FileSystemProvider): vscode.Disposable {
const handle = ++this._handlePool;
this._fsProvider.set(handle, provider);
const reg = provider.onDidChange(e => this._proxy.$onFileSystemChange(handle, <URI>e));
this._proxy.$registerFileSystemProvider(handle, authority);
return new Disposable(() => {
this._provider.delete(handle);
this._fsProvider.delete(handle);
reg.dispose();
});
}
$resolveFile(handle: number, resource: URI): TPromise<string> {
const provider = this._provider.get(handle);
const provider = this._fsProvider.get(handle);
return asWinJsPromise(token => provider.resolveContents(resource));
}
$storeFile(handle: number, resource: URI, content: string): TPromise<any> {
const provider = this._provider.get(handle);
const provider = this._fsProvider.get(handle);
return asWinJsPromise(token => provider.writeContents(resource, content));
}
registerSearchProvider(provider: vscode.SearchProvider): vscode.Disposable {
const handle = ++this._handlePool;
this._searchProvider.set(handle, provider);
return new Disposable(() => this._fsProvider.delete(handle));
}
$startSearch(handle: number, session: number, query): void {
const provider = this._searchProvider.get(handle);
const source = new CancellationTokenSource();
const progress = new Progress<any>(chunk => this._proxy.$updateSearchSession(session, chunk));
this._searchSession.set(session, source);
TPromise.wrap(provider.provideSearchResults(query, progress, source.token)).then(() => {
this._proxy.$finishSearchSession(session);
}, err => {
this._proxy.$finishSearchSession(session, err);
});
}
$cancelSearch(handle: number, session: number): void {
if (this._searchSession.has(session)) {
this._searchSession.get(session).cancel();
this._searchSession.delete(session);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册