|
From: <jas...@us...> - 2006-07-11 04:46:47
|
Revision: 1510 Author: jasonpmorrison Date: 2006-07-10 21:46:35 -0700 (Mon, 10 Jul 2006) ViewCVS: http://svn.sourceforge.net/rubyeclipse/?rev=1510&view=rev Log Message: ----------- Update action for Mark Occurences Modified Paths: -------------- branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultReferenceFinder.java Added Paths: ----------- branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/AbstractOccurencesFinder.java 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/IOccurrencesFinder.java Removed Paths: ------------- branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultOccurenceFinder.java Added: branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/AbstractOccurencesFinder.java =================================================================== --- branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/AbstractOccurencesFinder.java (rev 0) +++ branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/AbstractOccurencesFinder.java 2006-07-11 04:46:35 UTC (rev 1510) @@ -0,0 +1,48 @@ +package org.rubypeople.rdt.internal.ti; + +import java.util.Collection; +import java.util.List; + +import org.eclipse.jface.text.IDocument; +import org.jruby.ast.Node; +import org.rubypeople.rdt.core.IRubyElement; + +public class AbstractOccurencesFinder implements IOccurrencesFinder { + + public void collectOccurrenceMatches(IRubyElement element, + IDocument document, Collection resultingMatches) { + // TODO Auto-generated method stub + + } + + public String getElementName() { + // TODO Auto-generated method stub + return null; + } + + public String getJobLabel() { + // TODO Auto-generated method stub + return null; + } + + public String getUnformattedPluralLabel() { + // TODO Auto-generated method stub + return null; + } + + public String getUnformattedSingularLabel() { + // TODO Auto-generated method stub + return null; + } + + public String initialize(Node root, int offset, int length) { + // TODO Auto-generated method stub + return null; + } + + public List perform() { + // TODO Auto-generated method stub + return null; + } + +} Deleted: branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultOccurenceFinder.java =================================================================== --- branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultOccurenceFinder.java 2006-07-11 04:46:28 UTC (rev 1509) +++ branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultOccurenceFinder.java 2006-07-11 04:46:35 UTC (rev 1510) @@ -1,58 +0,0 @@ -package org.rubypeople.rdt.internal.ti; - -import java.util.Collection; -import java.util.List; - -import org.eclipse.jface.text.IDocument; -import org.jruby.ast.Node; -import org.rubypeople.rdt.core.IRubyElement; -import org.rubypeople.rdt.internal.ti.util.OffsetNodeLocator; -//import org.rubypeople.rdt.internal.ui.search.IOccurrencesFinder; - -public class DefaultOccurenceFinder /* implements IOccurrencesFinder */ { - - public void collectOccurrenceMatches(IRubyElement element, IDocument document, Collection resultingMatches) { - // TODO Auto-generated method stub - } - - public String getElementName() { - // TODO Auto-generated method stub - return null; - } - - public String getJobLabel() { - // TODO Auto-generated method stub - return null; - } - - public String getUnformattedPluralLabel() { - // TODO Auto-generated method stub - return null; - } - - public String getUnformattedSingularLabel() { - // TODO Auto-generated method stub - return null; - } - - private Node root; - private Node orig; - - public String initialize(Node root, int offset, int length) { - this.root = root; - this.orig = OffsetNodeLocator.Instance().getNodeAtOffset(root, offset); - if ( orig.getPosition().getEndOffset() > offset + length ) - { - // Selection spans nodes; not handling that for now. - return "Selection spans nodes; can only search for a single node."; - } - - return null; - } - - public List perform() { - // TODO Auto-generated method stub - return null; - } - -} Added: 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 (rev 0) +++ branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultOccurrencesFinder.java 2006-07-11 04:46:35 UTC (rev 1510) @@ -0,0 +1,385 @@ +package org.rubypeople.rdt.internal.ti; + +import java.util.LinkedList; +import java.util.List; + +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.ConstNode; +import org.jruby.ast.DVarNode; +import org.jruby.ast.DefnNode; +import org.jruby.ast.DefsNode; +import org.jruby.ast.GlobalAsgnNode; +import org.jruby.ast.GlobalVarNode; +import org.jruby.ast.InstAsgnNode; +import org.jruby.ast.InstVarNode; +import org.jruby.ast.LocalAsgnNode; +import org.jruby.ast.LocalVarNode; +import org.jruby.ast.Node; +import org.jruby.ast.VCallNode; +import org.jruby.lexer.yacc.ISourcePosition; +import org.jruby.lexer.yacc.SourcePosition; +import org.rubypeople.rdt.internal.ti.util.FirstPrecursorNodeLocator; +import org.rubypeople.rdt.internal.ti.util.INodeAcceptor; +import org.rubypeople.rdt.internal.ti.util.OffsetNodeLocator; +import org.rubypeople.rdt.internal.ti.util.ScopedNodeLocator; + +/** + * Implements "Mark Occurences" feature + * @author Jason Morrison + * + */ +public class DefaultOccurrencesFinder extends AbstractOccurencesFinder { + + // Root of the document to search + private Node root; + + // Originating node; corresponds to cursor selection + private Node orig; + + public String initialize(Node root, int offset, int length) { + this.root = root; + this.orig = OffsetNodeLocator.Instance().getNodeAtOffset(root, offset); + if ( orig.getPosition().getEndOffset() > offset + length ) + { + // Selection spans nodes; not handling that for now. + return "Selection spans nodes; can only search for a single node."; + } + + return null; + } + + public List<ISourcePosition> perform() { + // References to return + List<ISourcePosition> references = new LinkedList<ISourcePosition>(); + + if ( isLocalVarRef(orig) ) { + pushLocalVarRefs( root, orig, references ); + } + + if ( isInstanceVarRef(orig) ) { + pushInstVarRefs( root, orig, references ); + } + + if ( isGlobalVarRef(orig) ) { + pushGlobalVarRefs( root, orig, references ); + } + + // if ( isMethodRefNode(orig)) { + // pushMethodRefs( root, orig, references ); + // } + + if ( orig instanceof ConstNode ) + { + pushConstRefs( root, orig, references ); + } + + return references; + } + + private ISourcePosition getPositionOfName(Node node, Node scope) + { + ISourcePosition pos = node.getPosition(); + + //todo: refactor the getting-of-name + String name = null; + if ( isLocalVarRef(node) ) { name = getLocalVarRefName(node, scope); } + if ( isInstanceVarRef(node) ) { name = getInstVarRefName(node, scope); } + if ( isGlobalVarRef(node) ) { name = getGlobalVarRefName(node); } + if ( node instanceof ConstNode ) { name = ((ConstNode)node).getName(); } + + 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() ); + } + + /** + * Returns the name of a local var ref (LocalAsgnNode, ArgumentNode, LocalVarNode) + * @param node Node to get the name of + * @param scope Enclosing scope (to scrape args, etc.) + * @return + */ + private String getLocalVarRefName( Node node, Node scope ) { + if (node instanceof LocalAsgnNode) { + return ((LocalAsgnNode)node).getName(); + } + + if ( node instanceof ArgumentNode ) { + return ((ArgumentNode)node).getName(); + } + + if ( node instanceof LocalVarNode ) { + if ( scope instanceof DefnNode ) { + return ((DefnNode)scope).getBodyNode().getLocalNames()[((LocalVarNode)node).getCount()]; + } + 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.. + } + + if ( node instanceof DVarNode ) { + 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 String getGlobalVarRefName( Node node ) { + if ( node instanceof GlobalVarNode ) + { + return ((GlobalVarNode)node).getName(); + } + if ( node instanceof GlobalAsgnNode ) { + return ((GlobalAsgnNode)node).getName(); + } + return null; + } + + private String getMethodRefName( Node node ) { + if ( node instanceof DefnNode ) { + return ((DefnNode)node).getName(); + } + if ( node instanceof DefsNode ) { + return ((DefsNode)node).getName(); + } + if ( node instanceof CallNode ) { + return ((CallNode)node).getName(); + } + if ( node instanceof VCallNode ) { + return ((VCallNode)node).getMethodName(); + } + return null; + } + + + + private boolean isLocalVarRef( Node node ) { + return ( ( node instanceof LocalAsgnNode ) || ( node instanceof ArgumentNode ) || ( node instanceof LocalVarNode ) ); + } + + private boolean isInstanceVarRef( Node node ) { + return ( ( node instanceof InstAsgnNode ) || ( node instanceof InstVarNode ) ) ; + } + + private boolean isGlobalVarRef( Node node ) { + return ( ( node instanceof GlobalAsgnNode ) || ( node instanceof GlobalVarNode ) ); + } + + private boolean isMethodRefNode( Node node ) { + return ( ( node instanceof DefnNode ) || ( node instanceof DefsNode ) || ( node instanceof CallNode ) || ( node instanceof VCallNode ) ); + } + + + private void pushLocalVarRefs( Node root, Node orig, List<ISourcePosition> references ) { + System.out.println("Finding references 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; + } + + // 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); + + // Find all pertinent nodes + List<Node> searchResults = ScopedNodeLocator.Instance().findNodesInScope(searchSpace, new INodeAcceptor() { + public boolean doesAccept(Node node) { + String name = getLocalVarRefName(node, finalSearchSpace); +// System.out.println("Matching name" + name); + return ( name != null && name.equals(origName)); + } + }); + + // Scrape position from pertinent nodes + for ( Node searchResult : searchResults ) { + references.add(getPositionOfName(searchResult, searchSpace)); + } + +// System.out.println("Searching search space " + searchSpace.toString() + searchSpace.getPosition().toString() ); + } + + 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; + } + }); + BlockNode blockNode = new BlockNode(new SourcePosition("",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) { + if ( isInstanceVarRef(node) ) + { + String name = getInstVarRefName(node, finalSearchSpace); + return ( name != null && name.equals(origName)); + } + return false; + } + }); + + // Scrape position from pertinent nodes + for ( Node searchResult : searchResults ) { + references.add(getPositionOfName(searchResult, searchSpace)); + } + + } + + private void pushGlobalVarRefs( Node root, Node orig, List<ISourcePosition> references ) { + final Node searchSpace = root; + final String origName = getGlobalVarRefName(orig); + + // Find all pertinent nodes + List<Node> searchResults = ScopedNodeLocator.Instance().findNodesInScope(searchSpace, new INodeAcceptor() { + public boolean doesAccept(Node node) { + return isGlobalVarRef(node) && getGlobalVarRefName(node).equals(origName); + } + }); + + // Scrape position from pertinent nodes + for ( Node searchResult : searchResults ) { + references.add(getPositionOfName(searchResult, searchSpace)); + } + } + + //todo: complete +// private void pushMethodRefs( Node root, Node orig, List<ISourcePosition> references) { +// +// // DefnNode DefsNode CallNode VCallNode +// +// System.out.println("Finding references for method reference node " + orig.toString() ); +// +// final Node searchSpace = root; +// String origName = getMethodRefName(orig); +// +// // If orig is a method definition, find all references to that selector for the orig's enclosing type +// if ( orig instanceof DefnNode || orig instanceof DefsNode ) +// { +// ((DefnNode)orig).g +// } +// +// Node receiver = getMethodReceiver(orig); +// } + + private void pushConstRefs( Node root, Node orig, List<ISourcePosition> references) { + if ( !( orig instanceof ConstNode) ) + { + return; + } + + final String matchName = ((ConstNode)orig).getName(); + List <Node> searchResults = ScopedNodeLocator.Instance().findNodesInScope(root, new INodeAcceptor() { + public boolean doesAccept(Node node) { + if ( node instanceof ConstNode ) + { + return ((ConstNode)node).getName().equals(matchName); + } + return false; + } + }); + + for ( Node searchResult : searchResults ) { + references.add(getPositionOfName(searchResult, root ) ); + } + } +} 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-07-11 04:46:28 UTC (rev 1509) +++ branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultReferenceFinder.java 2006-07-11 04:46:35 UTC (rev 1510) @@ -1,13 +1,8 @@ package org.rubypeople.rdt.internal.ti; -import java.io.FileReader; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.LinkedList; import java.util.List; -import java.util.Map; -import org.jruby.ast.ArgsNode; import org.jruby.ast.ArgumentNode; import org.jruby.ast.BlockNode; import org.jruby.ast.CallNode; @@ -21,27 +16,16 @@ 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; import org.jruby.ast.VCallNode; -import org.jruby.ast.types.INameNode; import org.jruby.lexer.yacc.ISourcePosition; import org.jruby.lexer.yacc.SourcePosition; -import org.rubypeople.rdt.core.IRubyElement; -import org.rubypeople.rdt.core.ISourceRange; -import org.rubypeople.rdt.core.RubyModelException; -import org.rubypeople.rdt.internal.core.NamedMember; -import org.rubypeople.rdt.internal.core.RubyElement; -import org.rubypeople.rdt.internal.core.RubyScriptStructureBuilder; -import org.rubypeople.rdt.internal.core.SourceRange; -import org.rubypeople.rdt.internal.core.SourceRefElement; import org.rubypeople.rdt.internal.core.parser.RdtWarnings; import org.rubypeople.rdt.internal.core.parser.RubyParser; import org.rubypeople.rdt.internal.ti.util.FirstPrecursorNodeLocator; import org.rubypeople.rdt.internal.ti.util.INodeAcceptor; -import org.rubypeople.rdt.internal.ti.util.NodeLocator; import org.rubypeople.rdt.internal.ti.util.OffsetNodeLocator; import org.rubypeople.rdt.internal.ti.util.ScopedNodeLocator; @@ -74,9 +58,9 @@ pushGlobalVarRefs( root, orig, references ); } - if ( isMethodRefNode(orig)) { - pushMethodRefs( root, orig, references ); - } +// if ( isMethodRefNode(orig)) { +// pushMethodRefs( root, orig, references ); +// } if ( orig instanceof ConstNode ) { @@ -348,24 +332,25 @@ } } - private void pushMethodRefs( Node root, Node orig, List<ISourcePosition> references) { + //todo: complete +// private void pushMethodRefs( Node root, Node orig, List<ISourcePosition> references) { +// +// // DefnNode DefsNode CallNode VCallNode +// +// System.out.println("Finding references for method reference node " + orig.toString() ); +// +// final Node searchSpace = root; +// String origName = getMethodRefName(orig); +// +// // If orig is a method definition, find all references to that selector for the orig's enclosing type +// if ( orig instanceof DefnNode || orig instanceof DefsNode ) +// { +// ((DefnNode)orig).g +// } +// +// Node receiver = getMethodReceiver(orig); +// } - // DefnNode DefsNode CallNode VCallNode - - System.out.println("Finding references for method reference node " + orig.toString() ); - - final Node searchSpace = root; - String origName = getMethodRefName(orig); - - // If orig is a method definition, find all references to that selector for the orig's enclosing type - if ( orig instanceof DefnNode || orig instanceof DefsNode ) - { - ((DefnNode)orig).g - } - - Node receiver = getMethodReceiver(orig); - } - private void pushConstRefs( Node root, Node orig, List<ISourcePosition> references) { if ( !( orig instanceof ConstNode) ) { Copied: branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/IOccurrencesFinder.java (from rev 1505, branches/type_inferrence/trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/search/IOccurrencesFinder.java) =================================================================== --- branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/IOccurrencesFinder.java (rev 0) +++ branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/IOccurrencesFinder.java 2006-07-11 04:46:35 UTC (rev 1510) @@ -0,0 +1,77 @@ +/******************************************************************************* + * Copyright (c) 2000, 2006 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.rubypeople.rdt.internal.ti; + +import java.util.Collection; +import java.util.List; + +import org.eclipse.jface.text.IDocument; +import org.jruby.ast.Node; +import org.rubypeople.rdt.core.IRubyElement; + +public interface IOccurrencesFinder { + + /** + * + * @param root + * root AST Node + * @param offset + * position in source where selection is + * @param length + * length of the selection + * @return + */ + public String initialize(Node root, int offset, int length); + + /** + * Returns a lit of AST Nodes back (which contain their associated + * positions). + * + * @return List of AST Nodes + */ + public List perform(); + + public String getJobLabel(); + + /** + * Returns the plural label for this finder with 3 placeholders: + * <ul> + * <li>{0} for the {@link #getElementName() element name}</li> + * <li>{1} for the number of results found</li> + * <li>{2} for the scope (name of the compilation unit)</li> + * </ul> + * + * @return the unformatted label + */ + public String getUnformattedPluralLabel(); + + /** + * Returns the singular label for this finder with 2 placeholders: + * <ul> + * <li>{0} for the {@link #getElementName() element name}</li> + * <li>{1} for the scope (name of the compilation unit)</li> + * </ul> + * + * @return the unformatted label + */ + public String getUnformattedSingularLabel(); + + /** + * Returns the name of the lement to look for or <code>null</code> if the + * finder hasn't been initialized yet. + * + * @return the name of the element + */ + public String getElementName(); + + public void collectOccurrenceMatches(IRubyElement element, + IDocument document, Collection resultingMatches); +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |