|
From: <caw...@us...> - 2007-01-25 20:03:49
|
Revision: 1886
http://svn.sourceforge.net/rubyeclipse/?rev=1886&view=rev
Author: cawilliams
Date: 2007-01-25 12:03:44 -0800 (Thu, 25 Jan 2007)
Log Message:
-----------
try to move common code for resolving a type name to a type into RubyElementrequestor...
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/codeassist/RubyElementRequestor.java
trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/SelectionEngine.java
trunk/org.rubypeople.rdt.testunit/src/org/rubypeople/rdt/internal/testunit/ui/OpenEditorAtLineAction.java
trunk/org.rubypeople.rdt.testunit/src/org/rubypeople/rdt/testunit/wizards/RubyNewTestCaseWizardPage.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-01-25 19:34:32 UTC (rev 1885)
+++ trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/CompletionEngine.java 2007-01-25 20:03:44 UTC (rev 1886)
@@ -4,9 +4,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
-import java.util.LinkedList;
import java.util.List;
-import java.util.StringTokenizer;
import org.jruby.ast.ClassNode;
import org.jruby.ast.ClassVarAsgnNode;
@@ -26,15 +24,13 @@
import org.rubypeople.rdt.core.CompletionProposal;
import org.rubypeople.rdt.core.CompletionRequestor;
import org.rubypeople.rdt.core.Flags;
-import org.rubypeople.rdt.core.IImportDeclaration;
import org.rubypeople.rdt.core.IMethod;
import org.rubypeople.rdt.core.IParent;
import org.rubypeople.rdt.core.IRubyElement;
import org.rubypeople.rdt.core.IRubyProject;
import org.rubypeople.rdt.core.IRubyScript;
-import org.rubypeople.rdt.core.ISourceFolder;
-import org.rubypeople.rdt.core.ISourceFolderRoot;
import org.rubypeople.rdt.core.IType;
+import org.rubypeople.rdt.core.RubyCore;
import org.rubypeople.rdt.core.RubyModelException;
import org.rubypeople.rdt.internal.core.RubyElement;
import org.rubypeople.rdt.internal.core.RubyType;
@@ -100,76 +96,21 @@
} else { // method or variable
ITypeInferrer inferrer = new DefaultTypeInferrer();
List<ITypeGuess> guesses = inferrer.infer(source.toString(), offset);
-
- IRubyProject rubyProject = script.getRubyProject();
- ISourceFolderRoot[] roots = rubyProject.getSourceFolderRoots();
- // ILoadpathEntry[] loadpaths =
- // rubyProject.getResolvedLoadpath(true);
- for (int i = 0; i < roots.length; i++) {
- ISourceFolderRoot root = roots[i];
- IImportDeclaration[] imports = script.getImports();
- for (int j = 0; j < imports.length; j++) {
- String path = imports[j].getElementName();
- StringTokenizer tokenizer = new StringTokenizer(path, "\\/");
- List<String> tokens = new ArrayList<String>();
- while(tokenizer.hasMoreTokens()) {
- tokens.add(tokenizer.nextToken());
- }
- String name = tokens.remove(tokens.size() - 1) + ".rb";
- String[] pckgs = (String[]) tokens.toArray(new String[tokens.size()]);
- ISourceFolder folder = root.getSourceFolder(pckgs);
- if (!folder.exists()) continue;
- IRubyScript otherScript = folder.getRubyScript(name);
- if (!otherScript.exists()) continue;
- List<IType> types = getTypes(otherScript);
- for (IType type : types) {
- for (ITypeGuess guess : guesses) {
- if (guess.getType().equals(type.getElementName())) {
- IMethod[] methods = type.getMethods();
- for(int x = 0; x < methods.length; x++) {
- addProposal(replaceStart, CompletionProposal.METHOD_REF, methods[x].getElementName());
- }
- }
- }
- }
+ RubyElementRequestor requestor = new RubyElementRequestor(script);
+ for (ITypeGuess guess : guesses) {
+ String name = guess.getType();
+ IType[] types = requestor.findType(name);
+ for (int i = 0; i < types.length; i++) {
+ suggestMethods(replaceStart, guess.getConfidence(), types[i]);
}
}
-// bruteForceMethodSuggestion(script, replaceStart, guesses);
+ // FIXME Traverse the IRubyElement model, not nodes (and don't reparse!)
if (!isMethod)
getDocumentsRubyElementsInScope(script, source.toString(), offset, replaceStart);
}
this.requestor.endReporting();
}
- private List<IType> getTypes(IParent script) {
- List<IType> types = new ArrayList<IType>();
- try {
- IRubyElement[] children = script.getChildren();
- for (int i = 0; i < children.length; i++) {
- if (children[i].isType(IRubyElement.TYPE)) {
- types.add((IType) children[i]);
- }
- if (children[i] instanceof IParent) {
- types.addAll(getTypes((IParent) children[i]));
- }
- }
- } catch (RubyModelException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return types;
- }
-
- private void bruteForceMethodSuggestion(IRubyScript script, int replaceStart, List<ITypeGuess> guesses) throws RubyModelException {
- RubyElementRequestor completer = new RubyElementRequestor(script.getRubyProject());
- // TODO Search the loadpath + imports!
- for (Iterator iter = guesses.iterator(); iter.hasNext();) {
- ITypeGuess guess = (ITypeGuess) iter.next();
- IType type = completer.findType(guess.getType());
- suggestMethods(replaceStart, completer, guess, type);
- }
- }
-
private void suggestTypeNames(int replaceStart) {
List<String> types = ExperimentalIndex.getTypes();
// TODO Remove duplicates? Sort?
@@ -203,29 +144,6 @@
return this.prefix != null && this.prefix.length() > 0 && Character.isUpperCase(this.prefix.charAt(0));
}
- private void suggestMethods(int replaceStart, RubyElementRequestor completer, ITypeGuess guess, IType type) throws RubyModelException {
- if (type == null)
- return;
-
- suggestMethods(replaceStart, guess.getConfidence(), type);
- // Now grab methods from all the included modules
- String[] modules = type.getIncludedModuleNames();
- if (modules != null) {
- for (int x = 0; x < modules.length; x++) {
- IType tmpType = completer.findType(modules[x]);
- suggestMethods(replaceStart, guess.getConfidence(), tmpType);
- }
- }
- String superClass = type.getSuperclassName();
- if (superClass == null)
- return;
- // FIXME This shouldn't happen! Object shouldn't be a parent of itself!
- if (type.getElementName().equals("Object") && superClass.equals("Object"))
- return;
- IType parentClass = completer.findType(superClass);
- suggestMethods(replaceStart, completer, guess, parentClass);
- }
-
private void suggestMethods(int replaceStart, int confidence, IType type) throws RubyModelException {
if (type == null)
return;
@@ -318,11 +236,11 @@
getElementsOfType(script.getRubyProject(), new int[] { IRubyElement.GLOBAL }, replaceStart);
addClassesAndModulesInProject(script.getRubyProject(), replaceStart);
} catch (RubyModelException rme) {
- System.out.println("RubyModelException in CompletionEngine::getElementsInScope()");
- rme.printStackTrace();
+ RubyCore.log(rme);
+ RubyCore.log("RubyModelException in CompletionEngine::getElementsInScope()");
} catch (SyntaxException se) {
- System.out.println("SyntaxError in CompletionEngine::getElementsInScope()");
- se.printStackTrace();
+ RubyCore.log(se);
+ RubyCore.log("SyntaxError in CompletionEngine::getElementsInScope()");
}
}
@@ -520,7 +438,9 @@
System.out.println("Being asked for the type decl node for " + typeName);
// Find the named type
- IType type = findTypeFromAllProjects(typeName, script);
+ RubyElementRequestor requestor = new RubyElementRequestor(script);
+ IType[] types = requestor.findType(typeName);
+ IType type = types[0];
try {
if (type instanceof RubyType) {
@@ -557,18 +477,6 @@
return new ArrayList<Node>(0);
}
- private IType findTypeFromAllProjects(String typeName, IRubyScript rootScript) {
- // Grab the project and all referred projects
- List<IRubyProject> projects = new LinkedList<IRubyProject>();
- projects.add(rootScript.getRubyProject());
- // FIXME Search the loadpaths!
- // projects.addAll(rootScript.getRubyProject().getReferencedProjects());
-
- // Find the named type
- RubyElementRequestor completer = new RubyElementRequestor(projects.toArray(new IRubyProject[] {}));
- return completer.findType(typeName);
- }
-
private List<String> getIncludedMixinNames(String typeName, IRubyScript script) {
IType rubyType = new RubyType((RubyElement) script, typeName);
@@ -576,9 +484,8 @@
String[] includedModuleNames = rubyType.getIncludedModuleNames();
if (includedModuleNames != null) {
return Arrays.asList(rubyType.getIncludedModuleNames());
- } else {
- return new ArrayList<String>(0);
- }
+ }
+ return new ArrayList<String>(0);
} catch (RubyModelException e) {
return new ArrayList<String>(0);
}
Modified: trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/RubyElementRequestor.java
===================================================================
--- trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/RubyElementRequestor.java 2007-01-25 19:34:32 UTC (rev 1885)
+++ trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/RubyElementRequestor.java 2007-01-25 20:03:44 UTC (rev 1886)
@@ -1,33 +1,91 @@
package org.rubypeople.rdt.internal.codeassist;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.StringTokenizer;
+
+import org.rubypeople.rdt.core.IImportDeclaration;
+import org.rubypeople.rdt.core.IParent;
+import org.rubypeople.rdt.core.IRubyElement;
import org.rubypeople.rdt.core.IRubyProject;
+import org.rubypeople.rdt.core.IRubyScript;
+import org.rubypeople.rdt.core.ISourceFolder;
+import org.rubypeople.rdt.core.ISourceFolderRoot;
import org.rubypeople.rdt.core.IType;
+import org.rubypeople.rdt.core.RubyCore;
import org.rubypeople.rdt.core.RubyModelException;
public class RubyElementRequestor {
- private IRubyProject[] projects;
+ private static final String SEPARATOR_CHARS = "\\/";
+ private static final String RUBY_FILE_EXTENSION = ".rb";
+ private IRubyScript script;
- public RubyElementRequestor(IRubyProject[] projects) {
- this.projects = projects;
+ public RubyElementRequestor(IRubyScript script) {
+ this.script = script;
}
- public RubyElementRequestor(IRubyProject rubyProject) {
- this(new IRubyProject[] {rubyProject});
+ public IType[] findType(String typeName) {
+ List<IType> types = new ArrayList<IType>();
+ IRubyProject rubyProject = script.getRubyProject();
+ try {
+ ISourceFolderRoot[] roots = rubyProject.getSourceFolderRoots();
+ for (int i = 0; i < roots.length; i++) {
+ types.addAll(getTypeInSourceFolderRoot(roots[i]));
+ }
+ } catch (RubyModelException e) {
+ RubyCore.log(e);
+ }
+ List<IType> matches = new ArrayList<IType>();
+ for (IType type : types) {
+ if (type.getElementName().equals(typeName)) matches.add(type);
+ }
+ return (IType[]) types.toArray(new IType[matches.size()]);
}
- public IType findType(String typeName) {
+ private List<IType> getTypeInSourceFolderRoot(ISourceFolderRoot root) {
+ List<IType> types = new ArrayList<IType>();
try {
- for (int x = 0; x < projects.length; x++) {
- IRubyProject project = projects[x];
- IType type = project.findType(typeName);
- if (type != null)
- return type;
+ IImportDeclaration[] imports = script.getImports();
+ for (int j = 0; j < imports.length; j++) {
+ types.addAll(getTypeInImport(root, imports[j]));
}
} catch (RubyModelException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ RubyCore.log(e);
}
- return null;
+ return types;
}
+ private List<IType> getTypeInImport(ISourceFolderRoot root, IImportDeclaration importDecl) {
+ String path = importDecl.getElementName();
+ StringTokenizer tokenizer = new StringTokenizer(path, SEPARATOR_CHARS);
+ List<String> tokens = new ArrayList<String>();
+ while(tokenizer.hasMoreTokens()) {
+ tokens.add(tokenizer.nextToken());
+ }
+ String name = tokens.remove(tokens.size() - 1) + RUBY_FILE_EXTENSION;
+ String[] pckgs = (String[]) tokens.toArray(new String[tokens.size()]);
+ ISourceFolder folder = root.getSourceFolder(pckgs);
+ if (!folder.exists()) return new ArrayList<IType>();
+ IRubyScript otherScript = folder.getRubyScript(name);
+ if (!otherScript.exists()) return new ArrayList<IType>();
+ return getTypes(otherScript);
+ }
+
+ private List<IType> getTypes(IParent script) {
+ List<IType> types = new ArrayList<IType>();
+ try {
+ IRubyElement[] children = script.getChildren();
+ for (int i = 0; i < children.length; i++) {
+ if (children[i].isType(IRubyElement.TYPE)) {
+ types.add((IType) children[i]);
+ }
+ if (children[i] instanceof IParent) {
+ types.addAll(getTypes((IParent) children[i]));
+ }
+ }
+ } catch (RubyModelException e) {
+ // ignore
+ }
+ return types;
+ }
}
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-01-25 19:34:32 UTC (rev 1885)
+++ trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/SelectionEngine.java 2007-01-25 20:03:44 UTC (rev 1886)
@@ -51,14 +51,8 @@
if (element != null) {
return new IRubyElement[] { element };
}
- // TODO Search the required/loaded files first!
- // TODO Search scopes outward!
- // Now search across project for type
- IRubyProject[] projects = new IRubyProject[1];
- projects[0] = script.getRubyProject();
- RubyElementRequestor completer = new RubyElementRequestor(projects);
- IType type = completer.findType(name);
- return new IRubyElement[] { type };
+ RubyElementRequestor completer = new RubyElementRequestor(script);
+ return completer.findType(name);
}
if (isLocalVarRef(selected)) {
// TODO Try the local namespace first!
Modified: trunk/org.rubypeople.rdt.testunit/src/org/rubypeople/rdt/internal/testunit/ui/OpenEditorAtLineAction.java
===================================================================
--- trunk/org.rubypeople.rdt.testunit/src/org/rubypeople/rdt/internal/testunit/ui/OpenEditorAtLineAction.java 2007-01-25 19:34:32 UTC (rev 1885)
+++ trunk/org.rubypeople.rdt.testunit/src/org/rubypeople/rdt/internal/testunit/ui/OpenEditorAtLineAction.java 2007-01-25 20:03:44 UTC (rev 1886)
@@ -29,7 +29,6 @@
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;
-import org.rubypeople.rdt.core.IRubyElement;
import org.rubypeople.rdt.core.IRubyProject;
import org.rubypeople.rdt.internal.debug.ui.RubySourceLocator;
@@ -129,10 +128,6 @@
}
}
- protected IRubyElement findElement(IRubyProject project, String className) throws CoreException {
- return project.findType(className);
- }
-
public boolean isEnabled() {
return getInput() != null;
}
Modified: trunk/org.rubypeople.rdt.testunit/src/org/rubypeople/rdt/testunit/wizards/RubyNewTestCaseWizardPage.java
===================================================================
--- trunk/org.rubypeople.rdt.testunit/src/org/rubypeople/rdt/testunit/wizards/RubyNewTestCaseWizardPage.java 2007-01-25 19:34:32 UTC (rev 1885)
+++ trunk/org.rubypeople.rdt.testunit/src/org/rubypeople/rdt/testunit/wizards/RubyNewTestCaseWizardPage.java 2007-01-25 20:03:44 UTC (rev 1886)
@@ -336,11 +336,6 @@
}
IType type= rproject.findType(classToTestName);
-
- // search in java.lang
-// if (type == null) {
-// type= jproject.findType("java.lang", classToTestName); //$NON-NLS-1$
-// }
return type;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|