|
From: <jas...@us...> - 2006-07-20 04:52:45
|
Revision: 1530 Author: jasonpmorrison Date: 2006-07-19 21:52:38 -0700 (Wed, 19 Jul 2006) ViewCVS: http://svn.sourceforge.net/rubyeclipse/?rev=1530&view=rev Log Message: ----------- ConstNode/Class Decl Node matching Modified Paths: -------------- branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultOccurrencesFinder.java Modified: branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultOccurrencesFinder.java =================================================================== --- branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultOccurrencesFinder.java 2006-07-20 04:52:30 UTC (rev 1529) +++ branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultOccurrencesFinder.java 2006-07-20 04:52:38 UTC (rev 1530) @@ -20,6 +20,7 @@ import org.jruby.ast.InstVarNode; import org.jruby.ast.LocalAsgnNode; import org.jruby.ast.LocalVarNode; +import org.jruby.ast.ModuleNode; import org.jruby.ast.Node; import org.jruby.ast.SymbolNode; import org.jruby.ast.VCallNode; @@ -45,8 +46,11 @@ // Originating node; corresponds to cursor selection private Node orig; + // Original source + private String source; + public String initialize(String source, int offset, int length) { - + this.source = source; try { this.root = (new RubyParser()).parse(source); } @@ -95,7 +99,7 @@ // pushMethodRefs( root, orig, occurrences ); // } - if ( orig instanceof ConstNode ) + if ( isConstRef(orig) ) { pushConstRefs( root, orig, occurrences ); } @@ -153,6 +157,15 @@ return ( ( node instanceof DefnNode ) || ( node instanceof DefsNode ) || ( node instanceof CallNode ) || ( node instanceof VCallNode ) ); } + /** + * Determines whether a given node is a constant reference (class, module... others??) + * @param node + * @return + */ + private boolean isConstRef( Node node ) { + return ( ( node instanceof ConstNode ) || ( node instanceof ClassNode ) || ( node instanceof ModuleNode ) ); + } + // **************************************************************************** // * // * Worker methods - handles delegation of occurrence searches @@ -337,20 +350,20 @@ // } /** - * Collects all pertinent ConstNode occurrences + * Collects all pertinent const occurrences */ private void pushConstRefs( Node root, Node orig, List<ISourcePosition> occurrences) { - if ( !( orig instanceof ConstNode) ) + if ( !isConstRef(orig) ) { return; } - final String matchName = ((ConstNode)orig).getName(); + final String matchName = getConstRefName(orig); List <Node> searchResults = ScopedNodeLocator.Instance().findNodesInScope(root, new INodeAcceptor() { public boolean doesAccept(Node node) { - if ( node instanceof ConstNode ) + if ( isConstRef(node) ) { - return ((ConstNode)node).getName().equals(matchName); + return getConstRefName(node).equals(matchName); } return false; } @@ -382,12 +395,25 @@ if ( isLocalVarRef(node) ) { name = getLocalVarRefName(node, scope); } if ( isInstanceVarRef(node) ) { name = getInstVarRefName(node ); } if ( isGlobalVarRef(node) ) { name = getGlobalVarRefName(node); } +// if ( isConstRef(node) ) { name = getConstRefName(node); } if ( node instanceof ConstNode ) { name = ((ConstNode)node).getName(); } + if ( node instanceof ClassNode ) { + name = getClassNodeName( (ClassNode)node ); + String classDeclString = source.substring(pos.getStartOffset(), pos.getEndOffset()); + int begin = classDeclString.indexOf(name); + return new SourcePosition( pos.getFile(), pos.getStartLine(), pos.getEndLine(), begin, begin + name.length() ); + } + if ( node instanceof ModuleNode ) { + name = getModuleNodeName( (ModuleNode)node ); + String moduleDeclString = source.substring(pos.getStartOffset(), pos.getEndOffset()); + int begin = moduleDeclString.indexOf(name); + return new SourcePosition( pos.getFile(), pos.getStartLine(), pos.getEndLine(), begin, begin + name.length() ); + } + if ( node instanceof SymbolNode ) { //XXX: This is a hack to get around improper offsets in my JRuby copy; ":foo" returns offset for ":fo", so compensate by adding one name = ((SymbolNode)node).getName(); return new SourcePosition(pos.getFile(), pos.getStartLine(), pos.getEndLine(), pos.getStartOffset(), pos.getStartOffset() + name.length() + 1 ); - } if ( name == null ) @@ -519,4 +545,40 @@ return null; } + /** + * Helper method to get the class name from a ModuleNode + * @param classNode + * @return + */ + private String getModuleNodeName( ModuleNode moduleNode ) { + if ( moduleNode.getCPath() instanceof Colon2Node ) { + Colon2Node c2node = (Colon2Node) moduleNode.getCPath(); + return c2node.getName(); + } + System.err.println("ModuleNode.getCPath() returned other than Colon2Node: " + moduleNode.toString() ); + return null; + } + + /** + * Helper method to get the class name from a const ref node (Class/Module/Const) + * @param constRefNode + * @return + */ + private String getConstRefName( Node constRefNode ) { + if ( constRefNode instanceof ClassNode ) + { + return getClassNodeName( (ClassNode)constRefNode ); + } + if ( constRefNode instanceof ModuleNode ) + { + return getModuleNodeName( (ModuleNode)constRefNode ); + } + if ( constRefNode instanceof ConstNode ) + { + return ((ConstNode)constRefNode).getName(); + } + return null; + } + + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |