Skip to content

Commit 24fa330

Browse files
committed
fix(syntaxes): Adjust block syntax highlighting to require ( or { on same line
While our parser allows for a block name to be defined on a separate line from the expression or brace, the syntax highlighting will not. Because the extension may be used for older versions of Angular and we cannot turn highlighting on and off dynamically like we can the parser, we make the highlighting more restrictive. This should cover a majority of use-cases where `@` is used in older versions of Angular like in emails and _not_ begin block highlighting. Partially addresses #1958. Will need another change to detect Angular version and disable block parsing.
1 parent 33764db commit 24fa330

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

syntaxes/src/template-blocks.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ export const TemplateBlocks: GrammarDefinition = {
2222
},
2323

2424
block: {
25-
begin: /(@)((?:\w+\s*)+)/,
25+
// @ followed by words and spaces. Also require a { or ( on the same line but don't capture it.
26+
// The parser doesn't require this but we do for syntax highlighting to be more exclusive since
27+
// the extension can be used for older versions of Angular.
28+
begin: /(@)((?:\w+\s*)+)(?=\(|\{)/,
2629
beginCaptures: {
2730
1: {
2831
patterns: [

syntaxes/template-blocks.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"name": "keyword.control.block.transition.ng"
1313
},
1414
"block": {
15-
"begin": "(@)((?:\\w+\\s*)+)",
15+
"begin": "(@)((?:\\w+\\s*)+)(?=\\(|\\{)",
1616
"beginCaptures": {
1717
"1": {
1818
"patterns": [

syntaxes/test/data/template-blocks.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
goodbye
2727
}
2828

29-
@for (let item of items; as thing; track: $index) {
29+
@for (let item of items; track $index) {
3030
bla
3131
}
3232

@@ -36,3 +36,12 @@
3636
) {
3737
bla
3838
}
39+
40+
<!-- Should not highlight -->
41+
@if
42+
(items) {}
43+
44+
45+
46+
@for
47+
(let item of items; track $index) { }

syntaxes/test/data/template-blocks.html.snap

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@
108108
>}
109109
#^ template.blocks.ng control.block.ng punctuation.definition.block.ts
110110
>
111-
>@for (let item of items; as thing; track: $index) {
111+
>@for (let item of items; track $index) {
112112
#^ template.blocks.ng keyword.control.block.transition.ng
113113
# ^^^^ template.blocks.ng keyword.control.block.kind.ng
114114
# ^ template.blocks.ng control.block.ng meta.brace.round.ts
115-
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ template.blocks.ng control.block.ng control.block.expression.ng
116-
# ^ template.blocks.ng control.block.ng meta.brace.round.ts
117-
# ^ template.blocks.ng control.block.ng
118-
# ^ template.blocks.ng control.block.ng punctuation.definition.block.ts
115+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ template.blocks.ng control.block.ng control.block.expression.ng
116+
# ^ template.blocks.ng control.block.ng meta.brace.round.ts
117+
# ^ template.blocks.ng control.block.ng
118+
# ^ template.blocks.ng control.block.ng punctuation.definition.block.ts
119119
> bla
120120
#^^^^^^^^ template.blocks.ng control.block.ng control.block.body.ng
121121
>}
@@ -137,4 +137,19 @@
137137
#^^^^^^^^ template.blocks.ng control.block.ng control.block.body.ng
138138
>}
139139
#^ template.blocks.ng control.block.ng punctuation.definition.block.ts
140+
>
141+
><!-- Should not highlight -->
142+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ template.blocks.ng
143+
>@if
144+
#^^^^^ template.blocks.ng
145+
>(items) {}
146+
#^^^^^^^^^^^ template.blocks.ng
147+
>
148+
>some.email@google.com ({}) {}
149+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ template.blocks.ng
150+
>
151+
>@for
152+
#^^^^^^ template.blocks.ng
153+
>(let item of items; track $index) { }
154+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ template.blocks.ng
140155
>

0 commit comments

Comments
 (0)