|
From: <caw...@us...> - 2007-05-14 13:26:42
|
Revision: 2471
http://svn.sourceforge.net/rubyeclipse/?rev=2471&view=rev
Author: cawilliams
Date: 2007-05-14 06:26:38 -0700 (Mon, 14 May 2007)
Log Message:
-----------
fix grabbing local variables and arguments. Just grab the ecnlsoing scope for our offset (method, class, module, or root of script) and then add variables in the static scope, and all enclosing static scopes. This gets us locals in the top-level and locals inside a method (including method arguments) whereas before we only handled locals in scope of method.
Modified Paths:
--------------
trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/CompletionEngine.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-05-14 06:44:25 UTC (rev 2470)
+++ trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/CompletionEngine.java 2007-05-14 13:26:38 UTC (rev 2471)
@@ -1,7 +1,9 @@
package org.rubypeople.rdt.internal.codeassist;
+import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@@ -26,6 +28,7 @@
import org.jruby.ast.MethodDefNode;
import org.jruby.ast.ModuleNode;
import org.jruby.ast.Node;
+import org.jruby.ast.RootNode;
import org.jruby.lexer.yacc.SyntaxException;
import org.jruby.parser.StaticScope;
import org.rubypeople.rdt.core.CompletionProposal;
@@ -362,14 +365,20 @@
return;
}
- // Find the enclosing method to get locals and args
- Node enclosingMethodNode = ClosestSpanningNodeLocator.Instance().findClosestSpanner(rootNode, fContext.getOffset(), new INodeAcceptor() {
+ // XXX Just find enclosing scope and grab variables?
+ Node enclosingNode = ClosestSpanningNodeLocator.Instance().findClosestSpanner(rootNode, fContext.getOffset(), new INodeAcceptor() {
public boolean doesAccept(Node node) {
- return (node instanceof DefnNode || node instanceof DefsNode);
+ return (node instanceof DefnNode || node instanceof DefsNode || node instanceof ClassNode || node instanceof ModuleNode || node instanceof RootNode);
}
});
+
+ Collection<String> variables = addVariablesinScope(getScope(enclosingNode));
+ for (String variable : variables) {
+ CompletionProposal proposal = new CompletionProposal(CompletionProposal.LOCAL_VARIABLE_REF, variable, 100);
+ proposal.setReplaceRange(fContext.getReplaceStart(), fContext.getReplaceStart() + variable.length());
+ fRequestor.accept(proposal);
+ }
- addLocalVariablesAndArguments(enclosingMethodNode);
// Find the enclosing type (class or module) to get instance and
// classvars from
@@ -392,6 +401,34 @@
}
}
+ private Set<String> addVariablesinScope(StaticScope scope) {
+ Set<String> matches = new HashSet<String>();
+ if (scope == null) return matches;
+ String[] variables = scope.getVariables();
+ for(int i = 0; i < variables.length; i++) {
+ String local = variables[i];
+ if (!fContext.prefixStartsWith(local))
+ continue;
+ matches.add(local);
+ }
+ matches.addAll(addVariablesinScope(scope.getEnclosingScope()));
+ return matches;
+ }
+
+ private StaticScope getScope(Node enclosingNode) {
+ if (enclosingNode instanceof RootNode) {
+ RootNode root = (RootNode) enclosingNode;
+ return root.getStaticScope();
+ }
+ try {
+ Method getScopeMethod = enclosingNode.getClass().getMethod("getScope", new Class[] {});
+ Object scope = getScopeMethod.invoke(enclosingNode, new Object[0]);
+ return (StaticScope) scope;
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
private void addLocalVariablesAndArguments(Node enclosingMethodNode) {
// Add local vars and arguments
if (enclosingMethodNode != null && enclosingMethodNode instanceof MethodDefNode) {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|