From e3d6be193a035435cb9fc0954bbe5a96388ed2d4 Mon Sep 17 00:00:00 2001 From: Daniel Bauer Date: Sat, 15 Feb 2025 11:26:03 +0100 Subject: [PATCH] feat: add option for prefix suggestion --- src/DataviewSuggester.ts | 28 +++++++++++++++++++++++----- src/SuggestionIndex.ts | 4 ++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/DataviewSuggester.ts b/src/DataviewSuggester.ts index ac7b3c9..d9b4a99 100644 --- a/src/DataviewSuggester.ts +++ b/src/DataviewSuggester.ts @@ -86,9 +86,17 @@ export class DataviewSuggester extends EditorSuggest { suggestionText += value.replace(/(.*?)<\/mark>/g, "$1"); 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( this.app, - htmlToMarkdown(suggestionText), + markdownText, el, this.context!.file.path, this.app.workspace.getActiveViewOfType(MarkdownView)!, @@ -128,10 +136,20 @@ export class DataviewSuggester extends EditorSuggest { const { editor, start, end } = this.context!; editor.replaceRange(value, start, end); - const newCursorPos = { - line: end.line, - ch: start.ch + value.length + 1, - }; + // move cursor to end of suggestion for finished suggestions + // or to the end of the prefix + 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); } diff --git a/src/SuggestionIndex.ts b/src/SuggestionIndex.ts index bf0d4d3..6cb47c5 100644 --- a/src/SuggestionIndex.ts +++ b/src/SuggestionIndex.ts @@ -159,6 +159,10 @@ export class SuggestionIndex { let compositeValue = this.formatCompositeValue(key, value); compositeValues.push(compositeValue); + let prefixValue = key + ":: "; + if (compositeValues.indexOf(prefixValue) < 0) { + compositeValues.push(prefixValue); + } if (!this.filterCompositeValue(compositeValue)) { continue; }