Skip to content

Commit

Permalink
fix: Semantic highlighting rendered the JSON file
Browse files Browse the repository at this point in the history
Fixes redhat-developer#397

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Jul 2, 2024
1 parent 8df13c1 commit ed0554b
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 33 deletions.
2 changes: 1 addition & 1 deletion docs/DeveloperGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ to open `references/implementations` in a popup when clicking on a `Codelens` :
# Semantic tokens colors provider
Before you start reading this section, please read the [User Guide](UserGuide.md#semantic-tokens-support) to enable support for semantic tokens.
Before you start reading this section, please read the [User Guide](UserGuide.md#semantic-tokens-support) to configure support for semantic tokens.
When the language server supports semantic tokens, the decoded token type and token modifiers must be translated to an IntelliJ `TextAttributeKeys`.
using the [SemanticTokensColorsProvider](https://github.com/redhat-developer/lsp4ij/blob/main/src/main/java/com/redhat/devtools/lsp4ij/features/semanticTokens/SemanticTokensColorsProvider.java) API:
Expand Down
6 changes: 3 additions & 3 deletions docs/LSPSupport.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,14 +458,14 @@ You can change the notification behavior of `LSP/window/showMessageRequest` by u

#### Semantic Tokens

Before you start reading this section, please read the [User Guide](UserGuide.md#semantic-tokens-support) to enable support for semantic tokens.
Before you start reading this section, please read the [User Guide](UserGuide.md#semantic-tokens-support) to configure support for semantic tokens.

The semantic tokens support:

* uses only [textDocument/semanticTokens/full](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#semanticTokens_fullRequest) LSP request.
* uses the [IntelliJ Semantic Highlighting support](https://plugins.jetbrains.com/docs/intellij/syntax-highlighting-and-error-highlighting.html#semantic-highlighting)
by implementing [RainbowVisitor](https://github.com/JetBrains/intellij-community/blob/master/platform/analysis-impl/src/com/intellij/codeInsight/daemon/RainbowVisitor.java)
with the [LSPSemanticTokensRainbowVisitor](https://github.com/redhat-developer/lsp4ij/blob/main/src/main/java/com/redhat/devtools/lsp4ij/features/semanticTokens/LSPSemanticTokensRainbowVisitor.java) class.
by implementing [HighlightVisitor](https://github.com/JetBrains/intellij-community/blob/master/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/HighlightVisitor.java)
with the [LSPSemanticTokensHighlightVisitor](https://github.com/redhat-developer/lsp4ij/blob/main/src/main/java/com/redhat/devtools/lsp4ij/features/semanticTokens/LSPSemanticTokensHighlightVisitor.java) class.
* Use [SemanticTokensColorsProvider](https://github.com/redhat-developer/lsp4ij/blob/main/src/main/java/com/redhat/devtools/lsp4ij/features/semanticTokens/SemanticTokensColorsProvider.java) API to get TextAttributesKey from tokenType, tokenModifiers.
* By default, LSP4IJ, uses the [DefaultSemanticTokensColorsProvider](https://github.com/redhat-developer/lsp4ij/blob/main/src/main/java/com/redhat/devtools/lsp4ij/features/semanticTokens/DefaultSemanticTokensColorsProvider.java)
but you can use your own provider with the [semanticTokensColorsProvider extension point](./DeveloperGuide.md#semantic-tokens-colors-provider).
Expand Down
12 changes: 6 additions & 6 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ The following LSP features are integrated with the standard `Find Usages` menu (

![Find Usages menu](./images/find-usages/FindUsagesMenu.png)


![Find Usages result](./images/find-usages/FindUsagesResult.png)

### Refactoring
Expand All @@ -86,12 +85,14 @@ to apply some refactoring.

LSP4IJ provides **experimental** support for [LSP Semantic Tokens](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_semanticTokens).

### Enable semantic tokens
### Language Server coloration

LSP4IJ provides default colors for semantic tokens, configurable in `Editor / Color Scheme / Language Server`:

By default, semantic tokens is not activated it.
To activate it, you need to go to `Editor / Color Scheme / Language Server` and select the `Semantic highlighting` checkbox:
![Semantic tokens color settings](./images/LSPSemanticTokensColorPage.png)

![Enable LSP semantic tokens](./images/LSPSemanticTokensEnable.png)
You can see the mapping between token types/modifiers and IntelliJ's TextAttributeKey on the color settings page (e.g., Class declaration)
in [DefaultSemanticTokensColorsProvider](./LSPSupport.md#DefaultSemanticTokensColorsProvider) section.

### Semantic Tokens Inspector

Expand All @@ -111,7 +112,6 @@ that you can select in the left panel via the checkboxes:

If the `Semantic Tokens` doesn't show the expected result, please check that:

* you have [enabled semantic tokens](#enable-semantic-tokens).
* show in the [LSP console](#lsp-console) that your language server reports
LSP request `textDocument/semanticTokens/full`
* uses the [Semantic Tokens Inspector](#semantic-tokens-inspector) to understand more how the
Expand Down
Binary file added docs/images/LSPSemanticTokensColorPage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/images/LSPSemanticTokensEnable.png
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
******************************************************************************/
package com.redhat.devtools.lsp4ij.features.semanticTokens;

import com.intellij.codeInsight.daemon.RainbowVisitor;
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.daemon.impl.HighlightVisitor;
import com.intellij.codeInsight.daemon.impl.analysis.HighlightInfoHolder;
import com.intellij.openapi.progress.ProcessCanceledException;
Expand All @@ -20,7 +20,9 @@
import com.redhat.devtools.lsp4ij.LSPIJUtils;
import com.redhat.devtools.lsp4ij.LanguageServersRegistry;
import org.eclipse.lsp4j.SemanticTokensParams;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -33,29 +35,40 @@


/**
* LSP 'textDocument/semanticTokens support by implementing IntelliJ {@link RainbowVisitor}.
* LSP 'textDocument/semanticTokens support by implementing IntelliJ {@link HighlightVisitor}.
*
* <p>
* Implementing {@link RainbowVisitor} gives the capability to have an existing highlighter (custom highlighter, TextMate highlighter)
* Implementing {@link HighlightVisitor} gives the capability to have an existing highlighter (custom highlighter, TextMate highlighter)
* and add semantic coloration.
* </p>
*/
public class LSPSemanticTokensRainbowVisitor extends RainbowVisitor {
@ApiStatus.Internal
public class LSPSemanticTokensHighlightVisitor implements HighlightVisitor {

private static final Logger LOGGER = LoggerFactory.getLogger(LSPSemanticTokensRainbowVisitor.class);
private static final Logger LOGGER = LoggerFactory.getLogger(LSPSemanticTokensHighlightVisitor.class);

private PsiFile file;

private HighlightInfoHolder myHolder;

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

@Override
public boolean analyze(@NotNull PsiFile file, boolean updateWholeFile, @NotNull HighlightInfoHolder holder, @NotNull Runnable action) {
myHolder = holder;
if (LanguageServersRegistry.getInstance().isFileSupported(file)) {
enableSemanticTokensHighlighting(file);
return super.analyze(file, updateWholeFile, holder, action);
myHolder = holder;
try {
action.run();
}
finally {
myHolder = null;
}
return true;
}
return false;
}
Expand Down Expand Up @@ -99,7 +112,7 @@ public void visit(@NotNull PsiElement element) {
if (document == null) {
return;
}
semanticTokens.highlight(file, document, info -> super.addInfo(info));
semanticTokens.highlight(file, document, info -> addInfo(info));
// LSP semantic token is highlighted, don't highlight it again for the next visited PsiElement
disableSemanticTokensHighlighting();
}
Expand All @@ -120,7 +133,10 @@ private boolean isSemanticTokensHighlightingEnabled() {

@Override
public @NotNull HighlightVisitor clone() {
return new LSPSemanticTokensRainbowVisitor();
return new LSPSemanticTokensHighlightVisitor();
}

protected void addInfo(@Nullable HighlightInfo highlightInfo) {
myHolder.add(highlightInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
package com.redhat.devtools.lsp4ij.features.semanticTokens;

import com.intellij.codeHighlighting.RainbowHighlighter;
import com.intellij.lang.Language;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.fileTypes.PlainTextLanguage;
import com.intellij.openapi.fileTypes.SyntaxHighlighter;
import com.intellij.openapi.fileTypes.SyntaxHighlighterFactory;
import com.intellij.openapi.options.colors.AttributesDescriptor;
import com.intellij.openapi.options.colors.ColorDescriptor;
import com.intellij.openapi.options.colors.RainbowColorSettingsPage;
import com.intellij.openapi.options.colors.ColorSettingsPage;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.psi.codeStyle.DisplayPriority;
import com.intellij.psi.codeStyle.DisplayPrioritySortable;
Expand All @@ -33,7 +32,7 @@
/**
* LSP Semantic tokens color setting page.
*/
public class SemanticTokensColorSettingsPage implements RainbowColorSettingsPage, DisplayPrioritySortable {
public class SemanticTokensColorSettingsPage implements ColorSettingsPage, DisplayPrioritySortable {

private static final AttributesDescriptor[] ourDescriptors = {
// namespace
Expand Down Expand Up @@ -129,16 +128,6 @@ public class SemanticTokensColorSettingsPage implements RainbowColorSettingsPage
}
}

@Override
public boolean isRainbowType(TextAttributesKey type) {
return false;
}

@Override
public @Nullable Language getLanguage() {
return null;
}

@Override
public @Nullable Icon getIcon() {
return null;
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@

<!-- LSP textDocument/semanticTokens request support -->
<highlightVisitor
id="LSPRainbowVisitor"
implementation="com.redhat.devtools.lsp4ij.features.semanticTokens.LSPSemanticTokensRainbowVisitor"/>
id="LSPSemanticTokensHighlightVisitor"
implementation="com.redhat.devtools.lsp4ij.features.semanticTokens.LSPSemanticTokensHighlightVisitor"/>
<colorSettingsPage
implementation="com.redhat.devtools.lsp4ij.features.semanticTokens.SemanticTokensColorSettingsPage"/>

Expand Down

0 comments on commit ed0554b

Please sign in to comment.