From 90311f1b325c03d17ff55fc271df96cb195ce7cd Mon Sep 17 00:00:00 2001 From: Spencer Gouw Date: Thu, 13 Apr 2023 15:17:24 -0700 Subject: [PATCH] fix: snippet scans backwards in line to check lhs match --- main.ts | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/main.ts b/main.ts index 2f9755b..de20d11 100644 --- a/main.ts +++ b/main.ts @@ -214,16 +214,23 @@ export default class JellySnippets extends Plugin { for (let [lhs, search] of Object.entries(this.searches)) { let searchResult = search(curLineText); if (searchResult) { - let lateMatchPart = searchResult.matches.find( + let lastMatchPart = searchResult.matches.find( (part) => part[1] === curpos.ch, ); - if (lateMatchPart) { - // Since simpleSearch separates by word for some dumb reason, use the earliest match in searchResult.matches. - let earlyMatchPart = searchResult.matches[0]; - let from: EditorPosition = { line: line, ch: earlyMatchPart[0] }; - let to: EditorPosition = { line: line, ch: lateMatchPart[1] }; - editor.replaceRange(this.searchSnippets[lhs], from, to); - return true; + if (lastMatchPart) { + if (curpos.ch >= lhs.length) { + // TODO: This change fixes old bug but snippet now triggers even if there is no whitespace before the lhs. Or does it? Verify this... + // TODO: lhs still cannot have newlines in it. Wouldn't be hard to update however. + let from: EditorPosition = { line: line, ch: curpos.ch - lhs.length }; + let to: EditorPosition = { line: line, ch: lastMatchPart[1] }; + let lookBack = editor.getRange(from, to); + if (lookBack === lhs) { + editor.replaceRange(this.searchSnippets[lhs], from, to); + return true; + } + } + // snippet before cursor found but not triggered means no other snippet should trigger + return false; } } }