diff --git a/.changeset/large-ears-ask.md b/.changeset/large-ears-ask.md new file mode 100644 index 000000000000..d60878ca7066 --- /dev/null +++ b/.changeset/large-ears-ask.md @@ -0,0 +1,5 @@ +--- +'astro-vscode': patch +--- + +Fixes syntax highlighting sometimes not working when the frontmatter or script tags ended with certain TypeScript constructs diff --git a/.vscode/launch.json b/.vscode/launch.json index b575d4b70649..7aa3443bf9e4 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -7,16 +7,16 @@ "type": "extensionHost", "request": "launch", "runtimeExecutable": "${execPath}", - "args": ["--extensionDevelopmentPath=${workspaceRoot}/tools/vscode"], - "outFiles": ["${workspaceRoot}/tools/vscode/dist/**/*.js"] + "args": ["--extensionDevelopmentPath=${workspaceRoot}/packages/language-tools/vscode"], + "outFiles": ["${workspaceRoot}/*/*/dist/**/*.js"] }, { + "name": "Attach to Server", "type": "node", "request": "attach", - "name": "Attach to Server", - "port": 6040, + "port": 6009, "restart": true, - "outFiles": ["${workspaceRoot}/tools/languageserver/dist/**/*.js"], + "outFiles": ["${workspaceRoot}/*/*/dist/**/*.js"], "skipFiles": ["/**"] } ], @@ -26,4 +26,4 @@ "configurations": ["Launch Client", "Attach to Server"] } ] -} +} \ No newline at end of file diff --git a/packages/language-tools/vscode/syntaxes/astro.tmLanguage.json b/packages/language-tools/vscode/syntaxes/astro.tmLanguage.json index 558c04d22fb7..d82f722e0096 100644 --- a/packages/language-tools/vscode/syntaxes/astro.tmLanguage.json +++ b/packages/language-tools/vscode/syntaxes/astro.tmLanguage.json @@ -1,7 +1,9 @@ { "name": "Astro", "scopeName": "source.astro", - "fileTypes": ["astro"], + "fileTypes": [ + "astro" + ], "injections": { "L:(meta.script.astro) (meta.lang.json) - (meta source)": { "patterns": [ @@ -20,9 +22,20 @@ }, "L:(meta.script.astro) (meta.lang.js | meta.lang.javascript | meta.lang.partytown | meta.lang.node) - (meta source)": { "patterns": [ + { + "begin": "(?<=>)(?=[^\\n]+])", + "end": "(?=])", + "name": "meta.embedded.block.astro", + "contentName": "source.js", + "patterns": [ + { + "include": "source.js" + } + ] + }, { "begin": "(?<=>)(?!])", "name": "meta.embedded.block.astro", "contentName": "source.js", "patterns": [ @@ -35,9 +48,20 @@ }, "L:(meta.script.astro) (meta.lang.ts | meta.lang.typescript) - (meta source)": { "patterns": [ + { + "begin": "(?<=>)(?=[^\\n]+])", + "end": "(?=])", + "name": "meta.embedded.block.astro", + "contentName": "source.ts", + "patterns": [ + { + "include": "source.ts" + } + ] + }, { "begin": "(?<=>)(?!])", "name": "meta.embedded.block.astro", "contentName": "source.ts", "patterns": [ @@ -50,9 +74,20 @@ }, "L:meta.script.astro - meta.lang - (meta source)": { "patterns": [ + { + "begin": "(?<=>)(?=[^\\n]+])", + "end": "(?=])", + "name": "meta.embedded.block.astro", + "contentName": "source.js", + "patterns": [ + { + "include": "source.js" + } + ] + }, { "begin": "(?<=>)(?!])", "name": "meta.embedded.block.astro", "contentName": "source.js", "patterns": [ @@ -194,10 +229,28 @@ "name": "comment" } }, - "contentName": "source.ts", "patterns": [ { - "include": "source.ts" + "begin": "(?<=\\n)(?=[^\\n]+^-{3})", + "end": "(?=^-{3})", + "name": "meta.embedded.block.astro", + "contentName": "source.ts", + "patterns": [ + { + "include": "source.ts" + } + ] + }, + { + "begin": "(?<=\\n)", + "while": "^(?!\\s*-{3})", + "name": "meta.embedded.block.astro", + "contentName": "source.ts", + "patterns": [ + { + "include": "source.ts" + } + ] } ] }, @@ -841,4 +894,4 @@ ] } } -} +} \ No newline at end of file diff --git a/packages/language-tools/vscode/syntaxes/astro.tmLanguage.src.yaml b/packages/language-tools/vscode/syntaxes/astro.tmLanguage.src.yaml index 387e72624070..8ca69dd6138c 100644 --- a/packages/language-tools/vscode/syntaxes/astro.tmLanguage.src.yaml +++ b/packages/language-tools/vscode/syntaxes/astro.tmLanguage.src.yaml @@ -29,11 +29,17 @@ injections: # JavaScript | 'javascript' | 'source.js' | 'partytown' 'L:(meta.script.astro) (meta.lang.js | meta.lang.javascript | meta.lang.partytown | meta.lang.node) - (meta source)': - patterns: - [ + patterns: [ + { + begin: '(?<=>)(?=[^\n]+])', + end: '(?=])', + name: meta.embedded.block.astro, + contentName: source.js, + patterns: [{ include: source.js }], + }, { begin: '(?<=>)(?!])', name: meta.embedded.block.astro, contentName: source.js, patterns: [{ include: source.js }], @@ -42,11 +48,17 @@ injections: # TypeScript | 'ts' 'L:(meta.script.astro) (meta.lang.ts | meta.lang.typescript) - (meta source)': - patterns: - [ + patterns: [ + { + begin: '(?<=>)(?=[^\n]+])', + end: '(?=])', + name: meta.embedded.block.astro, + contentName: source.ts, + patterns: [{ include: source.ts }], + }, { begin: '(?<=>)(?!])', name: meta.embedded.block.astro, contentName: source.ts, patterns: [{ include: source.ts }], @@ -56,11 +68,17 @@ injections: # Script Languages # Default (JavaScript) 'L:meta.script.astro - meta.lang - (meta source)': - patterns: - [ + patterns: [ + { + begin: '(?<=>)(?=[^\n]+])', + end: '(?=])', + name: meta.embedded.block.astro, + contentName: source.js, + patterns: [{ include: source.js }], + }, { begin: '(?<=>)(?!])', name: meta.embedded.block.astro, contentName: source.js, patterns: [{ include: source.js }], @@ -180,9 +198,22 @@ repository: 1: { name: comment } endCaptures: 2: { name: comment } - contentName: source.ts - patterns: - - include: source.ts + patterns: [ + { + begin: '(?<=\n)(?=[^\n]+^-{3})', + end: '(?=^-{3})', + name: meta.embedded.block.astro, + contentName: source.ts, + patterns: [{ include: source.ts }], + }, + { + begin: '(?<=\n)', + while: '^(?!\s*-{3})', + name: meta.embedded.block.astro, + contentName: source.ts, + patterns: [{ include: source.ts }], + }, + ] # ------- # SCOPE diff --git a/packages/language-tools/vscode/test/grammar/fixtures/text.astro.snap b/packages/language-tools/vscode/test/grammar/fixtures/text.astro.snap index bb904ba90cb3..7f137596491c 100644 --- a/packages/language-tools/vscode/test/grammar/fixtures/text.astro.snap +++ b/packages/language-tools/vscode/test/grammar/fixtures/text.astro.snap @@ -1,11 +1,11 @@ >--- #^^^ source.astro comment >const cool = "our frontmatter" -#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ source.astro source.ts +#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ source.astro meta.embedded.block.astro source.ts >console.log("Hello, World!") -#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ source.astro source.ts +#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ source.astro meta.embedded.block.astro source.ts >console.log(`This is ${cool}!`) -#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ source.astro source.ts +#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ source.astro meta.embedded.block.astro source.ts >--- #^^^ source.astro comment > diff --git a/packages/language-tools/vscode/test/grammar/fixtures/types-ending.astro b/packages/language-tools/vscode/test/grammar/fixtures/types-ending.astro new file mode 100644 index 000000000000..df2c0eadc56a --- /dev/null +++ b/packages/language-tools/vscode/test/grammar/fixtures/types-ending.astro @@ -0,0 +1,9 @@ +--- +type Something = "Hello" +--- + + + +
\ No newline at end of file diff --git a/packages/language-tools/vscode/test/grammar/fixtures/types-ending.astro.snap b/packages/language-tools/vscode/test/grammar/fixtures/types-ending.astro.snap new file mode 100644 index 000000000000..4ad4ff967c51 --- /dev/null +++ b/packages/language-tools/vscode/test/grammar/fixtures/types-ending.astro.snap @@ -0,0 +1,25 @@ +>--- +#^^^ source.astro comment +>type Something = "Hello" +#^^^^^^^^^^^^^^^^^^^^^^^^^ source.astro meta.embedded.block.astro source.ts +>--- +#^^^ source.astro comment +> +> +#^^ source.astro meta.scope.tag.script.astro meta.script.astro meta.tag.end.astro punctuation.definition.tag.begin.astro +# ^^^^^^ source.astro meta.scope.tag.script.astro meta.script.astro meta.tag.end.astro entity.name.tag.astro +# ^ source.astro meta.scope.tag.script.astro meta.script.astro meta.tag.end.astro punctuation.definition.tag.end.astro +> +>
+#^ source.astro meta.scope.tag.div.astro meta.tag.start.astro punctuation.definition.tag.begin.astro +# ^^^ source.astro meta.scope.tag.div.astro meta.tag.start.astro entity.name.tag.astro +# ^ source.astro meta.scope.tag.div.astro meta.tag.start.astro punctuation.definition.tag.end.astro +# ^^ source.astro meta.scope.tag.div.astro meta.tag.end.astro punctuation.definition.tag.begin.astro +# ^^^ source.astro meta.scope.tag.div.astro meta.tag.end.astro entity.name.tag.astro +# ^ source.astro meta.scope.tag.div.astro meta.tag.end.astro punctuation.definition.tag.end.astro \ No newline at end of file