Skip to content

Commit

Permalink
api-ify search provider, needs query logic...
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Aug 17, 2017
1 parent fe20718 commit af195c9
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/vs/platform/progress/common/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -52,7 +52,7 @@ export class Progress<T> implements IProgress<T> {

report(item: T) {
this._value = item;
this._callback();
this._callback(this._value);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/vs/vscode.proposed.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
49 changes: 48 additions & 1 deletion src/vs/workbench/api/electron-browser/mainThreadWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/vs/workbench/api/node/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
45 changes: 38 additions & 7 deletions src/vs/workbench/api/node/extHostWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit af195c9

Please sign in to comment.