|
From: <caw...@us...> - 2007-02-22 19:35:45
|
Revision: 2001
http://svn.sourceforge.net/rubyeclipse/?rev=2001&view=rev
Author: cawilliams
Date: 2007-02-22 11:35:43 -0800 (Thu, 22 Feb 2007)
Log Message:
-----------
when a user selects a Colon2Node - which is a scoped constant (i.e. TMail::Mail), we check for types with that scope (so we search for all mail types, then filter to those with declaring types named TMail).
So go to declaration on the Mail part of TMail::Mail.new will send you to TMail::Mail, while selecting TMail on TMail::Mail will send you to the TMail module definition. Sweet!
Modified Paths:
--------------
trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/SelectionEngine.java
trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/util/ASTUtil.java
Modified: trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/SelectionEngine.java
===================================================================
--- trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/SelectionEngine.java 2007-02-22 18:59:17 UTC (rev 2000)
+++ trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/SelectionEngine.java 2007-02-22 19:35:43 UTC (rev 2001)
@@ -10,6 +10,7 @@
import org.jruby.ast.ClassVarAsgnNode;
import org.jruby.ast.ClassVarDeclNode;
import org.jruby.ast.ClassVarNode;
+import org.jruby.ast.Colon2Node;
import org.jruby.ast.ConstNode;
import org.jruby.ast.FCallNode;
import org.jruby.ast.InstAsgnNode;
@@ -26,6 +27,8 @@
import org.rubypeople.rdt.core.IType;
import org.rubypeople.rdt.core.RubyModelException;
import org.rubypeople.rdt.internal.core.parser.RubyParser;
+import org.rubypeople.rdt.internal.core.util.ASTUtil;
+import org.rubypeople.rdt.internal.core.util.Util;
import org.rubypeople.rdt.internal.ti.DefaultTypeInferrer;
import org.rubypeople.rdt.internal.ti.ITypeGuess;
import org.rubypeople.rdt.internal.ti.ITypeInferrer;
@@ -42,7 +45,23 @@
Node selected = OffsetNodeLocator.Instance().getNodeAtOffset(root,
start);
- if (selected instanceof ConstNode) {
+ if (selected instanceof Colon2Node) {
+// FIXME What if we have a constant with multiple parts (i.e. TMail::Mail)?
+ String simpleName = ((Colon2Node)selected).getName();
+ String fullyQualifiedName = ASTUtil.getFullyQualifiedName((Colon2Node) selected);
+ IRubyElement element = findChild(simpleName, IRubyElement.TYPE, script);
+ if (element != null && parentsMatch((IType)element, fullyQualifiedName)) {
+ return new IRubyElement[] { element };
+ }
+ RubyElementRequestor completer = new RubyElementRequestor(script);
+ IType[] types = completer.findType(simpleName);
+ List<IType> matches = new ArrayList<IType>();
+ for (int i = 0; i < types.length; i++) {
+ if (parentsMatch(types[i], fullyQualifiedName)) matches.add(types[i]);
+ }
+ return matches.toArray(new IType[matches.size()]);
+ }
+ if (selected instanceof ConstNode) {
ConstNode constNode = (ConstNode) selected;
String name = constNode.getName();
// Try to find a matching constant in this script
@@ -101,6 +120,22 @@
return new IRubyElement[0];
}
+ private boolean parentsMatch(IType type, String fullyQualifiedName) {
+ String[] names = getTrimmedSimpleNames(fullyQualifiedName);
+ for (int i = names.length - 2; i >= 0; i--) { // Start at second last name piece, go all the way to first
+ IType parent = type.getDeclaringType();
+ if (parent == null || !names[i].equals(parent.getElementName())) {
+ return false;
+ }
+ type = parent;
+ }
+ return true;
+ }
+
+ private String[] getTrimmedSimpleNames(String fullyQualifiedName) {
+ return fullyQualifiedName.split("::");
+ }
+
private IRubyElement findChild(String name, int type,
IParent parent) {
try {
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-22 18:59:17 UTC (rev 2000)
+++ trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/core/util/ASTUtil.java 2007-02-22 19:35:43 UTC (rev 2001)
@@ -7,6 +7,7 @@
import org.jruby.ast.ArgsNode;
import org.jruby.ast.ArgumentNode;
+import org.jruby.ast.Colon2Node;
import org.jruby.ast.ConstNode;
import org.jruby.ast.DStrNode;
import org.jruby.ast.FalseNode;
@@ -14,7 +15,6 @@
import org.jruby.ast.HashNode;
import org.jruby.ast.ListNode;
import org.jruby.ast.LocalAsgnNode;
-import org.jruby.ast.LocalVarNode;
import org.jruby.ast.NilNode;
import org.jruby.ast.Node;
import org.jruby.ast.SelfNode;
@@ -154,4 +154,17 @@
}
}
+ public static String getFullyQualifiedName(Colon2Node node) {
+ StringBuffer name = new StringBuffer();
+ Node left = node.getLeftNode();
+ if (left instanceof Colon2Node) {
+ name.append(getFullyQualifiedName((Colon2Node)left));
+ } else if (left instanceof ConstNode) {
+ name.append(((ConstNode)left).getName());
+ }
+ name.append("::");
+ name.append(node.getName());
+ return name.toString();
+ }
+
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|