mirror of
https://github.com/dnlbauer/obsidian-dataview-autocompletion.git
synced 2026-09-11 14:45:30 +00:00
fix: update deletes too many values
This commit is contained in:
@@ -20,8 +20,8 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
|||||||
|
|
||||||
initialized: boolean = false;
|
initialized: boolean = false;
|
||||||
suggestionsList: string[] = [];
|
suggestionsList: string[] = [];
|
||||||
suggestionsRefs: { [key: string]: string[] } = {}; // maps file paths to included suggestions
|
suggestionsRefs: Map<string, string[]> = new Map(); // maps file paths to included suggestions
|
||||||
suggestionsRefCount: { [key: string]: number } = {}; // maps suggestions to number of files including them
|
suggestionsRefCount: Map<string, number> = new Map(); // maps suggestions to number of files including them
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
plugin: DataviewAutocompletePlugin,
|
plugin: DataviewAutocompletePlugin,
|
||||||
@@ -140,8 +140,8 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
|||||||
const dataviewApi = getAPI(this.app);
|
const dataviewApi = getAPI(this.app);
|
||||||
|
|
||||||
const newSuggestions: string[] = [];
|
const newSuggestions: string[] = [];
|
||||||
const newSuggestionsRefs: { [key: string]: string[] } = {};
|
const newSuggestionsRefs: Map<string, string[]> = new Map();
|
||||||
const newSuggestionsRefCount: { [key: string]: number } = {};
|
const newSuggestionsRefCount: Map<string, number> = new Map();
|
||||||
|
|
||||||
const files = this.app.vault.getFiles();
|
const files = this.app.vault.getFiles();
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
@@ -170,16 +170,16 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
|||||||
if (newSuggestions.indexOf(compositeValue) === -1) {
|
if (newSuggestions.indexOf(compositeValue) === -1) {
|
||||||
// suggestion not seen on any page yet
|
// suggestion not seen on any page yet
|
||||||
pageRefs.push(compositeValue);
|
pageRefs.push(compositeValue);
|
||||||
newSuggestionsRefCount[compositeValue] = 1;
|
newSuggestionsRefCount.set(compositeValue, 1);
|
||||||
newSuggestions.push(compositeValue);
|
newSuggestions.push(compositeValue);
|
||||||
} else if (pageRefs.indexOf(compositeValue) === -1) {
|
} else if (pageRefs.indexOf(compositeValue) === -1) {
|
||||||
// suggestion not seen on this page, but on another
|
// suggestion not seen on this page, but on another
|
||||||
pageRefs.push(compositeValue);
|
pageRefs.push(compositeValue);
|
||||||
newSuggestionsRefCount[compositeValue] + -1;
|
newSuggestionsRefCount.set(compositeValue, newSuggestionsRefCount.get(compositeValue)! - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
newSuggestionsRefs[file.path] = pageRefs;
|
newSuggestionsRefs.set(file.path, pageRefs);
|
||||||
}
|
}
|
||||||
|
|
||||||
// replace old index
|
// replace old index
|
||||||
@@ -196,6 +196,7 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
|||||||
updateIndex(type: string, file: TFile, oldPath?: string) {
|
updateIndex(type: string, file: TFile, oldPath?: string) {
|
||||||
// also triggers on create!
|
// also triggers on create!
|
||||||
if (type === "update") {
|
if (type === "update") {
|
||||||
|
console.debug("update index", file);
|
||||||
const updateCompositeValues = [];
|
const updateCompositeValues = [];
|
||||||
|
|
||||||
const page = getAPI(this.app).page(file.path);
|
const page = getAPI(this.app).page(file.path);
|
||||||
@@ -219,39 +220,56 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
|||||||
updateCompositeValues.push(compositeValue);
|
updateCompositeValues.push(compositeValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (const compositeValue of this.suggestionsRefs[file.path]) {
|
}
|
||||||
if (updateCompositeValues.indexOf(compositeValue) === -1) {
|
|
||||||
// delete value
|
const oldCompositeValues = this.suggestionsRefs.get(file.path)!;
|
||||||
this.suggestionsRefCount[compositeValue] -= 1;
|
|
||||||
if (this.suggestionsRefCount[compositeValue] == 0) {
|
// deleting value from index if update reduces refcount to 0
|
||||||
this.suggestionsList.splice(this.suggestionsList.indexOf(compositeValue), 1);
|
for (const oldCompositeValue of oldCompositeValues) {
|
||||||
}
|
if (updateCompositeValues.indexOf(oldCompositeValue) === -1) {
|
||||||
}
|
// delete value
|
||||||
}
|
this.suggestionsRefCount.set(
|
||||||
for (const newCompositeValue of updateCompositeValues) {
|
oldCompositeValue,
|
||||||
if (this.suggestionsRefs[file.path].indexOf(newCompositeValue) === -1) {
|
this.suggestionsRefCount.get(oldCompositeValue)! - 1,
|
||||||
// add value (also check presence in other files via refcount first)
|
);
|
||||||
if (this.suggestionsList.indexOf(newCompositeValue) === -1) {
|
if (this.suggestionsRefCount.get(oldCompositeValue) === 0) {
|
||||||
this.suggestionsList.push(newCompositeValue);
|
console.debug("deleting value from suggestion index", oldCompositeValue);
|
||||||
this.suggestionsRefCount[newCompositeValue] += 1;
|
this.suggestionsList.splice(this.suggestionsList.indexOf(oldCompositeValue), 1);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.suggestionsRefs[file.path] = updateCompositeValues;
|
|
||||||
|
// 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,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.suggestionsRefs.set(file.path, updateCompositeValues);
|
||||||
} else if (type === "rename") {
|
} else if (type === "rename") {
|
||||||
this.suggestionsRefs[file.path] = this.suggestionsRefs[oldPath!];
|
this.suggestionsRefs.set(file.path, this.suggestionsRefs.get(oldPath!)!);
|
||||||
delete this.suggestionsRefs[oldPath!];
|
this.suggestionsRefs.delete(oldPath!);
|
||||||
} else if (type === "delete") {
|
} else if (type === "delete") {
|
||||||
// iterate suggestion refs in deleted file and decrement their ref count
|
// iterate suggestion refs in deleted file and decrement their ref count
|
||||||
// if the ref count reaches 0, remove the suggestion from the list
|
// if the ref count reaches 0, remove the suggestion from the list
|
||||||
for (const value of this.suggestionsRefs[file.path]) {
|
for (const value of this.suggestionsRefs.get(file.path)!) {
|
||||||
this.suggestionsRefCount[value] -= 1;
|
this.suggestionsRefCount.set(value, this.suggestionsRefCount.get(value)! - 1);
|
||||||
if (this.suggestionsRefCount[value] == 0) {
|
if (this.suggestionsRefCount.get(value) === 0) {
|
||||||
|
console.debug("deleting value from suggestion index", value);
|
||||||
this.suggestionsList.splice(this.suggestionsList.indexOf(value), 1);
|
this.suggestionsList.splice(this.suggestionsList.indexOf(value), 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
delete this.suggestionsRefs[file.path];
|
this.suggestionsRefs.delete(file.path);
|
||||||
} else {
|
} else {
|
||||||
console.warn("Unknown update type:", type, file, oldPath);
|
console.warn("Unknown update type:", type, file, oldPath);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user