mirror of
https://github.com/rabirabirara/obsidian-jelly-snippets.git
synced 2026-07-22 07:30:22 +00:00
fix: snippet scans backwards in line to check lhs match
This commit is contained in:
parent
eb7544e211
commit
90311f1b32
1 changed files with 15 additions and 8 deletions
23
main.ts
23
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue