From db44d771bba67ccfd81e8113f824b3a88d30c412 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Mon, 11 Apr 2011 20:30:21 +0000 Subject: [PATCH] Code streamlining. --- .../implementmethod/InsertLocation2.java | 5 +- ...MethodDefinitionInsertLocationFinder2.java | 82 ++++++++++--------- .../refactoring/utils/DefinitionFinder2.java | 2 +- .../ui/refactoring/utils/NodeHelper.java | 16 ++-- 4 files changed, 57 insertions(+), 48 deletions(-) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/implementmethod/InsertLocation2.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/implementmethod/InsertLocation2.java index 06c53779904..2835a22d333 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/implementmethod/InsertLocation2.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/implementmethod/InsertLocation2.java @@ -15,6 +15,7 @@ package org.eclipse.cdt.internal.ui.refactoring.implementmethod; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.CoreException; +import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.model.ITranslationUnit; @@ -59,8 +60,8 @@ public class InsertLocation2 { if (nodeToInsertBefore != null) { return nodeToInsertBefore.getFileLocation().getNodeOffset(); } else if (nodeToInsertAfter != null) { - return nodeToInsertAfter.getFileLocation().getNodeOffset() + - nodeToInsertAfter.getFileLocation().getNodeLength(); + IASTFileLocation fileLocation = nodeToInsertAfter.getFileLocation(); + return fileLocation.getNodeOffset() + fileLocation.getNodeLength(); } return 0; } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/implementmethod/MethodDefinitionInsertLocationFinder2.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/implementmethod/MethodDefinitionInsertLocationFinder2.java index fb917ed84d1..99967d86f2b 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/implementmethod/MethodDefinitionInsertLocationFinder2.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/implementmethod/MethodDefinitionInsertLocationFinder2.java @@ -14,6 +14,7 @@ package org.eclipse.cdt.internal.ui.refactoring.implementmethod; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import org.eclipse.core.runtime.CoreException; @@ -41,29 +42,8 @@ import org.eclipse.cdt.internal.ui.refactoring.utils.NodeHelper; */ public class MethodDefinitionInsertLocationFinder2 { - private static IASTNode findFunctionDefinitionInParents(IASTNode node) { - if (node == null) { - return null; - } else if (node instanceof IASTFunctionDefinition) { - if (node.getParent() instanceof ICPPASTTemplateDeclaration) { - node = node.getParent(); - } - return node; - } - return findFunctionDefinitionInParents(node.getParent()); - } - - private static IASTNode findFirstSurroundingParentFunctionNode(IASTNode definition) { - IASTNode functionDefinitionInParents = findFunctionDefinitionInParents(definition); - if (functionDefinitionInParents == null || - functionDefinitionInParents.getNodeLocations().length == 0) { - return null; - } - return functionDefinitionInParents; - } - - public static InsertLocation2 find(ITranslationUnit declarationTu, IASTFileLocation methodDeclarationLocation, IASTNode parent, - RefactoringASTCache astCache) throws CoreException { + public static InsertLocation2 find(ITranslationUnit declarationTu, IASTFileLocation methodDeclarationLocation, + IASTNode parent, RefactoringASTCache astCache) throws CoreException { IASTDeclaration[] declarations = NodeHelper.getDeclarations(parent); InsertLocation2 insertLocation = new InsertLocation2(); @@ -95,6 +75,29 @@ public class MethodDefinitionInsertLocationFinder2 { return insertLocation; } + private static IASTNode findFunctionDefinitionInParents(IASTNode node) { + if (node == null) { + return null; + } else if (node instanceof IASTFunctionDefinition) { + if (node.getParent() instanceof ICPPASTTemplateDeclaration) { + node = node.getParent(); + } + return node; + } + return findFunctionDefinitionInParents(node.getParent()); + } + + private static IASTNode findFirstSurroundingParentFunctionNode(IASTNode definition) { + IASTNode functionDefinitionInParents = findFunctionDefinitionInParents(definition); + if (functionDefinitionInParents == null) { + return null; + } + if (functionDefinitionInParents.getNodeLocations().length == 0) { + return null; + } + return functionDefinitionInParents; + } + /** * Searches the given class for all IASTSimpleDeclarations occurring before 'method' * and returns them in reverse order. @@ -105,29 +108,34 @@ public class MethodDefinitionInsertLocationFinder2 { */ private static Collection getAllPreviousSimpleDeclarationsFromClassInReverseOrder( IASTDeclaration[] declarations, IASTFileLocation methodPosition) { - ArrayList allIASTSimpleDeclarations = new ArrayList(); - for (IASTDeclaration decl : declarations) { - if (decl.getFileLocation().getStartingLineNumber() >= methodPosition.getStartingLineNumber()) { - return allIASTSimpleDeclarations; - } - if (isMemberFunctionDeclaration(decl)) { - allIASTSimpleDeclarations.add(0, (IASTSimpleDeclaration) decl); + ArrayList outputDeclarations = new ArrayList(); + if (declarations.length >= 0) { + for (IASTDeclaration decl : declarations) { + if (decl.getFileLocation().getStartingLineNumber() >= methodPosition.getStartingLineNumber()) { + break; + } + if (isMemberFunctionDeclaration(decl)) { + outputDeclarations.add((IASTSimpleDeclaration) decl); + } } } - return allIASTSimpleDeclarations; + Collections.reverse(outputDeclarations); + return outputDeclarations; } private static Collection getAllFollowingSimpleDeclarationsFromClass( IASTDeclaration[] declarations, IASTFileLocation methodPosition) { - ArrayList allIASTSimpleDeclarations = new ArrayList(); + ArrayList outputDeclarations = new ArrayList(); - for (IASTDeclaration decl : declarations) { - if (isMemberFunctionDeclaration(decl) && - decl.getFileLocation().getStartingLineNumber() > methodPosition.getStartingLineNumber() ) { - allIASTSimpleDeclarations.add((IASTSimpleDeclaration) decl); + if (declarations.length >= 0) { + for (IASTDeclaration decl : declarations) { + if (isMemberFunctionDeclaration(decl) && + decl.getFileLocation().getStartingLineNumber() > methodPosition.getStartingLineNumber() ) { + outputDeclarations.add((IASTSimpleDeclaration) decl); + } } } - return allIASTSimpleDeclarations; + return outputDeclarations; } private static boolean isMemberFunctionDeclaration(IASTDeclaration decl) { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/DefinitionFinder2.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/DefinitionFinder2.java index f767c459d95..5d5598a52e2 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/DefinitionFinder2.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/DefinitionFinder2.java @@ -45,8 +45,8 @@ public class DefinitionFinder2 { public static ASTNameInContext getDefinition(IASTSimpleDeclaration simpleDeclaration, RefactoringASTCache astCache) throws CoreException { - IASTDeclarator declarator = simpleDeclaration.getDeclarators()[0]; IIndex index = astCache.getIndex(); + IASTDeclarator declarator = simpleDeclaration.getDeclarators()[0]; if (index == null) { return null; } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/NodeHelper.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/NodeHelper.java index 06a56b5aa73..5783e442e35 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/NodeHelper.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/NodeHelper.java @@ -53,9 +53,9 @@ public class NodeHelper { } else if (parent instanceof CPPASTNamespaceDefinition) { return ((CPPASTNamespaceDefinition) parent).getDeclarations(); } - return new IASTDeclaration[0]; + return IASTDeclaration.EMPTY_DECLARATION_ARRAY; } - + public static IASTNode findFollowingNode(IASTNode currentNode) { if (currentNode == null || currentNode.getParent() == null) { return null; @@ -71,14 +71,14 @@ public class NodeHelper { } return null; } - + public static IASTNode findTopLevelParent(IASTNode currentNode) { while (currentNode != null && currentNode.getParent() != null && currentNode.getParent().getParent() != null) { return findTopLevelParent(currentNode.getParent()); } return currentNode; } - + public static boolean isSameNode(IASTNode node1, IASTNode node2) { if (node1 == null || node2 == null) { return false; @@ -87,7 +87,7 @@ public class NodeHelper { && node1.getNodeLocations()[0].getNodeLength() == node2.getNodeLocations()[0].getNodeLength() && new Path(node1.getFileLocation().getFileName()).equals(new Path(node2.getFileLocation().getFileName())); } - + public static IASTSimpleDeclaration findSimpleDeclarationInParents(IASTNode node) { while (node != null){ if (node instanceof IASTSimpleDeclaration) { @@ -97,7 +97,7 @@ public class NodeHelper { } return null; } - + public static MethodContext findMethodContext(IASTNode node, IIndex index) throws CoreException{ IASTTranslationUnit translationUnit = node.getTranslationUnit(); boolean found = false; @@ -171,7 +171,7 @@ public class NodeHelper { context.setMethodQName(qname); } } - + public static IASTCompoundStatement findCompoundStatementInAncestors(IASTNode node) { while (node != null){ if (node instanceof IASTCompoundStatement) { @@ -181,7 +181,7 @@ public class NodeHelper { } return null; } - + public static IASTCompositeTypeSpecifier findClassInAncestors(IASTNode node) { while (!(node instanceof IASTCompositeTypeSpecifier)){ if (node instanceof IASTTranslationUnit) {