fix: snippet scans backwards in line to check lhs match

This commit is contained in:
Spencer Gouw 2023-04-13 15:17:24 -07:00
parent eb7544e211
commit 90311f1b32

23
main.ts
View file

@ -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;
}
}
}