From 2d27b6de65f00b3010602ab36eb7d8c7021ce662 Mon Sep 17 00:00:00 2001 From: Andrew Niefer Date: Thu, 12 May 2005 16:20:31 +0000 Subject: [PATCH] binary search for finding nodes in the DOM AST view --- .../cdt/ui/tests/DOMAST/DOMASTNodeLeaf.java | 11 ++ .../cdt/ui/tests/DOMAST/DOMASTNodeParent.java | 115 ++++++++++++------ 2 files changed, 92 insertions(+), 34 deletions(-) diff --git a/core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/DOMASTNodeLeaf.java b/core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/DOMASTNodeLeaf.java index 3cc4afd5856..71b51ad2b49 100644 --- a/core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/DOMASTNodeLeaf.java +++ b/core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/DOMASTNodeLeaf.java @@ -47,6 +47,7 @@ import org.eclipse.cdt.core.dom.ast.c.ICASTPointer; import org.eclipse.cdt.core.dom.ast.gnu.c.IGCCASTArrayRangeDesignator; import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTPointer; import org.eclipse.cdt.core.parser.util.ArrayUtil; +import org.eclipse.cdt.internal.core.dom.parser.ASTNode; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.ui.views.properties.IPropertyDescriptor; import org.eclipse.ui.views.properties.IPropertySource; @@ -324,6 +325,16 @@ public class DOMASTNodeLeaf implements IAdaptable { return filterFlag; } + public int relativeNodePosition( IASTNode n ){ + ASTNode astNode = (ASTNode) n; + ASTNode thisNode = (ASTNode) getNode(); + if( thisNode.getOffset() > astNode.getOffset() ) + return -1; + if( (thisNode.getOffset() + thisNode.getLength()) < (astNode.getOffset() + astNode.getLength()) ) + return 1; + return 0; + } + private static class ASTPropertySource implements IPropertySource { private static final String DOMEXCEPTION_THIS_METHOD_ISN_T_SUPPORTED_BY_THIS_OBJECT = "DOMException - this method isn't supported by this Object"; //$NON-NLS-1$ private static final String FLUSH_CACHE_METHOD_NAME = "flushCache"; //$NON-NLS-1$ diff --git a/core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/DOMASTNodeParent.java b/core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/DOMASTNodeParent.java index aac031d2f8b..fd6d281d988 100644 --- a/core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/DOMASTNodeParent.java +++ b/core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/DOMASTNodeParent.java @@ -240,6 +240,22 @@ public class DOMASTNodeParent extends DOMASTNodeLeaf { return null; // nothing found } + public int relativeNodePosition( IASTNode n ){ + ASTNode astNode = (ASTNode) n; + if( !cleanupedElements ){ + cleanChildren(); + } + if( children.length > 0 ){ + ASTNode first = (ASTNode) children[0].getNode(); + if( first.getOffset() > astNode.getOffset() ) + return -1; + ASTNode last = (ASTNode) children[ children.length - 1 ].getNode(); + if( (last.getOffset() + last.getLength()) < (astNode.getOffset() + astNode.getLength()) ) + return 1; + return 0; + } + return super.relativeNodePosition( n ); + } /** * Returns the DOMASTNodeParent that corresponds to the IASTNode. This is the DOMASTNodeParent * that represents the IASTNode in the DOM AST View. @@ -250,50 +266,81 @@ public class DOMASTNodeParent extends DOMASTNodeLeaf { public DOMASTNodeParent findTreeObject(IASTNode node, boolean useOffset) { if (node == null) return null; - IASTNode nodeToFind = node; - // first check this node before checking children if (equalNodes(node, this.getNode(), useOffset)) { return this; } - - // build the chain of nodes... and use it to search the tree for the DOMASTNodeParent that contains the node - IASTNode[] nodeChain = new IASTNode[DEFAULT_NODE_CHAIN_SIZE]; - IASTNode topNode = node; - nodeChain = (IASTNode[])ArrayUtil.append(IASTNode.class, nodeChain, topNode); - while(topNode.getParent() != null && !(topNode.getParent() instanceof IASTTranslationUnit)) { - topNode = topNode.getParent(); + if( !useOffset || node instanceof IASTPreprocessorStatement) { + IASTNode nodeToFind = node; + // build the chain of nodes... and use it to search the tree for the DOMASTNodeParent that contains the node + IASTNode[] nodeChain = new IASTNode[DEFAULT_NODE_CHAIN_SIZE]; + IASTNode topNode = node; nodeChain = (IASTNode[])ArrayUtil.append(IASTNode.class, nodeChain, topNode); - } - - // loop through the chain of nodes and use it to only search the necessary children required to find the node - DOMASTNodeLeaf[] childrenToSearch = children; - outerLoop: for(int i=nodeChain.length-1; i>=0; i--) { - if (nodeChain[i] != null) { - nodeToFind = nodeChain[i]; - - for(int j=0; j=0; i--) { + if (nodeChain[i] != null) { + nodeToFind = nodeChain[i]; + + for(int j=0; j