refactor: consolidate file exclusion logic with shouldBeCached method

Move duplicate exclusion checks into a single shouldBeCached method and apply it consistently across file and folder events. Also fix glob pattern matching to correctly handle directory patterns ending with /.
This commit is contained in:
Andrew Beal 2025-10-30 08:34:11 +00:00
parent 211b8905ca
commit ae5a5ed5de
2 changed files with 20 additions and 18 deletions

View file

@ -59,6 +59,10 @@ export class VaultCacheService {
private registerFileEvents() {
this.vaultService.registerFileEvents((event, file, args) => {
if (!this.shouldBeCached(file.path) || (args.oldPath && !this.shouldBeCached(args.oldPath))) {
return;
}
if (file instanceof TFile) {
switch (event) {
case FileEvent.Create:
@ -90,18 +94,12 @@ export class VaultCacheService {
} else if (file instanceof TFolder) {
switch (event) {
case FileEvent.Create:
// Check if folder should be excluded before caching
if (this.vaultService.getAbstractFileByPath(file.path, false) !== null) {
this.folders.set(file.path, file);
}
this.folders.set(file.path, file);
break;
case FileEvent.Rename:
this.folders.delete(args.oldPath);
// Check if folder should be excluded before caching
if (this.vaultService.getAbstractFileByPath(file.path, false) !== null) {
this.folders.set(file.path, file);
}
this.folders.set(file.path, file);
break;
case FileEvent.Delete:
@ -158,4 +156,8 @@ export class VaultCacheService {
this.preparedFolders.push({ prepared: fuzzysort.prepare(folder.path), folder: folder });
});
}
private shouldBeCached(path: string) {
return this.vaultService.getAbstractFileByPath(path, false) !== null
}
}

View file

@ -228,9 +228,9 @@ export class VaultService {
}
private async createDirectories(filePath: string, allowAccessToPluginRoot: boolean = false) {
const dirPath: string = filePath.substring(0, filePath.lastIndexOf('/'));
const dirPath: string = filePath.substring(0, filePath.lastIndexOf("/"));
const dirs: string[] = dirPath.split('/');
const dirs: string[] = dirPath.split("/");
let currentPath = "";
for (const dir of dirs) {
@ -318,19 +318,19 @@ export class VaultService {
// First, temporarily replace wildcards to protect them from escaping
let regexPattern = pattern
.replace(/\*\*/g, '::DOUBLESTAR::') // Temporarily replace **
.replace(/\*/g, '::SINGLESTAR::') // Temporarily replace *
.replace(/[.+?^${}()|[\]\\]/g, '\\$&') // Escape special regex chars
.replace(/::SINGLESTAR::/g, '[^/]*') // * matches anything except /
.replace(/::DOUBLESTAR::/g, '.*'); // ** matches anything including /
.replace(/\*\*/g, "::DOUBLESTAR::") // Temporarily replace **
.replace(/\*/g, "::SINGLESTAR::") // Temporarily replace *
.replace(/[.+?^${}()|[\]\\]/g, "\\$&") // Escape special regex chars
.replace(/::SINGLESTAR::/g, "[^/]*") // * matches anything except /
.replace(/::DOUBLESTAR::/g, ".*"); // ** matches anything including /
// If pattern ends with /, match the directory and all its contents
if (pattern.endsWith('/')) {
regexPattern = regexPattern + '.*';
if (pattern.endsWith("/")) {
regexPattern = regexPattern + ".*";
}
// Add anchors for full path matching
const regex = new RegExp('^' + regexPattern + '$');
const regex = new RegExp("^" + regexPattern + "(/.*)?$");
return regex.test(filePath);
});