Skip to content

Commit

Permalink
Merge pull request #515 from vaisakhkannan/#29-PostConstructReturnTyp…
Browse files Browse the repository at this point in the history
…e-QuickFix

Fixes #513 - Implement code action for the QuickFix class PostConstructReturnTypeQuickFix.
  • Loading branch information
vaisakhkannan authored Oct 19, 2023
2 parents fa36527 + d2f708b commit a636bd8
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,22 @@
import com.intellij.psi.util.PsiTreeUtil;
import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.Messages;
import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.ModifyReturnTypeProposal;
import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.ExtendedCodeAction;
import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.IJavaCodeActionParticipant;
import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionContext;
import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionResolveContext;
import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal;
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.CodeActionKind;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4mp.commons.CodeActionResolveData;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Quick fix for AnnotationDiagnosticsCollector that changes the return type of a method to void.
Expand All @@ -33,19 +42,57 @@
* @author Yijia Jing
*
*/
public class PostConstructReturnTypeQuickFix {
public class PostConstructReturnTypeQuickFix implements IJavaCodeActionParticipant {

private static final Logger LOGGER = Logger.getLogger(PostConstructReturnTypeQuickFix.class.getName());
private final static String TITLE_MESSAGE = Messages.getMessage("ChangeReturnTypeToVoid");

@Override
public String getParticipantId() {
return PostConstructReturnTypeQuickFix.class.getName();
}

public List<? extends CodeAction> getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic) {
List<CodeAction> codeActions = new ArrayList<>();
PsiElement node = context.getCoveredNode();
PsiMethod parentType = getBinding(node);
String name = Messages.getMessage("ChangeReturnTypeToVoid");
ChangeCorrectionProposal proposal = new ModifyReturnTypeProposal(name, context.getSource().getCompilationUnit(),
context.getASTRoot(), parentType, 0, PsiPrimitiveType.VOID);
CodeAction codeAction = context.convertToCodeAction(proposal, diagnostic);
codeActions.add(codeAction);
final PsiElement node = context.getCoveredNode();
final PsiMethod parentType = getBinding(node);

if (parentType != null) {
codeActions.add(createCodeAction(context, diagnostic));
}
return codeActions;
}

@Override
public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) {
final CodeAction toResolve = context.getUnresolved();
final PsiElement node = context.getCoveredNode();
final PsiMethod parentType = getBinding(node);

assert parentType != null;
ChangeCorrectionProposal proposal = new ModifyReturnTypeProposal(TITLE_MESSAGE, context.getSource().getCompilationUnit(),
context.getASTRoot(), parentType, 0, PsiPrimitiveType.VOID);
try {
WorkspaceEdit we = context.convertToWorkspaceEdit(proposal);
toResolve.setEdit(we);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action to change return type to void", e);
}
return toResolve;
}

private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic) {
ExtendedCodeAction codeAction = new ExtendedCodeAction(TITLE_MESSAGE);
codeAction.setRelevance(0);
codeAction.setDiagnostics(Collections.singletonList(diagnostic));
codeAction.setKind(CodeActionKind.QuickFix);
codeAction.setData(new CodeActionResolveData(context.getUri(), getParticipantId(),
context.getParams().getRange(), Collections.emptyMap(),
context.getParams().isResourceOperationSupported(),
context.getParams().isCommandConfigurationUpdateSupported()));
return codeAction;
}

protected PsiMethod getBinding(PsiElement node) {
if (node instanceof PsiMethod) {
return (PsiMethod) node;
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@
targetDiagnostic="jakarta-annotations#MissingResourceNameAttribute"
implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.annotations.AddResourceMissingNameQuickFix"/>

<javaCodeActionParticipant kind="quickfix"
group="jakarta"
targetDiagnostic="jakarta-annotations#PostConstructReturnType"
implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.annotations.PostConstructReturnTypeQuickFix"/>

<configSourceProvider
implementation="io.openliberty.tools.intellij.lsp4mp4ij.psi.internal.core.providers.MicroProfileConfigSourceProvider"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4jakarta.commons.JakartaDiagnosticsParams;
import org.eclipse.lsp4jakarta.commons.JakartaJavaCodeActionParams;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -64,18 +63,53 @@ public void GeneratedAnnotation() throws Exception {

assertJavaDiagnostics(diagnosticsParams, utils, d1, d2, d3);

JakartaJavaCodeActionParams codeActionParams2 = createCodeActionParams(uri, d1);
TextEdit te3 = te(0, 0, 31, 1, "package io.openliberty.sample.jakarta.annotations;\n" +
"\n" +
"import jakarta.annotation.PostConstruct;\n" +
"import jakarta.annotation.Resource;\n" +
"\n" +
"@Resource(type = Object.class, name = \"aa\")\n" +
"public class PostConstructAnnotation {\n" +
"\n" +
" private Integer studentId;\n" +
"\n" +
" private boolean isHappy;\n" +
"\n" +
" private boolean isSad;\n" +
"\n" +
" @PostConstruct()\n" +
" public void getStudentId() {\n" +
" return this.studentId;\n" +
" }\n" +
"\n" +
" @PostConstruct\n" +
" public void getHappiness(String type) {\n" +
"\n" +
" }\n" +
"\n" +
" @PostConstruct\n" +
" public void throwTantrum() throws Exception {\n" +
" System.out.println(\"I'm sad\");\n" +
" }\n" +
"\n" +
" private String emailAddress;\n" +
"\n" +
"}");
CodeAction ca3 = ca(uri, "Change return type to void", d1, te3);
assertJavaCodeAction(codeActionParams2, utils, ca3);

// TODO : Enable the remaining test cases once the refactoring is completed.

if (CHECK_CODE_ACTIONS) {

JakartaJavaCodeActionParams codeActionParams1 = createCodeActionParams(uri, d2);
TextEdit te1 = te(19, 4, 20, 4, "");
TextEdit te2 = te(20, 29, 20, 40, "");
CodeAction ca1 = ca(uri, "Remove @PostConstruct", d2, te1);
CodeAction ca2 = ca(uri, "Remove all parameters", d2, te2);
assertJavaCodeAction(codeActionParams1, utils, ca1, ca2);

JakartaJavaCodeActionParams codeActionParams2 = createCodeActionParams(uri, d1);
TextEdit te3 = te(15, 11, 15, 18, "void");
CodeAction ca3 = ca(uri, "Change return type to void", d1, te3);
assertJavaCodeAction(codeActionParams2, utils, ca3);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ public void throwTantrum() throws Exception {

private String emailAddress;

}
}

0 comments on commit a636bd8

Please sign in to comment.