Skip to content

Commit

Permalink
API: Allow to use the file watcher for aribitrary folders (#3025)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Dec 29, 2021
1 parent 130532e commit cf1afd7
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/vs/workbench/api/browser/mainThreadFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class MainThreadFileSystem implements MainThreadFileSystemShape {
private readonly _proxy: ExtHostFileSystemShape;
private readonly _fileProvider = new Map<number, RemoteFileSystemProvider>();
private readonly _disposables = new DisposableStore();
private readonly _watches = new Map<number, IDisposable>();

constructor(
extHostContext: IExtHostContext,
Expand Down Expand Up @@ -153,6 +154,19 @@ export class MainThreadFileSystem implements MainThreadFileSystemShape {
$ensureActivation(scheme: string): Promise<void> {
return this._fileService.activateProvider(scheme);
}

$watch(session: number, resource: UriComponents, opts: IWatchOptions): void {
const subscription = this._fileService.watch(URI.revive(resource), opts);
this._watches.set(session, subscription);
}

$unwatch(session: number): void {
const subscription = this._watches.get(session);
if (subscription) {
subscription.dispose();
this._watches.delete(session);
}
}
}

class RemoteFileSystemProvider implements IFileSystemProviderWithFileReadWriteCapability, IFileSystemProviderWithOpenReadWriteCloseCapability, IFileSystemProviderWithFileFolderCopyCapability {
Expand Down
4 changes: 4 additions & 0 deletions src/vs/workbench/api/common/extHost.api.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,10 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
get fs() {
return extHostConsumerFileSystem.value;
},
get fs2() {
checkProposedApiEnabled(extension, 'fsWatch');
return extHostConsumerFileSystem.value as vscode.FileSystem2;
},
registerFileSearchProvider: (scheme: string, provider: vscode.FileSearchProvider) => {
checkProposedApiEnabled(extension, 'fileSearchProvider');
return extHostSearch.registerFileSearchProvider(scheme, provider);
Expand Down
2 changes: 2 additions & 0 deletions src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,8 @@ export interface MainThreadFileSystemShape extends IDisposable {
$copy(resource: UriComponents, target: UriComponents, opts: files.FileOverwriteOptions): Promise<void>;
$mkdir(resource: UriComponents): Promise<void>;
$delete(resource: UriComponents, opts: files.FileDeleteOptions): Promise<void>;
$watch(session: number, resource: UriComponents, opts: files.IWatchOptions): void;
$unwatch(session: number): void;

$ensureActivation(scheme: string): Promise<void>;
}
Expand Down
7 changes: 7 additions & 0 deletions src/vs/workbench/api/common/extHostFileSystemConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ export class ExtHostConsumerFileSystem {
return !(capabilities & files.FileSystemProviderCapabilities.Readonly);
}
return undefined;
},
watch(uri: vscode.Uri, options: { recursive: boolean; excludes: string[] } = { recursive: false, excludes: [] }): vscode.Disposable {
const session = Math.random();
that._proxy.$watch(session, uri, options);
return toDisposable(() => {
that._proxy.$unwatch(session);
});
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const allApiProposals = Object.freeze({
fileSearchProvider: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.fileSearchProvider.d.ts',
findTextInFiles: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.findTextInFiles.d.ts',
fsChunks: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.fsChunks.d.ts',
fsWatch: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.fsWatch.d.ts',
inlayHints: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.inlayHints.d.ts',
inlineCompletions: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.inlineCompletions.d.ts',
languageStatus: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.languageStatus.d.ts',
Expand Down
42 changes: 42 additions & 0 deletions src/vscode-dts/vscode.proposed.fsWatch.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

declare module 'vscode' {

// https://github.com/microsoft/vscode/issues/3025

export interface FileSystem2 extends FileSystem {

/**
* Subscribe to file change events in the file or folder denoted by `uri`. For folders,
* the option `recursive` indicates whether subfolders, sub-subfolders, etc. should
* be watched for file changes as well.
*
* The `excludes` array is used to indicate paths that should be excluded from file
* watching. Each entry can be be:
* - the absolute path to exclude
* - a relative path to exclude (for example `build/output`)
* - a simple glob pattern (for example `**\build`, `output/**`)
*
* File events will be delivered via {@linkcode workspace.createFileSystemWatcher createFileSystemWatcher}.
*
* @param uri The uri of the file or folder to be watched.
* @param options Configures the watch.
* @returns A disposable that tells the provider to stop watching the `uri`.
*/
watch(uri: Uri, options?: { recursive: boolean; excludes: string[] }): Disposable;
}

export namespace workspace {

/**
* A {@link FileSystem file system} instance that allows to interact with local and remote
* files, e.g. `vscode.workspace.fs.readDirectory(someUri)` allows to retrieve all entries
* of a directory or `vscode.workspace.fs.stat(anotherUri)` returns the meta data for a
* file.
*/
export const fs2: FileSystem2;
}
}

0 comments on commit cf1afd7

Please sign in to comment.