From 51b0bf3c7891fd014fc1508b9f1686c1c5d46355 Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Tue, 10 Oct 2017 12:20:38 +0200 Subject: [PATCH] support `findFiles` --- src/vs/vscode.proposed.d.ts | 3 + .../electron-browser/mainThreadFileSystem.ts | 58 ++++++++++++++++--- src/vs/workbench/api/node/extHost.protocol.ts | 3 + .../workbench/api/node/extHostFileSystem.ts | 8 +++ 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/vs/vscode.proposed.d.ts b/src/vs/vscode.proposed.d.ts index 85ca4a72cd1..0a2e63c7729 100644 --- a/src/vs/vscode.proposed.d.ts +++ b/src/vs/vscode.proposed.d.ts @@ -155,6 +155,9 @@ declare module 'vscode' { // todo@remote // create(resource: Uri): Thenable; + + // find files by names + findFiles?(query: string, progress: Progress, token: CancellationToken): Thenable; } export namespace workspace { diff --git a/src/vs/workbench/api/electron-browser/mainThreadFileSystem.ts b/src/vs/workbench/api/electron-browser/mainThreadFileSystem.ts index 76aac923806..5b6ea8b6f3a 100644 --- a/src/vs/workbench/api/electron-browser/mainThreadFileSystem.ts +++ b/src/vs/workbench/api/electron-browser/mainThreadFileSystem.ts @@ -5,7 +5,7 @@ 'use strict'; import URI from 'vs/base/common/uri'; -import { TPromise } from 'vs/base/common/winjs.base'; +import { TPromise, PPromise } from 'vs/base/common/winjs.base'; import { ExtHostContext, MainContext, IExtHostContext, MainThreadFileSystemShape, ExtHostFileSystemShape } from '../node/extHost.protocol'; import { IFileService, IFileSystemProvider, IStat, IFileChange } from 'vs/platform/files/common/files'; import { IDisposable, dispose } from 'vs/base/common/lifecycle'; @@ -13,6 +13,7 @@ import Event, { Emitter } from 'vs/base/common/event'; import { extHostNamedCustomer } from 'vs/workbench/api/electron-browser/extHostCustomers'; import { IProgress } from 'vs/platform/progress/common/progress'; import { IWorkspaceEditingService } from 'vs/workbench/services/workspace/common/workspaceEditing'; +import { ISearchResultProvider, ISearchQuery, ISearchComplete, ISearchProgressItem, QueryType, IFileMatch, ISearchService } from 'vs/platform/search/common/search'; @extHostNamedCustomer(MainContext.MainThreadFileSystem) export class MainThreadFileSystem implements MainThreadFileSystemShape { @@ -24,6 +25,7 @@ export class MainThreadFileSystem implements MainThreadFileSystemShape { constructor( extHostContext: IExtHostContext, @IFileService private readonly _fileService: IFileService, + @ISearchService private readonly _searchService: ISearchService, @IWorkspaceEditingService private readonly _workspaceEditService: IWorkspaceEditingService ) { this._proxy = extHostContext.get(ExtHostContext.ExtHostFileSystem); @@ -34,7 +36,7 @@ export class MainThreadFileSystem implements MainThreadFileSystemShape { } $registerFileSystemProvider(handle: number, scheme: string): void { - this._provider.set(handle, new RemoteFileSystemProvider(this._fileService, scheme, handle, this._proxy)); + this._provider.set(handle, new RemoteFileSystemProvider(this._fileService, this._searchService, scheme, handle, this._proxy)); } $unregisterFileSystemProvider(handle: number): void { @@ -53,28 +55,38 @@ export class MainThreadFileSystem implements MainThreadFileSystemShape { $reportFileChunk(handle: number, resource: URI, chunk: number[]): void { this._provider.get(handle).reportFileChunk(resource, chunk); } + + // --- search + + $handleSearchProgress(handle: number, session: number, resource: URI): void { + this._provider.get(handle).handleSearchProgress(session, resource); + } } -class RemoteFileSystemProvider implements IFileSystemProvider { +class RemoteFileSystemProvider implements IFileSystemProvider, ISearchResultProvider { private readonly _onDidChange = new Emitter(); - private readonly _registration: IDisposable; private readonly _reads = new Map>(); + private readonly _registrations: IDisposable[]; readonly onDidChange: Event = this._onDidChange.event; constructor( - service: IFileService, + fileService: IFileService, + searchService: ISearchService, scheme: string, private readonly _handle: number, private readonly _proxy: ExtHostFileSystemShape ) { - this._registration = service.registerProvider(scheme, this); + this._registrations = [ + fileService.registerProvider(scheme, this), + searchService.registerSearchResultProvider(this), + ]; } dispose(): void { - this._registration.dispose(); + dispose(this._registrations); this._onDidChange.dispose(); } @@ -115,4 +127,36 @@ class RemoteFileSystemProvider implements IFileSystemProvider { rmdir(resource: URI): TPromise { return this._proxy.$rmdir(this._handle, resource); } + + // --- search + + private _searches = new Map void>(); + private _searchesIdPool = 0; + + search(query: ISearchQuery): PPromise { + if (query.type === QueryType.Text) { + return PPromise.as({ results: [], stats: undefined }); + } + const id = ++this._searchesIdPool; + const matches: IFileMatch[] = []; + return new PPromise((resolve, reject, report) => { + this._proxy.$fileFiles(this._handle, id, query.filePattern).then(() => { + this._searches.delete(id); + resolve({ + results: matches, + stats: undefined + }); + }, reject); + + this._searches.set(id, resource => { + const match: IFileMatch = { resource }; + matches.push(match); + report(match); + }); + }); + } + + handleSearchProgress(session: number, resource: URI): void { + this._searches.get(session)(resource); + } } diff --git a/src/vs/workbench/api/node/extHost.protocol.ts b/src/vs/workbench/api/node/extHost.protocol.ts index 4016299feed..07a65209fe6 100644 --- a/src/vs/workbench/api/node/extHost.protocol.ts +++ b/src/vs/workbench/api/node/extHost.protocol.ts @@ -324,6 +324,8 @@ export interface MainThreadFileSystemShape extends IDisposable { $onDidAddFileSystemRoot(root: URI): void; $onFileSystemChange(handle: number, resource: IFileChange[]): void; $reportFileChunk(handle: number, resource: URI, chunk: number[] | null): void; + + $handleSearchProgress(handle: number, session: number, resource: URI): void; } export interface MainThreadTaskShape extends IDisposable { @@ -489,6 +491,7 @@ export interface ExtHostFileSystemShape { $mkdir(handle: number, resource: URI): TPromise; $readdir(handle: number, resource: URI): TPromise<[URI, IStat][]>; $rmdir(handle: number, resource: URI): TPromise; + $fileFiles(handle: number, session: number, query: string): TPromise; } export interface ExtHostExtensionServiceShape { diff --git a/src/vs/workbench/api/node/extHostFileSystem.ts b/src/vs/workbench/api/node/extHostFileSystem.ts index 6b7466381f5..a7b8c787310 100644 --- a/src/vs/workbench/api/node/extHostFileSystem.ts +++ b/src/vs/workbench/api/node/extHostFileSystem.ts @@ -74,4 +74,12 @@ export class ExtHostFileSystem implements ExtHostFileSystemShape { $rmdir(handle: number, resource: URI): TPromise { return asWinJsPromise(token => this._provider.get(handle).rmdir(resource)); } + $fileFiles(handle: number, session: number, query: string): TPromise { + const provider = this._provider.get(handle); + if (!provider.findFiles) { + return TPromise.as(undefined); + } + const progress = { report: (uri) => this._proxy.$handleSearchProgress(handle, session, uri) }; + return asWinJsPromise(token => provider.findFiles(query, progress, token)); + } } -- GitLab