forked from redhat-developer/lsp4ij
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support for
textDocument/semanticTokens
Fixes redhat-developer#238 Signed-off-by: azerr <[email protected]>
- Loading branch information
1 parent
c521ff9
commit 7fbf259
Showing
26 changed files
with
1,306 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
186 changes: 186 additions & 0 deletions
186
...m/redhat/devtools/lsp4ij/features/semanticTokens/DefaultSemanticTokensColorsProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Red Hat, Inc. | ||
* Distributed under license by Red Hat, Inc. All rights reserved. | ||
* This program is made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, | ||
* and is available at http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
******************************************************************************/ | ||
package com.redhat.devtools.lsp4ij.features.semanticTokens; | ||
|
||
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors; | ||
import com.intellij.openapi.editor.colors.TextAttributesKey; | ||
import com.intellij.psi.PsiFile; | ||
import org.eclipse.lsp4j.SemanticTokenModifiers; | ||
import org.eclipse.lsp4j.SemanticTokenTypes; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.List; | ||
|
||
public class DefaultSemanticTokensColorsProvider implements SemanticTokensColorsProvider { | ||
|
||
|
||
@Override | ||
public @Nullable TextAttributesKey getTextAttributesKey(@NotNull String tokenType, | ||
@NotNull List<String> tokenModifiers, | ||
@NotNull PsiFile file) { | ||
switch (tokenType) { | ||
|
||
// class: for identifiers that declare or reference a class type. | ||
case SemanticTokenTypes.Class: | ||
if (hasTokenModifiers(tokenModifiers, | ||
SemanticTokenModifiers.Declaration, | ||
SemanticTokenModifiers.Definition)) { | ||
// with declaration, definition modifiers | ||
return SemanticTokensHighlightingColors.CLASS_DECLARATION; | ||
} | ||
// with other modifiers | ||
return SemanticTokensHighlightingColors.CLASS_REFERENCE; | ||
|
||
// comment | ||
case SemanticTokenTypes.Comment: | ||
return SemanticTokensHighlightingColors.COMMENT; | ||
|
||
// decorator | ||
case SemanticTokenTypes.Decorator: | ||
return SemanticTokensHighlightingColors.DECORATOR; | ||
|
||
// enum | ||
case SemanticTokenTypes.Enum: | ||
return SemanticTokensHighlightingColors.ENUM; | ||
|
||
// enum member | ||
case SemanticTokenTypes.EnumMember: | ||
return SemanticTokensHighlightingColors.ENUM_MEMBER; | ||
|
||
// event | ||
case SemanticTokenTypes.Event: | ||
return SemanticTokensHighlightingColors.EVENT; | ||
|
||
// function | ||
case SemanticTokenTypes.Function: | ||
if (hasTokenModifiers(tokenModifiers, | ||
SemanticTokenModifiers.Declaration, | ||
SemanticTokenModifiers.Definition)) { | ||
// with declaration, definition modifiers | ||
return SemanticTokensHighlightingColors.FUNCTION_DECLARATION; | ||
} | ||
// with other modifiers | ||
return SemanticTokensHighlightingColors.FUNCTION_CALL; | ||
|
||
// interface | ||
case SemanticTokenTypes.Interface: | ||
return DefaultLanguageHighlighterColors.INTERFACE_NAME; | ||
|
||
// keyword | ||
case SemanticTokenTypes.Keyword: | ||
return SemanticTokensHighlightingColors.KEYWORD; | ||
|
||
// macro | ||
case SemanticTokenTypes.Macro: | ||
return SemanticTokensHighlightingColors.MACRO; | ||
|
||
// method | ||
case SemanticTokenTypes.Method: { | ||
if (hasTokenModifiers(tokenModifiers, | ||
SemanticTokenModifiers.Declaration, | ||
SemanticTokenModifiers.Definition)) { | ||
// with declaration, definition modifiers | ||
return SemanticTokensHighlightingColors.METHOD_DECLARATION; | ||
} | ||
// with other modifiers | ||
return SemanticTokensHighlightingColors.METHOD_CALL; | ||
} | ||
|
||
/*case "member": | ||
return SemanticTokensHighlightingColors.MEMBER_ATTRIBUTES; | ||
*/ | ||
|
||
// modifier | ||
case SemanticTokenTypes.Modifier: | ||
return SemanticTokensHighlightingColors.MODIFIER; | ||
|
||
// Namespace: for identifiers that declare or reference a namespace, module, or package. | ||
case SemanticTokenTypes.Namespace: | ||
if (hasTokenModifiers(tokenModifiers, | ||
SemanticTokenModifiers.Declaration, | ||
SemanticTokenModifiers.Definition)) { | ||
// with declaration, definition modifiers | ||
return SemanticTokensHighlightingColors.NAMESPACE_DECLARATION; | ||
} | ||
// with other modifiers | ||
return SemanticTokensHighlightingColors.NAMESPACE; | ||
|
||
// Number: for tokens that represent a number literal. | ||
case SemanticTokenTypes.Number: | ||
return SemanticTokensHighlightingColors.NUMBER; | ||
|
||
// Operator | ||
case SemanticTokenTypes.Operator: | ||
return DefaultLanguageHighlighterColors.OPERATION_SIGN; | ||
|
||
// Parameter | ||
case SemanticTokenTypes.Parameter: | ||
return DefaultLanguageHighlighterColors.PARAMETER; | ||
|
||
// Property | ||
case SemanticTokenTypes.Property: | ||
if (hasTokenModifiers(tokenModifiers, SemanticTokenModifiers.Static)) { | ||
if (hasTokenModifiers(tokenModifiers, SemanticTokenModifiers.Readonly)) { | ||
// with static, readonly modifiers | ||
return SemanticTokensHighlightingColors.STATIC_READONLY_PROPERTY; | ||
} | ||
// with static readonly modifiers | ||
return SemanticTokensHighlightingColors.STATIC_PROPERTY; | ||
} | ||
if (hasTokenModifiers(tokenModifiers, SemanticTokenModifiers.Readonly)) { | ||
// with readonly modifiers | ||
return SemanticTokensHighlightingColors.READONLY_PROPERTY; | ||
} | ||
// with other modifiers | ||
return SemanticTokensHighlightingColors.PROPERTY; | ||
|
||
case SemanticTokenTypes.Regexp: | ||
return DefaultLanguageHighlighterColors.REASSIGNED_LOCAL_VARIABLE; | ||
|
||
case SemanticTokenTypes.String: | ||
return SemanticTokensHighlightingColors.STRING; | ||
|
||
case SemanticTokenTypes.Struct: | ||
return DefaultLanguageHighlighterColors.VALID_STRING_ESCAPE; | ||
|
||
case SemanticTokenTypes.Type: | ||
return DefaultLanguageHighlighterColors.CLASS_REFERENCE; | ||
|
||
case SemanticTokenTypes.TypeParameter: | ||
return DefaultLanguageHighlighterColors.MARKUP_ATTRIBUTE; | ||
|
||
// variable | ||
case SemanticTokenTypes.Variable: | ||
return DefaultLanguageHighlighterColors.LOCAL_VARIABLE; | ||
} | ||
return null; | ||
} | ||
|
||
protected boolean hasTokenModifiers(List<String> tokenModifiers, String... checkedTokenModifiers) { | ||
if (tokenModifiers.isEmpty()) { | ||
return false; | ||
} | ||
for (var modifier : checkedTokenModifiers) { | ||
if (tokenModifiers.contains(modifier)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
protected boolean hasTokenModifiersOrEmpty(List<String> tokenModifiers, String... checkedTokenModifiers) { | ||
if (tokenModifiers.isEmpty()) { | ||
return true; | ||
} | ||
return hasTokenModifiers(tokenModifiers, checkedTokenModifiers); | ||
} | ||
} |
Oops, something went wrong.