mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Cosmetics.
This commit is contained in:
parent
41ee60794f
commit
0a9a6d9c7b
2 changed files with 94 additions and 106 deletions
|
@ -56,23 +56,19 @@ import org.eclipse.cdt.internal.ui.refactoring.NodeContainer.NameInformation;
|
||||||
* Handles the extraction of expression nodes, like return type determination.
|
* Handles the extraction of expression nodes, like return type determination.
|
||||||
*
|
*
|
||||||
* @author Mirko Stocker
|
* @author Mirko Stocker
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class ExtractExpression extends ExtractedFunctionConstructionHelper {
|
public class ExtractExpression extends ExtractedFunctionConstructionHelper {
|
||||||
|
|
||||||
final static char[] ZERO= { '0' };
|
final static char[] ZERO= { '0' };
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void constructMethodBody(IASTCompoundStatement compound,
|
public void constructMethodBody(IASTCompoundStatement compound, List<IASTNode> list,
|
||||||
List<IASTNode> list, ASTRewrite rewrite, TextEditGroup group) {
|
ASTRewrite rewrite, TextEditGroup group) {
|
||||||
|
|
||||||
CPPASTReturnStatement statement = new CPPASTReturnStatement();
|
CPPASTReturnStatement statement = new CPPASTReturnStatement();
|
||||||
IASTExpression nullReturnExp = new CPPASTLiteralExpression(IASTLiteralExpression.lk_integer_constant, ZERO);
|
IASTExpression nullReturnExp = new CPPASTLiteralExpression(IASTLiteralExpression.lk_integer_constant, ZERO);
|
||||||
statement.setReturnValue(nullReturnExp);
|
statement.setReturnValue(nullReturnExp);
|
||||||
ASTRewrite nestedRewrite = rewrite.insertBefore(compound, null, statement, group);
|
ASTRewrite nestedRewrite = rewrite.insertBefore(compound, null, statement, group);
|
||||||
|
|
||||||
nestedRewrite.replace(nullReturnExp, getExpression(list), group);
|
nestedRewrite.replace(nullReturnExp, getExpression(list), group);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private IASTExpression getExpression(List<IASTNode> list) {
|
private IASTExpression getExpression(List<IASTNode> list) {
|
||||||
|
@ -86,18 +82,17 @@ public class ExtractExpression extends ExtractedFunctionConstructionHelper {
|
||||||
} else {
|
} else {
|
||||||
return (IASTExpression) list.get(0).copy(CopyStyle.withLocations);
|
return (IASTExpression) list.get(0).copy(CopyStyle.withLocations);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IASTDeclSpecifier determineReturnType(IASTNode extractedNode, NameInformation _) {
|
public IASTDeclSpecifier determineReturnType(IASTNode extractedNode, NameInformation _) {
|
||||||
List<ITypedef> typdefs = getTypdefs(extractedNode);
|
List<ITypedef> typedefs = getTypedefs(extractedNode);
|
||||||
if (extractedNode instanceof IASTExpression) {
|
if (extractedNode instanceof IASTExpression) {
|
||||||
IASTExpression exp = (IASTExpression) extractedNode;
|
IASTExpression exp = (IASTExpression) extractedNode;
|
||||||
INodeFactory factory = extractedNode.getTranslationUnit().getASTNodeFactory();
|
INodeFactory factory = extractedNode.getTranslationUnit().getASTNodeFactory();
|
||||||
DeclarationGenerator generator = DeclarationGenerator.create(factory);
|
DeclarationGenerator generator = DeclarationGenerator.create(factory);
|
||||||
IType expressionType = exp.getExpressionType();
|
IType expressionType = exp.getExpressionType();
|
||||||
for (ITypedef typedef : typdefs) {
|
for (ITypedef typedef : typedefs) {
|
||||||
if (typedef.getType().isSameType(expressionType)) {
|
if (typedef.getType().isSameType(expressionType)) {
|
||||||
return generator.createDeclSpecFromType(typedef);
|
return generator.createDeclSpecFromType(typedef);
|
||||||
}
|
}
|
||||||
|
@ -108,7 +103,7 @@ public class ExtractExpression extends ExtractedFunctionConstructionHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<ITypedef> getTypdefs(IASTNode extractedNode) {
|
private List<ITypedef> getTypedefs(IASTNode extractedNode) {
|
||||||
final ArrayList<ITypedef> typeDefs = new ArrayList<ITypedef>();
|
final ArrayList<ITypedef> typeDefs = new ArrayList<ITypedef>();
|
||||||
extractedNode.accept(new ASTVisitor() {
|
extractedNode.accept(new ASTVisitor() {
|
||||||
{
|
{
|
||||||
|
@ -181,7 +176,8 @@ public class ExtractExpression extends ExtractedFunctionConstructionHelper {
|
||||||
IASTNode parent = function.getDeclarations()[0].getParent();
|
IASTNode parent = function.getDeclarations()[0].getParent();
|
||||||
if (parent instanceof CPPASTSimpleDeclaration) {
|
if (parent instanceof CPPASTSimpleDeclaration) {
|
||||||
CPPASTSimpleDeclaration declaration = (CPPASTSimpleDeclaration) parent;
|
CPPASTSimpleDeclaration declaration = (CPPASTSimpleDeclaration) parent;
|
||||||
return declaration.getDeclarators().length > 0 && declaration.getDeclarators()[0].getPointerOperators().length > 0;
|
return declaration.getDeclarators().length > 0 &&
|
||||||
|
declaration.getDeclarators()[0].getPointerOperators().length > 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,13 +30,12 @@ import org.eclipse.cdt.internal.ui.refactoring.NodeContainer;
|
||||||
import org.eclipse.cdt.internal.ui.refactoring.NodeContainer.NameInformation;
|
import org.eclipse.cdt.internal.ui.refactoring.NodeContainer.NameInformation;
|
||||||
|
|
||||||
final class SimilarFinderVisitor extends ASTVisitor {
|
final class SimilarFinderVisitor extends ASTVisitor {
|
||||||
|
|
||||||
private final ExtractFunctionRefactoring refactoring;
|
private final ExtractFunctionRefactoring refactoring;
|
||||||
|
|
||||||
private final Vector<IASTNode> trail;
|
private final Vector<IASTNode> trail;
|
||||||
private final IASTName name;
|
private final IASTName name;
|
||||||
private final List<IASTNode> stmts;
|
private final List<IASTNode> stmts;
|
||||||
private int i = 0;
|
private int i;
|
||||||
private NodeContainer similarContainer;
|
private NodeContainer similarContainer;
|
||||||
private final List<IASTStatement> stmtToReplace = new ArrayList<IASTStatement>();
|
private final List<IASTStatement> stmtToReplace = new ArrayList<IASTStatement>();
|
||||||
|
|
||||||
|
@ -51,15 +50,11 @@ final class SimilarFinderVisitor extends ASTVisitor {
|
||||||
this.stmts = stmts;
|
this.stmts = stmts;
|
||||||
this.collector = collector;
|
this.collector = collector;
|
||||||
this.similarContainer = new NodeContainer();
|
this.similarContainer = new NodeContainer();
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
shouldVisitStatements = true;
|
shouldVisitStatements = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int visit(IASTStatement stmt) {
|
public int visit(IASTStatement stmt) {
|
||||||
|
|
||||||
boolean isAllreadyInMainRefactoring = isInSelection(stmt);
|
boolean isAllreadyInMainRefactoring = isInSelection(stmt);
|
||||||
|
|
||||||
if ((!isAllreadyInMainRefactoring)
|
if ((!isAllreadyInMainRefactoring)
|
||||||
|
@ -69,7 +64,7 @@ final class SimilarFinderVisitor extends ASTVisitor {
|
||||||
++i;
|
++i;
|
||||||
|
|
||||||
if (i == stmts.size()) {
|
if (i == stmts.size()) {
|
||||||
//found similar code
|
// Found similar code
|
||||||
|
|
||||||
boolean similarOnReturnWays = true;
|
boolean similarOnReturnWays = true;
|
||||||
for (NameInformation nameInfo : similarContainer.getAllAfterUsedNames()) {
|
for (NameInformation nameInfo : similarContainer.getAllAfterUsedNames()) {
|
||||||
|
@ -102,8 +97,8 @@ final class SimilarFinderVisitor extends ASTVisitor {
|
||||||
IASTNode call = refactoring.getMethodCall(name,
|
IASTNode call = refactoring.getMethodCall(name,
|
||||||
this.refactoring.nameTrail, this.refactoring.names,
|
this.refactoring.nameTrail, this.refactoring.names,
|
||||||
this.refactoring.container, similarContainer);
|
this.refactoring.container, similarContainer);
|
||||||
ASTRewrite rewrite = collector.rewriterForTranslationUnit(stmtToReplace.get(0)
|
ASTRewrite rewrite =
|
||||||
.getTranslationUnit());
|
collector.rewriterForTranslationUnit(stmtToReplace.get(0).getTranslationUnit());
|
||||||
TextEditGroup editGroup = new TextEditGroup(Messages.SimilarFinderVisitor_replaceDuplicateCode);
|
TextEditGroup editGroup = new TextEditGroup(Messages.SimilarFinderVisitor_replaceDuplicateCode);
|
||||||
rewrite.replace(stmtToReplace.get(0), call, editGroup);
|
rewrite.replace(stmtToReplace.get(0), call, editGroup);
|
||||||
if (stmtToReplace.size() > 1) {
|
if (stmtToReplace.size() > 1) {
|
||||||
|
@ -112,16 +107,13 @@ final class SimilarFinderVisitor extends ASTVisitor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
clear();
|
clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
return PROCESS_SKIP;
|
return PROCESS_SKIP;
|
||||||
} else {
|
} else {
|
||||||
clear();
|
clear();
|
||||||
return super.visit(stmt);
|
return super.visit(stmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isInSelection(IASTStatement stmt) {
|
private boolean isInSelection(IASTStatement stmt) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue