Skip to content

Commit

Permalink
Merge pull request #533 from dessina-devasia/522-RemoveModifierConfli…
Browse files Browse the repository at this point in the history
…ctQuickFix

Fixes #522: Refactored RemoveModifierQuickFix and bring the test cases ready
  • Loading branch information
dessina-devasia authored Oct 18, 2023
2 parents 4ac1934 + 228454a commit fa36527
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class ModifyModifiersProposal extends ChangeCorrectionProposal {
// list of modifiers (if they exist) to remove
private final List<String> modifiersToRemove;

private boolean isFormatRequired = true;

/**
* Constructor for ModifyModifiersProposal that accepts both a list of modifiers to remove as well as to add
*
Expand All @@ -62,6 +64,25 @@ public ModifyModifiersProposal(String label, PsiFile sourceCU, PsiFile invocatio
this.modifiersToAdd = modifiersToAdd;
this.modifiersToRemove = modifiersToRemove;
}

/**
* Constructor for ModifyModifiersProposal that accepts both a list of modifiers to remove as well as to add
* this constructor having an optional parameter isFormatRequired
*
* @param modifiersToAdd list of valid modifiers as strings to be added
* @param modifiersToRemove list of modifiers as strings to be removed
*/
public ModifyModifiersProposal(String label, PsiFile sourceCU, PsiFile invocationNode,
PsiModifierListOwner binding, int relevance, PsiModifierList modifiers, List<String> modifiersToAdd, List<String> modifiersToRemove, boolean isFormatRequired) {
super(label, CodeActionKind.QuickFix, relevance);
this.sourceCU = sourceCU;
this.invocationNode = invocationNode;
this.binding = binding;
this.modifiers = modifiers;
this.modifiersToAdd = modifiersToAdd;
this.modifiersToRemove = modifiersToRemove;
this.isFormatRequired = isFormatRequired;
}

/**
* Constructor for ModifyModifiersProposal that accepts only a list of modifiers to add
Expand Down Expand Up @@ -100,7 +121,9 @@ public Change getChange() {
modifiers.setModifierProperty(modifier, true);
}
});
PositionUtils.formatDocument(binding); // add the necessary new lines, must use 'binding,' it's already in the document
if (isFormatRequired) {
PositionUtils.formatDocument(binding); // add the necessary new lines, must use 'binding,' it's already in the document
}
final Document document = invocationNode.getViewProvider().getDocument();
return new Change(sourceCU.getViewProvider().getDocument(), document);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ public class RemoveAbstractModifierQuickFix extends RemoveModifierConflictQuickF
public RemoveAbstractModifierQuickFix() {
super (false, "abstract");
}

@Override
public String getParticipantId() {
return RemoveAbstractModifierQuickFix.class.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public class RemoveFinalModifierQuickFix extends RemoveModifierConflictQuickFix
public RemoveFinalModifierQuickFix() {
super(false, "final");
}

@Override
public String getParticipantId() {
return RemoveFinalModifierQuickFix.class.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,19 @@
import com.intellij.psi.util.PsiTreeUtil;
import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.Messages;
import io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.ModifyModifiersProposal;
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 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.logging.Level;
import java.util.logging.Logger;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -32,11 +42,13 @@
* @author Himanshu Chotwani
*
*/
public class RemoveModifierConflictQuickFix {
public abstract class RemoveModifierConflictQuickFix implements IJavaCodeActionParticipant {

private final String[] modifiers;

protected final boolean generateOnlyOneCodeAction;

private static final Logger LOGGER = Logger.getLogger(RemoveModifierConflictQuickFix.class.getName());


/**
Expand Down Expand Up @@ -64,16 +76,16 @@ public RemoveModifierConflictQuickFix(boolean generateOnlyOneCodeAction, String.
this.generateOnlyOneCodeAction = generateOnlyOneCodeAction;
this.modifiers = modifiers;
}


public List<? extends CodeAction> getCodeActions(JavaCodeActionContext context, Diagnostic diagnostic) {
List<CodeAction> codeActions = new ArrayList<>();
removeModifiers(diagnostic, context, codeActions);
return codeActions;
}



protected void removeModifiers(Diagnostic diagnostic, JavaCodeActionContext context,
List<CodeAction> codeActions) {
List<CodeAction> codeActions) {
if (generateOnlyOneCodeAction || modifiers.length == 1) {
removeModifier(diagnostic, context, codeActions, modifiers);
} else {
Expand All @@ -83,26 +95,35 @@ protected void removeModifiers(Diagnostic diagnostic, JavaCodeActionContext cont
}
}
}

/**
* use setData() API with diagnostic to pass in ElementType in diagnostic collector class.
*
*/
private void removeModifier(Diagnostic diagnostic, JavaCodeActionContext context,
List<CodeAction> codeActions, String... modifier) {
PsiElement node = context.getCoveredNode();
PsiClass parentType = getBinding(node);
PsiModifierListOwner modifierListOwner = PsiTreeUtil.getParentOfType(node, PsiModifierListOwner.class);

String label = getLabel(modifierListOwner, modifier);
@Override
public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) {
final CodeAction toResolve = context.getUnresolved();
final PsiElement node = context.getCoveredNode();
final PsiClass parentType = getBinding(node);
final PsiModifierListOwner modifierListOwner = PsiTreeUtil.getParentOfType(node, PsiModifierListOwner.class);

String label = getLabel(modifierListOwner, modifiers);

assert parentType != null;
ModifyModifiersProposal proposal = new ModifyModifiersProposal(label, context.getSource().getCompilationUnit(),
context.getASTRoot(), parentType, 0, modifierListOwner.getModifierList(), Collections.emptyList(), Arrays.asList(modifier));
CodeAction codeAction = context.convertToCodeAction(proposal, diagnostic);
context.getASTRoot(), parentType, 0, modifierListOwner.getModifierList(), Collections.emptyList(), Arrays.asList(modifiers), false);

if (codeAction != null) {
codeActions.add(codeAction);
try {
WorkspaceEdit we = context.convertToWorkspaceEdit(proposal);
toResolve.setEdit(we);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Unable to create workspace edit for code action " + label, e);
}
return toResolve;
}

private void removeModifier(Diagnostic diagnostic, JavaCodeActionContext context,
List<CodeAction> codeActions, String... modifier) {
PsiElement node = context.getCoveredNode();
PsiModifierListOwner modifierListOwner = PsiTreeUtil.getParentOfType(node, PsiModifierListOwner.class);
String label = getLabel(modifierListOwner, modifier);
codeActions.add(createCodeAction(context, diagnostic, label));
}

private String getLabel(PsiModifierListOwner modifierListOwner, String... modifier) {
Expand All @@ -125,4 +146,16 @@ protected PsiClass getBinding(PsiElement node) {
return PsiTreeUtil.getParentOfType(node, PsiClass.class);
}

private CodeAction createCodeAction(JavaCodeActionContext context, Diagnostic diagnostic, String title) {
ExtendedCodeAction codeAction = new ExtendedCodeAction(title);
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public class RemoveStaticModifierQuickFix extends RemoveModifierConflictQuickFix
public RemoveStaticModifierQuickFix() {
super(false, "static");
}

@Override
public String getParticipantId() {
return RemoveStaticModifierQuickFix.class.getName();
}
}
12 changes: 12 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@
group="jakarta"
targetDiagnostic="jakarta-jax_rs#NonPublicResourceMethod"
implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.jax_rs.NonPublicResourceMethodQuickFix"/>
<javaCodeActionParticipant kind="quickfix"
group="jakarta"
targetDiagnostic="jakarta-di#RemoveInjectOrFinal"
implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.quickfix.RemoveFinalModifierQuickFix"/>
<javaCodeActionParticipant kind="quickfix"
group="jakarta"
targetDiagnostic="jakarta-di#RemoveInjectOrStatic"
implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.quickfix.RemoveStaticModifierQuickFix"/>
<javaCodeActionParticipant kind="quickfix"
group="jakarta"
targetDiagnostic="jakarta-di#RemoveInjectOrAbstract"
implementationClass="io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.codeAction.proposal.quickfix.RemoveAbstractModifierQuickFix"/>

<javaCodeActionParticipant kind="quickfix"
group="jakarta"
Expand Down
Loading

0 comments on commit fa36527

Please sign in to comment.