Skip to content

Commit

Permalink
dev: parse blocks in if/for/while more consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Oct 22, 2024
1 parent 4c164c7 commit 0ac36e7
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 57 deletions.
64 changes: 13 additions & 51 deletions syntaxes/textmate/main.mts
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ const ifStatement = (): textmate.Grammar => {
const ifStatement: textmate.Pattern = {
name: "meta.expr.if.typst",
begin: lookAhead(/(else\s+)?(if\b(?!-))/),
end: /(?<=\}|\])(?!\s*(else)\b(?!-))|(?<=else)(?!\s*(?:if\b(?!-)|[\[\{]))|(?=[;\}\]\)\n]|$)/,
end: /(?<=\}|\])(?!\s*(else)\b(?!-)|[\[\{])|(?<=else)(?!\s*(?:if\b(?!-)|[\[\{]))|(?=[;\}\]\)\n]|$)/,
patterns: [
{ include: "#comments" },
{ include: "#ifClause" },
Expand Down Expand Up @@ -966,24 +966,12 @@ const forStatement = (): textmate.Grammar => {
const forStatement: textmate.Pattern = {
name: "meta.expr.for.typst",
begin: lookAhead(/(for\b(?!-))\s*/),
end: /(?<=[\}\]])(?=\s*[\n\S;\}\]\)])(?!\s*[\{\[])|(?=[;\}\]\)\n]|$)/,
end: /(?<=[\}\]])(?![\{\[])|(?=[;\}\]\)\n]|$)/,
patterns: [
/// Matches any comments
{
include: "#comments",
},
/// Matches for clause
{
include: "#forClause",
},
/// Matches a code block after the for clause
{
include: "#codeBlock",
},
/// Matches a content block after the for clause
{
include: "#contentBlock",
},
{ include: "#comments" },
{ include: "#forClause" },
{ include: "#codeBlock" },
{ include: "#contentBlock" },
],
};

Expand All @@ -996,14 +984,7 @@ const forStatement = (): textmate.Grammar => {
name: "keyword.control.loop.typst",
},
},
patterns: [
{
include: "#comments",
},
{
include: "#expression",
},
],
patterns: [{ include: "#expression" }],
};

return {
Expand All @@ -1019,24 +1000,12 @@ const whileStatement = (): textmate.Grammar => {
const whileStatement: textmate.Pattern = {
name: "meta.expr.while.typst",
begin: lookAhead(/(while\b(?!-))/),
end: /(?<=\}|\])|(?=[;\}\]\)\n]|$)/,
end: /(?<=[\}\]])(?![\{\[])|(?=[;\}\]\)\n]|$)/,
patterns: [
/// Matches any comments
{
include: "#comments",
},
/// Matches while clause
{
include: "#whileClause",
},
/// Matches a code block after the while clause
{
include: "#codeBlock",
},
/// Matches a content block after the while clause
{
include: "#contentBlock",
},
{ include: "#comments" },
{ include: "#whileClause" },
{ include: "#codeBlock" },
{ include: "#contentBlock" },
],
};

Expand All @@ -1049,14 +1018,7 @@ const whileStatement = (): textmate.Grammar => {
name: "keyword.control.loop.typst",
},
},
patterns: [
{
include: "#comments",
},
{
include: "#expression",
},
],
patterns: [{ include: "#expression" }],
};

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,6 @@
# ^ source.typst meta.expr.for.typst
# ^ source.typst meta.expr.for.typst meta.brace.square.typst
# ^ source.typst meta.expr.for.typst meta.brace.square.typst
# ^ source.typst meta.expr.for.typst
# ^ source.typst meta.expr.for.typst meta.brace.square.typst
# ^ source.typst meta.expr.for.typst meta.brace.square.typst
# ^^^^ source.typst
>1
#^^ source.typst
4 changes: 1 addition & 3 deletions syntaxes/textmate/tests/unit/basic/control-flow-for.typ.snap
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,6 @@
# ^ source.typst meta.expr.for.typst
# ^ source.typst meta.expr.for.typst meta.brace.curly.typst
# ^ source.typst meta.expr.for.typst meta.brace.curly.typst
# ^ source.typst meta.expr.for.typst
# ^ source.typst meta.expr.for.typst meta.brace.curly.typst
# ^ source.typst meta.expr.for.typst meta.brace.curly.typst
# ^^^^ source.typst
>1
#^^ source.typst
16 changes: 16 additions & 0 deletions syntaxes/textmate/tests/unit/basic/space-control-flow.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#let A = []
#for i in range(0, 10) { A } { A }
#for i in { range(0, 10) } { A } { A }
#if { true } { A } { A }

#for i in range(0, 10){A}{A}
#for i in { range(0, 10) }{A}{A}
#if{true}{A}{A}

#for i in range(0, 10) { A }A
#for i in { range(0, 10) } { A }A
#if { true } { A }A

#for i in range(0, 10){A}A
#for i in { range(0, 10) }{A}A
#if{true}{A}A
Loading

0 comments on commit 0ac36e7

Please sign in to comment.