From 29ff950dc6b0411ba05b7fe8d88bcf6051a445bf Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Wed, 7 Oct 2020 17:22:15 +0200 Subject: [PATCH] files - add method affects() for file changes (#108255) * add a containsChildren method for #108164 * change to affects * comment --- src/vs/platform/files/common/files.ts | 32 ++++++++++++++++--- .../platform/files/test/common/files.test.ts | 15 +++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/vs/platform/files/common/files.ts b/src/vs/platform/files/common/files.ts index d099d55328a6..f9026d13ad3a 100644 --- a/src/vs/platform/files/common/files.ts +++ b/src/vs/platform/files/common/files.ts @@ -542,12 +542,24 @@ export class FileChangesEvent { } /** - * Returns true if this change event contains the provided file - * with the given change type (if provided). In case of type - * DELETED, this method will also return true if a folder got - * deleted that is the parent of the provided file path. + * Find out if the file change events match the provided resource. + * + * Note: when passing `FileChangeType.DELETED`, we consider a match + * also when the parent of the resource got deleted. */ contains(resource: URI, ...types: FileChangeType[]): boolean { + return this.doContains(resource, { includeChildren: false }, ...types); + } + + /** + * Find out if the file change events either match the provided + * resource, or contain a child of this resource. + */ + affects(resource: URI, ...types: FileChangeType[]): boolean { + return this.doContains(resource, { includeChildren: true }, ...types); + } + + private doContains(resource: URI, options: { includeChildren: boolean }, ...types: FileChangeType[]): boolean { if (!resource) { return false; } @@ -559,6 +571,10 @@ export class FileChangesEvent { if (this.added?.get(resource)) { return true; } + + if (options.includeChildren && this.added?.findSuperstr(resource)) { + return true; + } } // Updated @@ -566,6 +582,10 @@ export class FileChangesEvent { if (this.updated?.get(resource)) { return true; } + + if (options.includeChildren && this.updated?.findSuperstr(resource)) { + return true; + } } // Deleted @@ -573,6 +593,10 @@ export class FileChangesEvent { if (this.deleted?.findSubstr(resource) /* deleted also considers parent folders */) { return true; } + + if (options.includeChildren && this.deleted?.findSuperstr(resource)) { + return true; + } } return false; diff --git a/src/vs/platform/files/test/common/files.test.ts b/src/vs/platform/files/test/common/files.test.ts index 51055605190a..e51c40714766 100644 --- a/src/vs/platform/files/test/common/files.test.ts +++ b/src/vs/platform/files/test/common/files.test.ts @@ -26,19 +26,26 @@ suite('Files', () => { const event = new FileChangesEvent(changes, ignorePathCasing); assert(!event.contains(toResource.call(this, '/foo'), FileChangeType.UPDATED)); + assert(event.affects(toResource.call(this, '/foo'), FileChangeType.UPDATED)); assert(event.contains(toResource.call(this, '/foo/updated.txt'), FileChangeType.UPDATED)); + assert(event.affects(toResource.call(this, '/foo/updated.txt'), FileChangeType.UPDATED)); assert(event.contains(toResource.call(this, '/foo/updated.txt'), FileChangeType.UPDATED, FileChangeType.ADDED)); + assert(event.affects(toResource.call(this, '/foo/updated.txt'), FileChangeType.UPDATED, FileChangeType.ADDED)); assert(event.contains(toResource.call(this, '/foo/updated.txt'), FileChangeType.UPDATED, FileChangeType.ADDED, FileChangeType.DELETED)); assert(!event.contains(toResource.call(this, '/foo/updated.txt'), FileChangeType.ADDED, FileChangeType.DELETED)); assert(!event.contains(toResource.call(this, '/foo/updated.txt'), FileChangeType.ADDED)); assert(!event.contains(toResource.call(this, '/foo/updated.txt'), FileChangeType.DELETED)); + assert(!event.affects(toResource.call(this, '/foo/updated.txt'), FileChangeType.DELETED)); assert(event.contains(toResource.call(this, '/bar/folder'), FileChangeType.DELETED)); assert(event.contains(toResource.call(this, '/BAR/FOLDER'), FileChangeType.DELETED)); + assert(event.affects(toResource.call(this, '/BAR'), FileChangeType.DELETED)); if (ignorePathCasing) { assert(event.contains(toResource.call(this, '/BAR/folder'), FileChangeType.DELETED)); + assert(event.affects(toResource.call(this, '/bar'), FileChangeType.DELETED)); } else { assert(!event.contains(toResource.call(this, '/BAR/folder'), FileChangeType.DELETED)); + assert(event.affects(toResource.call(this, '/bar'), FileChangeType.DELETED)); } assert(event.contains(toResource.call(this, '/bar/folder/somefile'), FileChangeType.DELETED)); assert(event.contains(toResource.call(this, '/bar/folder/somefile/test.txt'), FileChangeType.DELETED)); @@ -78,10 +85,18 @@ suite('Files', () => { for (const change of changes) { assert(event.contains(change.resource, type)); + assert(event.affects(change.resource, type)); } + assert(event.affects(toResource.call(this, '/foo'), type)); + assert(event.affects(toResource.call(this, '/bar'), type)); + assert(event.affects(toResource.call(this, '/'), type)); + assert(!event.affects(toResource.call(this, '/foobar'), type)); + assert(!event.contains(toResource.call(this, '/some/foo/bar'), type)); + assert(!event.affects(toResource.call(this, '/some/foo/bar'), type)); assert(!event.contains(toResource.call(this, '/some/bar'), type)); + assert(!event.affects(toResource.call(this, '/some/bar'), type)); switch (type) { case FileChangeType.ADDED: