feat: add option for prefix suggestion

This commit is contained in:
Daniel Bauer
2025-02-15 11:26:03 +01:00
parent f5f9d49b0b
commit e3d6be193a
2 changed files with 27 additions and 5 deletions

View File

@@ -86,9 +86,17 @@ export class DataviewSuggester extends EditorSuggest<String> {
suggestionText += value.replace(/<mark>(.*?)<\/mark>/g, "$1"); suggestionText += value.replace(/<mark>(.*?)<\/mark>/g, "$1");
suggestionText += suggestionText.startsWith("(") ? ")" : "]"; suggestionText += suggestionText.startsWith("(") ? ")" : "]";
// Different rendering for prefix suggestions
var markdownText;
if (/.*\:: [\]\)]/.test(suggestionText)) {
markdownText = htmlToMarkdown("[" + suggestionText.slice(1, -1) + "*…*]");
} else {
markdownText = htmlToMarkdown(suggestionText);
}
console.log(markdownText);
MarkdownRenderer.render( MarkdownRenderer.render(
this.app, this.app,
htmlToMarkdown(suggestionText), markdownText,
el, el,
this.context!.file.path, this.context!.file.path,
this.app.workspace.getActiveViewOfType(MarkdownView)!, this.app.workspace.getActiveViewOfType(MarkdownView)!,
@@ -128,10 +136,20 @@ export class DataviewSuggester extends EditorSuggest<String> {
const { editor, start, end } = this.context!; const { editor, start, end } = this.context!;
editor.replaceRange(value, start, end); editor.replaceRange(value, start, end);
const newCursorPos = { // move cursor to end of suggestion for finished suggestions
line: end.line, // or to the end of the prefix
ch: start.ch + value.length + 1, var newCursorPos;
}; if (value.endsWith(":: ")) {
newCursorPos = {
line: end.line,
ch: start.ch + value.length,
};
} else {
newCursorPos = {
line: end.line,
ch: start.ch + value.length + 1,
};
}
editor.setCursor(newCursorPos); editor.setCursor(newCursorPos);
} }

View File

@@ -159,6 +159,10 @@ export class SuggestionIndex {
let compositeValue = this.formatCompositeValue(key, value); let compositeValue = this.formatCompositeValue(key, value);
compositeValues.push(compositeValue); compositeValues.push(compositeValue);
let prefixValue = key + ":: ";
if (compositeValues.indexOf(prefixValue) < 0) {
compositeValues.push(prefixValue);
}
if (!this.filterCompositeValue(compositeValue)) { if (!this.filterCompositeValue(compositeValue)) {
continue; continue;
} }