Skip to content

Commit

Permalink
[fix]: Separate rules for result keyword with and without <>
Browse files Browse the repository at this point in the history
fix bytecodealliance#35

This commit separates the syntax highlighting rules for `result` types with angle brackets (`result<...>`) and `result` types without type parameters.

Previously, both types were handled in a single rule, `meta.result.ty.wit`, which ended at a newline (technically "after"), comma, or closing bracket.
However, if a `result` type appeared as a parameter, the scope wouldn't end until one of these conditions was met.
(As observed, the scope didn’t end even when a newline was encountered, likely because it was captured by other rules, though...).

This change simplifies the `meta.result.ty.wit` scope by splitting the handling of `result<...>` and `result`.
Now, `result<...>` is handled similarly to `list<...>` and `tuple<...>`, while `result` without `<...>` is handled by `result-base`, which marks the `result` keyword as `entity.name.type.result.wit`.

As a consequence of this change, line comments on `result<xxx> // comment` will no longer be ignored. However, this is a known issue for other types as well.
  • Loading branch information
tanishiking committed Feb 21, 2025
1 parent e4c1ca6 commit 556290d
Show file tree
Hide file tree
Showing 15 changed files with 577 additions and 499 deletions.
63 changes: 26 additions & 37 deletions syntaxes/wit.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,9 @@
{
"include": "#container"
},
{
"include": "#result-base"
},
{
"include": "#identifier"
}
Expand Down Expand Up @@ -1032,6 +1035,11 @@
}
]
},
"result-base": {
"name": "entity.name.type.result.wit",
"comment": "Syntax for result types where both the `ok` and `err` types are `_`. This pattern must be placed after the #container pattern to correctly capture `result<...>`.",
"match": "\\b(result)\\b"
},
"tuple": {
"name": "meta.tuple.ty.wit",
"comment": "Syntax for WIT types such as tuple",
Expand Down Expand Up @@ -1135,7 +1143,7 @@
"result": {
"name": "meta.result.ty.wit",
"comment": "Syntax for WIT types such as result",
"begin": "\\b(result)\\b",
"begin": "\\b(result)\\b(\\<)",
"beginCaptures": {
"1": {
"name": "entity.name.type.result.wit"
Expand All @@ -1149,47 +1157,28 @@
"include": "#comment"
},
{
"name": "meta.inner.result.wit",
"begin": "(\\<)",
"beginCaptures": {
"1": {
"name": "punctuation.brackets.angle.begin.wit"
}
},
"patterns": [
{
"include": "#comment"
},
{
"name": "variable.other.inferred-type.result.wit",
"match": "(?<!\\w)(\\_)(?!\\w)"
},
{
"name": "meta.types.result.wit",
"include": "#types"
},
{
"name": "punctuation.comma.wit",
"match": "(?<!result)\\s*(\\,)"
},
{
"include": "#whitespace"
}
],
"end": "(\\>)",
"applyEndPatternLast": 1,
"endCaptures": {
"1": {
"name": "punctuation.brackets.angle.end.wit"
}
}
"name": "variable.other.inferred-type.result.wit",
"match": "(?<!\\w)(\\_)(?!\\w)"
},
{
"name": "meta.types.list.wit",
"include": "#types"
},
{
"name": "punctuation.comma.wit",
"match": "(?<!result)\\s*(\\,)"
},
{
"include": "#whitespace"
}
],
"end": "((?<=\\n)|(?=\\,)|(?=\\}))",
"applyEndPatternLast": 1
"end": "(\\>)",
"applyEndPatternLast": 1,
"endCaptures": {
"1": {
"name": "punctuation.brackets.angle.end.wit"
}
}
},
"handle": {
"name": "meta.handle.ty.wit",
Expand Down
18 changes: 9 additions & 9 deletions tests/grammar/integration/import-func.wit.snap
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@
# ^^ source.wit meta.world-item.wit meta.import-item.wit meta.function.wit meta.result-list.wit keyword.operator.arrow.skinny.wit
# ^ source.wit meta.world-item.wit meta.import-item.wit meta.function.wit meta.result-list.wit meta.whitespace.wit
# ^^^^^^ source.wit meta.world-item.wit meta.import-item.wit meta.function.wit meta.result-list.wit meta.result.ty.wit entity.name.type.result.wit
# ^ source.wit meta.world-item.wit meta.import-item.wit meta.function.wit meta.result-list.wit meta.result.ty.wit meta.inner.result.wit punctuation.brackets.angle.begin.wit
# ^^^^^^ source.wit meta.world-item.wit meta.import-item.wit meta.function.wit meta.result-list.wit meta.result.ty.wit meta.inner.result.wit meta.option.ty.wit entity.name.type.option.wit
# ^ source.wit meta.world-item.wit meta.import-item.wit meta.function.wit meta.result-list.wit meta.result.ty.wit meta.inner.result.wit meta.option.ty.wit punctuation.brackets.angle.begin.wit
# ^^^ source.wit meta.world-item.wit meta.import-item.wit meta.function.wit meta.result-list.wit meta.result.ty.wit meta.inner.result.wit meta.option.ty.wit entity.name.type.numeric.wit
# ^ source.wit meta.world-item.wit meta.import-item.wit meta.function.wit meta.result-list.wit meta.result.ty.wit meta.inner.result.wit meta.option.ty.wit punctuation.brackets.angle.end.wit
# ^ source.wit meta.world-item.wit meta.import-item.wit meta.function.wit meta.result-list.wit meta.result.ty.wit meta.inner.result.wit punctuation.comma.wit
# ^ source.wit meta.world-item.wit meta.import-item.wit meta.function.wit meta.result-list.wit meta.result.ty.wit meta.inner.result.wit meta.whitespace.wit
# ^^^^^^ source.wit meta.world-item.wit meta.import-item.wit meta.function.wit meta.result-list.wit meta.result.ty.wit meta.inner.result.wit entity.name.type.string.wit
# ^ source.wit meta.world-item.wit meta.import-item.wit meta.function.wit meta.result-list.wit meta.result.ty.wit meta.inner.result.wit punctuation.brackets.angle.end.wit
# ^ source.wit meta.world-item.wit meta.import-item.wit meta.function.wit meta.result-list.wit meta.result.ty.wit punctuation.brackets.angle.begin.wit
# ^^^^^^ source.wit meta.world-item.wit meta.import-item.wit meta.function.wit meta.result-list.wit meta.result.ty.wit meta.option.ty.wit entity.name.type.option.wit
# ^ source.wit meta.world-item.wit meta.import-item.wit meta.function.wit meta.result-list.wit meta.result.ty.wit meta.option.ty.wit punctuation.brackets.angle.begin.wit
# ^^^ source.wit meta.world-item.wit meta.import-item.wit meta.function.wit meta.result-list.wit meta.result.ty.wit meta.option.ty.wit entity.name.type.numeric.wit
# ^ source.wit meta.world-item.wit meta.import-item.wit meta.function.wit meta.result-list.wit meta.result.ty.wit meta.option.ty.wit punctuation.brackets.angle.end.wit
# ^ source.wit meta.world-item.wit meta.import-item.wit meta.function.wit meta.result-list.wit meta.result.ty.wit punctuation.comma.wit
# ^ source.wit meta.world-item.wit meta.import-item.wit meta.function.wit meta.result-list.wit meta.result.ty.wit meta.whitespace.wit
# ^^^^^^ source.wit meta.world-item.wit meta.import-item.wit meta.function.wit meta.result-list.wit meta.result.ty.wit entity.name.type.string.wit
# ^ source.wit meta.world-item.wit meta.import-item.wit meta.function.wit meta.result-list.wit meta.result.ty.wit punctuation.brackets.angle.end.wit
>}
#^ source.wit meta.world-item.wit punctuation.brackets.curly.end.wit
>
Loading

0 comments on commit 556290d

Please sign in to comment.