From acad0c618ad40dab42a8f62a1e624ca124ab1673 Mon Sep 17 00:00:00 2001 From: aboyko Date: Thu, 5 Dec 2024 10:01:44 -0500 Subject: [PATCH] Review comments --- .../jdt/text/tests/JdtTextTestSuite.java | 4 +- org.eclipse.jdt.ui/schema/semanticTokens.exsd | 16 +- .../SemanticHighlightingManager.java | 33 ++-- .../SemanticHighlightingReconciler.java | 175 +++++++++--------- 4 files changed, 120 insertions(+), 108 deletions(-) diff --git a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/JdtTextTestSuite.java b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/JdtTextTestSuite.java index 8f02b18f676..96bb66735d3 100644 --- a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/JdtTextTestSuite.java +++ b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/JdtTextTestSuite.java @@ -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 @@ -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; @@ -52,6 +53,7 @@ BracketInserterTest.class, SpellCheckEngineTestCase.class, SemanticHighlightingTest.class, + SemanticTokensProviderTest.class, AutoboxingSemanticHighlightingTest.class, Java23SemanticHighlightingTest.class, NewForLoopJavaContextTest.class, diff --git a/org.eclipse.jdt.ui/schema/semanticTokens.exsd b/org.eclipse.jdt.ui/schema/semanticTokens.exsd index e6dfbd435e0..7c45ce0890e 100644 --- a/org.eclipse.jdt.ui/schema/semanticTokens.exsd +++ b/org.eclipse.jdt.ui/schema/semanticTokens.exsd @@ -84,7 +84,17 @@ - [Enter extension point usage example here.] + The following is an example of a Semantic Tokens Provider contribution: + +<p> +<pre> + <extension point="org.eclipse.jdt.ui.semanticTokens"> + <provider + class="org.eclipse.example.jdt.ExampleJavaSemanticTokensProvider"> + </provider> + </extension> +</pre> +</p> @@ -93,7 +103,7 @@ - [Enter API information here.] + The contributed class must implement <code>org.eclipse.jdt.ui.text.java.ISemanticTokensProvider</code> @@ -102,7 +112,7 @@ - [Enter information about supplied implementation of this extension point.] + Allows contributing <code>org.eclipse.jdt.ui.text.java.ISemanticTokensProvide</code> 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. diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/SemanticHighlightingManager.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/SemanticHighlightingManager.java index b78cdd3bbea..7b8735cd01f 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/SemanticHighlightingManager.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/SemanticHighlightingManager.java @@ -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; } /** @@ -97,8 +97,8 @@ public void setEnabled(boolean isEnabled) { fIsEnabled= isEnabled; } - public String getKey() { - return fKey; + public String getPreferenceKey() { + return fPreferenceKey; } } @@ -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; } @@ -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()); } } @@ -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; diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/SemanticHighlightingReconciler.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/SemanticHighlightingReconciler.java index 6d9da8cadf2..667bd3b73e3 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/SemanticHighlightingReconciler.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/SemanticHighlightingReconciler.java @@ -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; @@ -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; } @@ -538,95 +537,95 @@ private void reconcilePositions(ASTNode[] subtrees, List