Skip to content

Commit

Permalink
Merge pull request #496 from mezarin/resolveCodeActionUpdates
Browse files Browse the repository at this point in the history
Code action resolve activation updates
  • Loading branch information
turkeylurkey authored Dec 12, 2023
2 parents 878e1eb + 4144bda commit d472301
Show file tree
Hide file tree
Showing 19 changed files with 986 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Bundle-SymbolicName: org.eclipse.lsp4jakarta.lsp4e.core;singleton:=true
Bundle-Version: 0.2.1.qualifier
Bundle-Activator: org.eclipse.lsp4jakarta.lsp4e.Activator
Require-Bundle:
com.google.gson,
org.eclipse.lsp4e,
org.eclipse.core.runtime,
org.eclipse.ui.workbench,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,14 @@ public Object getInitializationOptions(URI rootUri) {
trace.put("server", "verbose");
tools.put("trace", trace);
jakarta.put("tools", tools);
settings.put("jakararta", jakarta);
settings.put("jakarta", jakarta);
root.put("settings", settings);

// Set extended capabilities.
Map<String, Object> extendedClientCapabilities = new HashMap<>();
extendedClientCapabilities.put("shouldLanguageServerExitOnShutdown", Boolean.TRUE);
root.put("extendedClientCapabilities", extendedClientCapabilities);

return root;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2022 IBM Corporation and others.
* Copyright (c) 2020, 2023 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -35,6 +35,8 @@
import org.eclipse.lsp4jakarta.commons.JakartaJavaProjectLabelsParams;
import org.eclipse.lsp4jakarta.commons.JavaCursorContextResult;
import org.eclipse.lsp4jakarta.commons.ProjectLabelInfoEntry;
import org.eclipse.lsp4jakarta.commons.codeaction.CodeActionResolveData;
import org.eclipse.lsp4jakarta.commons.utils.JSONUtility;
import org.eclipse.lsp4jakarta.jdt.core.ProjectLabelManager;
import org.eclipse.lsp4jakarta.jdt.core.PropertiesManagerForJava;
import org.eclipse.lsp4jakarta.jdt.internal.core.ls.JDTUtilsLSImpl;
Expand All @@ -56,6 +58,9 @@ public boolean isCanceled() {
return monitor;
}

/**
* {@inheritDoc}
*/
@Override
public CompletableFuture<JakartaJavaCompletionResult> getJavaCompletion(JakartaJavaCompletionParams javaParams) {
return CompletableFutures.computeAsync(cancelChecker -> {
Expand All @@ -74,6 +79,9 @@ public CompletableFuture<JakartaJavaCompletionResult> getJavaCompletion(JakartaJ
});
}

/**
* {@inheritDoc}
*/
@Override
public CompletableFuture<List<ProjectLabelInfoEntry>> getAllJavaProjectLabels() {
return CompletableFutures.computeAsync((cancelChecker) -> {
Expand All @@ -82,6 +90,9 @@ public CompletableFuture<List<ProjectLabelInfoEntry>> getAllJavaProjectLabels()
});
}

/**
* {@inheritDoc}
*/
@Override
public CompletableFuture<ProjectLabelInfoEntry> getJavaProjectLabels(JakartaJavaProjectLabelsParams javaParams) {
return CompletableFutures.computeAsync((cancelChecker) -> {
Expand All @@ -91,6 +102,9 @@ public CompletableFuture<ProjectLabelInfoEntry> getJavaProjectLabels(JakartaJava
});
}

/**
* {@inheritDoc}
*/
@Override
public CompletableFuture<JakartaJavaFileInfo> getJavaFileInfo(JakartaJavaFileInfoParams javaParams) {
return CompletableFutures.computeAsync(cancelChecker -> {
Expand All @@ -99,6 +113,9 @@ public CompletableFuture<JakartaJavaFileInfo> getJavaFileInfo(JakartaJavaFileInf
});
}

/**
* {@inheritDoc}
*/
@Override
public CompletableFuture<List<PublishDiagnosticsParams>> getJavaDiagnostics(
JakartaJavaDiagnosticsParams javaParams) {
Expand All @@ -113,6 +130,9 @@ public CompletableFuture<List<PublishDiagnosticsParams>> getJavaDiagnostics(
});
}

/**
* {@inheritDoc}
*/
@Override
public CompletableFuture<List<CodeAction>> getJavaCodeAction(JakartaJavaCodeActionParams javaParams) {
return CompletableFutures.computeAsync((cancelChecker) -> {
Expand All @@ -126,17 +146,21 @@ public CompletableFuture<List<CodeAction>> getJavaCodeAction(JakartaJavaCodeActi
});
}

/**
* {@inheritDoc}
*/
@Override
public CompletableFuture<CodeAction> resolveCodeAction(CodeAction unresolved) {
return CompletableFutures.computeAsync((cancelChecker) -> {
IProgressMonitor monitor = getProgressMonitor(cancelChecker);
try {
CodeActionResolveData resolveData = JSONUtility.toModel(unresolved.getData(), CodeActionResolveData.class);
unresolved.setData(resolveData);
return (CodeAction) PropertiesManagerForJava.getInstance().resolveCodeAction(unresolved,
JDTUtilsLSImpl.getInstance(), monitor);
} catch (JavaModelException e) {
return null;
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Bundle-ActivationPolicy: lazy
Bundle-ClassPath: .
Export-Package:
org.eclipse.lsp4jakarta.commons,
org.eclipse.lsp4jakarta.commons.codeaction,
org.eclipse.lsp4jakarta.commons.utils,
org.eclipse.lsp4jakarta.jdt.core,
org.eclipse.lsp4jakarta.jdt.core.utils,
org.eclipse.lsp4jakarta.jdt.internal.core.ls
7 changes: 0 additions & 7 deletions jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@
<comments>Eclipse Public License 2.0</comments>
</license>
</licenses>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 IBM Corporation and others.
* Copyright (c) 2020, 2023 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -15,13 +15,15 @@

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import org.eclipse.lsp4j.CompletionOptions;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4j.InitializeResult;
import org.eclipse.lsp4j.InitializedParams;
import org.eclipse.lsp4j.ServerCapabilities;
import org.eclipse.lsp4j.TextDocumentSyncKind;
import org.eclipse.lsp4j.jsonrpc.CompletableFutures;
import org.eclipse.lsp4j.services.LanguageClient;
import org.eclipse.lsp4j.services.LanguageServer;
Expand All @@ -35,73 +37,145 @@
import org.eclipse.lsp4jakarta.ls.api.JakartaJavaProjectLabelsProvider;
import org.eclipse.lsp4jakarta.ls.api.JakartaLanguageClientAPI;
import org.eclipse.lsp4jakarta.ls.commons.ParentProcessWatcher.ProcessLanguageServer;
import org.eclipse.lsp4jakarta.ls.commons.client.ExtendedClientCapabilities;
import org.eclipse.lsp4jakarta.ls.commons.client.InitializationOptionsExtendedClientCapabilities;
import org.eclipse.lsp4jakarta.ls.java.JakartaTextDocuments;

import org.eclipse.lsp4jakarta.settings.AllJakartaSettings;
import org.eclipse.lsp4jakarta.settings.InitializationOptionsSettings;
import org.eclipse.lsp4jakarta.settings.JakartaGeneralClientSettings;
import org.eclipse.lsp4jakarta.settings.JakartaTraceSettings;
import org.eclipse.lsp4jakarta.settings.SharedSettings;
import org.eclipse.lsp4jakarta.settings.capabilities.JakartaCapabilityManager;
import org.eclipse.lsp4jakarta.settings.capabilities.ServerCapabilitiesInitializer;

/**
* Jakarta Language server.
*/
public class JakartaLanguageServer implements LanguageServer, ProcessLanguageServer, JakartaJavaProjectLabelsProvider, JakartaJavaFileInfoProvider {

private Integer parentProcessId;

private static final Logger LOGGER = Logger.getLogger(JakartaLanguageServer.class.getName());

private final WorkspaceService workspaceService;
private final TextDocumentService textDocumentService;
private final JakartaTextDocumentService textDocumentService;
private final JakartaTextDocuments javaDocuments;
private final SharedSettings sharedSettings;

private JakartaLanguageClientAPI languageClient;
private JakartaCapabilityManager capabilityManager;

/**
* Constructor
*/
public JakartaLanguageServer() {
// Workspace service handles workspace settings changes and calls update
// settings.
javaDocuments = new JakartaTextDocuments(this, this);
workspaceService = new JakartaWorkspaceService(this);
textDocumentService = new JakartaTextDocumentService(this, javaDocuments);
javaDocuments = new JakartaTextDocuments(this, this);
sharedSettings = new SharedSettings();
textDocumentService = new JakartaTextDocumentService(this, sharedSettings, javaDocuments);
}

/**
* {@inheritDoc}
*/
@Override
public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
LOGGER.info("Initializing Jakarta EE server");
LOGGER.info("Initializing Jakarta EE server using: " + System.getProperty("java.home"));
this.parentProcessId = params.getProcessId();
ServerCapabilities serverCapabilities = new ServerCapabilities();
serverCapabilities.setTextDocumentSync(TextDocumentSyncKind.Incremental);

// Consume the capabilities supported by the client.
ExtendedClientCapabilities extendedClientCapabilities = InitializationOptionsExtendedClientCapabilities.getExtendedClientCapabilities(params);
capabilityManager.setClientCapabilities(params.getCapabilities(), extendedClientCapabilities);
updateSettings(InitializationOptionsSettings.getSettings(params));
textDocumentService.updateClientCapabilities(params.getCapabilities(), extendedClientCapabilities);

// Send the capabilities supported by the sever to the client.
ServerCapabilities serverCapabilities = ServerCapabilitiesInitializer.getNonDynamicServerCapabilities(capabilityManager.getClientCapabilities());
InitializeResult initializeResult = new InitializeResult(serverCapabilities);
// Provide Completion Capability to the LS
initializeResult.getCapabilities().setCompletionProvider(new CompletionOptions(false, null));
initializeResult.getCapabilities().setHoverProvider(true);
initializeResult.getCapabilities().setCodeActionProvider(true);

return CompletableFuture.completedFuture(initializeResult);
}

/**
* Registers all capabilities that do not support client side preferences to
* turn on/off
*
* (non-Javadoc)
*
* @see org.eclipse.lsp4j.services.LanguageServer#initialized(org.eclipse.lsp4j.
* InitializedParams)
*/
@Override
public void initialized(InitializedParams params) {
capabilityManager.initializeCapabilities();
}

/**
* Update Jakarta settings configured by the client defined by the client flowing requests between
* the LS and JDT extensions.
*
* @param initializationOptionsSettings the Jakarta settings
*/
public synchronized void updateSettings(Object initializationOptionsSettings) {
if (initializationOptionsSettings == null) {
return;
}
// TODO: else update settings

initializationOptionsSettings = AllJakartaSettings.getJakartaToolsSettings(initializationOptionsSettings);
JakartaGeneralClientSettings clientSettings = JakartaGeneralClientSettings.getGeneralJakartaSettings(initializationOptionsSettings);
if (clientSettings != null) {
JakartaTraceSettings newTrace = clientSettings.getTrace();
if (newTrace != null) {
textDocumentService.updateTraceSettings(newTrace);
}
}
}

/**
* {@inheritDoc}
*/
@Override
public CompletableFuture<Object> shutdown() {
// when shutting down LS, TextDocumentService.didClose() may not be called
// properly, need to clear existing diagnostics
((JakartaTextDocumentService) textDocumentService).cleanDiagnostics();
// Perform some clean up. During shutdown, TextDocumentService.didClose() may not be called properly.
textDocumentService.cleanDiagnostics();

// If requested by the client, on shutdown (i.e. last file closed), shutdown the language server.
if (capabilityManager.getClientCapabilities().shouldLanguageServerExitOnShutdown()) {
LOGGER.info("Jakarta EE server is shutting down");
ScheduledExecutorService delayer = Executors.newScheduledThreadPool(1);
delayer.schedule(() -> exit(0), 1, TimeUnit.SECONDS);
}

return CompletableFutures.computeAsync(cc -> new Object());
}

/**
* {@inheritDoc}
*/
@Override
public void exit() {
exit(0);
}

/**
* {@inheritDoc}
*/
@Override
public void exit(int exitCode) {
System.exit(exitCode);
}

/**
* {@inheritDoc}
*/
@Override
public TextDocumentService getTextDocumentService() {
return this.textDocumentService;
}

/**
* {@inheritDoc}
*/
@Override
public WorkspaceService getWorkspaceService() {
return this.workspaceService;
Expand All @@ -113,27 +187,48 @@ public JakartaLanguageClientAPI getLanguageClient() {

public void setLanguageClient(LanguageClient languageClient) {
this.languageClient = (JakartaLanguageClientAPI) languageClient;
this.capabilityManager = new JakartaCapabilityManager(languageClient);
}

/**
* {@inheritDoc}
*/
@Override
public long getParentProcessId() {
return parentProcessId != null ? parentProcessId : 0;
}

/**
* {@inheritDoc}
*/
@Override
public CompletableFuture<ProjectLabelInfoEntry> getJavaProjectLabels(
JakartaJavaProjectLabelsParams javaParams) {
return getLanguageClient().getJavaProjectLabels(javaParams);
}

/**
* {@inheritDoc}
*/
@Override
public CompletableFuture<List<ProjectLabelInfoEntry>> getAllJavaProjectLabels() {
return getLanguageClient().getAllJavaProjectLabels();
}

/**
* {@inheritDoc}
*/
@Override
public CompletableFuture<JakartaJavaFileInfo> getJavaFileInfo(JakartaJavaFileInfoParams javaParams) {
return getLanguageClient().getJavaFileInfo(javaParams);
}

/**
* Returns the object that manages dynamic capabilities.
*
* @return The object that manages dynamic capabilities.
*/
public JakartaCapabilityManager getCapabilityManager() {
return capabilityManager;
}
}
Loading

0 comments on commit d472301

Please sign in to comment.