Skip to content

Commit 41bf82a

Browse files
authored
watcher - enable event correlation for all users of new proposed API (microsoft#196629)
1 parent bb1f532 commit 41bf82a

File tree

6 files changed

+34
-16
lines changed

6 files changed

+34
-16
lines changed

src/vs/workbench/api/browser/mainThreadFileSystemEventService.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ export class MainThreadFileSystemEventService implements MainThreadFileSystemEve
212212
this._listener.add(workingCopyFileService.onDidRunWorkingCopyFileOperation(e => this._proxy.$onDidRunFileOperation(e.operation, e.files)));
213213
}
214214

215-
async $watch(extensionId: string, session: number, resource: UriComponents, unvalidatedOpts: IWatchOptions): Promise<void> {
215+
async $watch(extensionId: string, session: number, resource: UriComponents, unvalidatedOpts: IWatchOptions, correlate: boolean): Promise<void> {
216216
const uri = URI.revive(resource);
217217

218218
const opts: IWatchOptions = {
@@ -234,8 +234,7 @@ export class MainThreadFileSystemEventService implements MainThreadFileSystemEve
234234
}
235235
}
236236

237-
// Correlated file watching is taken as is (for now we only opt into correlating with proposed new file system watcher API with excludes)
238-
const correlate = Array.isArray(unvalidatedOpts?.excludes) && unvalidatedOpts.excludes.length > 0;
237+
// Correlated file watching is taken as is
239238
if (correlate) {
240239
this._logService.trace(`MainThreadFileSystemEventService#$watch(): request to start watching correlated (extension: ${extensionId}, path: ${uri.toString(true)}, recursive: ${opts.recursive}, session: ${session})`);
241240

src/vs/workbench/api/common/extHost.api.impl.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { ExtHostDocuments } from 'vs/workbench/api/common/extHostDocuments';
2828
import { IExtHostDocumentsAndEditors } from 'vs/workbench/api/common/extHostDocumentsAndEditors';
2929
import { Extension, IExtHostExtensionService } from 'vs/workbench/api/common/extHostExtensionService';
3030
import { ExtHostFileSystem } from 'vs/workbench/api/common/extHostFileSystem';
31-
import { ExtHostFileSystemEventService } from 'vs/workbench/api/common/extHostFileSystemEventService';
31+
import { ExtHostFileSystemEventService, FileSystemWatcherCreateOptions } from 'vs/workbench/api/common/extHostFileSystemEventService';
3232
import { ExtHostLanguageFeatures } from 'vs/workbench/api/common/extHostLanguageFeatures';
3333
import { ExtHostLanguages } from 'vs/workbench/api/common/extHostLanguages';
3434
import { ExtHostMessageService } from 'vs/workbench/api/common/extHostMessageService';
@@ -947,17 +947,21 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
947947
return extHostBulkEdits.applyWorkspaceEdit(edit, extension, metadata);
948948
},
949949
createFileSystemWatcher: (pattern, optionsOrIgnoreCreate, ignoreChange?, ignoreDelete?): vscode.FileSystemWatcher => {
950-
let options: vscode.FileSystemWatcherOptions | undefined = undefined;
950+
let options: FileSystemWatcherCreateOptions | undefined = undefined;
951951

952952
if (typeof optionsOrIgnoreCreate === 'boolean') {
953953
options = {
954954
ignoreCreateEvents: Boolean(optionsOrIgnoreCreate),
955955
ignoreChangeEvents: Boolean(ignoreChange),
956-
ignoreDeleteEvents: Boolean(ignoreDelete)
956+
ignoreDeleteEvents: Boolean(ignoreDelete),
957+
correlate: false
957958
};
958959
} else if (optionsOrIgnoreCreate) {
959960
checkProposedApiEnabled(extension, 'createFileSystemWatcher');
960-
options = optionsOrIgnoreCreate;
961+
options = {
962+
...optionsOrIgnoreCreate,
963+
correlate: true
964+
};
961965
}
962966

963967
return extHostFileSystemEvent.createFileSystemWatcher(extHostWorkspace, extension, pattern, options);

src/vs/workbench/api/common/extHost.protocol.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,7 @@ export interface MainThreadFileSystemShape extends IDisposable {
13511351
}
13521352

13531353
export interface MainThreadFileSystemEventServiceShape extends IDisposable {
1354-
$watch(extensionId: string, session: number, resource: UriComponents, opts: files.IWatchOptions): void;
1354+
$watch(extensionId: string, session: number, resource: UriComponents, opts: files.IWatchOptions, correlate: boolean): void;
13551355
$unwatch(session: number): void;
13561356
}
13571357

src/vs/workbench/api/common/extHostFileSystemEventService.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import { ILogService } from 'vs/platform/log/common/log';
1818
import { IExtHostWorkspace } from 'vs/workbench/api/common/extHostWorkspace';
1919
import { Lazy } from 'vs/base/common/lazy';
2020

21-
interface FileSystemWatcherCreateOptions {
21+
export interface FileSystemWatcherCreateOptions {
22+
readonly correlate: boolean;
23+
2224
readonly ignoreCreateEvents?: boolean;
2325
readonly ignoreChangeEvents?: boolean;
2426
readonly ignoreDeleteEvents?: boolean;
@@ -71,9 +73,9 @@ class FileSystemWatcher implements vscode.FileSystemWatcher {
7173
const excludeOutOfWorkspaceEvents = typeof globPattern === 'string';
7274

7375
// 1.84.x introduces new proposed API for a watcher to set exclude
74-
// rules. When this is provided, we turn the file watcher into correlation
76+
// rules. In these cases, we turn the file watcher into correlation
7577
// mode and ignore any event that does not match the correlation ID.
76-
const excludeUncorrelatedEvents = Array.isArray(options?.excludes) && options.excludes.length > 0;
78+
const excludeUncorrelatedEvents = options?.correlate;
7779

7880
const subscription = dispatcher(events => {
7981
if (typeof events.session === 'number' && events.session !== this.session) {
@@ -110,10 +112,10 @@ class FileSystemWatcher implements vscode.FileSystemWatcher {
110112
}
111113
});
112114

113-
this._disposable = Disposable.from(this.ensureWatching(mainContext, extension, globPattern, options), this._onDidCreate, this._onDidChange, this._onDidDelete, subscription);
115+
this._disposable = Disposable.from(this.ensureWatching(mainContext, extension, globPattern, options, options?.correlate), this._onDidCreate, this._onDidChange, this._onDidDelete, subscription);
114116
}
115117

116-
private ensureWatching(mainContext: IMainContext, extension: IExtensionDescription, globPattern: string | IRelativePatternDto, options?: FileSystemWatcherCreateOptions): Disposable {
118+
private ensureWatching(mainContext: IMainContext, extension: IExtensionDescription, globPattern: string | IRelativePatternDto, options: FileSystemWatcherCreateOptions | undefined, correlate: boolean | undefined): Disposable {
117119
const disposable = Disposable.from();
118120

119121
if (typeof globPattern === 'string') {
@@ -127,7 +129,7 @@ class FileSystemWatcher implements vscode.FileSystemWatcher {
127129
recursive = true; // only watch recursively if pattern indicates the need for it
128130
}
129131

130-
proxy.$watch(extension.identifier.value, this.session, globPattern.baseUri, { recursive, excludes: options?.excludes ?? [] });
132+
proxy.$watch(extension.identifier.value, this.session, globPattern.baseUri, { recursive, excludes: options?.excludes ?? [] }, Boolean(correlate));
131133

132134
return Disposable.from({ dispose: () => proxy.$unwatch(this.session) });
133135
}

src/vs/workbench/api/test/browser/extHostFileSystemEventService.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ suite('ExtHostFileSystemEventService', () => {
2222
drain: undefined!
2323
};
2424

25-
const watcher1 = new ExtHostFileSystemEventService(protocol, new NullLogService(), undefined!).createFileSystemWatcher(undefined!, undefined!, '**/somethingInteresting', {});
25+
const watcher1 = new ExtHostFileSystemEventService(protocol, new NullLogService(), undefined!).createFileSystemWatcher(undefined!, undefined!, '**/somethingInteresting', { correlate: false });
2626
assert.strictEqual(watcher1.ignoreChangeEvents, false);
2727
assert.strictEqual(watcher1.ignoreCreateEvents, false);
2828
assert.strictEqual(watcher1.ignoreDeleteEvents, false);
2929
watcher1.dispose();
3030

31-
const watcher2 = new ExtHostFileSystemEventService(protocol, new NullLogService(), undefined!).createFileSystemWatcher(undefined!, undefined!, '**/somethingBoring', { ignoreCreateEvents: true, ignoreChangeEvents: true, ignoreDeleteEvents: true });
31+
const watcher2 = new ExtHostFileSystemEventService(protocol, new NullLogService(), undefined!).createFileSystemWatcher(undefined!, undefined!, '**/somethingBoring', { ignoreCreateEvents: true, ignoreChangeEvents: true, ignoreDeleteEvents: true, correlate: false });
3232
assert.strictEqual(watcher2.ignoreChangeEvents, true);
3333
assert.strictEqual(watcher2.ignoreCreateEvents, true);
3434
assert.strictEqual(watcher2.ignoreDeleteEvents, true);

src/vscode-dts/vscode.proposed.createFileSystemWatcher.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ declare module 'vscode' {
3333

3434
export namespace workspace {
3535

36+
/**
37+
* A variant of {@link workspace.createFileSystemWatcher} that optionally allows to specify
38+
* a set of glob patterns to exclude from watching.
39+
*
40+
* It provides the following advantages over the other {@link workspace.createFileSystemWatcher}
41+
* method:
42+
* - the configured excludes from `files.watcherExclude` setting are NOT applied
43+
* - requests for recursive file watchers inside the opened workspace are NOT ignored
44+
* - the watcher is ONLY notified for events from this request and not from any other watcher
45+
*
46+
* As such, this method is prefered in cases where you want full control over the watcher behavior
47+
* without being impacted by settings or other watchers that are installed.
48+
*/
3649
export function createFileSystemWatcher(pattern: RelativePattern, options?: FileSystemWatcherOptions): FileSystemWatcher;
3750
}
3851
}

0 commit comments

Comments
 (0)