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

Add Razor C# semantic tokens support in VS Code #6489

Merged

Conversation

allisonchou
Copy link
Contributor

Transitions C# in Razor to use semantic tokens instead of Textmate.

Fixes dotnet/razor#9336, dotnet/razor#4323, and dotnet/razor#4332

Examples of scenarios this fixes:

  • Before:
    image

  • After:
    image

  • Before:
    image

  • After:
    image

@allisonchou allisonchou requested review from a team as code owners October 5, 2023 00:28
@@ -5175,6 +5175,168 @@
"markupCommentPunctuation": [
"punctuation.definition.comment.html",
"comment.block.html"
],
"keyword": [
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is needed to override the default colors VS Code uses since they can differ compared to what C# uses. For example, keyword is colored purple by VS Code, but colored blue by C#.

Thanks @dibarbet for the tip!

Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting, so "keyword.cs" is already blue for VS Code and it just... knows?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm guessing C# sets the color somewhere, although I'm not sure where. Maybe @dibarbet or @JoeRobich would know?

Copy link
Member

Choose a reason for hiding this comment

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

I don't quite remember - it was potentially that the lsp token type for keyword mapped to keyword.control but keyword.cs falls back to keyword instead

Copy link
Member

Choose a reason for hiding this comment

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

@@ -5369,7 +5531,10 @@
{
"language": "aspnetcorerazor",
"scopeName": "text.aspnetcorerazor",
"path": "./src/razor/syntaxes/aspnetcorerazor.tmLanguage.json"
"path": "./src/razor/syntaxes/aspnetcorerazor.tmLanguage.json",
"unbalancedBracketScopes": [
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Needed to address this scenario:
image

Otherwise, VS Code will think there is an unclosed bracket and color it red.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if this is the only one - you might want to test less than / greather than as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah you're right, and I was missing bit shifting operators as well. Thanks!

src/razor/src/semantic/provideSemanticTokensResponse.ts Outdated Show resolved Hide resolved
src/razor/language-configuration.json Outdated Show resolved Hide resolved
src/razor/src/semantic/semanticTokensRangeHandler.ts Outdated Show resolved Hide resolved
src/razor/src/semantic/semanticTokensRangeHandler.ts Outdated Show resolved Hide resolved
SerializableSemanticTokensParams,
vscode.SemanticTokens,
any
>('roslyn/semanticTokenRanges');
Copy link
Contributor

Choose a reason for hiding this comment

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

This is the new endpoint for when the feature flag is on, so we probably don't want to use it. We should be able to use the standard textDocument/semanticTokens/range method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's fair. It is kind of weird since we're still being sent an array of ranges from the server, so I had to hardcode in ranges[0] to use the standard method.

Also, since we're going back to the standard method, I tested out the built-in vscode.provideDocumentRangeSemanticTokens command and was actually able to get it working this time around. 😄 Definitely simplified the code

Copy link
Contributor

@davidwengier davidwengier left a comment

Choose a reason for hiding this comment

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

Nice work!

@allisonchou allisonchou merged commit c118328 into dotnet:main Oct 5, 2023
8 checks passed
@allisonchou allisonchou deleted the dev/allichou/AddRazorSemanticTokens branch October 5, 2023 04:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[VS Code] Incorrect C# TextMate grammar
4 participants