Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support CodeAction resolve for Java code actions #299

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions microprofile.jdt/org.eclipse.lsp4mp.jdt.core/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<delegateCommandHandler class="org.eclipse.lsp4mp.jdt.internal.core.ls.MicroProfileDelegateCommandHandlerForJava">
<command id="microprofile/java/fileInfo"/>
<command id="microprofile/java/codeAction"/>
<command id="microprofile/java/codeActionResolve"/>
<command id="microprofile/java/codeLens"/>
<command id="microprofile/java/completion"/>
<command id="microprofile/java/definition"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*******************************************************************************
* Copyright (c) 2022 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lsp4mp.commons;

import java.util.Map;
import java.util.Objects;

import org.eclipse.lsp4j.Range;

/**
* Represents additional data that is needed to resolve a code action.
*
* @author datho7561
*/
public class CodeActionResolveData {

private String participantId;
private String documentUri;

private Range range;

private Map<String, Object> extendedData;

private boolean resourceOperationSupported;
private boolean commandConfigurationUpdateSupported;

/**
* Make code action data will all fields nulled/false
*
* Needed for Gson
*/
public CodeActionResolveData() {
this(null, null, null, null, false, false);
}

public CodeActionResolveData(String documentUri,
String participantId,
Range range,
Map<String, Object> extendedData,
boolean resourceOperationSupported,
boolean commandConfigurationUpdateSupported) {
this.documentUri = documentUri;
this.participantId = participantId;
this.range = range;
this.extendedData = extendedData;
this.resourceOperationSupported = resourceOperationSupported;
this.commandConfigurationUpdateSupported = commandConfigurationUpdateSupported;
}

/**
* Returns the uri of the document that this code action applies to.
*
* @return the uri of the document that this code action applies to
*/
public String getDocumentUri() {
return this.documentUri;
}

/**
* Returns the unique id of the IJavaCodeActionParticipant that can resolve the
* text edits for this code action.
*
* @return the unique id of the IJavaCodeActionParticipant that can resolve the
* text edits for this code action
*/
public String getParticipantId() {
return this.participantId;
}

/**
* Returns the range for which this CodeAction is applicable for.
*
* @return the range for which this CodeAction is applicable for
*/
public Range getRange() {
return this.range;
}

/**
* Returns the entry in the extended data for the given key
*
* @param key the key to get the entry for
* @return the entry in the extended data for the given key
*/
public Object getExtendedDataEntry(String key) {
return extendedData.get(key);
}

/**
* Returns true if the client supports resource operations and false otherwise.
*
* @return true if the client supports resource operations and false otherwise
*/
public boolean isResourceOperationSupported() {
return this.resourceOperationSupported;
}

/**
* Returns true if the client implements a command that allows the language
* server to update preferences, and false otherwise.
*
* @return true if the client implements a command that allows the language
* server to update preferences, and false otherwise
*/
public boolean isCommandConfigurationUpdateSupported() {
return this.commandConfigurationUpdateSupported;
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof CodeActionResolveData)) {
return false;
}
CodeActionResolveData that = (CodeActionResolveData) other;
return Objects.equals(this.documentUri, that.documentUri)
&& Objects.equals(this.participantId, that.participantId)
&& Objects.equals(this.range, that.range)
&& Objects.equals(this.extendedData, that.extendedData)
&& Objects.equals(this.resourceOperationSupported, that.resourceOperationSupported)
&& Objects.equals(this.commandConfigurationUpdateSupported, that.commandConfigurationUpdateSupported);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class MicroProfileJavaCodeActionParams extends CodeActionParams {

private boolean commandConfigurationUpdateSupported;

private boolean resolveSupported;

public MicroProfileJavaCodeActionParams() {
super();
}
Expand Down Expand Up @@ -59,4 +61,12 @@ public void setCommandConfigurationUpdateSupported(boolean commandConfigurationU
this.commandConfigurationUpdateSupported = commandConfigurationUpdateSupported;
}

public boolean isResolveSupported() {
return this.resolveSupported;
}

public void setResolveSupported(boolean resolveSupported) {
this.resolveSupported = resolveSupported;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*******************************************************************************
* Copyright (c) 2019 Red Hat Inc. and others.
Copy link
Contributor

Choose a reason for hiding this comment

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

2022

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This isn't new, I just copied it over from microprofile.ls

*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lsp4mp.commons.utils;

import java.util.HashMap;

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

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;

/**
* Utilities for working with JSON that has been converted to an Object using Gson.
*/
public class JSONUtility {

private JSONUtility() {
}

private static final Gson LSP4J_GSON = new MessageJsonHandler(new HashMap<>()).getGson();

private static final Gson EITHER_GSON = new GsonBuilder() //
.registerTypeAdapterFactory(new EitherTypeAdapter.Factory()).create();

/**
* Converts the given Object to the given class using lsp4j's GSON logic.
*
* @param <T> the class to convert the Object to
* @param object the object to convert
* @param clazz the class to convert the Object to
* @return the given Object converted to the given class using lsp4j's GSON
* logic
*/
public static <T> T toModel(Object object, Class<T> clazz) {
return toModel(getLsp4jGson(), object, clazz);
}

/**
* Converts the given Object to the given class using the given GSON instance.
*
* @param <T> the class to convert the Object to
* @param gson the gson instance to use to perform the conversion
* @param object the object to convert
* @param clazz the class to convert the Object to
* @return the given Object converted to the given class using the given GSON
* instance
*/
public static <T> T toModel(Gson gson, Object object, Class<T> clazz) {
if (object == null) {
return null;
}
if (clazz == null) {
throw new IllegalArgumentException("Class can not be null");
}
if (object instanceof JsonElement) {
return gson.fromJson((JsonElement) object, clazz);
}
if (clazz.isInstance(object)) {
return clazz.cast(object);
}
// if nothing else works, try serializing and deserializing again
return gson.fromJson(gson.toJson(object), clazz);
}

/**
* Returns a Gson instance configured similarly to the instance lsp4j uses.
*
* @return a Gson instance configured similarly to the instance lsp4j uses
*/
public static Gson getLsp4jGson() {
return LSP4J_GSON;
}

/**
* Returns a Gson instance with most of the default options, but with the
* ability to parse {@code org.eclipse.lsp4j.Either}.
*
* @return a Gson instance with most of the default options, but with the
* ability to parse {@code org.eclipse.lsp4j.Either}
*/
public static Gson getEitherGson() {
return EITHER_GSON;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ public List<? extends CodeAction> codeAction(MicroProfileJavaCodeActionParams pa
return codeActionHandler.codeAction(params, utils, monitor);
}

/**
* Returns the codeAction list according the given codeAction parameters.
*
* @param unresolved the CodeAction to resolve
* @param utils the utilities class
* @param monitor the monitor
* @return the codeAction list according the given codeAction parameters.
* @throws JavaModelException
*/
public CodeAction resolveCodeAction(CodeAction unresolved, IJDTUtils utils,
IProgressMonitor monitor) throws JavaModelException {
return codeActionHandler.resolveCodeAction(unresolved, utils, monitor);
}

/**
* Returns the codelens list according the given codelens parameters.
*
Expand Down Expand Up @@ -195,7 +209,8 @@ public CompletionList completion(MicroProfileJavaCompletionParams params, IJDTUt
}

completions.forEach(completion -> {
List<? extends CompletionItem> collectedCompletionItems = completion.collectCompletionItems(completionContext,
List<? extends CompletionItem> collectedCompletionItems = completion.collectCompletionItems(
completionContext,
monitor);
if (collectedCompletionItems != null) {
completionItems.addAll(collectedCompletionItems);
Expand Down Expand Up @@ -291,7 +306,8 @@ public List<PublishDiagnosticsParams> diagnostics(MicroProfileJavaDiagnosticsPar
return publishDiagnostics;
}

private void collectDiagnostics(String uri, IJDTUtils utils, DocumentFormat documentFormat, MicroProfileJavaDiagnosticsSettings settings,
private void collectDiagnostics(String uri, IJDTUtils utils, DocumentFormat documentFormat,
MicroProfileJavaDiagnosticsSettings settings,
List<Diagnostic> diagnostics, IProgressMonitor monitor) {
ITypeRoot typeRoot = resolveTypeRoot(uri, utils, monitor);
if (typeRoot == null) {
Expand Down Expand Up @@ -342,7 +358,8 @@ public Hover hover(MicroProfileJavaHoverParams params, IJDTUtils utils, IProgres
DocumentFormat documentFormat = params.getDocumentFormat();
boolean surroundEqualsWithSpaces = params.isSurroundEqualsWithSpaces();
List<Hover> hovers = new ArrayList<>();
collectHover(uri, typeRoot, hoverElement, utils, hoverPosition, documentFormat, surroundEqualsWithSpaces, hovers, monitor);
collectHover(uri, typeRoot, hoverElement, utils, hoverPosition, documentFormat, surroundEqualsWithSpaces,
hovers, monitor);
if (hovers.isEmpty()) {
return null;
}
Expand Down Expand Up @@ -401,7 +418,8 @@ private IJavaElement getHoveredMethodParameter(IMethod method, int offset) throw
}

private void collectHover(String uri, ITypeRoot typeRoot, IJavaElement hoverElement, IJDTUtils utils,
Position hoverPosition, DocumentFormat documentFormat, boolean surroundEqualsWithSpaces, List<Hover> hovers, IProgressMonitor monitor) {
Position hoverPosition, DocumentFormat documentFormat, boolean surroundEqualsWithSpaces, List<Hover> hovers,
IProgressMonitor monitor) {
// Collect all adapted hover participant
JavaHoverContext context = new JavaHoverContext(uri, typeRoot, utils, hoverElement, hoverPosition,
documentFormat, surroundEqualsWithSpaces);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@
public interface IJavaCodeActionParticipant {

/**
* Returns true if the code actions are adaptable for the given context and false
* Returns the unique identifier of this code action participant.
*
* @return the unique identifier of this code action participant
*/
String getParticipantId();

/**
* Returns true if the code actions are adaptable for the given context and
* false
* otherwise.
*
* <p>
Expand Down Expand Up @@ -59,4 +67,12 @@ default boolean isAdaptedForCodeAction(JavaCodeActionContext context, IProgressM
*/
List<? extends CodeAction> getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic,
IProgressMonitor monitor) throws CoreException;

/**
* Returns the code action with the TextEdits filled in.
*
* @param unresolved the code action to resolve
* @return the code action with the TextEdits filled in
*/
CodeAction resolveCodeAction(JavaCodeActionResolveContext context);
}
Loading