Skip to content

Commit

Permalink
Improve performance of code actions, and certain delegate commands
Browse files Browse the repository at this point in the history
- Handle paste event & smart semicolon detection synchronously (main thread)
- Permit only a single code action to be handled at once
A related issue - #2799

Signed-off-by: Snjezana Peco <snjezana.peco@redhat.com>
  • Loading branch information
snjeza authored and rgrunber committed Aug 22, 2023
1 parent 0377b4c commit a01129c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
* A delegate that extends the core language server to execute command
*/
public interface IDelegateCommandHandler {
/**
* the command ID for paste event
*/
String JAVA_EDIT_HANDLE_PASTE_EVENT = "java.edit.handlePasteEvent";
/**
* the command ID for smart semicolon detection
*/
String JAVA_EDIT_SMART_SEMICOLON_DETECTION = "java.edit.smartSemicolonDetection";

/**
* Language server to execute commands. One handler can handle multiple
* commands.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@

public class JDTDelegateCommandHandler implements IDelegateCommandHandler {


/* (non-Javadoc)
* @see org.eclipse.jdt.ls.core.internal.IDelegateCommandHandler#executeCommand(java.lang.String, java.util.List, org.eclipse.core.runtime.IProgressMonitor)
*/
Expand All @@ -76,7 +75,7 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
case "java.edit.stringFormatting":
FormatterHandler handler = new FormatterHandler(JavaLanguageServerPlugin.getPreferencesManager());
return handler.stringFormatting((String) arguments.get(0), JSONUtility.toModel(arguments.get(1), Map.class), Integer.parseInt((String) arguments.get(2)), monitor);
case "java.edit.handlePasteEvent":
case JAVA_EDIT_HANDLE_PASTE_EVENT:
return PasteEventHandler.handlePasteEvent(JSONUtility.toLsp4jModel(arguments.get(0), PasteEventParams.class), monitor);
case "java.project.resolveSourceAttachment":
return SourceAttachmentCommand.resolveSourceAttachment(arguments, monitor);
Expand Down Expand Up @@ -172,7 +171,7 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
} catch (URISyntaxException e) {
return false;
}
case "java.edit.smartSemicolonDetection":
case JAVA_EDIT_SMART_SEMICOLON_DETECTION:
if (!JavaLanguageServerPlugin.getPreferencesManager().getPreferences().isSmartSemicolonDetection()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.ls.core.internal.BaseJDTLanguageServer;
import org.eclipse.jdt.ls.core.internal.BuildWorkspaceStatus;
import org.eclipse.jdt.ls.core.internal.IDelegateCommandHandler;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.JSONUtility;
import org.eclipse.jdt.ls.core.internal.JVMConfigurator;
Expand Down Expand Up @@ -186,6 +187,7 @@ public class JDTLanguageServer extends BaseJDTLanguageServer implements Language
*/
private ServiceStatus status;
private TelemetryManager telemetryManager;
private Object codeActionLock = new Object();

private Job shutdownJob = new Job("Shutdown...") {

Expand Down Expand Up @@ -595,9 +597,16 @@ public void didChangeWatchedFiles(DidChangeWatchedFilesParams params) {
@Override
public CompletableFuture<Object> executeCommand(ExecuteCommandParams params) {
debugTrace(">> workspace/executeCommand " + (params == null ? null : params.getCommand()));
return computeAsync((monitor) -> {
return commandHandler.executeCommand(params, monitor);
});
// see https://github.com/eclipse-jdtls/eclipse.jdt.ls/issues/2799
// Handle the paste event synchronously in the main thread
if (IDelegateCommandHandler.JAVA_EDIT_HANDLE_PASTE_EVENT.equals(params.getCommand()) || IDelegateCommandHandler.JAVA_EDIT_SMART_SEMICOLON_DETECTION.equals(params.getCommand())) {
Object result = commandHandler.executeCommand(params, new NullProgressMonitor());
return CompletableFuture.completedFuture(result);
} else {
return computeAsync((monitor) -> {
return commandHandler.executeCommand(params, monitor);
});
}
}

/* (non-Javadoc)
Expand Down Expand Up @@ -741,7 +750,11 @@ public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(CodeActio
CodeActionHandler handler = new CodeActionHandler(this.preferenceManager);
return computeAsync((monitor) -> {
waitForLifecycleJobs(monitor);
return handler.getCodeActionCommands(params, monitor);
// see https://github.com/eclipse-jdtls/eclipse.jdt.ls/issues/2799
// Optimize the performance of the code actions
synchronized (codeActionLock) {
return handler.getCodeActionCommands(params, monitor);
}
});
}

Expand Down

0 comments on commit a01129c

Please sign in to comment.