Skip to content

Commit

Permalink
feat: parse math parentheses after [_^\/√∛∜] (#1106)
Browse files Browse the repository at this point in the history
* test: move math syntax highlight tests

* feat: parse math parentheses after `_` and `^`

* feat: all opening/closing kinds

* dev: more kinds
  • Loading branch information
Myriad-Dreamin authored Jan 3, 2025
1 parent d4ba43c commit 626efa0
Show file tree
Hide file tree
Showing 16 changed files with 209 additions and 16 deletions.
2 changes: 2 additions & 0 deletions editors/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@
"markup.raw.inline.typst",
"string.other.label.typst",
"string.quoted.double.typst",
"constant.other.symbol.typst",
"constant.character.escape",
"comment.block.typst",
"comment.line.double-slash.typst"
Expand All @@ -724,6 +725,7 @@
"markup.raw.inline.typst",
"string.other.label.typst",
"string.quoted.double.typst",
"constant.other.symbol.typst",
"constant.character.escape",
"comment.block.typst",
"comment.line.double-slash.typst"
Expand Down
63 changes: 62 additions & 1 deletion syntaxes/textmate/main.mts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ function oneOf(...patterns: RegExp[]) {
);
}

function replaceGroup(pattern: RegExp, group: string, replacement: RegExp) {
return new RegExp(pattern.source.replace(group, replacement.source));
}

const PAREN_BLOCK = generatePattern(6, "\\(", "\\)");
const exprEndReg =
/(?<!(?:if|and|or|not|in|!=|==|<=|>=|<|>|\+|-|\*|\/|=|\+=|-=|\*=|\/=)\s*)(?=[\[\{\n])|(?=[;\}\]\)\n]|$)/;
Expand Down Expand Up @@ -107,6 +111,14 @@ const primitiveTypes: textmate.PatternMatch = {
const IDENTIFIER = /(?<!\)|\]|\})\b[\p{XID_Start}_][\p{XID_Continue}_\-]*/u;
const MATH_IDENTIFIER = /\b[\p{XID_Start}_][\p{XID_Continue}_]+/u;

// const MATH_OPENING =
// /[\[\(\u{5b}\u{7b}\u{2308}\u{230a}\u{231c}\u{231e}\u{2772}\u{27e6}\u{27e8}\u{27ea}\u{27ec}\u{27ee}\u{2983}\u{2985}\u{2987}\u{2989}\u{298b}\u{298d}\u{298f}\u{2991}\u{2993}\u{2995}\u{2997}\u{29d8}\u{29da}\u{29fc}]/u;
// const MATH_CLOSING =
// /[\]\)\u{5d}\u{7d}\u{2309}\u{230b}\u{231d}\u{231f}\u{2773}\u{27e7}\u{27e9}\u{27eb}\u{27ed}\u{27ef}\u{2984}\u{2986}\u{2988}\u{298a}\u{298c}\u{298e}\u{2990}\u{2992}\u{2994}\u{2996}\u{2998}\u{29d9}\u{29db}\u{29fd}]/u;

const MATH_OPENING = /[\[\(\{⌈⌊⌜⌞❲⟦⟨⟪⟬⟮⦃⦅⦇⦉⦋⦍⦏⦑⦓⦕⦗⧘⧚⧼]/;
const MATH_CLOSING = /[\]\)\}⌉⌋⌝⌟❳⟧⟩⟫⟭⟯⦄⦆⦈⦊⦌⦎⦐⦒⦔⦖⦘⧙⧛⧽]/;

// todo: distinguish type and variable
const identifier: textmate.PatternMatch = {
match: IDENTIFIER,
Expand Down Expand Up @@ -154,6 +166,29 @@ const mathMoreBrace: textmate.PatternMatch = {
match: markupBrace.match,
};

const mathParen: textmate.Pattern = {
begin: MATH_OPENING,
end: replaceGroup(/({closing})|(?=\$)|$/, "{closing}", MATH_CLOSING),
beginCaptures: {
"0": {
name: "markup.content.brace.typst",
},
},
endCaptures: {
"0": {
name: "markup.content.brace.typst",
},
},
patterns: [
{
include: "#mathParen",
},
{
include: "#math",
},
],
};

const stringLiteral: textmate.PatternBeginEnd = {
name: "string.quoted.double.typst",
begin: /"/,
Expand Down Expand Up @@ -204,7 +239,32 @@ const markupMath: textmate.Pattern = {

const experimentalMathRules: textmate.Pattern[] = [
{
match: /[_^'\/&√∛∜]/,
begin: replaceGroup(/([_^\/√∛∜])\s*({opening})/, "{opening}", MATH_OPENING),
end: replaceGroup(/({closing})|(?=\$)|$/, "{closing}", MATH_CLOSING),
beginCaptures: {
"1": {
name: "punctuation.math.operator.typst",
},
"2": {
name: "constant.other.symbol.typst",
},
},
endCaptures: {
"0": {
name: "constant.other.symbol.typst",
},
},
patterns: [
{
include: "#mathParen",
},
{
include: "#math",
},
],
},
{
match: /[_^'&\/√∛∜]/,
name: "punctuation.math.operator.typst",
},
{ include: "#strictMathFuncCallOrPropAccess" },
Expand Down Expand Up @@ -1528,6 +1588,7 @@ export const typst: textmate.Grammar = {
markupHeading,
markupBrace,
mathBrace,
mathParen,
mathMoreBrace,

...expressions().repository,
Expand Down
1 change: 0 additions & 1 deletion syntaxes/textmate/tests/unit/bugs/tinymist-issue194.typ

This file was deleted.

12 changes: 0 additions & 12 deletions syntaxes/textmate/tests/unit/bugs/tinymist-issue194.typ.snap

This file was deleted.

2 changes: 0 additions & 2 deletions syntaxes/textmate/tests/unit/markup/math.typ

This file was deleted.

5 changes: 5 additions & 0 deletions syntaxes/textmate/tests/unit/math/bracket-issue194.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

// issue 194
$ x^"#" = "argmin"_(A x=b) ||x||_(cal(l)_2) $

$ x^"#" = "argmin"_(A x=b()) ||x||_(cal(l)_2) $
30 changes: 30 additions & 0 deletions syntaxes/textmate/tests/unit/math/bracket-issue194.typ.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
>
>// issue 194
#^^ source.typst comment.line.double-slash.typst punctuation.definition.comment.typst
# ^^^^^^^^^^ source.typst comment.line.double-slash.typst
>$ x^"#" = "argmin"_(A x=b) ||x||_(cal(l)_2) $
#^ source.typst markup.math.typst punctuation.definition.string.math.typst
# ^^^ source.typst markup.math.typst
# ^ source.typst markup.math.typst string.quoted.double.typst punctuation.definition.string.typst
# ^ source.typst markup.math.typst string.quoted.double.typst
# ^ source.typst markup.math.typst string.quoted.double.typst punctuation.definition.string.typst
# ^^^ source.typst markup.math.typst
# ^ source.typst markup.math.typst string.quoted.double.typst punctuation.definition.string.typst
# ^^^^^^ source.typst markup.math.typst string.quoted.double.typst
# ^ source.typst markup.math.typst string.quoted.double.typst punctuation.definition.string.typst
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ source.typst markup.math.typst
# ^ source.typst markup.math.typst punctuation.definition.string.math.typst
>
>$ x^"#" = "argmin"_(A x=b()) ||x||_(cal(l)_2) $
#^ source.typst markup.math.typst punctuation.definition.string.math.typst
# ^^^ source.typst markup.math.typst
# ^ source.typst markup.math.typst string.quoted.double.typst punctuation.definition.string.typst
# ^ source.typst markup.math.typst string.quoted.double.typst
# ^ source.typst markup.math.typst string.quoted.double.typst punctuation.definition.string.typst
# ^^^ source.typst markup.math.typst
# ^ source.typst markup.math.typst string.quoted.double.typst punctuation.definition.string.typst
# ^^^^^^ source.typst markup.math.typst string.quoted.double.typst
# ^ source.typst markup.math.typst string.quoted.double.typst punctuation.definition.string.typst
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ source.typst markup.math.typst
# ^ source.typst markup.math.typst punctuation.definition.string.math.typst
>
14 changes: 14 additions & 0 deletions syntaxes/textmate/tests/unit/math/bracket.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

$ ||x||_[] $

$ ||x||_[) $

$ ||x||_⧼) $

$ 1 / () $

$ √() $

$ ∛() $

$ ∛∜() $
36 changes: 36 additions & 0 deletions syntaxes/textmate/tests/unit/math/bracket.typ.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
>
>$ ||x||_[] $
#^ source.typst markup.math.typst punctuation.definition.string.math.typst
# ^^^^^^^^^^ source.typst markup.math.typst
# ^ source.typst markup.math.typst punctuation.definition.string.math.typst
>
>$ ||x||_[) $
#^ source.typst markup.math.typst punctuation.definition.string.math.typst
# ^^^^^^^^^^ source.typst markup.math.typst
# ^ source.typst markup.math.typst punctuation.definition.string.math.typst
>
>$ ||x||_⧼) $
#^ source.typst markup.math.typst punctuation.definition.string.math.typst
# ^^^^^^^^^^ source.typst markup.math.typst
# ^ source.typst markup.math.typst punctuation.definition.string.math.typst
>
>$ 1 / () $
#^ source.typst markup.math.typst punctuation.definition.string.math.typst
# ^^^^^^^^ source.typst markup.math.typst
# ^ source.typst markup.math.typst punctuation.definition.string.math.typst
>
>$ √() $
#^ source.typst markup.math.typst punctuation.definition.string.math.typst
# ^^^^^ source.typst markup.math.typst
# ^ source.typst markup.math.typst punctuation.definition.string.math.typst
>
>$ ∛() $
#^ source.typst markup.math.typst punctuation.definition.string.math.typst
# ^^^^^ source.typst markup.math.typst
# ^ source.typst markup.math.typst punctuation.definition.string.math.typst
>
>$ ∛∜() $
#^ source.typst markup.math.typst punctuation.definition.string.math.typst
# ^^^^^^ source.typst markup.math.typst
# ^ source.typst markup.math.typst punctuation.definition.string.math.typst
>
11 changes: 11 additions & 0 deletions syntaxes/textmate/tests/unit/math/bracket_unmatch.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
$ ||x||_(cal(l)_2() $

$ ||x||_) $

$ ||x||_[ $

$ ||x||_[ $

$ ||x||_⧼ $

$ ||x||_) $
30 changes: 30 additions & 0 deletions syntaxes/textmate/tests/unit/math/bracket_unmatch.typ.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
>$ ||x||_(cal(l)_2() $
#^ source.typst markup.math.typst punctuation.definition.string.math.typst
# ^^^^^^^^^^^^^^^^^^^ source.typst markup.math.typst
# ^ source.typst markup.math.typst punctuation.definition.string.math.typst
>
>$ ||x||_) $
#^ source.typst markup.math.typst punctuation.definition.string.math.typst
# ^^^^^^^^^ source.typst markup.math.typst
# ^ source.typst markup.math.typst punctuation.definition.string.math.typst
>
>$ ||x||_[ $
#^ source.typst markup.math.typst punctuation.definition.string.math.typst
# ^^^^^^^^^ source.typst markup.math.typst
# ^ source.typst markup.math.typst punctuation.definition.string.math.typst
>
>$ ||x||_[ $
#^ source.typst markup.math.typst punctuation.definition.string.math.typst
# ^^^^^^^^^ source.typst markup.math.typst
# ^ source.typst markup.math.typst punctuation.definition.string.math.typst
>
>$ ||x||_$
#^ source.typst markup.math.typst punctuation.definition.string.math.typst
# ^^^^^^^^^ source.typst markup.math.typst
# ^ source.typst markup.math.typst punctuation.definition.string.math.typst
>
>$ ||x||_) $
#^ source.typst markup.math.typst punctuation.definition.string.math.typst
# ^^^^^^^^^ source.typst markup.math.typst
# ^ source.typst markup.math.typst punctuation.definition.string.math.typst
>
1 change: 1 addition & 0 deletions syntaxes/textmate/tests/unit/math/dollar.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$\$$
5 changes: 5 additions & 0 deletions syntaxes/textmate/tests/unit/math/dollar.typ.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
>$\$$
#^ source.typst markup.math.typst punctuation.definition.string.math.typst
# ^^ source.typst markup.math.typst constant.character.escape.content.typst
# ^ source.typst markup.math.typst punctuation.definition.string.math.typst
>
1 change: 1 addition & 0 deletions syntaxes/textmate/tests/unit/math/dollar_in_dollar.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ a #[$b$] $
12 changes: 12 additions & 0 deletions syntaxes/textmate/tests/unit/math/dollar_in_dollar.typ.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
>$ a #[$b$] $
#^ source.typst markup.math.typst punctuation.definition.string.math.typst
# ^^^ source.typst markup.math.typst
# ^ source.typst markup.math.typst keyword.control.hash.typst
# ^ source.typst markup.math.typst meta.brace.square.typst
# ^ source.typst markup.math.typst markup.math.typst punctuation.definition.string.math.typst
# ^ source.typst markup.math.typst markup.math.typst
# ^ source.typst markup.math.typst markup.math.typst punctuation.definition.string.math.typst
# ^ source.typst markup.math.typst meta.brace.square.typst
# ^ source.typst markup.math.typst
# ^ source.typst markup.math.typst punctuation.definition.string.math.typst
>

0 comments on commit 626efa0

Please sign in to comment.