diff --git a/src/DataviewSuggester.ts b/src/DataviewSuggester.ts index 0ccf903..c125eb0 100644 --- a/src/DataviewSuggester.ts +++ b/src/DataviewSuggester.ts @@ -133,6 +133,16 @@ export class DataviewSuggester extends EditorSuggest { return true; } + filterFile(filepath: string): boolean { + for (const filterPattern of this.plugin.settings.ignoredFiles) { + const regex = new RegExp(filterPattern); + if (regex.test(filepath)) { + return false; + } + } + return true; + } + buildNewIndex() { console.log("Begin Rebuilding dataview suggestion index"); const startTime = performance.now(); @@ -144,6 +154,10 @@ export class DataviewSuggester extends EditorSuggest { const files = this.app.vault.getFiles(); for (const file of files) { + if (!this.filterFile(file.path)) { + continue; + } + const page = dataviewApi.page(file.path); const fields = Object.keys(page) .filter((k) => k !== "file") @@ -198,7 +212,9 @@ export class DataviewSuggester extends EditorSuggest { updateIndex(type: string, file: TFile, oldPath?: string) { // also triggers on create! if (type === "update") { - console.debug("update index", file); + if (!this.filterFile(file.path)) { + return; + } const updateCompositeValues = []; const page = getAPI(this.app).page(file.path); @@ -227,7 +243,7 @@ export class DataviewSuggester extends EditorSuggest { } } - const oldCompositeValues = this.suggestionsRefs.get(file.path)!; + const oldCompositeValues = this.suggestionsRefs.get(file.path) || []; // deleting value from index if update reduces refcount to 0 for (const oldCompositeValue of oldCompositeValues) { @@ -238,7 +254,6 @@ export class DataviewSuggester extends EditorSuggest { this.suggestionsRefCount.get(oldCompositeValue)! - 1, ); if (this.suggestionsRefCount.get(oldCompositeValue) === 0) { - console.debug("deleting value from suggestion index", oldCompositeValue); this.suggestionsList.splice(this.suggestionsList.indexOf(oldCompositeValue), 1); } } @@ -246,24 +261,36 @@ export class DataviewSuggester extends EditorSuggest { // adding value to index if not present in index for (const newCompositeValue of updateCompositeValues) { - if (oldCompositeValues.indexOf(newCompositeValue) === -1) { - // add value (also check presence in other files via refcount first) - if (!this.suggestionsRefCount.has(newCompositeValue)) { - console.debug("adding value from suggestion index", newCompositeValue); - this.suggestionsList.push(newCompositeValue); - this.suggestionsRefCount.set(newCompositeValue, 1); - } else { - this.suggestionsRefCount.set( - newCompositeValue, - this.suggestionsRefCount.get(newCompositeValue)! + 1, - ); - } + if (!this.suggestionsRefCount.has(newCompositeValue)) { + // not seen in this or other files + this.suggestionsList.push(newCompositeValue); + this.suggestionsRefCount.set(newCompositeValue, 1); + } else if (oldCompositeValues.indexOf(newCompositeValue) === -1) { + this.suggestionsRefCount.set( + newCompositeValue, + this.suggestionsRefCount.get(newCompositeValue)! + 1, + ); } } this.suggestionsRefs.set(file.path, updateCompositeValues); } else if (type === "rename") { - this.suggestionsRefs.set(file.path, this.suggestionsRefs.get(oldPath!)!); - this.suggestionsRefs.delete(oldPath!); + if (this.filterFile(file.path) && this.filterFile(oldPath!)) { + // both not ignored -> move refs + this.suggestionsRefs.set(file.path, this.suggestionsRefs.get(oldPath!)!); + this.suggestionsRefs.delete(oldPath!); + } else if (this.filterFile(file.path)) { + // old path ignored, new path not ignored -> upsert + this.updateIndex("update", file, undefined); + } else if (this.filterFile(oldPath!)) { + // old path not ignored, new path ignored -> delete + for (const value of this.suggestionsRefs.get(oldPath!)!) { + this.suggestionsRefCount.set(value, this.suggestionsRefCount.get(value)! - 1); + if (this.suggestionsRefCount.get(value) === 0) { + this.suggestionsList.splice(this.suggestionsList.indexOf(value), 1); + } + } + this.suggestionsRefs.delete(oldPath!); + } } else if (type === "delete") { // iterate suggestion refs in deleted file and decrement their ref count // if the ref count reaches 0, remove the suggestion from the list diff --git a/src/SettingsTab.ts b/src/SettingsTab.ts index 054d6e8..385b6aa 100644 --- a/src/SettingsTab.ts +++ b/src/SettingsTab.ts @@ -36,5 +36,22 @@ export class SettingsTab extends PluginSettingTab { this.plugin.suggester?.buildNewIndex(); }); }); + new Setting(containerEl) + .setName("Exclude files") + .setDesc("Path pattern to exclude files or folders from suggestions. One pattern per line.") + .addTextArea((textArea) => { + textArea.inputEl.setAttribute("rows", "5"); + textArea + .setPlaceholder("templates/") + .setValue(this.plugin.settings.ignoredFiles.join("\n")) + .onChange(async (value) => { + this.plugin.settings.ignoredFiles = value + .split("\n") + .map((val) => val.trim()) + .filter((val) => val.length > 0); + await this.plugin.saveSettings(); + this.plugin.suggester?.buildNewIndex(); + }); + }); } } diff --git a/src/main.ts b/src/main.ts index 9ace2ab..2b3a8ed 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,10 +5,12 @@ import { SettingsTab } from "./SettingsTab"; interface DataviewAutocompleteSettings { ignoredFields: string[]; + ignoredFiles: string[]; } const DEFAULT_SETTINGS: DataviewAutocompleteSettings = { ignoredFields: ["created.*", "modified.*", "date"], + ignoredFiles: ["templates/"], }; export default class DataviewAutocompletePlugin extends Plugin { diff --git a/test-vault/templates/Ignored file.md b/test-vault/templates/Ignored file.md new file mode 100644 index 0000000..ab587df --- /dev/null +++ b/test-vault/templates/Ignored file.md @@ -0,0 +1 @@ +[ignored:: test]