From b769bf70be0f0d1b6fc3665d09df34e7b5d0cad0 Mon Sep 17 00:00:00 2001 From: Spencer Gouw Date: Fri, 22 Sep 2023 02:43:17 -0500 Subject: [PATCH] refac: properly acknowledge __MR snippet type info Does not fix interaction with auto trigger by enter yet. --- main.ts | 12 +++++++----- snippet.ts | 1 + symbol.ts | 5 +++++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/main.ts b/main.ts index 960b3e4..b5bbf3d 100644 --- a/main.ts +++ b/main.ts @@ -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; @@ -242,7 +244,7 @@ export default class JellySnippets extends Plugin { // YesWS let curpos = editor.getCursor(); if (snippetType === SnippetType.MLSR) { - // RCNN - insert newline ("repeat enter") / replace curpos with newline + // RCNN - insert newline ("repeat enter") / replace curpos with newline editor.exec("newlineAndIndent"); editor.exec("indentLess"); } else { @@ -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; } diff --git a/snippet.ts b/snippet.ts index b2a7a4b..1218341 100644 --- a/snippet.ts +++ b/snippet.ts @@ -2,6 +2,7 @@ export type LHS = string; export interface RHS { data: string; info: { + hasNewline: boolean; cursorEnd: number; }; } diff --git a/symbol.ts b/symbol.ts index 60525c5..2c0562d 100644 --- a/symbol.ts +++ b/symbol.ts @@ -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, };