fix(language-tools): improve multiline style embedded language detection#16165
fix(language-tools): improve multiline style embedded language detection#16165Misrilal-Sah wants to merge 2 commits intowithastro:mainfrom
Conversation
|
There was a problem hiding this comment.
Pull request overview
Updates the Astro TextMate grammar to improve embedded language injection for <style> blocks (e.g., lang="sass") and reduce cases where styles fall back to incorrect CSS highlighting.
Changes:
- Adds a special-case pattern for “open + content +
</style>on the same line” style blocks for multiple style languages. - Replaces
end: (?=</)with awhile:guard intended to keep the embedded grammar active until</style>.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/language-tools/vscode/syntaxes/astro.tmLanguage.src.yaml | Updates injected style-language patterns (stylus/sass/css/scss/less/postcss/default) to avoid early termination on </. |
| packages/language-tools/vscode/syntaxes/astro.tmLanguage.json | Applies the same injection-grammar updates in the generated JSON form. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| { | ||
| begin: '(?<=>)(?!</)', | ||
| end: '(?=</)', | ||
| while: '^(?!\s*</style[\s>])', | ||
| name: meta.embedded.block.astro, | ||
| contentName: source.stylus, | ||
| patterns: [{ include: source.stylus }], |
There was a problem hiding this comment.
Using begin + while anchored to ^ will only terminate the embedded block when </style> starts a line (optionally preceded by whitespace). This regresses cases where the closing tag appears after content on the same line (e.g., body { ... }</style>), because the while check won’t fail on that line, so the embedded scope can incorrectly consume </style> and potentially run to EOF. A more robust approach is to use end: '(?=</style[\\s>])' (instead of while) so termination works regardless of where the closing tag appears on the line; this also makes the new single-line special-case pattern unnecessary.
| { | ||
| "begin": "(?<=>)(?!</)", | ||
| "end": "(?=</)", | ||
| "while": "^(?!\\s*</style[\\s>])", | ||
| "name": "meta.embedded.block.astro", | ||
| "contentName": "source.stylus", | ||
| "patterns": [ |
There was a problem hiding this comment.
Same issue as the YAML source: while: '^(?!\\s*</style[\\s>])' only detects a closing </style> at the start of a line, so lines like ... }</style> won’t terminate the embedded region correctly. Consider switching back to a proper end condition that targets only the style close tag (e.g., (?=</style[\\s>])) so the embedded scope ends correctly even when </style> is not at BOL, and then you can drop the extra single-line pattern.
|
@Misrilal-Sah can you add a changeset? |
ematipico
left a comment
There was a problem hiding this comment.
- changeset missing
- restore PR template
- use magic comments to auto close the issue
|
There's been a lot of previous tries at this and none of them worked. I'd love to see a test added to make sure it actually does in fact work. |
|
Thanks for working on this. I took some time to test the current approach locally on top of With the current patch, I also tried a different approach locally by updating Given Princesseuh’s note about previous attempts, I think adding dedicated grammar fixtures would probably make this much easier to validate with confidence:
What do you think about moving the fix closer to |
|
I don't have any good ideas on how to solve this <style>css
css
css<style> html <!-- comment
still comement -->and you definitely cannot just anchor to the start of a line |
Issue ID: #14657
Description: Fixes syntax-highlighting language injection when lang=sass is not on the same line as style start, preventing CSS fallback mis-highlighting.