1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Code streamlining.

This commit is contained in:
Sergey Prigogin 2011-04-11 20:30:21 +00:00
parent f3dc616c97
commit db44d771bb
4 changed files with 57 additions and 48 deletions

View file

@ -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;
}

View file

@ -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<IASTSimpleDeclaration> getAllPreviousSimpleDeclarationsFromClassInReverseOrder(
IASTDeclaration[] declarations, IASTFileLocation methodPosition) {
ArrayList<IASTSimpleDeclaration> allIASTSimpleDeclarations = new ArrayList<IASTSimpleDeclaration>();
ArrayList<IASTSimpleDeclaration> outputDeclarations = new ArrayList<IASTSimpleDeclaration>();
if (declarations.length >= 0) {
for (IASTDeclaration decl : declarations) {
if (decl.getFileLocation().getStartingLineNumber() >= methodPosition.getStartingLineNumber()) {
return allIASTSimpleDeclarations;
break;
}
if (isMemberFunctionDeclaration(decl)) {
allIASTSimpleDeclarations.add(0, (IASTSimpleDeclaration) decl);
outputDeclarations.add((IASTSimpleDeclaration) decl);
}
}
return allIASTSimpleDeclarations;
}
Collections.reverse(outputDeclarations);
return outputDeclarations;
}
private static Collection<IASTSimpleDeclaration> getAllFollowingSimpleDeclarationsFromClass(
IASTDeclaration[] declarations, IASTFileLocation methodPosition) {
ArrayList<IASTSimpleDeclaration> allIASTSimpleDeclarations = new ArrayList<IASTSimpleDeclaration>();
ArrayList<IASTSimpleDeclaration> outputDeclarations = new ArrayList<IASTSimpleDeclaration>();
if (declarations.length >= 0) {
for (IASTDeclaration decl : declarations) {
if (isMemberFunctionDeclaration(decl) &&
decl.getFileLocation().getStartingLineNumber() > methodPosition.getStartingLineNumber() ) {
allIASTSimpleDeclarations.add((IASTSimpleDeclaration) decl);
outputDeclarations.add((IASTSimpleDeclaration) decl);
}
}
return allIASTSimpleDeclarations;
}
return outputDeclarations;
}
private static boolean isMemberFunctionDeclaration(IASTDeclaration decl) {

View file

@ -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;
}

View file

@ -53,7 +53,7 @@ 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) {