From a4cebf17d39023ececfc4fcaebf3a5ea6ac84394 Mon Sep 17 00:00:00 2001 From: Diomede T Date: Wed, 16 Feb 2022 21:59:36 +0100 Subject: [PATCH] little cleanup and fix the regex that triggers the autocomplete --- src/main.ts | 1 - src/suggest/codemirror-suggest.ts | 129 ------------------------------ src/suggest/template-suggest.ts | 13 +-- 3 files changed, 3 insertions(+), 140 deletions(-) delete mode 100644 src/suggest/codemirror-suggest.ts diff --git a/src/main.ts b/src/main.ts index 5e1552a..6448bfe 100644 --- a/src/main.ts +++ b/src/main.ts @@ -27,7 +27,6 @@ export default class LiquidTemplates extends Plugin { async saveSettings(): Promise { // the templates folder can change - // this.setupAutosuggest(); await this.saveData(this.settings); } diff --git a/src/suggest/codemirror-suggest.ts b/src/suggest/codemirror-suggest.ts deleted file mode 100644 index 36e9a71..0000000 --- a/src/suggest/codemirror-suggest.ts +++ /dev/null @@ -1,129 +0,0 @@ -import { App, ISuggestOwner, Scope } from "obsidian"; - -import Suggest from "./suggest"; - -function checkForInputPhrase( - cmEditor: CodeMirror.Editor, - pos: CodeMirror.Position, - phrase: string -): boolean { - const from = { - line: pos.line, - ch: pos.ch - phrase.length, - }; - return cmEditor.getRange(from, pos) === phrase; -} - -function isCursorBeforePos( - pos: CodeMirror.Position, - cursor: CodeMirror.Position -): boolean { - if (pos.line === cursor.line) { - return cursor.ch < pos.ch; - } - return cursor.line < pos.line; -} - -export default abstract class CodeMirrorSuggest implements ISuggestOwner { - protected app: App; - protected cmEditor: CodeMirror.Editor; - private scope: Scope; - - private suggestEl: HTMLElement; - private instructionsEl: HTMLElement; - private suggest: Suggest; - - private startPos: CodeMirror.Position; - private triggerPhrase: string; - - constructor(app: App, triggerPhrase: string) { - this.triggerPhrase = triggerPhrase; - this.app = app; - this.scope = new Scope(); - - this.suggestEl = createDiv("suggestion-container"); - const suggestion = this.suggestEl.createDiv("suggestion"); - this.instructionsEl = this.suggestEl.createDiv("prompt-instructions"); - this.suggest = new Suggest(this, suggestion, this.scope); - - this.scope.register([], "Escape", this.close.bind(this)); - } - - public setInstructions( - createInstructionsFn: (containerEl: HTMLElement) => void - ): void { - this.instructionsEl.empty(); - createInstructionsFn(this.instructionsEl); - } - - public update( - cmEditor: CodeMirror.Editor, - changeObj: CodeMirror.EditorChange - ): boolean { - if (this.cmEditor !== cmEditor) { - this.suggestEl?.detach(); - } - this.cmEditor = cmEditor; - const cursorPos = cmEditor.getCursor(); - - // autosuggest is open - if (this.suggestEl.parentNode) { - if (isCursorBeforePos(this.startPos, cursorPos)) { - this.close(); - return false; - } - this.attachAtCursor(); - } else { - if ( - changeObj.text.length === 1 && // ignore multi-cursors - checkForInputPhrase(this.cmEditor, cursorPos, this.triggerPhrase) && - !document.querySelector(".suggestion-container") // don't trigger multiple autosuggests - ) { - this.startPos = cursorPos; - this.open(); - this.attachAtCursor(); - } - } - - return false; - } - - protected getStartPos(): CodeMirror.Position { - return { - line: this.startPos.line, - ch: this.startPos.ch - this.triggerPhrase.length, - }; - } - - protected getInputStr(): string { - // return string from / to cursor - const cursor = this.cmEditor.getCursor(); - const line = this.cmEditor.getLine(cursor.line); - return line.substring(this.startPos.ch, cursor.ch); - } - - private attachAtCursor() { - const inputStr = this.getInputStr(); - const suggestions = this.getSuggestions(inputStr); - this.suggest.setSuggestions(suggestions); - - this.cmEditor.addWidget(this.cmEditor.getCursor(), this.suggestEl, true); - } - - open(): void { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (this.app).keymap.pushScope(this.scope); - } - - close(): void { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (this.app).keymap.popScope(this.scope); - this.startPos = null; - this.suggest.setSuggestions([]); - this.suggestEl.detach(); - } - - abstract getSuggestions(inputStr: string): T[]; - abstract renderSuggestion(item: T, el: HTMLElement): void; - abstract selectSuggestion(item: T, evt: MouseEvent | KeyboardEvent): void; -} diff --git a/src/suggest/template-suggest.ts b/src/suggest/template-suggest.ts index 40f4c2a..4da1669 100644 --- a/src/suggest/template-suggest.ts +++ b/src/suggest/template-suggest.ts @@ -124,7 +124,7 @@ export default class TemplateSuggest extends EditorSuggest const match = lineContents .substring(0, cursor.ch) - .match(new RegExp(`(?:^|\s|\W)(${this.triggerPhrase}[^${this.triggerPhrase}]*$)`)); + .match(new RegExp(`${this.triggerPhrase}(.*)`)); if (match === null) return null; @@ -141,16 +141,9 @@ export default class TemplateSuggest extends EditorSuggest cursor: EditorPosition ): EditorSuggestTriggerInfo { return { - start: this.getStartPos(match, cursor.line), + start: { line: cursor.line, ch: match.index }, end: cursor, - query: match[1].substring(this.triggerPhrase.length), - }; - } - - protected getStartPos(match: RegExpMatchArray, line: number): EditorPosition { - return { - line: line, - ch: match.index + match[0].length - match[1].length, + query: match[1], }; } }