Skip to content

Commit

Permalink
feat(quick-edit-extension): implement excludes on cloudflare fs
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung committed Aug 21, 2024
1 parent b96df7c commit db7c462
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions packages/quick-edit-extension/src/cfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,19 +527,49 @@ declare module "*.bin" {
_options: FileSearchOptions,
_token: CancellationToken
): ProviderResult<Uri[]> {
return this._findFiles(query.pattern);
return this._findFiles(query.pattern, _options.excludes);
}

private _findFiles(query: string | undefined): Uri[] {
private _findFiles(query: string | undefined, excludes: string[]): Uri[] {
const files = this._getFiles();
const result: Uri[] = [];

const pattern = query
? new RegExp(this._convertSimple2RegExpPattern(query))
: null;

// The memfs implementation does not support the `files.exclude` and `search.exclude` settings
// This implement a simple mechanism to filters out files by just matching the filename
// Which is different from the actual VS Code implementation
const excludePatterns = excludes.reduce<Array<RegExp>>(
(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);
}

return patterns;
},
[]
);

for (const file of files) {
if (!pattern || pattern.exec(file.name)) {
if (
(!pattern || pattern.exec(file.name)) &&
// Ensure the file name is not excluded
excludePatterns.every((regex) => !regex.exec(file.name))
) {
result.push(file.uri);
}
}
Expand All @@ -557,7 +587,7 @@ declare module "*.bin" {
) {
const result: TextSearchComplete = { limitHit: false };

const files = this._findFiles(options.includes[0]);
const files = this._findFiles(options.includes[0], options.excludes);
if (files) {
for (const file of files) {
const content = this._textDecoder.decode(await this.readFile(file));
Expand Down

0 comments on commit db7c462

Please sign in to comment.