refac: denest triggerSearchSnippet

This commit is contained in:
Spencer Gouw 2023-08-19 08:16:32 -05:00
parent 1df6b5b0ed
commit 0c1ed70b5c

55
main.ts
View file

@ -267,36 +267,35 @@ export default class JellySnippets extends Plugin {
for (let [lhs, search] of Object.entries(this.searches)) {
let searchResult = search(curLineText);
if (searchResult) {
let lastMatchPart = searchResult.matches.find(
(part) => part[1] === curpos.ch
);
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;
if (!searchResult) {
continue;
}
let lastMatchPart = searchResult.matches.find(
(part) => part[1] === curpos.ch
);
if (!lastMatchPart) {
continue;
}
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;
}
return false;
}