Don't automatically match fences

Fixes #306
This commit is contained in:
Laurenz 2023-09-06 12:07:37 +02:00
parent b76e8d5db9
commit fe402759c0
5 changed files with 14 additions and 23 deletions

View File

@ -282,7 +282,7 @@ fn math_expr_prec(p: &mut Parser, min_prec: usize, stop: SyntaxKind) {
math_class(p.current_text()), math_class(p.current_text()),
None | Some(MathClass::Alphabetic) None | Some(MathClass::Alphabetic)
); );
if !maybe_delimited(p, true) { if !maybe_delimited(p) {
p.eat(); p.eat();
} }
} }
@ -321,7 +321,7 @@ fn math_expr_prec(p: &mut Parser, min_prec: usize, stop: SyntaxKind) {
if continuable if continuable
&& min_prec < 3 && min_prec < 3
&& p.prev_end() == p.current_start() && p.prev_end() == p.current_start()
&& maybe_delimited(p, false) && maybe_delimited(p)
{ {
p.wrap(m, SyntaxKind::Math); p.wrap(m, SyntaxKind::Math);
} }
@ -397,29 +397,20 @@ fn math_expr_prec(p: &mut Parser, min_prec: usize, stop: SyntaxKind) {
} }
} }
fn maybe_delimited(p: &mut Parser, allow_fence: bool) -> bool { fn maybe_delimited(p: &mut Parser) -> bool {
if allow_fence && math_class(p.current_text()) == Some(MathClass::Fence) { let open = math_class(p.current_text()) == Some(MathClass::Opening);
math_delimited(p, MathClass::Fence); if open {
true math_delimited(p);
} else if math_class(p.current_text()) == Some(MathClass::Opening) {
math_delimited(p, MathClass::Closing);
true
} else {
false
} }
open
} }
fn math_delimited(p: &mut Parser, stop: MathClass) { fn math_delimited(p: &mut Parser) {
let m = p.marker(); let m = p.marker();
p.eat(); p.eat();
let m2 = p.marker(); let m2 = p.marker();
while !p.eof() && !p.at(SyntaxKind::Dollar) { while !p.eof() && !p.at(SyntaxKind::Dollar) {
let class = math_class(p.current_text()); if math_class(p.current_text()) == Some(MathClass::Closing) {
if stop == MathClass::Fence && class == Some(MathClass::Closing) {
break;
}
if class == Some(stop) {
p.wrap(m2, SyntaxKind::Math); p.wrap(m2, SyntaxKind::Math);
p.eat(); p.eat();
p.wrap(m, SyntaxKind::MathDelimited); p.wrap(m, SyntaxKind::MathDelimited);

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 25 KiB

View File

@ -23,5 +23,5 @@ $ (-1)^n + (1/2 + 3)^(-1/2) $
$ x_1 p_1 frak(p)_1 2_1 dot_1 lg_1 !_1 \\_1 ]_1 "ip"_1 op("iq")_1 \ $ x_1 p_1 frak(p)_1 2_1 dot_1 lg_1 !_1 \\_1 ]_1 "ip"_1 op("iq")_1 \
x^1 b^1 frak(b)^1 2^1 dot^1 lg^1 !^1 \\^1 ]^1 "ib"^1 op("id")^1 \ x^1 b^1 frak(b)^1 2^1 dot^1 lg^1 !^1 \\^1 ]^1 "ib"^1 op("id")^1 \
x_1 y_1 "_"_1 x^1 l^1 "`"^1 attach(I,tl:1,bl:1,tr:1,br:1) x_1 y_1 "_"_1 x^1 l^1 "`"^1 attach(I,tl:1,bl:1,tr:1,br:1)
scripts(sum)_1^1 integral_1^1 |1/2|_1^1 \ scripts(sum)_1^1 integral_1^1 abs(1/2)_1^1 \
x^1_1, "("b y")"^1_1 != (b y)^1_1, "[∫]"_1 [integral]_1 $ x^1_1, "("b y")"^1_1 != (b y)^1_1, "[∫]"_1 [integral]_1 $

View File

@ -2,8 +2,8 @@
--- ---
// Test automatic matching. // Test automatic matching.
$ (a) + {b/2} + |a|/2 + (b) $ $ (a) + {b/2} + abs(a)/2 + (b) $
$f(x/2) < zeta(c^2 + |a + b/2|)$ $f(x/2) < zeta(c^2 + abs(a + b/2))$
--- ---
// Test unmatched. // Test unmatched.
@ -17,7 +17,7 @@ $ lr(| ]1,2\[ + 1/2|) $
--- ---
// Test fence confusion. // Test fence confusion.
$ |x + |y| + z/a| \ $ |x + |y| + z/a| \
|x + lr(|y|) + z/a| $ lr(|x + |y| + z/a|) $
--- ---
// Test that symbols aren't matched automatically. // Test that symbols aren't matched automatically.

View File

@ -4,7 +4,7 @@
// Test spacing cases. // Test spacing cases.
$ä, +, c, (, )$ \ $ä, +, c, (, )$ \
$=), (+), {times}$ $=), (+), {times}$
$<, |-|, [=$ \ $<, abs(-), [=$ \
$a=b, a==b$ \ $a=b, a==b$ \
$-a, +a$ \ $-a, +a$ \
$a not b$ \ $a not b$ \