Skip to content

Commit

Permalink
fix(autofraction): only strip parentheses when they match
Browse files Browse the repository at this point in the history
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 artisticat1#206
  • Loading branch information
duanwilliam committed Oct 30, 2023
1 parent af82276 commit b38d9ad
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/features/autofraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down

0 comments on commit b38d9ad

Please sign in to comment.