Skip to content

Semantic tokens from LS available in the Java editor - #1161

Merged
rubenporras merged 1 commit into
eclipse-lsp4e:mainfrom
BoykoAlex:jdt-sync-syntax-highlighting
Apr 10, 2025
Merged

rubenporras merged 1 commit into
eclipse-lsp4e:mainfrom
BoykoAlex:jdt-sync-syntax-highlighting

Conversation

@BoykoAlex

Copy link
Copy Markdown
Contributor

Based on the latest addition of semantic tokens extension for Java editor the support is added for having semantic tokens from the language server in the Java editor.
There is a preference add to turn off semantic tokens support in the Java editor. The preference is defined but the UI is not and is expected to come from 3rd party plugins using this functionality (i.e. spring boot ls plugin) otherwise it's unclear where to fit this preference in the UI at the moment

@BoykoAlex
BoykoAlex force-pushed the jdt-sync-syntax-highlighting branch from 2a87bc2 to 347e9d1 Compare March 31, 2025 17:07
@BoykoAlex

Copy link
Copy Markdown
Contributor Author

@rubenporras I've rebased this PR on the latest from main however I still get the pesky Null Annotations compiler error:

  Error: 6,958 [ERROR] Failed to execute goal org.eclipse.tycho:tycho-compiler-plugin:4.0.12:compile (default-compile) on project org.eclipse.lsp4e.jdt: Compilation failure: Compilation failure: 
  Error: 6,958 [ERROR] /Users/runner/work/lsp4e/lsp4e/org.eclipse.lsp4e.jdt/src/org/eclipse/lsp4e/jdt/LSJavaSemanticTokensProvider.java:[42] 
  Error: 6,958 [ERROR] 	public Collection<ISemanticTokensProvider.SemanticToken> computeSemanticTokens(CompilationUnit ast) {
  Error: 6,958 [ERROR] 	       ^^^^^^^^^^
  Error: 6,958 [ERROR] The return type is incompatible with 'Collection<ISemanticTokensProvider.SemanticToken>' returned from ISemanticTokensProvider.computeSemanticTokens(CompilationUnit) (mismatching null constraints)
  Error: 6,958 [ERROR] 1 problem (1 error)
  Error: 6,958 [ERROR] -> [Help 1]

I have added JDT UI into eclipse platform with the new ISemanticTokensProvider API: https://github.com/vegardit/no-npe/blob/main/libs/eea-eclipse-platform/src/main/resources/org/eclipse/jdt/ui/text/java/ISemanticTokensProvider.eea but it doesn't seem to help :-. Is there anything else I can do other than change the preferences for LSP4E JDT plugin to flag this issues as a warning rather than error?

@mickaelistria

Copy link
Copy Markdown
Contributor

@sebthom Can you please help regarding this null analysis error?

@sebthom

sebthom commented Mar 31, 2025 •

Copy link
Copy Markdown
Member

I have added JDT UI into eclipse platform with the new ISemanticTokensProvider API: vegardit/no-npe@main/libs/eea-eclipse-platform/src/main/resources/org/eclipse/jdt/ui/text/java/ISemanticTokensProvider.eea but it doesn't seem to help :-. Is there anything else I can do other than change the preferences for LSP4E JDT plugin to flag this issues as a warning rather than error?

@BoykoAlex I can reproduce this. It is a limitation of the Eclipse compiler's null analysis. In this case the @NonNullByDefault annotation in the package-info.java provokes a conflict with the under-specified non-null external annotations of ISemanticTokensProvider#computeSemanticTokens. I updated the external annotations file. Please rebase your PR. It should compile now.

@BoykoAlex
BoykoAlex force-pushed the jdt-sync-syntax-highlighting branch from ad75116 to a2ca2f3 Compare March 31, 2025 21:34
@BoykoAlex

Copy link
Copy Markdown
Contributor Author

@sebthom many thanks for helping with Null Annotations issue and a code review. Looks like everything is in order to merge this PR now!-)

@sebthom

sebthom commented Mar 31, 2025

Copy link
Copy Markdown
Member

The preference is defined but the UI is not and is expected to come from 3rd party plugins using this functionality (i.e. spring boot ls plugin) otherwise it's unclear where to fit this preference in the UI at the moment

I haven't looked at the functionality provided by this PR, but is it reasonable to expect third parties to globally enable/disable this, given there's no UI option to turn it off? Considering potential side effects, wouldn’t it make more sense to have the feature disabled by default (and expect 3rd party plugins to enable it) or to directly provide a UI option to enable/disable it?

@BoykoAlex

BoykoAlex commented Mar 31, 2025 •

Copy link
Copy Markdown
Contributor Author

@sebthom I was wondering about the same: #1161 (comment)
If there is a place in the UI where i can put the preference I'd be happy to do so.
Yes, we can set it off by default...

import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.SemanticTokensLegend;

class JavaSemanticTokensProcessor {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class has a lot of common code with org.eclipse.lsp4e.operations.semanticTokens.SemanticTokensDataStreamProcessor, could we have a common implementation?

return null;
}

private @Nullable SemanticTokensLegend getSemanticTokensLegend(final LanguageServerWrapper wrapper) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method (and others) is an exact copy of org.eclipse.lsp4e.operations.semanticTokens.SemanticHighlightReconcilerStrategy.getSemanticTokensLegend(LanguageServerWrapper).

Others are very similar. Do you think we could refactor the code to avoid the redundancy?

@BoykoAlex

Copy link
Copy Markdown
Contributor Author

@sebthom @rubenporras Thank you for the review comments. Indeed the code needed quite bit of tidy up! Hopefully looks better now. The build fails because there is something off with the HighlightJS style fetching... host server is down it seems...
Anything you'd like me to try with the preference switching on/off LSP semantic tokens in the Java editor? A Java page under "Language Servers" preferences?

@BoykoAlex
BoykoAlex requested review from rubenporras and sebthom April 2, 2025 14:58
@sebthom

sebthom commented Apr 2, 2025

Copy link
Copy Markdown
Member

Anything you'd like me to try with the preference switching on/off LSP semantic tokens in the Java editor? A Java page under "Language Servers" preferences?

@BoykoAlex @rubenporras Maybe it would make sense to add a "Features" preferences page under "Language Servers" where one can enable/disable semantic tokens and in future other features.

@BoykoAlex

Copy link
Copy Markdown
Contributor Author

I have added the Java preference page under Language Servers preferences. It's contributed via the LSP4E JDT plugin hence would only be shown for those having the LSP4E JDT plugin...

@BoykoAlex
BoykoAlex force-pushed the jdt-sync-syntax-highlighting branch from e419d09 to dceec6d Compare April 4, 2025 20:48
@BoykoAlex

Copy link
Copy Markdown
Contributor Author

Rebased and squashed commits

@BoykoAlex

Copy link
Copy Markdown
Contributor Author

Here is how it looks at the moment. SQL queries are styled after Java editor semantic tokens... i.e. keywords, variables, properties, methods etc.

Screenshot 2025-04-04 at 16 50 01

@BoykoAlex

Copy link
Copy Markdown
Contributor Author

@rubenporras @sebthom shall we fold this in? More feedback on the PR is welcomed...

return SemanticHighlightReconcilerStrategy.requestFullSemanticTokens(theDocument, (legend, semanticTokens) -> convertTokens(legend, theDocument, semanticTokens))
.thenApply(o -> o.orElse(Collections.emptyList())).get(300, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
LanguageServerPlugin.logError("Failed to fetch semantic tokens for '%s' from Language Servers".formatted(resource.getLocation()), e);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far we log TimeoutException as a warning that includes the timeout value, could we do that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good.

try {
return SemanticHighlightReconcilerStrategy.requestFullSemanticTokens(theDocument, (legend, semanticTokens) -> convertTokens(legend, theDocument, semanticTokens))
.thenApply(o -> o.orElse(Collections.emptyList())).get(300, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not call Thread.currentThread().interrupt() for InterruptedException?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we should. Will make this change.

final var styleRangeHolder = this.styleRangeHolder;
if (!dataStream.isEmpty() && semanticTokensDataStreamProcessor != null && styleRangeHolder != null) {
List<StyleRange> styleRanges = semanticTokensDataStreamProcessor.getStyleRanges(dataStream,
List<StyleRange> styleRanges = semanticTokensDataStreamProcessor.getTokens(dataStream,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to call getTokens and to save it into a variable of type StyleRange is a bit awkward, but I do not have a suggestion for a better name at this moment, do you have a better idea?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getVisualTokens()?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since it is an ...TokensDataStreamProcessor, maybe getTokensData?

After that I would be happy to merge.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me - made this change.

}

private SemanticTokensParams getSemanticTokensParams() {
private static SemanticTokensParams getSemanticTokensParams(IDocument document) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that the document is still a class field, even though many methods are now static and take it as an argument. Should we maybe split the class so that we do not have methods which take a variable as an argument that hides a field in the class?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, let me look into this...

@rubenporras

Copy link
Copy Markdown
Contributor

@BoykoAlex , thanks for the reminder, I was very busy the last days and completely forgot this PR. A have made a new round of comments, all but the last one are very minor, thanks for the changes!

@BoykoAlex

Copy link
Copy Markdown
Contributor Author

@rubenporras I addressed your comments with the exception of the method renaming, i.e. getTokens()... getTokenVisuals()? Not really sure myself of a proper name... getTokensVisualData()?

@BoykoAlex
BoykoAlex requested a review from rubenporras April 9, 2025 18:06
@BoykoAlex
BoykoAlex force-pushed the jdt-sync-syntax-highlighting branch from 02e9c1e to 13cb746 Compare April 10, 2025 14:07
@BoykoAlex

Copy link
Copy Markdown
Contributor Author

Made requested changes and squashed commits

@rubenporras rubenporras left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

@rubenporras
rubenporras merged commit eac934b into eclipse-lsp4e:main Apr 10, 2025
@BoykoAlex

Copy link
Copy Markdown
Contributor Author

@rubenporras @sebthom Thank you for the feedback on this PR - much appreciated!-)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants