Skip to content

Make LSPSemanticTokensHighlightVisitor More Extensible and Compatible with Custom Parsers #1040

Closed

Description

@d-biehl

First, thank you for your great work!!!

And now:

I have written my own parser for my RobotCode plugin based on LSP4IJ. Actually, I extended the TextMate parser from IntelliJ to adapt it to the requirements of the Robot Framework language and to provide it with its own settings page for coloring and so on, but also to use the VSCode tmLanguage file.

Robot Framework is not a programming language in the traditional sense but rather a documentation language for business test cases. Therefore, many of the predefined token types like Keyword/Class/Method in the predefined TextMate scopes as well as the predefined types of the LSP Semantic Tokens don't really fit. This is no big thing in VSCode, because you can define your editor.tokenColorCustomizations in your vscode package, but in the Intellij platform this is not really possible and you have to define your TextAttributes by hand in code.

Most of the work is handled by my extended TextMate parser, and then the semantic tokens just add a few extensions, such as embedded arguments in keywords or specific scopes like built-ins, test templates or i18n things etc.

Robot Framework/TextMate coloring works, but my problem now is that the semantic token highlights are not displayed in PyCharm since LSP4IJ Version 0.12 or maybe 0.11, with 0.10 it is working.

The reason why they are not displayed is quite simple: in

, it is checked whether the element is a LeafElement. This may make sense in some cases, but in my case, it is not appropriate because there are no LeafElements in the TextMate parser. However, I am not sure if it is really necessary, as no methods of LeafElement are used in the visit method.

So, I thought, okay, then I'll write my own HighlightVisitor for Robot Framework by deriving it from LSPSemanticTokensHighlightVisitor in LSP4IJ and overriding the relevant methods.

That doesn't work either, as the internals like holder and lazyInfos, as well as the highlightSemanticTokens method, are private.

Then I thought I would write my own HighlightVisitor here and reimplement the logic in Kotlin, which I find quite valid. This works, but my plugin no longer passes the Plugin Approval process because I use internal APIs since the LSPFileSupport class in LSP4IJ is marked as Internal.

Now, my questions are:

  • Does LSPFileSupport really need to be internal?
  • Could LSPSemanticTokensHighlightVisitor be modified to not check for LeafElement?
  • Or could LSPSemanticTokensHighlightVisitor be implemented in such a way that it is easier to derive from and perhaps also provides access to the internals, e.g., by making them protected?
  • Maybe a version of LSPSemanticTokensHighlightVisitor that just highlights without checking whether the file is of a certain type or whether the element is derived from LeafElement could be provided. This could then be used for custom implementations.
  • What other options are there to make this work?

Hope that is understandable, and thanks in advance for a response! ;-)

Activity

  1. angelozerr commented on May 13, 2025

    @angelozerr
    Contributor

    First, thank you for your great work!!!

    Thanks so much for your great feedback. I have set a lot of code in internal because I do a lot of refactoring, so it is not possible for instance to provide LSPFileSupport as non internal.

    To customize LSP behaviour in LSP4IJ iq to use LSP-API.

    For instance to customize LSP semantic features, see https://github.com/redhat-developer/lsp4ij/blob/main/docs/LSPApi.md#lsp-semanticTokens-feature to add for instance your own TextAttribute key.

    TextAttributesKey getTextAttributesKey(@NotNull String tokenType, List tokenModifiers, PsiFile file)

    And now:

    I have written my own parser for my RobotCode plugin based on LSP4IJ. Actually, I extended the TextMate parser from IntelliJ to adapt it to the requirements of the Robot Framework language and to provide it with its own settings page for coloring and so on, but also to use the VSCode tmLanguage file.

    Robot Framework is not a programming language in the traditional sense but rather a documentation language for business test cases. Therefore, many of the predefined token types like Keyword/Class/Method in the predefined TextMate scopes as well as the predefined types of the LSP Semantic Tokens don't really fit. This is no big thing in VSCode, because you can define your editor.tokenColorCustomizations in your vscode package, but in the Intellij platform this is not really possible and you have to define your TextAttributes by hand in code.

    You can do that with

    LSPSemanticTokensFeature#TextAttributesKey getTextAttributesKey(@NotNull String tokenType, List tokenModifiers, PsiFile file)

    right?

    Most of the work is handled by my extended TextMate parser, and then the semantic tokens just add a few extensions, such as embedded arguments in keywords or specific scopes like built-ins, test templates or i18n things etc.

    Robot Framework/TextMate coloring works, but my problem now is that the semantic token highlights are not displayed in PyCharm since LSP4IJ Version 0.12 or maybe 0.11, with 0.10 it is working.

    The reason why they are not displayed is quite simple: in

    lsp4ij/src/main/java/com/redhat/devtools/lsp4ij/features/semanticTokens/LSPSemanticTokensHighlightVisitor.java

    Line 89 in 481bf4f

    if (lazyInfos == null || !(element instanceof LeafElement))
    , it is checked whether the element is a LeafElement. This may make sense in some cases, but in my case, it is not appropriate because there are no LeafElements in the TextMate parser. However, I am not sure if it is really necessary, as no methods of LeafElement are used in the visit method.
    So, I thought, okay, then I'll write my own HighlightVisitor for Robot Framework by deriving it from LSPSemanticTokensHighlightVisitor in LSP4IJ and overriding the relevant methods.

    No please do not that because

    public boolean suitableForFile(@NotNull PsiFile file) {
            return LanguageServersRegistry.getInstance().isFileSupported(file);
        }
    

    will return true and you will have 2 LSP semantic highlight visitor which will be processed

    That doesn't work either, as the internals like holder and lazyInfos, as well as the highlightSemanticTokens method, are private.

    Then I thought I would write my own HighlightVisitor here and reimplement the logic in Kotlin, which I find quite valid.

    If I understand you:

    • consume action.run() before highlight
    • do nothing in the visit method

    Is only that?

    This works, but my plugin no longer passes the Plugin Approval process because I use internal APIs since the LSPFileSupport class in LSP4IJ is marked as Internal.

    I don't want to provide access to LSPFileSupport because I spend my time to do some refactoring.

    Now, my questions are:

    • Does LSPFileSupport really need to be internal?

    Yes, because it the core of LSP feature which change every time.

    • Could LSPSemanticTokensHighlightVisitor be modified to not check for LeafElement?

    Yes we could do that with a new methode API like LSPSemanticFeatures#canVisit(PsiElement element, boolean hasLazyInfos) for instance.

    But what about the order of action.run()?

    Just one question you PsiFile is not a TextMate PsiFile?

    • Or could LSPSemanticTokensHighlightVisitor be implemented in such a way that it is easier to derive from and perhaps also provides access to the internals, e.g., by making them protected?

    No because you will have several LSP semantic tokens which will be processed.

    • Maybe a version of LSPSemanticTokensHighlightVisitor that just highlights without checking whether the file is of a certain type or whether the element is derived from LeafElement could be provided. This could then be used for custom implementations.

    LSPSemanticFeatures#canVisit(PsiElement element, boolean hasLazyInfos) is the best idea I think.

    • What other options are there to make this work?

    Hope that is understandable, and thanks in advance for a response! ;-)

  2. added a commit that references this issue on May 14, 2025
    b59ca27
  3. added this to the 0.13.0 milestone on May 14, 2025
  4. moved this to Pending review in Java Toolingon May 14, 2025
  5. self-assigned this
    on May 14, 2025
  6. added 5 commits that reference this issue on May 14, 2025
    58d6ccd
    1e7d329
    b0e193a
    ef9f95d
    c58c387
  7. added a commit that references this issue on May 14, 2025
    b75091f
  8. moved this from Pending review to Done in Java Toolingon May 14, 2025
  9. angelozerr commented on May 14, 2025

    @angelozerr
    Contributor
  10. angelozerr commented on May 14, 2025

    @angelozerr
    Contributor

    @d-biehl please install LSP4IJ 0.13.0 which should be available on marketplace.

  11. d-biehl commented on May 14, 2025

    @d-biehl
    ContributorAuthor

    I started writing a longer response last night, but since it was already so late, I decided to get some sleep and continue today... And what do I find?
    Yeah: You got me, and that's exactly what I needed!!! That saves me a lot of work and unnecessary code. Thanks!!!

  12. angelozerr commented on May 14, 2025

    @angelozerr
    Contributor

    I started writing a longer response last night, but since it was already so late, I decided to get some sleep and continue today... And what do I find? Yeah: You got me, and that's exactly what I needed!!! That saves me a lot of work and unnecessary code. Thanks!!!

    That's super great that LSP API can help you. Thanks for your feedback.

    If you like LSP4IJ don't hesitate to add a review in JetBrains marketplace to help us to promote LSP4IJ.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementsemantic tokens

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions