|
From: <caw...@us...> - 2007-02-09 15:08:46
|
Revision: 1943
http://svn.sourceforge.net/rubyeclipse/?rev=1943&view=rev
Author: cawilliams
Date: 2007-02-09 07:08:44 -0800 (Fri, 09 Feb 2007)
Log Message:
-----------
remove some duplicated code
Modified Paths:
--------------
trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/CompletionEngine.java
trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/util/ASTUtil.java
trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultOccurrencesFinder.java
Modified: trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/CompletionEngine.java
===================================================================
--- trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/CompletionEngine.java 2007-02-09 14:39:35 UTC (rev 1942)
+++ trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/CompletionEngine.java 2007-02-09 15:08:44 UTC (rev 1943)
@@ -43,6 +43,7 @@
import org.rubypeople.rdt.internal.core.RubyType;
import org.rubypeople.rdt.internal.core.parser.RubyParser;
import org.rubypeople.rdt.internal.core.search.ExperimentalIndex;
+import org.rubypeople.rdt.internal.core.util.ASTUtil;
import org.rubypeople.rdt.internal.ti.DefaultTypeInferrer;
import org.rubypeople.rdt.internal.ti.ITypeGuess;
import org.rubypeople.rdt.internal.ti.ITypeInferrer;
@@ -351,7 +352,7 @@
if (instanceAndClassVars != null) {
// Get the unique names of instance and class variables
for (Node varNode : instanceAndClassVars) {
- String name = getNameReflectively(varNode);
+ String name = ASTUtil.getNameReflectively(varNode);
if (!context.prefixStartsWith(name))
continue;
fields.add(name);
@@ -455,26 +456,6 @@
return new ArrayList<String>(0);
}
}
-
- /**
- * Gets the name of a node by reflectively invoking "getName()" on it;
- * helper method just to cut many "instanceof/cast" pairs.
- *
- * @param node
- * @return name or null
- */
- // TODO Copy/pasted from DefaultOccurrencesFinder, refactor these two
- // methods to a common location.
- private String getNameReflectively(Node node) {
- try {
- Method getNameMethod = node.getClass().getMethod("getName", new Class[] {});
- Object name = getNameMethod.invoke(node, new Object[0]);
- return (String) name;
- } catch (Exception e) {
- return null;
- }
- }
-
private class NodeMethod implements IMethod {
Modified: trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/util/ASTUtil.java
===================================================================
--- trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/util/ASTUtil.java 2007-02-09 14:39:35 UTC (rev 1942)
+++ trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/util/ASTUtil.java 2007-02-09 15:08:44 UTC (rev 1943)
@@ -1,5 +1,6 @@
package org.rubypeople.rdt.internal.core.util;
+import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -133,4 +134,24 @@
return buffer.toString();
}
+ /**
+ * Gets the name of a node by reflectively invoking "getName()" on it;
+ * helper method just to cut many "instanceof/cast" pairs.
+ *
+ * @param node
+ * @return name or null
+ */
+ public static String getNameReflectively(Node node) {
+ if (node instanceof INameNode) {
+ return ((INameNode)node).getName();
+ }
+ try {
+ Method getNameMethod = node.getClass().getMethod("getName", new Class[] {});
+ Object name = getNameMethod.invoke(node, new Object[0]);
+ return (String) name;
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
}
Modified: trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultOccurrencesFinder.java
===================================================================
--- trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultOccurrencesFinder.java 2007-02-09 14:39:35 UTC (rev 1942)
+++ trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultOccurrencesFinder.java 2007-02-09 15:08:44 UTC (rev 1943)
@@ -1,6 +1,5 @@
package org.rubypeople.rdt.internal.ti;
-import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
@@ -27,11 +26,11 @@
import org.jruby.ast.ModuleNode;
import org.jruby.ast.Node;
import org.jruby.ast.SymbolNode;
-import org.jruby.ast.types.INameNode;
import org.jruby.lexer.yacc.ISourcePosition;
import org.jruby.lexer.yacc.SourcePosition;
import org.jruby.lexer.yacc.SyntaxException;
import org.rubypeople.rdt.internal.core.parser.RubyParser;
+import org.rubypeople.rdt.internal.core.util.ASTUtil;
import org.rubypeople.rdt.internal.ti.util.FirstPrecursorNodeLocator;
import org.rubypeople.rdt.internal.ti.util.INodeAcceptor;
import org.rubypeople.rdt.internal.ti.util.OffsetNodeLocator;
@@ -39,186 +38,199 @@
/**
* Implements "Mark Occurences" feature
+ *
* @author Jason Morrison
- *
+ *
*/
-public class DefaultOccurrencesFinder extends AbstractOccurencesFinder {
+public class DefaultOccurrencesFinder extends AbstractOccurencesFinder {
// Root of the document to search
private Node root;
-
+
// Originating node; corresponds to cursor selection
private Node orig;
-
+
// Original source
private String source;
-
+
public String initialize(String source, int offset, int length) {
- if ( source == null ) { return null; }
-
+ if (source == null) {
+ return null;
+ }
+
this.source = source;
try {
RubyParser rubyParser = new RubyParser();
this.root = rubyParser.parse(source);
- if ( this.root == null ) { return null; }
+ if (this.root == null) {
+ return null;
+ }
}
- //TODO: Is there anything else the parsing could choke on that should be silently ignored with no markings?
- catch (SyntaxException se)
- {
+ // TODO: Is there anything else the parsing could choke on that should
+ // be silently ignored with no markings?
+ catch (SyntaxException se) {
this.root = null;
return null;
}
this.orig = OffsetNodeLocator.Instance().getNodeAtOffset(root, offset);
- if ( orig == null ) { return null; }
- if ( orig.getPosition().getEndOffset() > offset + length )
- {
+ if (orig == null) {
+ return null;
+ }
+ 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;
}
/**
- * Determines the kind of originating node, and collects occurrences accordingly
+ * Determines the kind of originating node, and collects occurrences
+ * accordingly
*/
public List<Position> perform() {
- // Mark no occurrences if root is null (AST couldn't be parsed correctly.)
- if ( root == null ) return new LinkedList<Position>();
- if ( orig == null ) return new LinkedList<Position>();
-
+ // Mark no occurrences if root is null (AST couldn't be parsed
+ // correctly.)
+ if (root == null)
+ return new LinkedList<Position>();
+ if (orig == null)
+ return new LinkedList<Position>();
+
// occurrences to return
List<ISourcePosition> occurrences = new LinkedList<ISourcePosition>();
- if ( fMarkLocalVariableOccurrences && isLocalVarRef(orig) ) {
- pushLocalVarRefs( root, orig, occurrences );
- }
-
- if ( fMarkLocalVariableOccurrences && isDVarRef(orig) ) {
- pushDVarRefs( root, orig, occurrences );
- }
-
- //XXX: Add pref for instvars
- if ( fMarkLocalVariableOccurrences && isInstanceVarRef(orig) ) {
- pushInstVarRefs( root, orig, occurrences );
- }
-
- //XXX: Add pref for classvars
- if ( fMarkLocalVariableOccurrences && isClassVarRef(orig) ) {
- pushClassVarRefs( root, orig, occurrences );
- }
-
- //XXX: Add pref for global vars
- if ( fMarkLocalVariableOccurrences && isGlobalVarRef(orig) ) {
- pushGlobalVarRefs( root, orig, occurrences );
- }
-
- //XXX: Add pref for symbols
- if ( fMarkConstantOccurrences && orig instanceof SymbolNode ) {
- pushSymbolRefs( root, orig, occurrences );
- }
-
+ if (fMarkLocalVariableOccurrences && isLocalVarRef(orig)) {
+ pushLocalVarRefs(root, orig, occurrences);
+ }
+
+ if (fMarkLocalVariableOccurrences && isDVarRef(orig)) {
+ pushDVarRefs(root, orig, occurrences);
+ }
+
+ // XXX: Add pref for instvars
+ if (fMarkLocalVariableOccurrences && isInstanceVarRef(orig)) {
+ pushInstVarRefs(root, orig, occurrences);
+ }
+
+ // XXX: Add pref for classvars
+ if (fMarkLocalVariableOccurrences && isClassVarRef(orig)) {
+ pushClassVarRefs(root, orig, occurrences);
+ }
+
+ // XXX: Add pref for global vars
+ if (fMarkLocalVariableOccurrences && isGlobalVarRef(orig)) {
+ pushGlobalVarRefs(root, orig, occurrences);
+ }
+
+ // XXX: Add pref for symbols
+ if (fMarkConstantOccurrences && orig instanceof SymbolNode) {
+ pushSymbolRefs(root, orig, occurrences);
+ }
+
// if ( isMethodRefNode(orig)) {
// pushMethodRefs( root, orig, occurrences );
// }
-
- if ( fMarkConstantOccurrences && isConstRef(orig) )
- {
- pushConstRefs( root, orig, occurrences );
- }
-
- if ( fMarkTypeOccurrences && isTypeRef(orig) )
- {
- pushTypeRefs( root, orig, occurrences );
- }
-
- // Convert ISourcePosition to IPosition
- List<Position> positions = new LinkedList<Position>();
- for (ISourcePosition occurrence : occurrences) {
- Position position = new Position(occurrence.getStartOffset(),occurrence.getEndOffset() - occurrence.getStartOffset());
- positions.add(position);
+
+ if (fMarkConstantOccurrences && isConstRef(orig)) {
+ pushConstRefs(root, orig, occurrences);
}
-
- // Uniqueify positions
- positions = new LinkedList<Position>( new HashSet<Position>(positions) );
-
- return positions;
+
+ if (fMarkTypeOccurrences && isTypeRef(orig)) {
+ pushTypeRefs(root, orig, occurrences);
+ }
+
+ // Convert ISourcePosition to IPosition
+ List<Position> positions = new LinkedList<Position>();
+ for (ISourcePosition occurrence : occurrences) {
+ Position position = new Position(occurrence.getStartOffset(), occurrence.getEndOffset() - occurrence.getStartOffset());
+ positions.add(position);
+ }
+
+ // Uniqueify positions
+ positions = new LinkedList<Position>(new HashSet<Position>(positions));
+
+ return positions;
}
-
+
// ****************************************************************************
// *
// * Reference kind definitions
// *
// ****************************************************************************
-
/**
* Determines whether a given node is a local variable reference
+ *
* @param node
* @return
*/
- private boolean isLocalVarRef( Node node ) {
- return ( ( node instanceof LocalAsgnNode ) || ( node instanceof ArgumentNode ) || ( node instanceof LocalVarNode ) );
+ private boolean isLocalVarRef(Node node) {
+ return ((node instanceof LocalAsgnNode) || (node instanceof ArgumentNode) || (node instanceof LocalVarNode));
}
/**
* 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 ) );
+ 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
*/
- private boolean isInstanceVarRef( Node node ) {
- return ( ( node instanceof InstAsgnNode ) || ( node instanceof InstVarNode ) ) ;
+ private boolean isInstanceVarRef(Node node) {
+ return ((node instanceof InstAsgnNode) || (node instanceof InstVarNode));
}
/**
* Determines whether a given node is a class variable reference
+ *
* @param node
* @return
*/
- private boolean isClassVarRef( Node node ) {
- return ( ( node instanceof ClassVarNode ) || ( node instanceof ClassVarAsgnNode ) || ( node instanceof ClassVarDeclNode ) );
+ private boolean isClassVarRef(Node node) {
+ return ((node instanceof ClassVarNode) || (node instanceof ClassVarAsgnNode) || (node instanceof ClassVarDeclNode));
}
/**
* Determines whether a given node is a global variable reference
+ *
* @param node
* @return
*/
- private boolean isGlobalVarRef( Node node ) {
- return ( ( node instanceof GlobalAsgnNode ) || ( node instanceof GlobalVarNode ) );
+ private boolean isGlobalVarRef(Node node) {
+ return ((node instanceof GlobalAsgnNode) || (node instanceof GlobalVarNode));
}
-
+
/**
* Determines whether a given node is a constant reference (constant)
+ *
* @param node
* @return
*/
- private boolean isConstRef( Node node ) {
- return ( node instanceof ConstNode );
+ private boolean isConstRef(Node node) {
+ return (node instanceof ConstNode);
}
-
-
+
/**
* Determines whether a given node is a type reference (class, module)
+ *
* @param node
* @return
*/
- private boolean isTypeRef( Node node ) {
- //TODO: Classes can be referred to as a ConstNode; i.e. "class Klass;end; k = Klass.new" the last reference is a ConstNode, not a ClassNode. Special way to handle this?
- return ( ( node instanceof ClassNode ) || ( node instanceof ModuleNode ) || ( node instanceof ConstNode ));
+ private boolean isTypeRef(Node node) {
+ // TODO: Classes can be referred to as a ConstNode; i.e. "class
+ // Klass;end; k = Klass.new" the last reference is a ConstNode, not a
+ // ClassNode. Special way to handle this?
+ return ((node instanceof ClassNode) || (node instanceof ModuleNode) || (node instanceof ConstNode));
}
-
-
-
+
// ****************************************************************************
// *
// * Worker methods - handles delegation of occurrence searches
@@ -227,456 +239,380 @@
/**
* Collects all corresponding local variable occurrences
- * @param root Root node to search
- * @param orig Originating node
- * @param occurrences
+ *
+ * @param root
+ * Root node to search
+ * @param orig
+ * Originating node
+ * @param occurrences
*/
- private void pushLocalVarRefs( Node root, Node orig, List<ISourcePosition> occurrences ) {
-// System.out.println("Finding occurrences for a local variable " + orig.toString());
-
+ private void pushLocalVarRefs(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? */ );
+ return ((node instanceof DefnNode) || (node instanceof DefsNode) /*
+ * TODO:
+ * Block
+ * Body?
+ */);
}
});
-
+
// If no enclosing node found, search the entire space
- if ( searchSpace == null ) {
+ if (searchSpace == null) {
searchSpace = root;
}
-
+
// Finalize searchSpace because Java's scoping rules are the awesome
- final Node finalSearchSpace = searchSpace;
+ final Node finalSearchSpace = searchSpace;
// Get name of local variable reference
- final String origName = getLocalVarRefName(orig);
+ final String origName = ASTUtil.getNameReflectively(orig);
// Find all pertinent nodes
List<Node> searchResults = ScopedNodeLocator.Instance().findNodesInScope(searchSpace, new INodeAcceptor() {
public boolean doesAccept(Node node) {
- String name = getLocalVarRefName(node);
- return ( name != null && name.equals(origName));
+ String name = ASTUtil.getNameReflectively(node);
+ return (name != null && name.equals(origName));
}
});
-
+
// Scrape position from pertinent nodes
- for ( Node searchResult : searchResults ) {
+ for (Node searchResult : searchResults) {
occurrences.add(getPositionOfName(searchResult, searchSpace));
}
}
-
+
/**
* Collects all corresponding dynamic variable occurrences
- * @param root Root node to search
- * @param orig Originating node
- * @param 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());
-
+ 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? */ );
+ return ((node instanceof DefnNode) || (node instanceof DefsNode) /*
+ * TODO:
+ * Block
+ * Body?
+ */);
}
});
-
+
// If no enclosing node found, search the entire space
- if ( searchSpace == null ) {
+ if (searchSpace == null) {
searchSpace = root;
}
// Get name of local variable reference
- final String origName = getDVarRefName(orig);
+ final String origName = ASTUtil.getNameReflectively(orig);
// Find all pertinent nodes
List<Node> searchResults = ScopedNodeLocator.Instance().findNodesInScope(searchSpace, new INodeAcceptor() {
public boolean doesAccept(Node node) {
- if ( isDVarRef(node))
- {
- String name = getDVarRefName(node);
- return ( name != null && name.equals(origName));
+ if (isDVarRef(node)) {
+ String name = ASTUtil.getNameReflectively(node);
+ return (name != null && name.equals(origName));
}
return false;
}
});
-
+
// Scrape position from pertinent nodes
- for ( Node searchResult : searchResults ) {
+ for (Node searchResult : searchResults) {
occurrences.add(getPositionOfName(searchResult, searchSpace));
}
}
-
+
/**
* Collects all instance variable occurrences
- * @param root
+ *
+ * @param root
* @param orig
* @param occurrences
*/
- private void pushInstVarRefs( Node root, Node orig, List<ISourcePosition> occurrences ) {
-// System.out.println("Finding occurrences for an instance variable " + orig.toString() );
-
+ private void pushInstVarRefs(Node root, Node orig, List<ISourcePosition> occurrences) {
+ // System.out.println("Finding occurrences for an instance variable " +
+ // orig.toString() );
+
Node searchSpace = determineSearchSpace(root, orig);
-
+
// Finalize searchSpace because Java's scoping rules are the awesome
- //todo: not needed?
- //final Node finalSearchSpace = searchSpace;
-
+ // todo: not needed?
+ // final Node finalSearchSpace = searchSpace;
+
// Get name of local variable reference
- final String origName = getInstVarRefName(orig);
-
+ final String origName = ASTUtil.getNameReflectively(orig);
+
// 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);
- return ( name != null && name.equals(origName));
+ if (isInstanceVarRef(node)) {
+ String name = ASTUtil.getNameReflectively(node);
+ return (name != null && name.equals(origName));
}
return false;
}
});
-
+
// Scrape position from pertinent nodes
- for ( Node searchResult : searchResults ) {
+ for (Node searchResult : searchResults) {
occurrences.add(getPositionOfName(searchResult, searchSpace));
}
-
+
}
private Node determineSearchSpace(Node root, Node orig) {
// Find the name of the enclosing class
- ClassNode enclosingClass = (ClassNode)FirstPrecursorNodeLocator.Instance().findFirstPrecursor(root, orig.getPosition().getStartOffset(), new INodeAcceptor() {
+ ClassNode enclosingClass = (ClassNode) FirstPrecursorNodeLocator.Instance().findFirstPrecursor(root, orig.getPosition().getStartOffset(), new INodeAcceptor() {
public boolean doesAccept(Node node) {
- return ( node instanceof ClassNode );
+ return (node instanceof ClassNode);
}
});
-
- // If no enclosing class is identified, search root.
- if ( enclosingClass == null ) {
+
+ // If no enclosing class is identified, search root.
+ if (enclosingClass == null) {
return root;
}
- // Find the search space - all ClassNodes for that name within root scope
+ // 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);
+ 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 );
+ BlockNode blockNode = new BlockNode(new SourcePosition("", 0));
+ for (Node classNode : classNodes) {
+ blockNode.add(classNode);
}
return blockNode;
}
}
-
+
/**
* Collects all class variable occurrences
- * @param root
+ *
+ * @param root
* @param orig
* @param occurrences
*/
- private void pushClassVarRefs( Node root, Node orig, List<ISourcePosition> occurrences ) {
-// System.out.println("Finding occurrences for an instance variable " + orig.toString() );
+ private void pushClassVarRefs(Node root, Node orig, List<ISourcePosition> occurrences) {
+ // System.out.println("Finding occurrences for an instance variable " +
+ // orig.toString() );
Node searchSpace = determineSearchSpace(root, orig);
-
+
// Finalize searchSpace because Java's scoping rules are the awesome
- //todo: not needed?
- //final Node finalSearchSpace = searchSpace;
-
+ // todo: not needed?
+ // final Node finalSearchSpace = searchSpace;
+
// Get name of local variable reference
- final String origName = getClassVarRefName(orig);
-
+ final String origName = ASTUtil.getNameReflectively(orig);
+
// Find all pertinent nodes
List<Node> searchResults = ScopedNodeLocator.Instance().findNodesInScope(searchSpace, new INodeAcceptor() {
public boolean doesAccept(Node node) {
- if ( isClassVarRef(node) )
- {
- String name = getClassVarRefName(node);
- return ( name != null && name.equals(origName));
+ if (isClassVarRef(node)) {
+ String name = ASTUtil.getNameReflectively(node);
+ return (name != null && name.equals(origName));
}
return false;
}
});
-
+
// Scrape position from pertinent nodes
- for ( Node searchResult : searchResults ) {
+ for (Node searchResult : searchResults) {
occurrences.add(getPositionOfName(searchResult, searchSpace));
}
-
+
}
-
+
/**
* Collects all global variable occurrences
+ *
* @param root
* @param orig
* @param occurrences
*/
- private void pushGlobalVarRefs( Node root, Node orig, List<ISourcePosition> occurrences ) {
+ private void pushGlobalVarRefs(Node root, Node orig, List<ISourcePosition> occurrences) {
final Node searchSpace = root;
- final String origName = getGlobalVarRefName(orig);
-
+ final String origName = ASTUtil.getNameReflectively(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);
+ return isGlobalVarRef(node) && ASTUtil.getNameReflectively(node).equals(origName);
}
});
-
+
// Scrape position from pertinent nodes
- for ( Node searchResult : searchResults ) {
+ for (Node searchResult : searchResults) {
occurrences.add(getPositionOfName(searchResult, searchSpace));
- }
+ }
}
-
-
+
/**
* Collects all symbol occurrences
+ *
* @param root
* @param orig
* @param occurrences
*/
- private void pushSymbolRefs( Node root, Node orig, List<ISourcePosition> occurrences ) {
+ private void pushSymbolRefs(Node root, Node orig, List<ISourcePosition> occurrences) {
final Node searchSpace = root;
- final String origName = ((SymbolNode)orig).getName();
-
+ final String origName = ((SymbolNode) orig).getName();
+
// Find all pertinent nodes
List<Node> searchResults = ScopedNodeLocator.Instance().findNodesInScope(searchSpace, new INodeAcceptor() {
public boolean doesAccept(Node node) {
- return ( node instanceof SymbolNode ) && ((SymbolNode)node).getName().equals(origName);
+ return (node instanceof SymbolNode) && ((SymbolNode) node).getName().equals(origName);
}
});
-
+
// Scrape position from pertinent nodes
- for ( Node searchResult : searchResults ) {
+ for (Node searchResult : searchResults) {
occurrences.add(getPositionOfName(searchResult, searchSpace));
- }
+ }
}
-
-
-
- //todo: complete
-// private void pushMethodRefs( Node root, Node orig, List<ISourcePosition> occurrences) {
-//
-// // DefnNode DefsNode CallNode VCallNode
-//
-// System.out.println("Finding occurrences for method reference node " + orig.toString() );
-//
-// final Node searchSpace = root;
-// String origName = getMethodRefName(orig);
-//
-// // If orig is a method definition, find all occurrences to that selector for the orig's enclosing type
-// if ( orig instanceof DefnNode || orig instanceof DefsNode )
-// {
-// ((DefnNode)orig).g
-// }
-//
-// Node receiver = getMethodReceiver(orig);
-// }
-
+
+ // todo: complete
+ // private void pushMethodRefs( Node root, Node orig, List<ISourcePosition>
+ // occurrences) {
+ //
+ // // DefnNode DefsNode CallNode VCallNode
+ //
+ // System.out.println("Finding occurrences for method reference node " +
+ // orig.toString() );
+ //
+ // final Node searchSpace = root;
+ // String origName = getMethodRefName(orig);
+ //
+ // // If orig is a method definition, find all occurrences to that selector
+ // for the orig's enclosing type
+ // if ( orig instanceof DefnNode || orig instanceof DefsNode )
+ // {
+ // ((DefnNode)orig).g
+ // }
+ //
+ // Node receiver = getMethodReceiver(orig);
+ // }
+
/**
* Collects all pertinent const occurrences
*/
- private void pushConstRefs( Node root, Node orig, List<ISourcePosition> occurrences) {
- if ( !isConstRef(orig) )
- {
+ private void pushConstRefs(Node root, Node orig, List<ISourcePosition> occurrences) {
+ if (!isConstRef(orig)) {
return;
}
-
- final String matchName = getConstRefName(orig);
- List <Node> searchResults = ScopedNodeLocator.Instance().findNodesInScope(root, new INodeAcceptor() {
+
+ final String matchName = ASTUtil.getNameReflectively(orig);
+ List<Node> searchResults = ScopedNodeLocator.Instance().findNodesInScope(root, new INodeAcceptor() {
public boolean doesAccept(Node node) {
- if ( isConstRef(node) )
- {
- return getConstRefName(node).equals(matchName);
+ if (isConstRef(node)) {
+ return ASTUtil.getNameReflectively(node).equals(matchName);
}
return false;
}
});
-
- for ( Node searchResult : searchResults ) {
- occurrences.add(getPositionOfName(searchResult, root ) );
+
+ for (Node searchResult : searchResults) {
+ occurrences.add(getPositionOfName(searchResult, root));
}
}
-
+
/**
* Collects all pertinent type ref occurrences
*/
- private void pushTypeRefs( Node root, Node orig, List<ISourcePosition> occurrences) {
- if ( !isTypeRef(orig) )
- {
+ private void pushTypeRefs(Node root, Node orig, List<ISourcePosition> occurrences) {
+ if (!isTypeRef(orig)) {
return;
}
-
- final String matchName = getConstRefName(orig);
- List <Node> searchResults = ScopedNodeLocator.Instance().findNodesInScope(root, new INodeAcceptor() {
+
+ final String matchName = ASTUtil.getNameReflectively(orig);
+ List<Node> searchResults = ScopedNodeLocator.Instance().findNodesInScope(root, new INodeAcceptor() {
public boolean doesAccept(Node node) {
- if ( isTypeRef(node) )
- {
+ if (isTypeRef(node)) {
return getTypeRefName(node).equals(matchName);
}
return false;
}
});
-
- for ( Node searchResult : searchResults ) {
- occurrences.add(getPositionOfName(searchResult, root ) );
+
+ for (Node searchResult : searchResults) {
+ occurrences.add(getPositionOfName(searchResult, root));
}
}
-
-
+
// ****************************************************************************
// *
// * Utility methods
// *
// ****************************************************************************
-
+
/**
* Gets the position of the name for the specified node.
- * @param node Node that responds to getName() or some variant
- * @param scope Scope that holds the node (pertinent for locals and args)
+ *
+ * @param node
+ * Node that responds to getName() or some variant
+ * @param scope
+ * Scope that holds the node (pertinent for locals and args)
* @return ISourcePosition that holds the name of the node
*/
- private ISourcePosition getPositionOfName(Node node, Node scope)
- {
+ private ISourcePosition getPositionOfName(Node node, Node scope) {
ISourcePosition pos = node.getPosition();
-
- //todo: refactor the getting-of-name
+
+ // TODO refactor the getting-of-name
String name = null;
- if ( isLocalVarRef(node) ) { name = getLocalVarRefName(node); }
- if ( isDVarRef(node) ) { name = getDVarRefName(node); }
- if ( isInstanceVarRef(node) ) { name = getInstVarRefName(node ); }
- if ( isGlobalVarRef(node) ) { name = getGlobalVarRefName(node); }
- if ( isClassVarRef(node) ) { name = getClassVarRefName(node); }
- if ( isConstRef(node) ) { name = getConstRefName(node); }
- if ( node instanceof ClassNode ) {
- name = getClassNodeName( (ClassNode)node );
+ if (isLocalVarRef(node) || isDVarRef(node) || isInstanceVarRef(node) || isGlobalVarRef(node) || isClassVarRef(node) || isConstRef(node)) {
+ name = ASTUtil.getNameReflectively(node);
+ } else if (node instanceof ClassNode) {
+ name = getClassNodeName((ClassNode) node);
String classDeclString = source.substring(pos.getStartOffset(), pos.getEndOffset());
int begin = pos.getStartOffset() + classDeclString.indexOf(name);
- return new SourcePosition( pos.getFile(), pos.getStartLine(), pos.getEndLine(), begin, begin + name.length() );
- }
- if ( node instanceof ModuleNode ) {
- name = getModuleNodeName( (ModuleNode)node );
+ return new SourcePosition(pos.getFile(), pos.getStartLine(), pos.getEndLine(), begin, begin + name.length());
+ } else 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() );
+ return new SourcePosition(pos.getFile(), pos.getStartLine(), pos.getEndLine(), begin, begin + name.length());
+ } else 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 ( 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 )
- {
+
+ if (name == null) {
throw new RuntimeException("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() );
+ 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
- * @return
- */
- private String getLocalVarRefName( Node node ) {
- if (node instanceof INameNode) {
- return ((INameNode)node).getName();
- }
-
- return null;
- }
-
- /**
- * 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;
- return getNameReflectively( node );
- }
/**
- * Gets the name of an instance variable reference
- * @param node Instance variable reference
- * @return
- */
- private String getInstVarRefName( Node node ) {
-// if ( node instanceof InstAsgnNode ) {
-// return ((InstAsgnNode)node).getName();
-// }
-//
-// if ( node instanceof InstVarNode ) {
-// return ((InstVarNode)node).getName();
-// }
-//
-// if ( node instanceof DVarNode ) {
-// return ((DVarNode)node).getName();
-// }
-// return null;
- return getNameReflectively( node );
- }
-
- /**
- * Gets the name of a class variable reference
- * @param node Class variable reference
- * @return
- */
- private String getClassVarRefName( Node node ) {
-// if ( node instanceof ClassVarNode ) {
-// return ((ClassVarNode)node).getName();
-// }
-// if ( node instanceof ClassVarDeclNode ) {
-// return ((ClassVarDeclNode)node).getName();
-// }
-// if ( node instanceof ClassVarAsgnNode ) {
-// return ((ClassVarAsgnNode)node).getName();
-// }
-// return null;
- return getNameReflectively( node );
- }
-
- /**
- * Gets the name of a global variable reference
- * @param node
- * @return
- */
- private String getGlobalVarRefName( Node node ) {
-// if ( node instanceof GlobalVarNode )
-// {
-// return ((GlobalVarNode)node).getName();
-// }
-// if ( node instanceof GlobalAsgnNode ) {
-// return ((GlobalAsgnNode)node).getName();
-// }
-// return null;
- return getNameReflectively( node );
- }
-
- /**
* Helper method to get the class name froma ClassNode
+ *
* @param classNode
* @return
*/
- private String getClassNodeName( ClassNode classNode ) {
+ private String getClassNodeName(ClassNode classNode) {
if (classNode.getCPath() instanceof Colon2Node) {
Colon2Node c2node = (Colon2Node) classNode.getCPath();
return c2node.getName();
@@ -686,11 +622,12 @@
/**
* Helper method to get the class name from a ModuleNode
+ *
* @param classNode
* @return
*/
- private String getModuleNodeName( ModuleNode moduleNode ) {
- if ( moduleNode.getCPath() instanceof Colon2Node ) {
+ private String getModuleNodeName(ModuleNode moduleNode) {
+ if (moduleNode.getCPath() instanceof Colon2Node) {
Colon2Node c2node = (Colon2Node) moduleNode.getCPath();
return c2node.getName();
}
@@ -698,66 +635,19 @@
}
/**
- * Helper method to get the class name from a const ref node
- * @param node
- * @return
- */
- private String getConstRefName( Node node ) {
-// if ( constRefNode instanceof ConstNode )
-// {
-// return ((ConstNode)node).getName();
-// }
-// return null;
- return getNameReflectively( node );
- }
-
- /**
* Helper method to get the class name from a const ref node (Class/Module)
+ *
* @param node
* @return
*/
- private String getTypeRefName( Node node ) {
- if ( node instanceof ClassNode )
- {
- return getClassNodeName((ClassNode)node);
+ private String getTypeRefName(Node node) {
+ if (node instanceof ClassNode) {
+ return getClassNodeName((ClassNode) node);
}
- if ( node instanceof ModuleNode )
- {
- return getModuleNodeName((ModuleNode)node);
+ if (node instanceof ModuleNode) {
+ return getModuleNodeName((ModuleNode) node);
}
- return getNameReflectively( node );
+ return ASTUtil.getNameReflectively(node);
}
-
- /**
- * Gets the name of a node by reflectively invoking "getName()" on it;
- * helper method just to cut many "instanceof/cast" pairs.
- * @param node
- * @return name or null
- */
- private String getNameReflectively( Node node ) {
- try {
- Method getNameMethod = node.getClass().getMethod("getName", new Class[]{});
- Object name = getNameMethod.invoke( node, new Object[0] );
- return (String)name;
- } catch (Exception e) {
- return null;
- }
-// } catch (SecurityException e) {
-// // TODO Auto-generated catch block
-// e.printStackTrace();
-// } catch (NoSuchMethodException e) {
-// return null;
-// } catch (IllegalArgumentException e) {
-// // TODO Auto-generated catch block
-// e.printStackTrace();
-// } catch (IllegalAccessException e) {
-// // TODO Auto-generated catch block
-// e.printStackTrace();
-// } catch (InvocationTargetException e) {
-// // TODO Auto-generated catch block
-// e.printStackTrace();
-// }
- }
-
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|