Skip to content

Commit

Permalink
Support java.apply.workspaceEdit
Browse files Browse the repository at this point in the history
Fixes issue#376
This follows exactly same pattern as organizeImports was done.
This should preserve existing behaviour for vscode
  • Loading branch information
theli-ua committed Jun 6, 2023
1 parent 0e04fdd commit ed793ec
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions org.eclipse.jdt.ls.core/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
<command
id="java.edit.organizeImports">
</command>
<command
id="java.apply.workspaceEdit">
</command>
<command
id="java.edit.stringFormatting">
</command>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
// workspaceEdit on the custom command.
return result;
}
case "java.apply.workspaceEdit":
final WorkspaceEdit r = (WorkspaceEdit) JSONUtility.toLsp4jModel(arguments.get(0), WorkspaceEdit.class);

if (JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences()
.isWorkspaceApplyEditSupported()) {
JavaLanguageServerPlugin.getInstance().getClientConnection()
.applyWorkspaceEdit((WorkspaceEdit) r);
// return an empty object to avoid errors on client
return new Object();
} else {
// we are returning a workspace edit here in order to accomodate the clients
// that
// did not implement workspace/applyEdit from LSP. This still allows them to
// implement applying
// workspaceEdit on the custom command.
return r;
}
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.jdt.ls.core.internal;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler;

Expand Down Expand Up @@ -61,6 +62,9 @@ private static <T> T toModel(Gson gson, Object object, Class<T> clazz) {
if (object instanceof String json) {
return gson.fromJson(json, clazz);
}
if (object instanceof Map map) {
return gson.fromJson(gson.toJsonTree(map), clazz);
}
return null;
}
}

0 comments on commit ed793ec

Please sign in to comment.