Skip to content

Commit

Permalink
dev: simplify if statement handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Oct 22, 2024
1 parent 7a54bc1 commit 4c164c7
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 93 deletions.
99 changes: 10 additions & 89 deletions syntaxes/textmate/main.mts
Original file line number Diff line number Diff line change
Expand Up @@ -918,124 +918,45 @@ const letStatement = (): textmate.Grammar => {
};
};

// todo: #if [] == [] [] {}
/**
* Matches a (strict grammar) if in markup context.
*/
const ifStatement = (): textmate.Grammar => {
const ifStatement: textmate.Pattern = {
name: "meta.expr.if.typst",
begin: lookAhead(/(else\s+)?(if\b(?!-))/),
end: /(?<=\}|\])(?!\s*else\b(?!-))|(?=[;\}\]\)\n]|$)/,
end: /(?<=\}|\])(?!\s*(else)\b(?!-))|(?<=else)(?!\s*(?:if\b(?!-)|[\[\{]))|(?=[;\}\]\)\n]|$)/,
patterns: [
/// Matches any comments
{
include: "#comments",
},
// todo
/// Matches if clause with a code block expression
/// Matches if clause
{
include: "#ifClause",
},
/// Matches else clause
{
include: "#elseClause",
},
/// Matches else content clause
{
include: "#elseContentClause",
},
/// Matches a code block after the if clause
{
include: "#codeBlock",
},
/// Matches a content block after the if clause
{
include: "#contentBlock",
},
{ include: "#comments" },
{ include: "#ifClause" },
{ include: "#elseClause" },
{ include: "#codeBlock" },
{ include: "#contentBlock" },
],
};

const ifClause: textmate.Pattern = {
// name: "meta.if.clause.typst",
begin: /(?:(\belse)\s+)?(\bif)\s+/,
begin: /(\bif)\s+/,
end: exprEndReg,
beginCaptures: {
"1": {
name: "keyword.control.conditional.typst",
},
"2": {
name: "keyword.control.conditional.typst",
},
},
patterns: [
{
include: "#comments",
},
{
include: "#expression",
},
],
patterns: [{ include: "#expression" }],
};

const elseClause: textmate.Pattern = {
// name: "meta.else.clause.typst",
begin: /(\belse)\s*(\{)/,
end: /\}/,
beginCaptures: {
"1": {
name: "keyword.control.conditional.typst",
},
"2": {
name: "meta.brace.curly.typst",
},
},
endCaptures: {
"0": {
name: "meta.brace.curly.typst",
},
},
patterns: [
{
include: "#code",
},
],
};

const elseContentClause: textmate.Pattern = {
// name: "meta.else.clause.typst",
begin: /(\belse)\s*(\[)/,
end: /\]/,
beginCaptures: {
"1": {
name: "keyword.control.conditional.typst",
},
"2": {
name: "meta.brace.square.typst",
},
},
endCaptures: {
"0": {
name: "meta.brace.square.typst",
},
},
patterns: [
{
include: "#code",
},
{
include: "#markupBrace",
},
],
match: /\belse\b(?!-)/,
name: "keyword.control.conditional.typst",
};

return {
repository: {
ifStatement,
ifClause,
elseClause,
elseContentClause,
},
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@
1
# if () [] else []
1

A#if false {} else thing


Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,19 @@
# ^^^^^^^^^^^^^^^^^ source.typst
>1
#^^ source.typst
>
>A#if false {} else thing
#^ source.typst
# ^ source.typst keyword.control.hash.typst
# ^^ source.typst meta.expr.if.typst keyword.control.conditional.typst
# ^ source.typst meta.expr.if.typst
# ^^^^^ source.typst meta.expr.if.typst entity.name.type.primitive.typst
# ^ source.typst meta.expr.if.typst
# ^ source.typst meta.expr.if.typst meta.brace.curly.typst
# ^ source.typst meta.expr.if.typst meta.brace.curly.typst
# ^ source.typst meta.expr.if.typst
# ^^^^ source.typst meta.expr.if.typst keyword.control.conditional.typst
# ^^^^^^^ source.typst
>
>
>
5 changes: 2 additions & 3 deletions syntaxes/textmate/tests/unit/basic/control-flow-if.typ.snap
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@
# ^^^^ source.typst meta.expr.if.typst keyword.control.conditional.typst
# ^ source.typst meta.expr.if.typst
# ^ source.typst meta.expr.if.typst meta.brace.square.typst
# ^^^^ source.typst meta.expr.if.typst variable.other.readwrite.typst
# ^^^^ source.typst meta.expr.if.typst
# ^ source.typst meta.expr.if.typst meta.brace.square.typst
> if content == type [Nope] else [ve.]
#^^ source.typst
Expand All @@ -350,8 +350,7 @@
# ^^^^ source.typst meta.expr.if.typst keyword.control.conditional.typst
# ^ source.typst meta.expr.if.typst
# ^ source.typst meta.expr.if.typst meta.brace.square.typst
# ^^ source.typst meta.expr.if.typst variable.other.readwrite.typst
# ^ source.typst meta.expr.if.typst keyword.operator.accessor.typst
# ^^^ source.typst meta.expr.if.typst
# ^ source.typst meta.expr.if.typst meta.brace.square.typst
>}
#^ source.typst meta.brace.curly.typst
Expand Down
3 changes: 2 additions & 1 deletion syntaxes/textmate/tests/unit/editing/if4.typ.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
# ^ source.typst meta.expr.if.typst
# ^ source.typst meta.expr.if.typst meta.brace.curly.typst
# ^ source.typst meta.expr.if.typst meta.brace.curly.typst
# ^^^^^ source.typst meta.expr.if.typst
# ^ source.typst meta.expr.if.typst
# ^^^^ source.typst meta.expr.if.typst keyword.control.conditional.typst
# ^ source.typst meta.brace.curly.typst
>
>test
Expand Down

0 comments on commit 4c164c7

Please sign in to comment.