-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor ElseIfCleanUp to jdt.core.manipulation (#1374)
- create new ElseIfCleanUpCore and ElseIfFixCore classes - modify CodeStyleTabPage, CleanUpPerfTest, and plugin.xml to refer to new core clean-up - fixes #1373
- Loading branch information
Showing
5 changed files
with
105 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...t.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/ElseIfFixCore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Fabrice TIERCELIN and others. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Fabrice TIERCELIN - initial API and implementation | ||
* Red Hat Inc. - code extracted from ElseIfCleanUp | ||
*******************************************************************************/ | ||
package org.eclipse.jdt.internal.corext.fix; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.eclipse.core.runtime.CoreException; | ||
|
||
import org.eclipse.text.edits.TextEditGroup; | ||
|
||
import org.eclipse.jdt.core.dom.ASTVisitor; | ||
import org.eclipse.jdt.core.dom.Block; | ||
import org.eclipse.jdt.core.dom.CompilationUnit; | ||
import org.eclipse.jdt.core.dom.IfStatement; | ||
import org.eclipse.jdt.core.dom.Statement; | ||
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; | ||
|
||
import org.eclipse.jdt.internal.corext.dom.ASTNodes; | ||
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; | ||
|
||
import org.eclipse.jdt.ui.cleanup.ICleanUpFix; | ||
|
||
import org.eclipse.jdt.internal.ui.fix.MultiFixMessages; | ||
|
||
public class ElseIfFixCore extends CompilationUnitRewriteOperationsFixCore { | ||
|
||
private static class ElseIfOperation extends CompilationUnitRewriteOperation { | ||
private final IfStatement visited; | ||
private final IfStatement innerIf; | ||
|
||
public ElseIfOperation(final IfStatement visited, final IfStatement innerIf) { | ||
this.visited= visited; | ||
this.innerIf= innerIf; | ||
} | ||
|
||
@Override | ||
public void rewriteAST(final CompilationUnitRewrite cuRewrite, final LinkedProposalModelCore linkedModel) throws CoreException { | ||
ASTRewrite rewrite= cuRewrite.getASTRewrite(); | ||
TextEditGroup group= createTextEditGroup(MultiFixMessages.CodeStyleCleanUp_ElseIf_description, cuRewrite); | ||
|
||
rewrite.replace(visited.getElseStatement(), ASTNodes.createMoveTarget(rewrite, innerIf), group); | ||
} | ||
} | ||
|
||
public static ICleanUpFix createCleanUp(final CompilationUnit unit) { | ||
|
||
final List<CompilationUnitRewriteOperation> rewriteOperations= new ArrayList<>(); | ||
|
||
unit.accept(new ASTVisitor() { | ||
@Override | ||
public boolean visit(final IfStatement visited) { | ||
Statement elseStatement= visited.getElseStatement(); | ||
|
||
if (elseStatement instanceof Block) { | ||
IfStatement innerIf= ASTNodes.as(elseStatement, IfStatement.class); | ||
|
||
if (innerIf != null) { | ||
rewriteOperations.add(new ElseIfOperation(visited, innerIf)); | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
}); | ||
|
||
if (rewriteOperations.isEmpty()) { | ||
return null; | ||
} | ||
|
||
return new CompilationUnitRewriteOperationsFixCore(MultiFixMessages.CodeStyleCleanUp_ElseIf_description, unit, | ||
rewriteOperations.toArray(new CompilationUnitRewriteOperation[0])); | ||
|
||
} | ||
|
||
protected ElseIfFixCore(final String name, final CompilationUnit compilationUnit, CompilationUnitRewriteOperation[] fixRewriteOperations) { | ||
super(name, compilationUnit, fixRewriteOperations); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters