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

Support custom semantic tokens & modifiers #958

Merged
merged 2 commits into from
Mar 24, 2022
Merged

Conversation

radeksimko
Copy link
Member

@radeksimko radeksimko commented Mar 3, 2022

The feature is effectively gated on the server capability, so client and server part can be released independently. Our extension would just continue using the fallback token types and that one modifier until/unless the server claims support of the custom ones.

Related PRs:

Follow-up issues:


Possible UX

I may not have picked the best colour combinations, but the point I'm trying to make here is not the colours, but the ability to assign colours to the relevant constructs in a meaningful way.

{
  "name": "Example Theme",
  "semanticHighlighting": true,
  "semanticTokenColors": {
    "hcl-blockType.terraform-resource": {"fontStyle": "bold", "foreground": "#fff"},
    "hcl-blockType.terraform-data": {"fontStyle": "bold", "foreground": "#fff"},
    "hcl-blockType.terraform-module": {"fontStyle": "bold", "foreground": "#3d5aff"},
    "hcl-blockLabel.terraform-module.terraform-name": {"foreground": "#bac4ff"},
    "hcl-blockLabel.terraform-type": {"foreground": "#844FBA", "fontStyle": "bold"},
    "hcl-blockLabel.terraform-resource.terraform-name": {"foreground": "#a589b3"}
  }
}

Screenshot 2022-03-17 at 11 50 55

Background

This enables the wider ecosystem of themes by providing custom token types & modifiers for highlighting in a way, which:

  1. doesn't hamper UX with default themes
  2. allows custom themes to be more specific
  3. doesn't hamper other language clients which may not have the same themes as VSCode
  4. allows us to make changes in both server and client independently

Language server is mainly responsible for associating all the custom token types and modifiers to the relevant pieces of code, assuming that the client communicates via client capabilities that it supports these custom types & modifiers. This allows the server to still provide sensible semantic highlighting to clients which do not support these custom types & modifiers.

We could use these types & modifiers in our static grammar too, but I have not explored that possibility too deeply.

  • One clear benefit would be that themes would be applied immediately (before LS starts)
  • The grammar may not be as accurate as semantic highlighting
  • Use cases involving both custom themes and environment without language server are likely to be very few, if any
  • There is maintenance cost associated with this

so given the above I'm leaning more towards keeping this mostly driven by the language server, at least for now.

The token types and modifiers are prefixed because apparently this is a global namespace and we don't want to end up in a conflict with any other extension.
microsoft/vscode#103097

@radeksimko radeksimko added enhancement New feature or request syntax labels Mar 3, 2022
@radeksimko radeksimko changed the title WIP - custom semantic tokens Support custom semantic tokens & modifiers Mar 3, 2022
@radeksimko radeksimko self-assigned this Mar 3, 2022
@radeksimko radeksimko added this to the 2.21.0 milestone Mar 3, 2022
@radeksimko radeksimko force-pushed the f-custom-semantic-tokens branch 2 times, most recently from 95ca56c to 63e81e5 Compare March 4, 2022 17:17
@hashicorp-cla
Copy link

hashicorp-cla commented Mar 12, 2022

CLA assistant check
All committers have signed the CLA.

Comment on lines +109 to +123
"scopes": {
"hcl-attrName": ["variable.other.property"],
"hcl-blockType": ["entity.name.type"],
"hcl-blockLabel": ["variable.other.enummember"],

"hcl-bool": ["keyword.control"],
"hcl-string": ["string"],
"hcl-number": ["constant.numeric"],
"hcl-objectKey": ["variable.parameter"],
"hcl-mapKey": ["variable.parameter"],
"hcl-keyword": ["keyword.control"],
"hcl-traversalStep": ["variable.other.readwrite"],
"hcl-typeCapsule": ["keyword.control"],
"hcl-typePrimitive": ["keyword.control"]
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

@radeksimko radeksimko Mar 21, 2022

Choose a reason for hiding this comment

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

As per conversation in microsoft/vscode#145312 we could make these more specific, e.g. by appending .hcl to all scopes, but I'm not sure what is the best long-term strategy for token/scope "parity", so I just mostly mimicked the existing behaviours for semantic tokens for now.

@radeksimko radeksimko marked this pull request as ready for review March 17, 2022 17:38
@radeksimko radeksimko requested a review from a team March 17, 2022 17:38
@radeksimko radeksimko modified the milestones: 2.21.0, v2.22.0 Mar 21, 2022
Copy link
Member

@dbanck dbanck left a comment

Choose a reason for hiding this comment

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

LGTM and works as expected; so much room for possibilities now!

Just a minor style suggestion, but feel free to ignore and merge anyway.

src/extension.ts Outdated Show resolved Hide resolved
@radeksimko radeksimko merged commit 036593c into main Mar 24, 2022
@radeksimko radeksimko deleted the f-custom-semantic-tokens branch March 24, 2022 18:53
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request syntax
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants