From 35b63ae1b6c2a8ee1cfa41b3e7bf8cbc9d694e33 Mon Sep 17 00:00:00 2001 From: Edmund Hung Date: Fri, 23 Aug 2024 14:49:30 +0100 Subject: [PATCH] simplify how exclude are matched against each file --- packages/quick-edit-extension/src/cfs.ts | 35 +++++++----------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/packages/quick-edit-extension/src/cfs.ts b/packages/quick-edit-extension/src/cfs.ts index 4a1cad7dc957..85d9c5e707b6 100644 --- a/packages/quick-edit-extension/src/cfs.ts +++ b/packages/quick-edit-extension/src/cfs.ts @@ -539,36 +539,21 @@ declare module "*.bin" { : null; // The memfs implementation does not support the `files.exclude` and `search.exclude` settings - // This implements a simple mechanism to filter out files by just matching the filename - // which does not take into account the full path of the file - const excludePatterns = excludes.reduce>( - (patterns, exclude) => { - if (exclude) { - const lastSlashIndex = exclude.lastIndexOf("/"); - // Excludes might include a glob pattern, e.g. `**/*.js` - // As we are only comparing the filename, we need to make sure the file path is stripped - // This makes the final RegExp /.*\.js/ - const filenamePattern = new RegExp( - this._convertSimple2RegExpPattern( - lastSlashIndex !== -1 - ? exclude.substring(lastSlashIndex + 1) - : exclude - ) - ); - - patterns.push(filenamePattern); - } + // This implements a simple mechanism to filter out files by matching against the file path + // e.g. Both `package.json` and `**/package.json` will exclude all files named `package.json` in any folder + const excludePatterns = excludes.map((exclude) => { + if (!exclude) { + return null; + } - return patterns; - }, - [] - ); + return new RegExp(this._convertSimple2RegExpPattern(exclude)); + }); for (const file of files) { if ( (!pattern || pattern.exec(file.name)) && - // Ensure the file name is not excluded - excludePatterns.every((regex) => !regex.exec(file.name)) + // Ensure the file is not excluded + !excludePatterns.some((regex) => regex?.exec(file.uri.path)) ) { result.push(file.uri); }