mirror of
https://github.com/dnlbauer/obsidian-dataview-autocompletion.git
synced 2026-09-10 14:15:29 +00:00
filter fields at index time
This commit is contained in:
@@ -53,26 +53,15 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getSuggestions(context: EditorSuggestContext): string[] | Promise<string[]> {
|
getSuggestions(context: EditorSuggestContext): string[] | Promise<string[]> {
|
||||||
// TODO: add filter at index-time?
|
const idxs = this.searcher.filter(this.suggestionsList, context.query);
|
||||||
let filtered = this.suggestionsList;
|
|
||||||
for (const filterPattern of this.plugin.settings.ignoredFields) {
|
|
||||||
const regex = new RegExp(`^(${filterPattern})::.*`);
|
|
||||||
const ignoredSuggestions = filtered.filter((suggestion) => regex.test(suggestion));
|
|
||||||
// minimatch.match(filtered, filterPattern + ":: *", { partial: true })
|
|
||||||
for (const ignoredSuggestion of ignoredSuggestions) {
|
|
||||||
filtered = filtered.slice(filtered.indexOf(ignoredSuggestion) + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const idxs = this.searcher.filter(filtered, context.query);
|
|
||||||
if (idxs != null && idxs.length > 0) {
|
if (idxs != null && idxs.length > 0) {
|
||||||
let info = this.searcher.info(idxs, filtered, context.query);
|
let info = this.searcher.info(idxs, this.suggestionsList, context.query);
|
||||||
let order = this.searcher.sort(info, filtered, context.query);
|
let order = this.searcher.sort(info, this.suggestionsList, context.query);
|
||||||
|
|
||||||
// return top N suggestions with marks
|
// return top N suggestions with marks
|
||||||
return order
|
return order
|
||||||
.slice(0, this.maxSuggestions)
|
.slice(0, this.maxSuggestions)
|
||||||
.map((idx) => [idx, filtered[info.idx[idx]]])
|
.map((idx) => [idx, this.suggestionsList[info.idx[idx]]])
|
||||||
.map((suggestion: [number, string]) => uFuzzy.highlight(suggestion[1], info.ranges[suggestion[0]]));
|
.map((suggestion: [number, string]) => uFuzzy.highlight(suggestion[1], info.ranges[suggestion[0]]));
|
||||||
}
|
}
|
||||||
return [];
|
return [];
|
||||||
@@ -134,6 +123,16 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
|||||||
return `${key}:: ${stringValue}`;
|
return `${key}:: ${stringValue}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
filterCompositeValue(compositeValue: string): boolean {
|
||||||
|
for (const filterPattern of this.plugin.settings.ignoredFields) {
|
||||||
|
const regex = new RegExp(`^(${filterPattern})::.*`);
|
||||||
|
if (regex.test(compositeValue)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
buildNewIndex() {
|
buildNewIndex() {
|
||||||
console.log("Begin Rebuilding dataview suggestion index");
|
console.log("Begin Rebuilding dataview suggestion index");
|
||||||
const startTime = performance.now();
|
const startTime = performance.now();
|
||||||
@@ -166,6 +165,9 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
|||||||
if (value === null || value === undefined) continue; // skip empty fields
|
if (value === null || value === undefined) continue; // skip empty fields
|
||||||
|
|
||||||
let compositeValue = this.formatCompositeValue(key, value);
|
let compositeValue = this.formatCompositeValue(key, value);
|
||||||
|
if (!this.filterCompositeValue(compositeValue)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (newSuggestions.indexOf(compositeValue) === -1) {
|
if (newSuggestions.indexOf(compositeValue) === -1) {
|
||||||
// suggestion not seen on any page yet
|
// suggestion not seen on any page yet
|
||||||
@@ -216,6 +218,9 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
|||||||
if (value === null || value === undefined) continue; // skip empty fields
|
if (value === null || value === undefined) continue; // skip empty fields
|
||||||
|
|
||||||
let compositeValue = this.formatCompositeValue(key, value);
|
let compositeValue = this.formatCompositeValue(key, value);
|
||||||
|
if (!this.filterCompositeValue(compositeValue)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (updateCompositeValues.indexOf(compositeValue) === -1) {
|
if (updateCompositeValues.indexOf(compositeValue) === -1) {
|
||||||
updateCompositeValues.push(compositeValue);
|
updateCompositeValues.push(compositeValue);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ export class SettingsTab extends PluginSettingTab {
|
|||||||
.map((val) => val.trim())
|
.map((val) => val.trim())
|
||||||
.filter((val) => val.length > 0);
|
.filter((val) => val.length > 0);
|
||||||
await this.plugin.saveSettings();
|
await this.plugin.saveSettings();
|
||||||
|
this.plugin.suggester?.buildNewIndex();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user