|
From: <jas...@us...> - 2006-06-30 04:10:24
|
Revision: 1504 Author: jasonpmorrison Date: 2006-06-29 21:10:18 -0700 (Thu, 29 Jun 2006) ViewCVS: http://svn.sourceforge.net/rubyeclipse/?rev=1504&view=rev Log Message: ----------- - Added additional local var matching - Added instance var matching Modified Paths: -------------- branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultReferenceFinder.java Modified: branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultReferenceFinder.java =================================================================== --- branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultReferenceFinder.java 2006-06-30 04:10:03 UTC (rev 1503) +++ branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultReferenceFinder.java 2006-06-30 04:10:18 UTC (rev 1504) @@ -11,6 +11,8 @@ import org.jruby.ast.ArgumentNode; import org.jruby.ast.BlockNode; import org.jruby.ast.CallNode; +import org.jruby.ast.ClassNode; +import org.jruby.ast.Colon2Node; import org.jruby.ast.DVarNode; import org.jruby.ast.DefnNode; import org.jruby.ast.DefsNode; @@ -18,6 +20,7 @@ import org.jruby.ast.GlobalVarNode; import org.jruby.ast.InstAsgnNode; import org.jruby.ast.InstVarNode; +import org.jruby.ast.ListNode; import org.jruby.ast.LocalAsgnNode; import org.jruby.ast.LocalVarNode; import org.jruby.ast.Node; @@ -58,7 +61,6 @@ System.out.println("Origin: " + orig.getClass().getName()); - // LocalAsgnNode if ( isLocalVarRef(orig) ) { pushLocalVarRefs( root, orig, references ); } @@ -81,7 +83,17 @@ private ISourcePosition getPositionOfName(Node node, Node scope) { ISourcePosition pos = node.getPosition(); + + //todo: refactor the getting-of-name String name = getLocalVarRefName(node, scope); + if ( name == null ) + { + name = getInstVarRefName(node, scope); + } + if ( name == null ) + { + System.err.println("Couldn't get the name for: " + node.toString() + " in " + scope.toString() ); + } return new SourcePosition(pos.getFile(), pos.getStartLine(), pos.getEndLine(), pos.getStartOffset(), pos.getStartOffset() + name.length() ); } @@ -107,6 +119,24 @@ if ( scope instanceof DefsNode ) { return ((DefsNode)scope).getBodyNode().getLocalNames()[((LocalVarNode)node).getCount()]; } + + // No enclosing ScopeNode found, try searching backwards for an AsgnNode + final int localVarCount = ((LocalVarNode)node).getCount(); + Node previousAssign = FirstPrecursorNodeLocator.Instance().findFirstPrecursor(scope, node.getPosition().getStartOffset(), new INodeAcceptor() { + public boolean doesAccept(Node node) { + if ( node instanceof LocalAsgnNode ) + { + return ((LocalAsgnNode)node).getCount() == localVarCount; + } + return false; + } + }); + if ( previousAssign != null ) + { + return ((LocalAsgnNode)previousAssign).getName(); + } + + System.err.println("Unhandled scope for local var ref node found: " + scope.toString() ); //TODO: if scope instanceof Block Body? what type is this.. } @@ -114,9 +144,41 @@ return ((DVarNode)node).getName(); } +// System.err.println("Encountered unhandled node type in getLocalVarRefName: " + node.toString() + " in " + scope.toString()); return null; } + private String getClassNodeName( ClassNode classNode ) { + if (classNode.getCPath() instanceof Colon2Node) { + Colon2Node c2node = (Colon2Node) classNode.getCPath(); + return c2node.getName(); + } + System.err.println("ClassNode.getCPath() returned other than Colon2Node: " + classNode.toString() ); + return null; + } + + + private String getInstVarRefName( Node node, Node scope ) { + if ( node instanceof InstAsgnNode ) { + return ((InstAsgnNode)node).getName(); + } + + if ( node instanceof ArgumentNode ) { + return ((InstAsgnNode)node).getName(); + } + + if ( node instanceof InstVarNode ) { + return ((InstVarNode)node).getName(); + } + + if ( node instanceof DVarNode ) { + return ((DVarNode)node).getName(); + } + +// System.err.println("Encountered unhandled node type for getInstVarRefName: " + node.toString() + " in " + scope.toString()); + return null; + } + private boolean isLocalVarRef( Node node ) { return ( ( node instanceof LocalAsgnNode ) || ( node instanceof ArgumentNode ) || ( node instanceof LocalVarNode ) ); } @@ -135,15 +197,22 @@ private void pushLocalVarRefs( Node root, Node orig, List<ISourcePosition> references ) { -// System.out.println("Finding references for a local variable " + orig.toString()); + System.out.println("Finding references for a local variable " + orig.toString()); // Find the search space - final Node searchSpace = FirstPrecursorNodeLocator.Instance().findFirstPrecursor(root, orig.getPosition().getStartOffset(), new INodeAcceptor() { + 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; + } + + // Finalize searchSpace because Java's scoping rules are the awesome + final Node finalSearchSpace = searchSpace; // Get name of local variable reference final String origName = getLocalVarRefName(orig,searchSpace); @@ -151,7 +220,7 @@ // Find all pertinent nodes List<Node> searchResults = ScopedNodeLocator.Instance().findNodesInScope(searchSpace, new INodeAcceptor() { public boolean doesAccept(Node node) { - String name = getLocalVarRefName(node, searchSpace); + String name = getLocalVarRefName(node, finalSearchSpace); // System.out.println("Matching name" + name); return ( name != null && name.equals(origName)); } @@ -166,7 +235,61 @@ } private void pushInstVarRefs( Node root, Node orig, List<ISourcePosition> references ) { + System.out.println("Finding references for an instance variable " + orig.toString() ); + Node searchSpace; + + // Find the name of the enclosing class + ClassNode enclosingClass = (ClassNode)FirstPrecursorNodeLocator.Instance().findFirstPrecursor(root, orig.getPosition().getStartOffset(), new INodeAcceptor() { + public boolean doesAccept(Node node) { + return ( node instanceof ClassNode ); + } + }); + + // If no enclosing class is identified, search root. + if ( enclosingClass == null ) { + searchSpace = root; + } + // Find the search space - all ClassNodes for that name within root scope + else { + final String className = getClassNodeName(enclosingClass); + List<Node> classNodes = ScopedNodeLocator.Instance().findNodesInScope(root, new INodeAcceptor() { + public boolean doesAccept(Node node) { + if ( node instanceof ClassNode ) + { + return getClassNodeName((ClassNode)node).equals(className); + } + return false; + } + }); + //todo: is this cool with the "n/a" and all? + BlockNode blockNode = new BlockNode(new SourcePosition("n/a",0));//new ListNode(new SourcePosition("n/a",0)); + for ( Node classNode : classNodes ) + { + blockNode.add( classNode ); + } + searchSpace = blockNode; + } + + // Finalize searchSpace because Java's scoping rules are the awesome + final Node finalSearchSpace = searchSpace; + + // Get name of local variable reference + final String origName = getInstVarRefName(orig,searchSpace); + + // Find all pertinent nodes + List<Node> searchResults = ScopedNodeLocator.Instance().findNodesInScope(searchSpace, new INodeAcceptor() { + public boolean doesAccept(Node node) { + String name = getInstVarRefName(node, finalSearchSpace); + return ( name != null && name.equals(origName)); + } + }); + + // Scrape position from pertinent nodes + for ( Node searchResult : searchResults ) { + references.add(getPositionOfName(searchResult, searchSpace)); + } + } private void pushGlobalVarRefs( Node root, Node orig, List<ISourcePosition> references ) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |