Skip to content

Commit e062101

Browse files
authored
fix(vscode): Correctly handle TypeScript blocks ending with types (#15109)
* fix(vscode): Correctly handle TypeScript blocks ending with types * chore: changeset
1 parent a012a86 commit e062101

7 files changed

Lines changed: 151 additions & 28 deletions

File tree

.changeset/large-ears-ask.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro-vscode': patch
3+
---
4+
5+
Fixes syntax highlighting sometimes not working when the frontmatter or script tags ended with certain TypeScript constructs

.vscode/launch.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
"type": "extensionHost",
88
"request": "launch",
99
"runtimeExecutable": "${execPath}",
10-
"args": ["--extensionDevelopmentPath=${workspaceRoot}/tools/vscode"],
11-
"outFiles": ["${workspaceRoot}/tools/vscode/dist/**/*.js"]
10+
"args": ["--extensionDevelopmentPath=${workspaceRoot}/packages/language-tools/vscode"],
11+
"outFiles": ["${workspaceRoot}/*/*/dist/**/*.js"]
1212
},
1313
{
14+
"name": "Attach to Server",
1415
"type": "node",
1516
"request": "attach",
16-
"name": "Attach to Server",
17-
"port": 6040,
17+
"port": 6009,
1818
"restart": true,
19-
"outFiles": ["${workspaceRoot}/tools/languageserver/dist/**/*.js"],
19+
"outFiles": ["${workspaceRoot}/*/*/dist/**/*.js"],
2020
"skipFiles": ["<node_internals>/**"]
2121
}
2222
],
@@ -26,4 +26,4 @@
2626
"configurations": ["Launch Client", "Attach to Server"]
2727
}
2828
]
29-
}
29+
}

packages/language-tools/vscode/syntaxes/astro.tmLanguage.json

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"name": "Astro",
33
"scopeName": "source.astro",
4-
"fileTypes": ["astro"],
4+
"fileTypes": [
5+
"astro"
6+
],
57
"injections": {
68
"L:(meta.script.astro) (meta.lang.json) - (meta source)": {
79
"patterns": [
@@ -20,9 +22,20 @@
2022
},
2123
"L:(meta.script.astro) (meta.lang.js | meta.lang.javascript | meta.lang.partytown | meta.lang.node) - (meta source)": {
2224
"patterns": [
25+
{
26+
"begin": "(?<=>)(?=[^\\n]+</script[\\s>])",
27+
"end": "(?=</script[\\s>])",
28+
"name": "meta.embedded.block.astro",
29+
"contentName": "source.js",
30+
"patterns": [
31+
{
32+
"include": "source.js"
33+
}
34+
]
35+
},
2336
{
2437
"begin": "(?<=>)(?!</)",
25-
"end": "(?=</)",
38+
"while": "^(?!\\s*</script[\\s>])",
2639
"name": "meta.embedded.block.astro",
2740
"contentName": "source.js",
2841
"patterns": [
@@ -35,9 +48,20 @@
3548
},
3649
"L:(meta.script.astro) (meta.lang.ts | meta.lang.typescript) - (meta source)": {
3750
"patterns": [
51+
{
52+
"begin": "(?<=>)(?=[^\\n]+</script[\\s>])",
53+
"end": "(?=</script[\\s>])",
54+
"name": "meta.embedded.block.astro",
55+
"contentName": "source.ts",
56+
"patterns": [
57+
{
58+
"include": "source.ts"
59+
}
60+
]
61+
},
3862
{
3963
"begin": "(?<=>)(?!</)",
40-
"end": "(?=</)",
64+
"while": "^(?!\\s*</script[\\s>])",
4165
"name": "meta.embedded.block.astro",
4266
"contentName": "source.ts",
4367
"patterns": [
@@ -50,9 +74,20 @@
5074
},
5175
"L:meta.script.astro - meta.lang - (meta source)": {
5276
"patterns": [
77+
{
78+
"begin": "(?<=>)(?=[^\\n]+</script[\\s>])",
79+
"end": "(?=</script[\\s>])",
80+
"name": "meta.embedded.block.astro",
81+
"contentName": "source.js",
82+
"patterns": [
83+
{
84+
"include": "source.js"
85+
}
86+
]
87+
},
5388
{
5489
"begin": "(?<=>)(?!</)",
55-
"end": "(?=</)",
90+
"while": "^(?!\\s*</script[\\s>])",
5691
"name": "meta.embedded.block.astro",
5792
"contentName": "source.js",
5893
"patterns": [
@@ -194,10 +229,28 @@
194229
"name": "comment"
195230
}
196231
},
197-
"contentName": "source.ts",
198232
"patterns": [
199233
{
200-
"include": "source.ts"
234+
"begin": "(?<=\\n)(?=[^\\n]+^-{3})",
235+
"end": "(?=^-{3})",
236+
"name": "meta.embedded.block.astro",
237+
"contentName": "source.ts",
238+
"patterns": [
239+
{
240+
"include": "source.ts"
241+
}
242+
]
243+
},
244+
{
245+
"begin": "(?<=\\n)",
246+
"while": "^(?!\\s*-{3})",
247+
"name": "meta.embedded.block.astro",
248+
"contentName": "source.ts",
249+
"patterns": [
250+
{
251+
"include": "source.ts"
252+
}
253+
]
201254
}
202255
]
203256
},
@@ -841,4 +894,4 @@
841894
]
842895
}
843896
}
844-
}
897+
}

packages/language-tools/vscode/syntaxes/astro.tmLanguage.src.yaml

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,17 @@ injections:
2929

3030
# JavaScript | 'javascript' | 'source.js' | 'partytown'
3131
'L:(meta.script.astro) (meta.lang.js | meta.lang.javascript | meta.lang.partytown | meta.lang.node) - (meta source)':
32-
patterns:
33-
[
32+
patterns: [
33+
{
34+
begin: '(?<=>)(?=[^\n]+</script[\s>])',
35+
end: '(?=</script[\s>])',
36+
name: meta.embedded.block.astro,
37+
contentName: source.js,
38+
patterns: [{ include: source.js }],
39+
},
3440
{
3541
begin: '(?<=>)(?!</)',
36-
end: '(?=</)',
42+
while: '^(?!\s*</script[\s>])',
3743
name: meta.embedded.block.astro,
3844
contentName: source.js,
3945
patterns: [{ include: source.js }],
@@ -42,11 +48,17 @@ injections:
4248

4349
# TypeScript | 'ts'
4450
'L:(meta.script.astro) (meta.lang.ts | meta.lang.typescript) - (meta source)':
45-
patterns:
46-
[
51+
patterns: [
52+
{
53+
begin: '(?<=>)(?=[^\n]+</script[\s>])',
54+
end: '(?=</script[\s>])',
55+
name: meta.embedded.block.astro,
56+
contentName: source.ts,
57+
patterns: [{ include: source.ts }],
58+
},
4759
{
4860
begin: '(?<=>)(?!</)',
49-
end: '(?=</)',
61+
while: '^(?!\s*</script[\s>])',
5062
name: meta.embedded.block.astro,
5163
contentName: source.ts,
5264
patterns: [{ include: source.ts }],
@@ -56,11 +68,17 @@ injections:
5668
# Script Languages
5769
# Default (JavaScript)
5870
'L:meta.script.astro - meta.lang - (meta source)':
59-
patterns:
60-
[
71+
patterns: [
72+
{
73+
begin: '(?<=>)(?=[^\n]+</script[\s>])',
74+
end: '(?=</script[\s>])',
75+
name: meta.embedded.block.astro,
76+
contentName: source.js,
77+
patterns: [{ include: source.js }],
78+
},
6179
{
6280
begin: '(?<=>)(?!</)',
63-
end: '(?=</)',
81+
while: '^(?!\s*</script[\s>])',
6482
name: meta.embedded.block.astro,
6583
contentName: source.js,
6684
patterns: [{ include: source.js }],
@@ -180,9 +198,22 @@ repository:
180198
1: { name: comment }
181199
endCaptures:
182200
2: { name: comment }
183-
contentName: source.ts
184-
patterns:
185-
- include: source.ts
201+
patterns: [
202+
{
203+
begin: '(?<=\n)(?=[^\n]+^-{3})',
204+
end: '(?=^-{3})',
205+
name: meta.embedded.block.astro,
206+
contentName: source.ts,
207+
patterns: [{ include: source.ts }],
208+
},
209+
{
210+
begin: '(?<=\n)',
211+
while: '^(?!\s*-{3})',
212+
name: meta.embedded.block.astro,
213+
contentName: source.ts,
214+
patterns: [{ include: source.ts }],
215+
},
216+
]
186217

187218
# -------
188219
# SCOPE

packages/language-tools/vscode/test/grammar/fixtures/text.astro.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
>---
22
#^^^ source.astro comment
33
>const cool = "our frontmatter"
4-
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ source.astro source.ts
4+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ source.astro meta.embedded.block.astro source.ts
55
>console.log("Hello, World!")
6-
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ source.astro source.ts
6+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ source.astro meta.embedded.block.astro source.ts
77
>console.log(`This is ${cool}!`)
8-
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ source.astro source.ts
8+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ source.astro meta.embedded.block.astro source.ts
99
>---
1010
#^^^ source.astro comment
1111
>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
type Something = "Hello"
3+
---
4+
5+
<script>
6+
type Something = "Hello"
7+
</script>
8+
9+
<div></div>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
>---
2+
#^^^ source.astro comment
3+
>type Something = "Hello"
4+
#^^^^^^^^^^^^^^^^^^^^^^^^^ source.astro meta.embedded.block.astro source.ts
5+
>---
6+
#^^^ source.astro comment
7+
>
8+
><script>
9+
#^ source.astro meta.scope.tag.script.astro meta.script.astro meta.tag.start.astro punctuation.definition.tag.begin.astro
10+
# ^^^^^^ source.astro meta.scope.tag.script.astro meta.script.astro meta.tag.start.astro entity.name.tag.astro
11+
# ^ source.astro meta.scope.tag.script.astro meta.script.astro meta.tag.start.astro punctuation.definition.tag.end.astro
12+
>type Something = "Hello"
13+
#^^^^^^^^^^^^^^^^^^^^^^^^^ source.astro meta.scope.tag.script.astro meta.script.astro meta.embedded.block.astro source.js
14+
></script>
15+
#^^ source.astro meta.scope.tag.script.astro meta.script.astro meta.tag.end.astro punctuation.definition.tag.begin.astro
16+
# ^^^^^^ source.astro meta.scope.tag.script.astro meta.script.astro meta.tag.end.astro entity.name.tag.astro
17+
# ^ source.astro meta.scope.tag.script.astro meta.script.astro meta.tag.end.astro punctuation.definition.tag.end.astro
18+
>
19+
><div></div>
20+
#^ source.astro meta.scope.tag.div.astro meta.tag.start.astro punctuation.definition.tag.begin.astro
21+
# ^^^ source.astro meta.scope.tag.div.astro meta.tag.start.astro entity.name.tag.astro
22+
# ^ source.astro meta.scope.tag.div.astro meta.tag.start.astro punctuation.definition.tag.end.astro
23+
# ^^ source.astro meta.scope.tag.div.astro meta.tag.end.astro punctuation.definition.tag.begin.astro
24+
# ^^^ source.astro meta.scope.tag.div.astro meta.tag.end.astro entity.name.tag.astro
25+
# ^ source.astro meta.scope.tag.div.astro meta.tag.end.astro punctuation.definition.tag.end.astro

0 commit comments

Comments
 (0)