Skip to content

Commit

Permalink
Refactor LocalCorrectionsSubProcessor to jdt.core.manipulation (#1849)
Browse files Browse the repository at this point in the history
* Refactor LocalCorrectionsSubProcessor to jdt.core.manipulation

- create new LocalCorrectionsBaseSubProcessor in jdt.core.manipulation
  and have LocalCorrectionsSubProcessor sub-class it
- add new constructors where needed to support specifying a core
  class and an image
- fixes #1847
  • Loading branch information
jjohnstn authored Dec 11, 2024
1 parent cae21d0 commit 960a909
Show file tree
Hide file tree
Showing 14 changed files with 2,272 additions and 1,782 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;

import org.eclipse.jdt.internal.core.manipulation.StubUtility;
import org.eclipse.jdt.internal.core.manipulation.dom.ASTResolving;
Expand Down Expand Up @@ -710,4 +711,26 @@ public static CompilationUnit findCUForMethod(CompilationUnit compilationUnit, I
return compilationUnit;
}

public static ASTNode getCopyOfInner(ASTRewrite rewrite, ASTNode statement, boolean toControlStatementBody) {
if (statement.getNodeType() == ASTNode.BLOCK) {
Block block= (Block) statement;
List<Statement> innerStatements= block.statements();
int nStatements= innerStatements.size();
if (nStatements == 1) {
return rewrite.createCopyTarget(innerStatements.get(0));
} else if (nStatements > 1) {
if (toControlStatementBody) {
return rewrite.createCopyTarget(block);
}
ListRewrite listRewrite= rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY);
ASTNode first= innerStatements.get(0);
ASTNode last= innerStatements.get(nStatements - 1);
return listRewrite.createCopyTarget(first, last);
}
return null;
} else {
return rewrite.createCopyTarget(statement);
}
}

}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -1858,28 +1858,6 @@ private static boolean containsQuickFixableRenameLocal(IProblemLocation[] locati
return false;
}

public static ASTNode getCopyOfInner(ASTRewrite rewrite, ASTNode statement, boolean toControlStatementBody) {
if (statement.getNodeType() == ASTNode.BLOCK) {
Block block= (Block) statement;
List<Statement> innerStatements= block.statements();
int nStatements= innerStatements.size();
if (nStatements == 1) {
return rewrite.createCopyTarget(innerStatements.get(0));
} else if (nStatements > 1) {
if (toControlStatementBody) {
return rewrite.createCopyTarget(block);
}
ListRewrite listRewrite= rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY);
ASTNode first= innerStatements.get(0);
ASTNode last= innerStatements.get(nStatements - 1);
return listRewrite.createCopyTarget(first, last);
}
return null;
} else {
return rewrite.createCopyTarget(statement);
}
}


private static boolean getUnWrapProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
ASTNode outer= node;
Expand Down Expand Up @@ -1975,7 +1953,7 @@ private static boolean getUnWrapProposals(IInvocationContext context, ASTNode no
return false;
}
ASTRewrite rewrite= ASTRewrite.create(outer.getAST());
ASTNode inner= getCopyOfInner(rewrite, body, ASTNodes.isControlStatementBody(outer.getLocationInParent()));
ASTNode inner= QuickAssistProcessorUtil.getCopyOfInner(rewrite, body, ASTNodes.isControlStatementBody(outer.getLocationInParent()));
if (inner == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public ConstructorFromSuperclassProposal(ICompilationUnit cu, TypeDeclaration ty
super("", cu, null, relevance, getProposalImage(), new ConstructorFromSuperclassProposalCore(cu, typeNode, superConstructor, relevance)); //$NON-NLS-1$
}

public ConstructorFromSuperclassProposal(ConstructorFromSuperclassProposalCore core) {
super("", core.getCompilationUnit(), null, core.getRelevance(), getProposalImage(), core); //$NON-NLS-1$
}

@Override
public String getName() {
return ((ConstructorFromSuperclassProposalCore) getDelegate()).getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public CreateNewObjectProposal(ICompilationUnit cu, VariableDeclarationFragment
super("", cu, null, relevance, null, new CreateNewObjectProposalCore(cu, variableDeclarationFragment, variableBinding, relevance)); //$NON-NLS-1$
}

public CreateNewObjectProposal(CreateNewObjectProposalCore core) {
super("", core.getCompilationUnit(), null, core.getRelevance(), null, core); //$NON-NLS-1$
}

@Override
public Image getImage() {
return JavaPlugin.getImageDescriptorRegistry().get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public CreateObjectReferenceProposal(ICompilationUnit cu, ASTNode selectedNode,
super("", cu, null, relevance, null, new CreateObjectReferenceProposalCore(cu, selectedNode, typeNode, relevance)); //$NON-NLS-1$
}

public CreateObjectReferenceProposal(CreateObjectReferenceProposalCore core) {
super("", core.getCompilationUnit(), null, core.getRelevance(), null, core); //$NON-NLS-1$
}

@Override
public Image getImage() {
return JavaPlugin.getImageDescriptorRegistry().get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public CreateVariableReferenceProposal(ICompilationUnit cu, VariableDeclarationF
super("", cu, null, relevance, null, new CreateVariableReferenceProposalCore(cu, selectedNode, typeNode, relevance)); //$NON-NLS-1$
}

public CreateVariableReferenceProposal(CreateVariableReferenceProposalCore core) {
super("", core.getCompilationUnit(), null, core.getRelevance(), null, core); //$NON-NLS-1$
}

@Override
public Image getImage() {
return JavaPlugin.getImageDescriptorRegistry().get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public FixCorrectionProposal(IProposableFix fix, ICleanUp cleanUp, int relevance
this.fCleanUp= cleanUp;
}

public FixCorrectionProposal(FixCorrectionProposalCore core, Image image) {
super(core, image);
this.fCleanUp= core.getCleanUp();
}

public void resolve(MultiFixTarget[] targets, final IProgressMonitor monitor) throws CoreException {
if (targets.length == 0)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public MissingAnnotationAttributesProposal(ICompilationUnit cu, Annotation annot
super(CorrectionMessages.MissingAnnotationAttributesProposal_add_missing_attributes_label, cu, null, relevance,
JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE), new MissingAnnotationAttributesProposalCore(cu, annotation, relevance));
}

public MissingAnnotationAttributesProposal(MissingAnnotationAttributesProposalCore core) {
super(CorrectionMessages.MissingAnnotationAttributesProposal_add_missing_attributes_label, core.getCompilationUnit(), null, core.getRelevance(),
JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE), core);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ public class ModifierChangeCorrectionProposal extends LinkedCorrectionProposal {
public ModifierChangeCorrectionProposal(String label, ICompilationUnit targetCU, IBinding binding, ASTNode node, int includedModifiers, int excludedModifiers, int relevance, Image image) {
super(label, targetCU, null, relevance, image, new ModifierChangeCorrectionProposalCore(label, targetCU, binding, node, includedModifiers, excludedModifiers, relevance));
}

public ModifierChangeCorrectionProposal(ModifierChangeCorrectionProposalCore core, Image image) {
super(core.getName(), core.getCompilationUnit(), null, core.getRelevance(), image, core);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ public RefactoringCorrectionProposal(String name, ICompilationUnit cu, int relev
super(name, cu, null, relevance, image, delegate);
}

public RefactoringCorrectionProposal(RefactoringCorrectionProposalCore core, Image image) {
super(core, image);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ public ReplaceCorrectionProposal(ReplaceCorrectionProposalCore core) {
public ReplaceCorrectionProposal(String name, ICompilationUnit cu, int offset, int length, String replacementString, int relevance) {
super(name, cu, relevance, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE), new ReplaceCorrectionProposalCore(name, cu, offset, length, replacementString, relevance));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ public CUCorrectionProposal(String name, ICompilationUnit cu, TextChange change,
}


/**
* Constructs a correction proposal based on a core proposal and an image
*
* @param core the core proposal
* @param image the image to use
* @throws CoreException if obtaining text change has issue
* @since 3.34
*
*/
public CUCorrectionProposal(CUCorrectionProposalCore core, Image image) throws CoreException {
this(core.getName(), core.getCompilationUnit(), core.getTextChange(), core.getRelevance(), image);
}

/**
* Constructs a correction proposal working on a compilation unit.
Expand Down

0 comments on commit 960a909

Please sign in to comment.