|
From: <jas...@us...> - 2006-07-20 22:34:33
|
Revision: 1535 Author: jasonpmorrison Date: 2006-07-20 15:34:27 -0700 (Thu, 20 Jul 2006) ViewCVS: http://svn.sourceforge.net/rubyeclipse/?rev=1535&view=rev Log Message: ----------- Initial support for mark occurrence on dynamic var refs (block args etc.) 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 22:15:32 UTC (rev 1534) +++ branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultOccurrencesFinder.java 2006-07-20 22:34:27 UTC (rev 1535) @@ -14,6 +14,7 @@ import org.jruby.ast.ClassVarNode; import org.jruby.ast.Colon2Node; import org.jruby.ast.ConstNode; +import org.jruby.ast.DAsgnNode; import org.jruby.ast.DVarNode; import org.jruby.ast.DefnNode; import org.jruby.ast.DefsNode; @@ -88,6 +89,10 @@ pushLocalVarRefs( root, orig, occurrences ); } + if ( isDVarRef(orig) ) { + pushDVarRefs( root, orig, occurrences ); + } + if ( isInstanceVarRef(orig) ) { pushInstVarRefs( root, orig, occurrences ); } @@ -140,6 +145,15 @@ } /** + * Determines whether a given node is a dynamic variable reference + * @param node + * @return + */ + private boolean isDVarRef( Node node ) { + return ( ( node instanceof DVarNode ) || ( node instanceof DAsgnNode ) ); + } + + /** * Determines whether a given node is an instance variable reference * @param node * @return @@ -232,6 +246,44 @@ } /** + * Collects all corresponding dynamic variable occurrences + * @param root Root node to search + * @param orig Originating node + * @param occurrences + */ + private void pushDVarRefs( Node root, Node orig, List<ISourcePosition> occurrences ) { +// System.out.println("Finding occurrences for a local variable " + orig.toString()); + + // Find the search space + Node searchSpace = FirstPrecursorNodeLocator.Instance().findFirstPrecursor(root, orig.getPosition().getStartOffset(), new INodeAcceptor() { + public boolean doesAccept(Node node) { + return ( ( node instanceof DefnNode ) || ( node instanceof DefsNode ) /*TODO: Block Body? */ ); + } + }); + + // If no enclosing node found, search the entire space + if ( searchSpace == null ) { + searchSpace = root; + } + + // Get name of local variable reference + final String origName = getDVarRefName(orig); + + // Find all pertinent nodes + List<Node> searchResults = ScopedNodeLocator.Instance().findNodesInScope(searchSpace, new INodeAcceptor() { + public boolean doesAccept(Node node) { + String name = getDVarRefName(node); + return ( name != null && name.equals(origName)); + } + }); + + // Scrape position from pertinent nodes + for ( Node searchResult : searchResults ) { + occurrences.add(getPositionOfName(searchResult, searchSpace)); + } + } + + /** * Collects all instance variable occurrences * @param root * @param orig @@ -479,6 +531,7 @@ //todo: refactor the getting-of-name String name = null; if ( isLocalVarRef(node) ) { name = getLocalVarRefName(node, scope); } + if ( isDVarRef(node) ) { name = getDVarRefName(node); } if ( isInstanceVarRef(node) ) { name = getInstVarRefName(node ); } if ( isGlobalVarRef(node) ) { name = getGlobalVarRefName(node); } if ( isClassVarRef(node) ) { name = getClassVarRefName(node); } @@ -487,7 +540,7 @@ if ( node instanceof ClassNode ) { name = getClassNodeName( (ClassNode)node ); String classDeclString = source.substring(pos.getStartOffset(), pos.getEndOffset()); - int begin = classDeclString.indexOf(name); + int begin = pos.getStartOffset() + classDeclString.indexOf(name); return new SourcePosition( pos.getFile(), pos.getStartLine(), pos.getEndLine(), begin, begin + name.length() ); } if ( node instanceof ModuleNode ) { @@ -561,6 +614,21 @@ } /** + * Gets the name of a dynamic variable reference + * @param node Dynamic variable reference + * @return + */ + private String getDVarRefName( Node node ) { + if ( node instanceof DVarNode ) { + return ((DVarNode)node).getName(); + } + if ( node instanceof DAsgnNode ) { + return ((DAsgnNode)node).getName(); + } + return null; + } + + /** * Gets the name of an instance variable reference * @param node Instance variable reference * @return This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |