Skip to content

Commit

Permalink
Move semantic tokens to LSP implementation
Browse files Browse the repository at this point in the history
Signed-off-by: 0dinD <zerodind@gmail.com>
  • Loading branch information
0dinD committed Jun 24, 2021
1 parent 5483c3a commit ca21621
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 223 deletions.
6 changes: 0 additions & 6 deletions org.eclipse.jdt.ls.core/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,6 @@
<command
id="java.project.refreshDiagnostics">
</command>
<command
id="java.project.provideSemanticTokens">
</command>
<command
id="java.project.getSemanticTokensLegend">
</command>
<command
id="java.project.import">
</command>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@
import org.eclipse.jdt.ls.core.internal.commands.ProjectCommand.ClasspathOptions;
import org.eclipse.jdt.ls.core.internal.handlers.FormatterHandler;
import org.eclipse.jdt.ls.core.internal.handlers.ResolveSourceMappingHandler;
import org.eclipse.jdt.ls.core.internal.commands.SemanticTokensCommand;
import org.eclipse.jdt.ls.core.internal.commands.SourceAttachmentCommand;
import org.eclipse.jdt.ls.core.internal.commands.TypeHierarchyCommand;
import org.eclipse.jdt.ls.core.internal.semantictokens.SemanticTokensLegend;
import org.eclipse.lsp4j.ResolveTypeHierarchyItemParams;
import org.eclipse.lsp4j.TextDocumentPositionParams;
import org.eclipse.lsp4j.TypeHierarchyDirection;
Expand Down Expand Up @@ -84,10 +82,6 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
return ProjectCommand.getAllJavaProjects();
case "java.project.refreshDiagnostics":
return DiagnosticsCommand.refreshDiagnostics((String) arguments.get(0), (String) arguments.get(1), (boolean) arguments.get(2));
case "java.project.provideSemanticTokens":
return SemanticTokensCommand.provide((String) arguments.get(0));
case "java.project.getSemanticTokensLegend":
return new SemanticTokensLegend();
case "java.project.import":
ProjectCommand.importProject(monitor);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -38,11 +39,14 @@
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
import org.eclipse.lsp4j.CodeActionOptions;
import org.eclipse.lsp4j.CodeLensOptions;
import org.eclipse.lsp4j.DocumentFilter;
import org.eclipse.lsp4j.DocumentOnTypeFormattingOptions;
import org.eclipse.lsp4j.ExecuteCommandOptions;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4j.InitializeResult;
import org.eclipse.lsp4j.SaveOptions;
import org.eclipse.lsp4j.SemanticTokensServerFull;
import org.eclipse.lsp4j.SemanticTokensWithRegistrationOptions;
import org.eclipse.lsp4j.ServerCapabilities;
import org.eclipse.lsp4j.TextDocumentSyncKind;
import org.eclipse.lsp4j.TextDocumentSyncOptions;
Expand Down Expand Up @@ -178,6 +182,16 @@ public void registerCapabilities(InitializeResult initializeResult) {
wsCapabilities.setWorkspaceFolders(wsFoldersOptions);
capabilities.setWorkspace(wsCapabilities);

SemanticTokensWithRegistrationOptions semanticTokensOptions = new SemanticTokensWithRegistrationOptions();
semanticTokensOptions.setFull(new SemanticTokensServerFull(false));
semanticTokensOptions.setRange(false);
semanticTokensOptions.setDocumentSelector(List.of(
new DocumentFilter("java", "file", null),
new DocumentFilter("java", "jdt", null)
));
semanticTokensOptions.setLegend(SemanticTokensHandler.getLegend());
capabilities.setSemanticTokensProvider(semanticTokensOptions);

initializeResult.setCapabilities(capabilities);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@
import org.eclipse.lsp4j.RenameParams;
import org.eclipse.lsp4j.SelectionRange;
import org.eclipse.lsp4j.SelectionRangeParams;
import org.eclipse.lsp4j.SemanticTokens;
import org.eclipse.lsp4j.SemanticTokensParams;
import org.eclipse.lsp4j.SignatureHelp;
import org.eclipse.lsp4j.SignatureHelpParams;
import org.eclipse.lsp4j.SymbolInformation;
Expand Down Expand Up @@ -455,12 +457,12 @@ public void didChangeConfiguration(DidChangeConfigurationParams params) {
syncCapabilitiesToSettings();
boolean jvmChanged = false;
try {
jvmChanged = jvmConfigurator.configureJVMs(preferenceManager.getPreferences(), this.client);
jvmChanged = JVMConfigurator.configureJVMs(preferenceManager.getPreferences(), this.client);
} catch (Exception e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
}
try {
boolean autoBuildChanged = pm.setAutoBuilding(preferenceManager.getPreferences().isAutobuildEnabled());
boolean autoBuildChanged = ProjectsManager.setAutoBuilding(preferenceManager.getPreferences().isAutobuildEnabled());
if (jvmChanged) {
buildWorkspace(Either.forLeft(true));
} else if (autoBuildChanged) {
Expand Down Expand Up @@ -983,7 +985,7 @@ public CompletableFuture<List<SymbolInformation>> searchSymbols(SearchSymbolPara

@Override
public CompletableFuture<List<CallHierarchyItem>> prepareCallHierarchy(CallHierarchyPrepareParams params) {
logInfo(">> textDocumentt/prepareCallHierarchy");
logInfo(">> textDocument/prepareCallHierarchy");
return computeAsyncWithClientProgress((monitor) -> new CallHierarchyHandler().prepareCallHierarchy(params, monitor));
}

Expand All @@ -999,6 +1001,15 @@ public CompletableFuture<List<CallHierarchyOutgoingCall>> callHierarchyOutgoingC
return computeAsyncWithClientProgress((monitor) -> new CallHierarchyHandler().callHierarchyOutgoingCalls(params, monitor));
}

@Override
public CompletableFuture<SemanticTokens> semanticTokensFull(SemanticTokensParams params) {
logInfo(">> textDocument/semanticTokens/full");
return computeAsync(monitor -> {
waitForLifecycleJobs(monitor);
return SemanticTokensHandler.provide(monitor, params);
});
}

private <R> CompletableFuture<R> computeAsyncWithClientProgress(Function<IProgressMonitor, R> code) {
return CompletableFutures.computeAsync((cc) -> {
IProgressMonitor monitor = progressReporterManager.getProgressReporter(cc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,48 @@
* Microsoft Corporation - initial API and implementation
*******************************************************************************/

package org.eclipse.jdt.ls.core.internal.commands;
package org.eclipse.jdt.ls.core.internal.handlers;

import java.util.Arrays;
import java.util.Collections;
import java.util.stream.Collectors;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.manipulation.CoreASTProvider;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.JobHelpers;
import org.eclipse.jdt.ls.core.internal.handlers.DocumentLifeCycleHandler;
import org.eclipse.jdt.ls.core.internal.semantictokens.SemanticTokens;
import org.eclipse.jdt.ls.core.internal.semantictokens.SemanticTokensVisitor;
import org.eclipse.jdt.ls.core.internal.semantictokens.TokenModifier;
import org.eclipse.jdt.ls.core.internal.semantictokens.TokenType;
import org.eclipse.lsp4j.SemanticTokens;
import org.eclipse.lsp4j.SemanticTokensLegend;
import org.eclipse.lsp4j.SemanticTokensParams;

public class SemanticTokensCommand {
public static SemanticTokens provide(String uri) {
JobHelpers.waitForJobs(DocumentLifeCycleHandler.DOCUMENT_LIFE_CYCLE_JOBS, null);
return doProvide(uri);
}
public class SemanticTokensHandler {

private static SemanticTokens doProvide(String uri) {
ITypeRoot typeRoot = JDTUtils.resolveTypeRoot(uri);
if (typeRoot == null) {
return new SemanticTokens(new int[0]);
public static SemanticTokens provide(IProgressMonitor monitor, SemanticTokensParams params) {
ITypeRoot typeRoot = JDTUtils.resolveTypeRoot(params.getTextDocument().getUri());
if (typeRoot == null || monitor.isCanceled()) {
return new SemanticTokens(Collections.emptyList());
}

CompilationUnit root = CoreASTProvider.getInstance().getAST(typeRoot, CoreASTProvider.WAIT_YES, new NullProgressMonitor());
if (root == null) {
return new SemanticTokens(new int[0]);
if (root == null || monitor.isCanceled()) {
return new SemanticTokens(Collections.emptyList());
}

SemanticTokensVisitor collector = new SemanticTokensVisitor(root);
root.accept(collector);
return collector.getSemanticTokens();
}

public static SemanticTokensLegend getLegend() {
return new SemanticTokensLegend(
Arrays.stream(TokenType.values()).map(TokenType::toString).collect(Collectors.toList()),
Arrays.stream(TokenModifier.values()).map(TokenModifier::toString).collect(Collectors.toList())
);
}

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.eclipse.jdt.core.dom.TagElement;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeLiteral;
import org.eclipse.lsp4j.SemanticTokens;

public class SemanticTokensVisitor extends ASTVisitor {
private CompilationUnit cu;
Expand Down Expand Up @@ -90,9 +91,9 @@ public SemanticTokens getSemanticTokens() {
return new SemanticTokens(encodedTokens());
}

private int[] encodedTokens() {
private List<Integer> encodedTokens() {
int numTokens = tokens.size();
int[] data = new int[numTokens * 5];
List<Integer> data = new ArrayList<>(numTokens * 5);
int currentLine = 0;
int currentColumn = 0;
for (int i = 0; i < numTokens; i++) {
Expand All @@ -111,12 +112,11 @@ private int[] encodedTokens() {
int tokenTypeIndex = token.getTokenType().ordinal();
int tokenModifiers = token.getTokenModifiers();

int offset = i * 5;
data[offset] = deltaLine;
data[offset + 1] = deltaColumn;
data[offset + 2] = token.getLength();
data[offset + 3] = tokenTypeIndex;
data[offset + 4] = tokenModifiers;
data.add(deltaLine);
data.add(deltaColumn);
data.add(token.getLength());
data.add(tokenTypeIndex);
data.add(tokenModifiers);
}
}
return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.TypeParameter;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.lsp4j.SemanticTokenModifiers;

public enum TokenModifier {
// Standard LSP token modifiers, see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_semanticTokens
ABSTRACT("abstract"),
STATIC("static"),
FINAL("readonly"),
DEPRECATED("deprecated"),
DECLARATION("declaration"),
DOCUMENTATION("documentation"),
ABSTRACT(SemanticTokenModifiers.Abstract),
STATIC(SemanticTokenModifiers.Static),
FINAL(SemanticTokenModifiers.Readonly),
DEPRECATED(SemanticTokenModifiers.Deprecated),
DECLARATION(SemanticTokenModifiers.Declaration),
DOCUMENTATION(SemanticTokenModifiers.Documentation),

// Custom token modifiers
PUBLIC("public"),
Expand Down

0 comments on commit ca21621

Please sign in to comment.