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

Merge code-action-unification branch into main #538

Merged
merged 34 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
147e3cd
Proof-of-concept for unifying the design for MicroProfile and Jakarta…
mrglavas Jul 27, 2023
ae06e56
[issue24_refactor_httpservlet]: Refactored HTTPServletQuickFix
Rahul-Biju Oct 4, 2023
2018767
Enable builds for code-action-unification feature branch
TrevCraw Oct 5, 2023
8132c8e
Resolve functional issues with ExtendClassProposal. This fixes an NPE…
mrglavas Oct 11, 2023
b6c6802
[#24] Implement Code Action Participant in HttpServletQuickFix
Rahul-Biju Oct 16, 2023
636e2fc
[#517] Implement Code Action Participant in HttpServletQuickFix
Rahul-Biju Oct 17, 2023
e3c33e7
added spaceing.
Rahul-Biju Oct 17, 2023
4ede496
Refactored the InsertAnnotationQuickfix class
aparnamichael Sep 29, 2023
8d54ef9
Added extension point in plugin.xml for AddPathParamsAnnotation
aparnamichael Sep 29, 2023
6d50b78
enabled the code action test case and updated test case
aparnamichael Sep 29, 2023
2827d77
Enabled quick fix testcase and updated the expected value
aparnamichael Oct 4, 2023
c597525
Added extension point for AddResourceMissingName and AddResourceMissi…
aparnamichael Oct 9, 2023
a7c9fd0
Formatted the newly added code and added back the removed comment
aparnamichael Oct 9, 2023
41bac1e
updated the resource annotation test
aparnamichael Oct 17, 2023
42519fb
[#522]: Refactoring and enabling tests on RemoveModifierConflictQuickFix
dessina-devasia Oct 11, 2023
8868624
[Fixes #522]: Resolved the issues in RemoveModifierConflictQuickFix a…
dessina-devasia Oct 17, 2023
6f0d509
Reverted changes in JakartaCodeActionHandler.java
dessina-devasia Oct 18, 2023
3326cd8
[OpenLiberty#513] - Implemented code action for the QuickFix class Po…
vaisakhkannan Oct 4, 2023
b568431
[OpenLiberty#513] - Reverting the changes done for GeneratedAnnotatio…
vaisakhkannan Oct 5, 2023
b0235a3
Updated source PsiFile parameter
vaisakhkannan Oct 13, 2023
18b34b3
[#517] fixes the plugin in HttpServletQuickFix.
Rahul-Biju Oct 20, 2023
29bec0d
Transforming Quickfixes related to AddConstructorProposal #514
anusreelakshmi934 Oct 4, 2023
5abdd08
Changes completed #514
anusreelakshmi934 Oct 10, 2023
d4be8d6
Formatted the code and addressed review comments #514
anusreelakshmi934 Oct 10, 2023
cf23423
Moved the checks into getCodeActions() #514
anusreelakshmi934 Oct 18, 2023
dbed823
conflicts
anusreelakshmi934 Oct 19, 2023
fd638df
Revert "conflicts"
anusreelakshmi934 Oct 19, 2023
7b2033a
conflicts
anusreelakshmi934 Oct 19, 2023
9e427db
Moved the createCodeAction method to JDTUtils
aparnamichael Oct 16, 2023
31911e7
Removed duplicate implementation of createCodeAction
aparnamichael Oct 18, 2023
2ec5274
Removed duplicate createCodeAction method
aparnamichael Oct 19, 2023
51200ae
removed create code action method from quick fix class
Rahul-Biju Oct 20, 2023
30c21a5
removed the extension points
Rahul-Biju Oct 20, 2023
ccbf815
added spaces in between extension point
Rahul-Biju Oct 20, 2023
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
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: '**'
pull_request:
branches: [ main ]
branches: [ main, code-action-unification ]

jobs:
build:
Expand Down Expand Up @@ -62,4 +62,4 @@ jobs:
./**/*liberty-tools-intellij*.zip
./**/libs/*liberty-tools-intellij*.jar
if-no-files-found: warn
retention-days: 7
retention-days: 7
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiJavaFile;
import com.intellij.psi.PsiMethod;
import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.ExtendedCodeAction;
import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionContext;
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.CodeActionKind;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4mp.commons.CodeActionResolveData;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class JDTUtils {
Expand Down Expand Up @@ -73,4 +80,26 @@ public static List<PsiMethod> getFieldAccessors(PsiJavaFile unit, PsiField field
}
return accessors;
}

/**
* Returns CodeAction object, which contains details of quickfix.
*
* @param context JavaCodeActionContext
* @param diagnostic diagnostic message
* @param quickFixMessage quickfix message
* @param participantId participant id
* @return CodeAction
*/
public static CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic,
String quickFixMessage, String participantId) {
ExtendedCodeAction codeAction = new ExtendedCodeAction(quickFixMessage);
codeAction.setRelevance(0);
codeAction.setDiagnostics(Collections.singletonList(diagnostic));
codeAction.setKind(CodeActionKind.QuickFix);
codeAction.setData(new CodeActionResolveData(context.getUri(), participantId,
context.getParams().getRange(), Collections.emptyMap(),
context.getParams().isResourceOperationSupported(),
context.getParams().isCommandConfigurationUpdateSupported()));
return codeAction;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiTreeUtil;
import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.JakartaCodeActionHandler;
import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.diagnostics.DiagnosticsHandler;
import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.utils.IPsiUtils;
import io.openliberty.tools.intellij.lsp4mp4ij.psi.internal.core.java.codeaction.CodeActionHandler;
import io.openliberty.tools.intellij.lsp4mp4ij.psi.internal.core.ls.PsiUtilsLSImpl;
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.PublishDiagnosticsParams;
import org.eclipse.lsp4jakarta.commons.*;
import org.eclipse.lsp4mp.commons.MicroProfileJavaCodeActionParams;
import org.eclipse.lsp4mp.commons.MicroProfileJavaDiagnosticsParams;
import org.eclipse.lsp4mp.commons.MicroProfileJavaDiagnosticsSettings;
import org.slf4j.Logger;
Expand All @@ -51,12 +52,13 @@ public static PropertiesManagerForJakarta getInstance() {
return INSTANCE;
}

private JakartaCodeActionHandler codeActionHandler = new JakartaCodeActionHandler();
private List<DiagnosticsCollector> diagnosticsCollectors = new ArrayList<>();
private final CodeActionHandler codeActionHandler;

private final DiagnosticsHandler diagnosticsHandler;

private PropertiesManagerForJakarta() {
codeActionHandler = new JakartaCodeActionHandler();
codeActionHandler = new CodeActionHandler("jakarta");
diagnosticsHandler = new DiagnosticsHandler("jakarta");
}

Expand Down Expand Up @@ -346,7 +348,17 @@ private static PsiFile resolveTypeRoot(String uri, Project project) {

public List<CodeAction> getCodeAction(JakartaJavaCodeActionParams params, IPsiUtils utils) {
return ApplicationManager.getApplication().runReadAction((Computable<List<CodeAction>>) () -> {
return codeActionHandler.codeAction(params, utils);
final List<? extends CodeAction> unresolvedCodeActions = codeActionHandler.codeAction(adapt(params), utils);
if (unresolvedCodeActions != null) {
final List<CodeAction> resolvedCodeActions = new ArrayList<>(unresolvedCodeActions.size());
unresolvedCodeActions.forEach(unresolvedCodeAction -> {
if (unresolvedCodeAction != null) {
resolvedCodeActions.add(codeActionHandler.resolveCodeAction(unresolvedCodeAction, utils));
}
});
return resolvedCodeActions;
}
return null;
});
}

Expand All @@ -356,4 +368,11 @@ private MicroProfileJavaDiagnosticsParams adapt(JakartaDiagnosticsParams params)
mpParams.setDocumentFormat(params.getDocumentFormat());
return mpParams;
}

private MicroProfileJavaCodeActionParams adapt(JakartaJavaCodeActionParams params) {
MicroProfileJavaCodeActionParams mpParams = new MicroProfileJavaCodeActionParams(params.getTextDocument(), params.getRange(), params.getContext());
mpParams.setResourceOperationSupported(params.isResourceOperationSupported());
mpParams.setResolveSupported(true);
return mpParams;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,25 @@
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiPrimitiveType;
import com.intellij.psi.util.PsiTreeUtil;
import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.JDTUtils;
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 +43,45 @@
* @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(JDTUtils.createCodeAction(context, diagnostic, TITLE_MESSAGE, getParticipantId()));
}
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;
}

protected PsiMethod getBinding(PsiElement node) {
if (node instanceof PsiMethod) {
return (PsiMethod) node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,25 @@

import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiModifierListOwner;
import com.intellij.psi.util.PsiTreeUtil;
import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.Messages;
import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.AddConstructorProposal;
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;

/**
*
Expand All @@ -37,57 +44,70 @@
*
*/

public class ManagedBeanNoArgConstructorQuickFix {
public class ManagedBeanNoArgConstructorQuickFix implements IJavaCodeActionParticipant {
private static final Logger LOGGER = Logger.getLogger(ManagedBeanNoArgConstructorQuickFix.class.getName());

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

public List<? extends CodeAction> getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic) {
PsiElement node = context.getCoveredNode();
PsiClass parentType = getBinding(node);

if (parentType != null) {
List<CodeAction> codeActions = new ArrayList<>();
return addConstructor(diagnostic, context);
}
return Collections.emptyList();
}

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

codeActions.addAll(addConstructor(diagnostic, context, parentType));
String constructorName = toResolve.getTitle();
ChangeCorrectionProposal proposal = new AddConstructorProposal(constructorName,
context.getSource().getCompilationUnit(), context.getASTRoot(), parentType, 0,
constructorName.equals(Messages.getMessage("AddProtectedConstructor")) ? "protected" : "public");

return codeActions;
try {
WorkspaceEdit we = context.convertToWorkspaceEdit(proposal);
toResolve.setEdit(we);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Unable to create workspace edit for code actions to add constructors", e);
}
return Collections.emptyList();

return toResolve;
}

protected PsiClass getBinding(PsiElement node) {
return PsiTreeUtil.getParentOfType(node, PsiClass.class);
}

private List<CodeAction> addConstructor(Diagnostic diagnostic, JavaCodeActionContext context, PsiClass notUsed) {
private List<CodeAction> addConstructor(Diagnostic diagnostic, JavaCodeActionContext context) {
List<CodeAction> codeActions = new ArrayList<>();
JavaCodeActionContext targetContext = null;
PsiElement node = null;
PsiClass parentType = null;

// option for protected constructor
targetContext = context.copy();
node = targetContext.getCoveredNode();
parentType = getBinding(node);
String name = Messages.getMessage("AddProtectedConstructor");
ChangeCorrectionProposal proposal = new AddConstructorProposal(name,
targetContext.getSource().getCompilationUnit(), targetContext.getASTRoot(), parentType, 0);
CodeAction codeAction = targetContext.convertToCodeAction(proposal, diagnostic);

if (codeAction != null) {
codeActions.add(codeAction);
}
String[] constructorNames = {Messages.getMessage("AddProtectedConstructor"), Messages.getMessage("AddPublicConstructor")};

// option for public constructor
targetContext = context.copy();
node = targetContext.getCoveredNode();
parentType = getBinding(node);
name = Messages.getMessage("AddPublicConstructor");
proposal = new AddConstructorProposal(name,
targetContext.getSource().getCompilationUnit(), targetContext.getASTRoot(), parentType, 0, "public");
codeAction = targetContext.convertToCodeAction(proposal, diagnostic);

if (codeAction != null) {
for (String name : constructorNames) {
CodeAction codeAction = createCodeAction(context, diagnostic, name);
codeActions.add(codeAction);
}

return codeActions;
}
}

private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic, String label) {
ExtendedCodeAction codeAction = new ExtendedCodeAction(label);
codeAction.setRelevance(0);
codeAction.setDiagnostics(Collections.singletonList(diagnostic));
codeAction.setKind(CodeActionKind.QuickFix);
codeAction.setTitle(label);
codeAction.setData(new CodeActionResolveData(context.getUri(), getParticipantId(),
context.getParams().getRange(), Collections.emptyMap(),
context.getParams().isResourceOperationSupported(),
context.getParams().isCommandConfigurationUpdateSupported()));
return codeAction;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public class AddResourceMissingNameQuickFix extends InsertAnnotationAttributesQu
public AddResourceMissingNameQuickFix() {
super("jakarta.annotation.Resource", false, "name");
}

@Override
public String getParticipantId() {
return AddResourceMissingNameQuickFix.class.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public class AddResourceMissingTypeQuickFix extends InsertAnnotationAttributesQu
public AddResourceMissingTypeQuickFix() {
super("jakarta.annotation.Resource", false, "type");
}

@Override
public String getParticipantId() {
return AddResourceMissingTypeQuickFix.class.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,36 @@
*******************************************************************************/
package io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal;

import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiFile;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ASTRewriteCorrectionProposal;
import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ImplementInterfaceProposal;
import org.eclipse.lsp4j.CodeActionKind;

public class ExtendClassProposal extends ImplementInterfaceProposal {
public class ExtendClassProposal extends ASTRewriteCorrectionProposal {

public ExtendClassProposal(String name, PsiFile sourceCU, PsiClass binding, PsiFile astRoot,
String interfaceType, int relevance) {
super(null, binding, astRoot, interfaceType, relevance, sourceCU);
private final PsiClass fBinding;
private final String parentClassType;

public ExtendClassProposal(String name, PsiFile targetCU, PsiFile sourceCU,
PsiClass binding, String parentClassType, int relevance) {
super(name, CodeActionKind.QuickFix, targetCU, relevance, sourceCU);
fBinding = binding;
this.parentClassType = parentClassType;
}

@Override
public void performUpdate() {
final Project project = fBinding.getProject();
final PsiClass parentClass = JavaPsiFacade.getInstance(project).
findClass(parentClassType, GlobalSearchScope.allScope(project));
if (parentClass != null) {
final PsiReferenceList extendsList = fBinding.getExtendsList();
if (extendsList != null) {
extendsList.add(PsiElementFactory.getInstance(project).
createClassReferenceElement(parentClass));
}
}
}
}
Loading
Loading