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.resources.IFile;
|
||||||
import org.eclipse.core.runtime.CoreException;
|
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.dom.ast.IASTNode;
|
||||||
import org.eclipse.cdt.core.model.ITranslationUnit;
|
import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||||
|
|
||||||
|
@ -59,8 +60,8 @@ public class InsertLocation2 {
|
||||||
if (nodeToInsertBefore != null) {
|
if (nodeToInsertBefore != null) {
|
||||||
return nodeToInsertBefore.getFileLocation().getNodeOffset();
|
return nodeToInsertBefore.getFileLocation().getNodeOffset();
|
||||||
} else if (nodeToInsertAfter != null) {
|
} else if (nodeToInsertAfter != null) {
|
||||||
return nodeToInsertAfter.getFileLocation().getNodeOffset() +
|
IASTFileLocation fileLocation = nodeToInsertAfter.getFileLocation();
|
||||||
nodeToInsertAfter.getFileLocation().getNodeLength();
|
return fileLocation.getNodeOffset() + fileLocation.getNodeLength();
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ package org.eclipse.cdt.internal.ui.refactoring.implementmethod;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
import org.eclipse.core.runtime.CoreException;
|
import org.eclipse.core.runtime.CoreException;
|
||||||
|
|
||||||
|
@ -41,29 +42,8 @@ import org.eclipse.cdt.internal.ui.refactoring.utils.NodeHelper;
|
||||||
*/
|
*/
|
||||||
public class MethodDefinitionInsertLocationFinder2 {
|
public class MethodDefinitionInsertLocationFinder2 {
|
||||||
|
|
||||||
private static IASTNode findFunctionDefinitionInParents(IASTNode node) {
|
public static InsertLocation2 find(ITranslationUnit declarationTu, IASTFileLocation methodDeclarationLocation,
|
||||||
if (node == null) {
|
IASTNode parent, RefactoringASTCache astCache) throws CoreException {
|
||||||
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 {
|
|
||||||
IASTDeclaration[] declarations = NodeHelper.getDeclarations(parent);
|
IASTDeclaration[] declarations = NodeHelper.getDeclarations(parent);
|
||||||
InsertLocation2 insertLocation = new InsertLocation2();
|
InsertLocation2 insertLocation = new InsertLocation2();
|
||||||
|
|
||||||
|
@ -95,6 +75,29 @@ public class MethodDefinitionInsertLocationFinder2 {
|
||||||
return insertLocation;
|
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'
|
* Searches the given class for all IASTSimpleDeclarations occurring before 'method'
|
||||||
* and returns them in reverse order.
|
* and returns them in reverse order.
|
||||||
|
@ -105,29 +108,34 @@ public class MethodDefinitionInsertLocationFinder2 {
|
||||||
*/
|
*/
|
||||||
private static Collection<IASTSimpleDeclaration> getAllPreviousSimpleDeclarationsFromClassInReverseOrder(
|
private static Collection<IASTSimpleDeclaration> getAllPreviousSimpleDeclarationsFromClassInReverseOrder(
|
||||||
IASTDeclaration[] declarations, IASTFileLocation methodPosition) {
|
IASTDeclaration[] declarations, IASTFileLocation methodPosition) {
|
||||||
ArrayList<IASTSimpleDeclaration> allIASTSimpleDeclarations = new ArrayList<IASTSimpleDeclaration>();
|
ArrayList<IASTSimpleDeclaration> outputDeclarations = new ArrayList<IASTSimpleDeclaration>();
|
||||||
for (IASTDeclaration decl : declarations) {
|
if (declarations.length >= 0) {
|
||||||
if (decl.getFileLocation().getStartingLineNumber() >= methodPosition.getStartingLineNumber()) {
|
for (IASTDeclaration decl : declarations) {
|
||||||
return allIASTSimpleDeclarations;
|
if (decl.getFileLocation().getStartingLineNumber() >= methodPosition.getStartingLineNumber()) {
|
||||||
}
|
break;
|
||||||
if (isMemberFunctionDeclaration(decl)) {
|
}
|
||||||
allIASTSimpleDeclarations.add(0, (IASTSimpleDeclaration) decl);
|
if (isMemberFunctionDeclaration(decl)) {
|
||||||
|
outputDeclarations.add((IASTSimpleDeclaration) decl);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return allIASTSimpleDeclarations;
|
Collections.reverse(outputDeclarations);
|
||||||
|
return outputDeclarations;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Collection<IASTSimpleDeclaration> getAllFollowingSimpleDeclarationsFromClass(
|
private static Collection<IASTSimpleDeclaration> getAllFollowingSimpleDeclarationsFromClass(
|
||||||
IASTDeclaration[] declarations, IASTFileLocation methodPosition) {
|
IASTDeclaration[] declarations, IASTFileLocation methodPosition) {
|
||||||
ArrayList<IASTSimpleDeclaration> allIASTSimpleDeclarations = new ArrayList<IASTSimpleDeclaration>();
|
ArrayList<IASTSimpleDeclaration> outputDeclarations = new ArrayList<IASTSimpleDeclaration>();
|
||||||
|
|
||||||
for (IASTDeclaration decl : declarations) {
|
if (declarations.length >= 0) {
|
||||||
if (isMemberFunctionDeclaration(decl) &&
|
for (IASTDeclaration decl : declarations) {
|
||||||
decl.getFileLocation().getStartingLineNumber() > methodPosition.getStartingLineNumber() ) {
|
if (isMemberFunctionDeclaration(decl) &&
|
||||||
allIASTSimpleDeclarations.add((IASTSimpleDeclaration) decl);
|
decl.getFileLocation().getStartingLineNumber() > methodPosition.getStartingLineNumber() ) {
|
||||||
|
outputDeclarations.add((IASTSimpleDeclaration) decl);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return allIASTSimpleDeclarations;
|
return outputDeclarations;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isMemberFunctionDeclaration(IASTDeclaration decl) {
|
private static boolean isMemberFunctionDeclaration(IASTDeclaration decl) {
|
||||||
|
|
|
@ -45,8 +45,8 @@ public class DefinitionFinder2 {
|
||||||
|
|
||||||
public static ASTNameInContext getDefinition(IASTSimpleDeclaration simpleDeclaration,
|
public static ASTNameInContext getDefinition(IASTSimpleDeclaration simpleDeclaration,
|
||||||
RefactoringASTCache astCache) throws CoreException {
|
RefactoringASTCache astCache) throws CoreException {
|
||||||
IASTDeclarator declarator = simpleDeclaration.getDeclarators()[0];
|
|
||||||
IIndex index = astCache.getIndex();
|
IIndex index = astCache.getIndex();
|
||||||
|
IASTDeclarator declarator = simpleDeclaration.getDeclarators()[0];
|
||||||
if (index == null) {
|
if (index == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,9 +53,9 @@ public class NodeHelper {
|
||||||
} else if (parent instanceof CPPASTNamespaceDefinition) {
|
} else if (parent instanceof CPPASTNamespaceDefinition) {
|
||||||
return ((CPPASTNamespaceDefinition) parent).getDeclarations();
|
return ((CPPASTNamespaceDefinition) parent).getDeclarations();
|
||||||
}
|
}
|
||||||
return new IASTDeclaration[0];
|
return IASTDeclaration.EMPTY_DECLARATION_ARRAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IASTNode findFollowingNode(IASTNode currentNode) {
|
public static IASTNode findFollowingNode(IASTNode currentNode) {
|
||||||
if (currentNode == null || currentNode.getParent() == null) {
|
if (currentNode == null || currentNode.getParent() == null) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -71,14 +71,14 @@ public class NodeHelper {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IASTNode findTopLevelParent(IASTNode currentNode) {
|
public static IASTNode findTopLevelParent(IASTNode currentNode) {
|
||||||
while (currentNode != null && currentNode.getParent() != null && currentNode.getParent().getParent() != null) {
|
while (currentNode != null && currentNode.getParent() != null && currentNode.getParent().getParent() != null) {
|
||||||
return findTopLevelParent(currentNode.getParent());
|
return findTopLevelParent(currentNode.getParent());
|
||||||
}
|
}
|
||||||
return currentNode;
|
return currentNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isSameNode(IASTNode node1, IASTNode node2) {
|
public static boolean isSameNode(IASTNode node1, IASTNode node2) {
|
||||||
if (node1 == null || node2 == null) {
|
if (node1 == null || node2 == null) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -87,7 +87,7 @@ public class NodeHelper {
|
||||||
&& node1.getNodeLocations()[0].getNodeLength() == node2.getNodeLocations()[0].getNodeLength()
|
&& node1.getNodeLocations()[0].getNodeLength() == node2.getNodeLocations()[0].getNodeLength()
|
||||||
&& new Path(node1.getFileLocation().getFileName()).equals(new Path(node2.getFileLocation().getFileName()));
|
&& new Path(node1.getFileLocation().getFileName()).equals(new Path(node2.getFileLocation().getFileName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IASTSimpleDeclaration findSimpleDeclarationInParents(IASTNode node) {
|
public static IASTSimpleDeclaration findSimpleDeclarationInParents(IASTNode node) {
|
||||||
while (node != null){
|
while (node != null){
|
||||||
if (node instanceof IASTSimpleDeclaration) {
|
if (node instanceof IASTSimpleDeclaration) {
|
||||||
|
@ -97,7 +97,7 @@ public class NodeHelper {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MethodContext findMethodContext(IASTNode node, IIndex index) throws CoreException{
|
public static MethodContext findMethodContext(IASTNode node, IIndex index) throws CoreException{
|
||||||
IASTTranslationUnit translationUnit = node.getTranslationUnit();
|
IASTTranslationUnit translationUnit = node.getTranslationUnit();
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
|
@ -171,7 +171,7 @@ public class NodeHelper {
|
||||||
context.setMethodQName(qname);
|
context.setMethodQName(qname);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IASTCompoundStatement findCompoundStatementInAncestors(IASTNode node) {
|
public static IASTCompoundStatement findCompoundStatementInAncestors(IASTNode node) {
|
||||||
while (node != null){
|
while (node != null){
|
||||||
if (node instanceof IASTCompoundStatement) {
|
if (node instanceof IASTCompoundStatement) {
|
||||||
|
@ -181,7 +181,7 @@ public class NodeHelper {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IASTCompositeTypeSpecifier findClassInAncestors(IASTNode node) {
|
public static IASTCompositeTypeSpecifier findClassInAncestors(IASTNode node) {
|
||||||
while (!(node instanceof IASTCompositeTypeSpecifier)){
|
while (!(node instanceof IASTCompositeTypeSpecifier)){
|
||||||
if (node instanceof IASTTranslationUnit) {
|
if (node instanceof IASTTranslationUnit) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue