refac: properly acknowledge __MR snippet type info

Does not fix interaction with auto trigger by enter yet.
This commit is contained in:
Spencer Gouw 2023-09-22 02:43:17 -05:00
parent 641fef15df
commit b769bf70be
3 changed files with 13 additions and 5 deletions

10
main.ts
View file

@ -136,12 +136,13 @@ export default class JellySnippets extends Plugin {
let snippetLines = this.settings.snippetsFile.split(snippetDivider);
for (let snippet of snippetLines) {
// Trim newlines. Instead, use symbols to let people insert whitespace.
// This split means only the first division of the part divider is the LHS.
let snippetParts = snippet
.trim()
.split(this.settings.snippetPartDivider);
if (snippetParts.length !== 2) {
console.log("found more than 2 snippet parts: ");
console.log(snippetParts);
// probably an incomplete snippet
continue;
}
// Produce lhs. Continue if undefined.
let lhs = snippetParts.shift();
@ -150,7 +151,8 @@ export default class JellySnippets extends Plugin {
continue;
}
// Produce rhs (raw data).
let rhsData = snippetParts.join(this.settings.snippetPartDivider); // why a join? because
// * This is a join in case they used their snippetPartDivider too many times.
let rhsData = snippetParts.join(this.settings.snippetPartDivider);
// Scan rhs for symbols and perform the replacements; acquire RHS.
let rhs = Symbol.replaceSymbolsOnParse(rhsData);
this.multilineSnippets[lhs] = rhs;
@ -326,7 +328,7 @@ export default class JellySnippets extends Plugin {
let type = SnippetType.SLSR;
// Compiler doesn't complain if we convert boolean to number with unary '+'.
type |= +lhs.contains("\n") && SnippetType.MLSR;
type |= +rhs.data.contains("\n") && SnippetType.SLMR;
type |= +rhs.info.hasNewline && SnippetType.SLMR;
return type;
}

View file

@ -2,6 +2,7 @@ export type LHS = string;
export interface RHS {
data: string;
info: {
hasNewline: boolean;
cursorEnd: number;
};
}

View file

@ -22,6 +22,7 @@ export namespace Symbol {
let i = 0;
let cursor = 0;
let endFoundIdx = 0;
let hasNewline = false;
let found;
while (i < inputStr.length) {
found = false;
@ -32,6 +33,9 @@ export namespace Symbol {
cursor += REPLACEABLE[symbol].length;
i += symbol.length;
found = true;
if (symbol == Symbol.Newline) {
hasNewline = true;
}
break;
}
}
@ -50,6 +54,7 @@ export namespace Symbol {
}
let data = result.join("");
let info = {
hasNewline,
cursorEnd: data.length - endFoundIdx,
};