Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax and language configuration fixes and improvements #286

Merged
merged 13 commits into from
May 20, 2022

Conversation

Princesseuh
Copy link
Member

@Princesseuh Princesseuh commented May 16, 2022

Changes

This fixes a few issues with our syntax file and our language configuration file and also adds some missing features:

  • Added support for is:raw on script tags, style tags, components and other elements
  • Added support for JSON script tags using application/ld+json (I think this makes us one of the rare ones to support this, I thought it was neat and fairly easy so, why not)
  • Added support for script tags with unknown types (treated as unknown content)
  • Added template literals as surrounding pair for autoclose support
  • Added support for auto closing {/* */}
  • Added support for automatically indenting certain patterns (HTML, brackets etc)
  • Added better support for identifying words in the file (the frontmatter dashes are no longer included, yay!)
  • Fixed expressions brackets not using JSX's coloring
  • Fixed leaving the frontmatter into the template making an indent

Fix #7
Fix #112 however, auto indent is super hard to get right and very flaky. Even in HTML and TSX files it doesn't always works very nicely. I ended up mostly copying the TSX's auto indent rules with a few tweaks from the HTML ones
Fix #274 - It doesn't really fix it but making this PR made me realize that it's not a really possible thing to do the way I thought we could do it when I wrote that issue and that things worked differently than I thought, so it's no longer relevant

Half of #285

I tried to do #271, but I couldn't figure it out. Including the html:comment pattern inside expressions worked in some cases but didn't in others and I don't really get why. I then tried to inject HTML comments inside the JSX grammar and ended up with nothing more than an headache. If someone has a solution, I'll gladly take it ha!

Testing

Added a test suite powered by vscode-tmgrammar-test with instructions on how to use and when to update the snapshots

Docs

No docs needed?

@changeset-bot
Copy link

changeset-bot bot commented May 16, 2022

🦋 Changeset detected

Latest commit: 1154e1e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
astro-vscode Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -3,7 +3,6 @@
"blockComment": ["<!--", "-->"]
},
"brackets": [
["---", "---"],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the frontmatter bracket because it causes an indent bump into the template (since the entry and the exit of the bracket are the same characters). If we want to provide an indent into the frontmatter, we should use entry rules

@Princesseuh Princesseuh self-assigned this May 17, 2022
@Princesseuh Princesseuh changed the title [WIP] Syntax and language configuration fixes [WIP] Syntax and language configuration fixes and improvements May 18, 2022
@@ -5,8 +5,7 @@
"brackets": [
["<!--", "-->"],
["<", ">"],
["{", "}"],
["(", ")"]
Copy link
Member Author

@Princesseuh Princesseuh May 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no cases where you want parenthesizes to count as brackets in the source.astro scope, in contexts where they do matter (ex: source.ts, source.js, source.tsx) the languages used there will add them as brackets

@Princesseuh Princesseuh marked this pull request as ready for review May 20, 2022 14:49
@Princesseuh Princesseuh requested a review from a team as a code owner May 20, 2022 14:49
@Princesseuh Princesseuh changed the title [WIP] Syntax and language configuration fixes and improvements Syntax and language configuration fixes and improvements May 20, 2022
Copy link
Member

@natemoo-re natemoo-re left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fantastic! None of these comments are blocking, just curious to hear your thoughts.

Comment on lines +17 to +18
{ "open": "{/*", "close": " */}", "notIn": ["comment", "string"] },
{ "open": "<!--", "close": " -->", "notIn": ["comment", "string"] },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intentional that the close has a leading space?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! For comments it makes for a nicer experience since people usually add a space at the end. This is actually a consistency fix, we already had the space for JS comments but not HTML ones

Comment on lines +64 to 67
"indentationRules": {
"increaseIndentPattern": "<(?!\\?|(?:area|base|br|col|frame|hr|html|img|input|keygen|link|menuitem|meta|param|source|track|wbr)\\b|[^>]*\\/>)([-_\\.A-Za-z0-9]+)(?=\\s|>)\\b[^>]*>(?!.*<\\/\\1>)|<!--(?!.*-->)|\\{[^}\"']*$",
"decreaseIndentPattern": "^\\s*(<\\/(?!html)[-_\\.A-Za-z0-9]+\\b[^>]*>|-->|\\})"
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, this will be great! Nice job ☺️

},
{
"name": "meta.tag.component.astro",
"begin": "(</?)([$A-Z_][^/?!\\s<>]*|[^/?!\\s<>.]+\\.[^/?!\\s<>]+)\\b",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks unchanged, but just want to confirm that namespace.component is properly highlighted as a component?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, what gives it the special highlighting is actually the support.class.component.astro scope. Since it makes it highlighted like a class (in TSX files, it's support.class.component.tsx)

Comment on lines +174 to +176
"comment": "Treat script tag with application/ld+json as JSON. This needs to be higher than is:raw since it's a very possible situation to have is:raw on a JSON script tag",
"begin": "\\G(?=\\s*[^>]*?type\\s*=\\s*(['\"]|)(?i:application/ld\\+json)\\1)",
"end": "(?=</|/>)",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love to see it!

]
},
{
"comment": "Treat script tag with is:raw as plain text",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious about the highlighting here... I don't think is:raw has any effect on script or style (braces are already ignored), so do we really want highlighting to change to plaintext? I think everything will still be parsed and bundled.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I might have misunderstood what is:raw does on those tags then!

I thought it would act like a more intense is:inline basically. Especially for script tags, I felt like having a weird type + is:raw could be a "common" case

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decided to remove is:raw as plaintext for style tags and script tags after confirming that it doesn't do anything in Astro

}
}

snapShotTest();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests look awesome!

@Princesseuh Princesseuh merged commit fec2817 into main May 20, 2022
@Princesseuh Princesseuh deleted the syntax-fixes branch May 20, 2022 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants