mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Code streamlining.
This commit is contained in:
parent
f3dc616c97
commit
db44d771bb
4 changed files with 57 additions and 48 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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>();
|
||||
for (IASTDeclaration decl : declarations) {
|
||||
if (decl.getFileLocation().getStartingLineNumber() >= methodPosition.getStartingLineNumber()) {
|
||||
return allIASTSimpleDeclarations;
|
||||
}
|
||||
if (isMemberFunctionDeclaration(decl)) {
|
||||
allIASTSimpleDeclarations.add(0, (IASTSimpleDeclaration) decl);
|
||||
ArrayList<IASTSimpleDeclaration> outputDeclarations = new ArrayList<IASTSimpleDeclaration>();
|
||||
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<IASTSimpleDeclaration> getAllFollowingSimpleDeclarationsFromClass(
|
||||
IASTDeclaration[] declarations, IASTFileLocation methodPosition) {
|
||||
ArrayList<IASTSimpleDeclaration> allIASTSimpleDeclarations = new ArrayList<IASTSimpleDeclaration>();
|
||||
ArrayList<IASTSimpleDeclaration> outputDeclarations = new ArrayList<IASTSimpleDeclaration>();
|
||||
|
||||
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) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Add table
Reference in a new issue