From b38d9ad4fcd1d97bca30b5423d05f520873e23dd Mon Sep 17 00:00:00 2001 From: william duan <38791932+duanwilliam@users.noreply.github.com> Date: Tue, 24 Oct 2023 19:29:28 -0700 Subject: [PATCH] fix(autofraction): only strip parentheses when they match previously, `(a)(b)/` would be erroneously transformed into `\frac{a)(b}{}`, as algorithm would just check if first and last were open and closed parens. now, only removes outer parentheses if they are matched. fixes #206 --- src/features/autofraction.ts | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/features/autofraction.ts b/src/features/autofraction.ts index 6756c5f..9fdaa5f 100644 --- a/src/features/autofraction.ts +++ b/src/features/autofraction.ts @@ -94,17 +94,22 @@ export const runAutoFractionCursor = (view: EditorView, ctx: Context, range: Sel } } - // Run autofraction - let numerator = view.state.sliceDoc(start, to); - // Don't run on an empty line - if (numerator === "") return false; - + if (start === to) { return false; } + + // Remove unnecessary outer parentheses + const { beg, end } = (() => { + if (curLine.charAt(start) === "(" && curLine.charAt(to - 1) === ")") { + const closing = findMatchingBracket(curLine, start, "(", ")", false, to); + if (closing === to - 1) { + return { beg: start + 1, end: to - 1 }; + } + } + return { beg: start, end: to }; + })(); - // Remove brackets - if (curLine.charAt(start) === "(" && curLine.charAt(to - 1) === ")") { - numerator = numerator.slice(1, -1); - } + // Run autofraction + const numerator = view.state.sliceDoc(beg, end); const replacement = `${settings.autofractionSymbol}{${numerator}}{$0}$1`