Skip to content

Commit

Permalink
Review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
BoykoAlex authored and jukzi committed Dec 9, 2024
1 parent 389cb9c commit acad0c6
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2023 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -20,6 +20,7 @@
import org.eclipse.jdt.text.tests.codemining.CodeMiningTriggerTest;
import org.eclipse.jdt.text.tests.codemining.ParameterNamesCodeMiningTest;
import org.eclipse.jdt.text.tests.contentassist.ContentAssistTestSuite;
import org.eclipse.jdt.text.tests.semantictokens.SemanticTokensProviderTest;
import org.eclipse.jdt.text.tests.spelling.SpellCheckEngineTestCase;
import org.eclipse.jdt.text.tests.templates.TemplatesTestSuite;

Expand Down Expand Up @@ -52,6 +53,7 @@
BracketInserterTest.class,
SpellCheckEngineTestCase.class,
SemanticHighlightingTest.class,
SemanticTokensProviderTest.class,
AutoboxingSemanticHighlightingTest.class,
Java23SemanticHighlightingTest.class,
NewForLoopJavaContextTest.class,
Expand Down
16 changes: 13 additions & 3 deletions org.eclipse.jdt.ui/schema/semanticTokens.exsd
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,17 @@
<meta.section type="examples"/>
</appInfo>
<documentation>
[Enter extension point usage example here.]
The following is an example of a Semantic Tokens Provider contribution:

&lt;p&gt;
&lt;pre&gt;
&lt;extension point=&quot;org.eclipse.jdt.ui.semanticTokens&quot;&gt;
&lt;provider
class=&quot;org.eclipse.example.jdt.ExampleJavaSemanticTokensProvider&quot;&gt;
&lt;/provider&gt;
&lt;/extension&gt;
&lt;/pre&gt;
&lt;/p&gt;
</documentation>
</annotation>

Expand All @@ -93,7 +103,7 @@
<meta.section type="apiinfo"/>
</appInfo>
<documentation>
[Enter API information here.]
The contributed class must implement &lt;code&gt;org.eclipse.jdt.ui.text.java.ISemanticTokensProvider&lt;/code&gt;
</documentation>
</annotation>

Expand All @@ -102,7 +112,7 @@
<meta.section type="implementation"/>
</appInfo>
<documentation>
[Enter information about supplied implementation of this extension point.]
Allows contributing &lt;code&gt;org.eclipse.jdt.ui.text.java.ISemanticTokensProvide&lt;/code&gt; which will colour the code with colours allowed in the Java Editor. This is useful for languages embedded into Java via annotation parameters for example such as SQL queries, Spring Expression Language snippets etc. The contributions are not sorted. It is recommneded therefore not to override semantic tokens between providers. This extension is experimental and only used by LSP4E project at the moment.
</documentation>
</annotation>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ static class Highlighting { // TODO: rename to HighlightingStyle
/** Enabled state */
private boolean fIsEnabled;
/** Preference Key */
private String fKey;
private final String fPreferenceKey;

/**
* Initialize with the given text attribute.
* @param textAttribute The text attribute
* @param isEnabled the enabled state
*/
public Highlighting(String key, TextAttribute textAttribute, boolean isEnabled) {
public Highlighting(String preferenceKey, TextAttribute textAttribute, boolean isEnabled) {
setTextAttribute(textAttribute);
setEnabled(isEnabled);
this.fKey = key;
this.fPreferenceKey = preferenceKey;
}

/**
Expand Down Expand Up @@ -97,8 +97,8 @@ public void setEnabled(boolean isEnabled) {
fIsEnabled= isEnabled;
}

public String getKey() {
return fKey;
public String getPreferenceKey() {
return fPreferenceKey;
}

}
Expand Down Expand Up @@ -384,10 +384,9 @@ private HighlightedPosition[] createHardcodedPositions() {
* @return the corresponding highlighting
*/
private Highlighting getHighlighting(String key) {
for (int i= 0; i < fHighlightings.length; i++) {
Highlighting highlighting= fHighlightings[i];
if (key.equals(highlighting.getKey()))
return fHighlightings[i];
for (Highlighting highlighting : fHighlightings) {
if (key.equals(highlighting.getPreferenceKey()))
return highlighting;
}
return null;
}
Expand Down Expand Up @@ -456,10 +455,10 @@ private void initializeHighlightings() {
}

SyntaxColorHighlighting[] syntaxHighlightings= SyntaxColorHighlighting.getSyntaxColorHighlightings();
fSyntaxHighlightings = new Highlighting[syntaxHighlightings.length];
for (int i = 0; i < syntaxHighlightings.length; i++) {
SyntaxColorHighlighting h = syntaxHighlightings[i];
fSyntaxHighlightings[i] = createHighlightingFromPereferences(h.preferenceKey(), true, h.preferenceKey(), h.getBoldPreferenceKey(), h.getItalicPreferenceKey(), h.getStrikethroughPreferenceKey(), h.getUnderlinePreferenceKey());
fSyntaxHighlightings= new Highlighting[syntaxHighlightings.length];
for (int i= 0; i < syntaxHighlightings.length; i++) {
SyntaxColorHighlighting h= syntaxHighlightings[i];
fSyntaxHighlightings[i]= createHighlightingFromPereferences(h.preferenceKey(), true, h.preferenceKey(), h.getBoldPreferenceKey(), h.getItalicPreferenceKey(), h.getStrikethroughPreferenceKey(), h.getUnderlinePreferenceKey());
}
}

Expand All @@ -484,10 +483,12 @@ private Highlighting createHighlightingFromPereferences(String key, boolean isEn
* Dispose the semantic highlightings.
*/
private void disposeHighlightings() {
for (SemanticHighlighting fSemanticHighlighting : fSemanticHighlightings)
for (SemanticHighlighting fSemanticHighlighting : fSemanticHighlightings) {
removeColor(SemanticHighlightings.getColorPreferenceKey(fSemanticHighlighting));
for (Highlighting h : fSyntaxHighlightings)
removeColor(h.getKey());
}
for (Highlighting h : fSyntaxHighlightings) {
removeColor(h.getPreferenceKey());
}

fSemanticHighlightings= null;
fHighlightings= null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ private void addPosition(int offset, int length, Highlighting highlighting) {
}

private static final String JAVA_EDITOR_SEMANTIC_TOKENS_EXTENSION_POINT= "org.eclipse.jdt.ui.semanticTokens"; //$NON-NLS-1$
private static final String ATTR_CLASS = "class"; //$NON-NLS-1$

private static ISemanticTokensProvider[] fSemanticTokensProviders;

Expand Down Expand Up @@ -399,20 +400,18 @@ public void aboutToBeReconciled() {
// Do nothing
}

private static ISemanticTokensProvider[] getContributedSemanticTokensProviders() {
private static synchronized ISemanticTokensProvider[] getContributedSemanticTokensProviders() {
if (fSemanticTokensProviders == null) {
synchronized(SemanticHighlightingReconciler.class) {
IExtensionRegistry registry= Platform.getExtensionRegistry();
IConfigurationElement[] elements= registry.getConfigurationElementsFor(JAVA_EDITOR_SEMANTIC_TOKENS_EXTENSION_POINT);
fSemanticTokensProviders = Arrays.stream(elements).map(ce -> {
try {
return (ISemanticTokensProvider) ce.createExecutableExtension("class"); //$NON-NLS-1$
} catch (Exception e) {
JavaPlugin.getDefault().getLog().error("Cannot instatiate semantic tokens provider", e); //$NON-NLS-1$
return null;
}
}).filter(Objects::nonNull).toArray(ISemanticTokensProvider[]::new);
}
IExtensionRegistry registry= Platform.getExtensionRegistry();
IConfigurationElement[] elements= registry.getConfigurationElementsFor(JAVA_EDITOR_SEMANTIC_TOKENS_EXTENSION_POINT);
fSemanticTokensProviders = Arrays.stream(elements).map(ce -> {
try {
return (ISemanticTokensProvider) ce.createExecutableExtension(ATTR_CLASS);
} catch (Exception e) {
JavaPlugin.getDefault().getLog().error("Cannot instatiate semantic tokens provider '%s' contributed by '%s'".formatted(ce.getAttribute(ATTR_CLASS), ce.getContributor().getName()), e); //$NON-NLS-1$
return null;
}
}).filter(Objects::nonNull).toArray(ISemanticTokensProvider[]::new);
}
return fSemanticTokensProviders;
}
Expand Down Expand Up @@ -538,95 +537,95 @@ private void reconcilePositions(ASTNode[] subtrees, List<ISemanticTokensProvider
}

private Highlighting fromSemanticTokenType(ISemanticTokensProvider.TokenType type) {
if (type != null) {
switch (type) {
case OPERATOR:
return findSyntaxHighlighting(PreferenceConstants.EDITOR_JAVA_OPERATOR_COLOR);
case SINGLE_LINE_COMMENT:
return findSyntaxHighlighting(PreferenceConstants.EDITOR_SINGLE_LINE_COMMENT_COLOR);
case KEYWORD:
return findSyntaxHighlighting(PreferenceConstants.EDITOR_JAVA_KEYWORD_COLOR);
case BRACKET:
return findSyntaxHighlighting(PreferenceConstants.EDITOR_JAVA_BRACKET_COLOR);
case MULTI_LINE_COMMENT:
return findSyntaxHighlighting(PreferenceConstants.EDITOR_MULTI_LINE_COMMENT_COLOR);
case STRING:
return findSyntaxHighlighting(PreferenceConstants.EDITOR_STRING_COLOR);
case METHOD:
return findSemanticHighlighting(SemanticHighlightings.METHOD);
case ABSTRACT_CLASS:
return findSemanticHighlighting(SemanticHighlightings.ABSTRACT_CLASS);
case ABSTRACT_METHOD_INVOCATION:
return findSemanticHighlighting(SemanticHighlightings.ABSTRACT_METHOD_INVOCATION);
case ANNOTATION:
return findSemanticHighlighting(SemanticHighlightings.ANNOTATION);
case ANNOTATION_ELEMENT_REFERENCE:
return findSemanticHighlighting(SemanticHighlightings.ANNOTATION_ELEMENT_REFERENCE);
case AUTOBOXING:
return findSemanticHighlighting(SemanticHighlightings.AUTOBOXING);
case CLASS:
return findSemanticHighlighting(SemanticHighlightings.CLASS);
case DEPRECATED_MEMBER:
return findSemanticHighlighting(SemanticHighlightings.DEPRECATED_MEMBER);
case ENUM:
return findSemanticHighlighting(SemanticHighlightings.ENUM);
case FIELD:
return findSemanticHighlighting(SemanticHighlightings.FIELD);
case INHERITED_FIELD:
return findSemanticHighlighting(SemanticHighlightings.INHERITED_FIELD);
case INHERITED_METHOD_INVOCATION:
return findSemanticHighlighting(SemanticHighlightings.INHERITED_METHOD_INVOCATION);
case INTERFACE:
return findSemanticHighlighting(SemanticHighlightings.INTERFACE);
case LOCAL_VARIABLE:
return findSemanticHighlighting(SemanticHighlightings.LOCAL_VARIABLE);
case LOCAL_VARIABLE_DECLARATION:
return findSemanticHighlighting(SemanticHighlightings.LOCAL_VARIABLE_DECLARATION);
case METHOD_DECLARATION:
return findSemanticHighlighting(SemanticHighlightings.METHOD_DECLARATION);
case NUMBER:
return findSemanticHighlighting(SemanticHighlightings.NUMBER);
case PARAMETER_VARIABLE:
return findSemanticHighlighting(SemanticHighlightings.PARAMETER_VARIABLE);
case STATIC_FIELD:
return findSemanticHighlighting(SemanticHighlightings.STATIC_FIELD);
case STATIC_FINAL_FIELD:
return findSemanticHighlighting(SemanticHighlightings.STATIC_FINAL_FIELD);
case STATIC_METHOD_INVOCATION:
return findSemanticHighlighting(SemanticHighlightings.STATIC_METHOD_INVOCATION);
case TYPE_ARGUMENT:
return findSemanticHighlighting(SemanticHighlightings.TYPE_ARGUMENT);
case TYPE_VARIABLE:
return findSemanticHighlighting(SemanticHighlightings.TYPE_VARIABLE);
case RESTRICTED_IDENTIFIER:
return findSemanticHighlighting(SemanticHighlightingsCore.RESTRICTED_KEYWORDS);
case DEFAULT:
default:
return findSyntaxHighlighting(PreferenceConstants.EDITOR_JAVA_DEFAULT_COLOR);
}
if (type == null) {
return null;
}
switch (type) {
case OPERATOR:
return findSyntaxHighlighting(PreferenceConstants.EDITOR_JAVA_OPERATOR_COLOR);
case SINGLE_LINE_COMMENT:
return findSyntaxHighlighting(PreferenceConstants.EDITOR_SINGLE_LINE_COMMENT_COLOR);
case KEYWORD:
return findSyntaxHighlighting(PreferenceConstants.EDITOR_JAVA_KEYWORD_COLOR);
case BRACKET:
return findSyntaxHighlighting(PreferenceConstants.EDITOR_JAVA_BRACKET_COLOR);
case MULTI_LINE_COMMENT:
return findSyntaxHighlighting(PreferenceConstants.EDITOR_MULTI_LINE_COMMENT_COLOR);
case STRING:
return findSyntaxHighlighting(PreferenceConstants.EDITOR_STRING_COLOR);
case METHOD:
return findSemanticHighlighting(SemanticHighlightings.METHOD);
case ABSTRACT_CLASS:
return findSemanticHighlighting(SemanticHighlightings.ABSTRACT_CLASS);
case ABSTRACT_METHOD_INVOCATION:
return findSemanticHighlighting(SemanticHighlightings.ABSTRACT_METHOD_INVOCATION);
case ANNOTATION:
return findSemanticHighlighting(SemanticHighlightings.ANNOTATION);
case ANNOTATION_ELEMENT_REFERENCE:
return findSemanticHighlighting(SemanticHighlightings.ANNOTATION_ELEMENT_REFERENCE);
case AUTOBOXING:
return findSemanticHighlighting(SemanticHighlightings.AUTOBOXING);
case CLASS:
return findSemanticHighlighting(SemanticHighlightings.CLASS);
case DEPRECATED_MEMBER:
return findSemanticHighlighting(SemanticHighlightings.DEPRECATED_MEMBER);
case ENUM:
return findSemanticHighlighting(SemanticHighlightings.ENUM);
case FIELD:
return findSemanticHighlighting(SemanticHighlightings.FIELD);
case INHERITED_FIELD:
return findSemanticHighlighting(SemanticHighlightings.INHERITED_FIELD);
case INHERITED_METHOD_INVOCATION:
return findSemanticHighlighting(SemanticHighlightings.INHERITED_METHOD_INVOCATION);
case INTERFACE:
return findSemanticHighlighting(SemanticHighlightings.INTERFACE);
case LOCAL_VARIABLE:
return findSemanticHighlighting(SemanticHighlightings.LOCAL_VARIABLE);
case LOCAL_VARIABLE_DECLARATION:
return findSemanticHighlighting(SemanticHighlightings.LOCAL_VARIABLE_DECLARATION);
case METHOD_DECLARATION:
return findSemanticHighlighting(SemanticHighlightings.METHOD_DECLARATION);
case NUMBER:
return findSemanticHighlighting(SemanticHighlightings.NUMBER);
case PARAMETER_VARIABLE:
return findSemanticHighlighting(SemanticHighlightings.PARAMETER_VARIABLE);
case STATIC_FIELD:
return findSemanticHighlighting(SemanticHighlightings.STATIC_FIELD);
case STATIC_FINAL_FIELD:
return findSemanticHighlighting(SemanticHighlightings.STATIC_FINAL_FIELD);
case STATIC_METHOD_INVOCATION:
return findSemanticHighlighting(SemanticHighlightings.STATIC_METHOD_INVOCATION);
case TYPE_ARGUMENT:
return findSemanticHighlighting(SemanticHighlightings.TYPE_ARGUMENT);
case TYPE_VARIABLE:
return findSemanticHighlighting(SemanticHighlightings.TYPE_VARIABLE);
case RESTRICTED_IDENTIFIER:
return findSemanticHighlighting(SemanticHighlightingsCore.RESTRICTED_KEYWORDS);
case DEFAULT:
default:
return findSyntaxHighlighting(PreferenceConstants.EDITOR_JAVA_DEFAULT_COLOR);
}
return null;
}

private Highlighting findSemanticHighlighting(String preferenceKey) {
for (int i = 0; i < fJobHighlightings.length; i++) {
if (Objects.equals(preferenceKey, fJobHighlightings[i].getKey())) {
if (!fJobHighlightings[i].isEnabled()) {
for (Highlighting h : fJobHighlightings) {
if (Objects.equals(preferenceKey, h.getPreferenceKey())) {
if (!h.isEnabled()) {
break;
}
return fJobHighlightings[i];
return h;
}
}
return null;
}

private Highlighting findSyntaxHighlighting(String key) {
for (int i = 0; i < fJobSyntaxHighlightings.length; i++) {
if (Objects.equals(key, fJobSyntaxHighlightings[i].getKey())) {
if (!fJobSyntaxHighlightings[i].isEnabled()) {
for (Highlighting h : fJobSyntaxHighlightings) {
if (Objects.equals(key, h.getPreferenceKey())) {
if (!h.isEnabled()) {
break;
}
return fJobSyntaxHighlightings[i];
return h;
}
}
return null;
Expand Down

0 comments on commit acad0c6

Please sign in to comment.