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

Semantic Highlighting vs Injected Grammars #113640

Closed
mitsuhiko opened this issue Dec 31, 2020 · 6 comments
Closed

Semantic Highlighting vs Injected Grammars #113640

mitsuhiko opened this issue Dec 31, 2020 · 6 comments
Labels
feature-request Request for new features or functionality semantic-tokens Semantic tokens issues

Comments

@mitsuhiko
Copy link
Contributor

  • VSCode Version: 1.52.1
  • OS Version: Darwin x64 19.6.0

This is a meta bug report as I'm not sure if this feature is intentional or indeed a bug. Currently if one uses injectTo to "inject" a sub language into an already existing grammar this stops working when semantic highlighting is provided by a language server.

Configuration wise my extension does something like this:

    "grammars": [
      {
        "language": "insta-snapshots",
        "scopeName": "source.insta-snapshots",
        "path": "./syntaxes/insta-snapshots.tmLanguage.json"
      },
      {
        "scopeName": "source.inline-insta-snapshots",
        "injectTo": [
          "source.rust"
        ],
        "path": "./syntaxes/inline-insta-snapshots.tmLanguage.json",
        "embeddedLanguages": {
          "meta.embedded.inline-insta-snapshot": "insta-snapshots"
        }
      }
    ]

Then in the grammar I'm matching on something. This all works and eventually I end up injecting a custom sub syntax into strings that look like @r"""...""". The issue here now is that once the rust language server runs a "string" semantic token is put over the entire region which I just parsed which completely removes my custom syntax again.

The following screenshot shows the issue:

image

The token under the cursor is correctly determined to be keyword.insta but the styling in the theme is discarded because of the string semantic token which takes precedence.

Currently there does not appear to be a way to resolve this except for turning off semantic highlighting.

@vscodebot
Copy link

vscodebot bot commented Dec 31, 2020

(Experimental duplicate detection)
Thanks for submitting this issue. Please also check if it is already covered by an existing one, like:

@alexdima
Copy link
Member

alexdima commented Jan 4, 2021

This is intentional. TM tokens can only be an approximation of the parsed language/AST and cannot benefit from semantic analysis. That is why we have made the design decision that semantic tokens always overwrite TM tokens. The overwrite works like a mask, so if a range is not covered by a semantic token, the TM token is used instead.

For this particular case, I think you could try to coordinate with the rust extension / rust semantic tokens provider implementation to give you an option or an API that would prevent the semantic tokens provider from covering those strings. Also, if the semantic tokens provider just creates string tokens, (which would be equal with the tokens created by TM), then they could simply stop creating them altogether without any visible effects.

@alexdima alexdima added the info-needed Issue requires more information from poster label Jan 4, 2021
@mitsuhiko
Copy link
Contributor Author

The issue I see with this is that injecting sub languages into strings is a very common thing. That applies to regular expressions in many languages, template engines in strings, embedded html and much more.

This right now means the moment semantic highlighting exists, all these use cases do not have a supported solution within vscode and require custom extension points in the relevant RLS.

@alexdima alexdima added feature-request Request for new features or functionality semantic-tokens Semantic tokens issues and removed info-needed Issue requires more information from poster labels Jan 11, 2021
@alexdima alexdima removed their assignment Feb 3, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Apr 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request Request for new features or functionality semantic-tokens Semantic tokens issues
Projects
None yet
Development

No branches or pull requests

3 participants
@mitsuhiko @alexdima and others