|
From: <caw...@us...> - 2006-11-01 15:58:08
|
Revision: 1645
http://svn.sourceforge.net/rubyeclipse/?rev=1645&view=rev
Author: cawilliams
Date: 2006-11-01 07:57:54 -0800 (Wed, 01 Nov 2006)
Log Message:
-----------
start to implement a hacky go to declaration F3 command
Modified Paths:
--------------
trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/SelectionEngine.java
trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/rubyeditor/RubyEditor.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 2006-11-01 15:57:28 UTC (rev 1644)
+++ trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/codeassist/SelectionEngine.java 2006-11-01 15:57:54 UTC (rev 1645)
@@ -1,25 +1,118 @@
package org.rubypeople.rdt.internal.codeassist;
-import java.io.CharArrayReader;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
import org.eclipse.core.resources.IFile;
+import org.jruby.ast.ArgumentNode;
+import org.jruby.ast.ClassVarAsgnNode;
+import org.jruby.ast.ClassVarDeclNode;
+import org.jruby.ast.ClassVarNode;
+import org.jruby.ast.ConstNode;
+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.types.INameNode;
+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.IType;
import org.rubypeople.rdt.core.RubyModelException;
import org.rubypeople.rdt.internal.core.parser.RubyParser;
+import org.rubypeople.rdt.internal.ti.util.OffsetNodeLocator;
public class SelectionEngine {
-
- public IRubyElement[] select(IRubyScript script, int start, int end) throws RubyModelException {
- char[] contents = script.getBuffer().getCharacters();
+
+ public IRubyElement[] select(IRubyScript script, int start, int end)
+ throws RubyModelException {
+ String source = script.getSource();
RubyParser parser = new RubyParser();
- Node node = parser.parse((IFile) script.getResource(), new CharArrayReader(contents));
-
- SelectionVisitor visitor = new SelectionVisitor(script, start, end);
- if (node != null) node.accept(visitor);
-
- return visitor.getElements();
+ Node root = parser.parse((IFile) script.getResource(),
+ new StringReader(source));
+ Node selected = OffsetNodeLocator.Instance().getNodeAtOffset(root,
+ start);
+
+ if (selected instanceof ConstNode) {
+ ConstNode constNode = (ConstNode) selected;
+ String name = constNode.getName();
+ // TODO Find constants too (not just types)
+ // TODO Search scopes outward!
+ IRubyProject[] projects = new IRubyProject[1];
+ projects[0] = script.getRubyProject();
+ RubyElementRequestor completer = new RubyElementRequestor(projects);
+ IType type = completer.findType(name);
+ return new IRubyElement[] { type };
+ }
+ if (isLocalVarRef(selected)) {
+ List<IRubyElement> possible = getChildrenWithName(script
+ .getChildren(), IRubyElement.LOCAL_VARIABLE,
+ getName(selected));
+ return convertToArray(possible);
+ }
+ if (isInstanceVarRef(selected)) {
+ List<IRubyElement> possible = getChildrenWithName(script
+ .getChildren(), IRubyElement.INSTANCE_VAR,
+ getName(selected));
+ return convertToArray(possible);
+ }
+ if (isClassVarRef(selected)) {
+ List<IRubyElement> possible = getChildrenWithName(script
+ .getChildren(), IRubyElement.CLASS_VAR, getName(selected));
+ return convertToArray(possible);
+ }
+ return new IRubyElement[0];
}
+ private IRubyElement[] convertToArray(List<IRubyElement> possible) {
+ IRubyElement[] elements = new IRubyElement[possible.size()];
+ int x = 0;
+ for (IRubyElement element : possible) {
+ elements[x++] = element;
+ }
+ return elements;
+ }
+
+ private List<IRubyElement> getChildrenWithName(IRubyElement[] children,
+ int type, String name) throws RubyModelException {
+ List<IRubyElement> possible = new ArrayList<IRubyElement>();
+ for (int i = 0; i < children.length; i++) {
+ IRubyElement child = children[i];
+ if (child.getElementType() == type) {
+ if (child.getElementName().equals(name))
+ possible.add(child);
+ }
+ if (child instanceof IParent) {
+ possible.addAll(getChildrenWithName(((IParent) child)
+ .getChildren(), type, name));
+ }
+ }
+ return possible;
+ }
+
+ private String getName(Node node) {
+ if (node instanceof INameNode) {
+ return ((INameNode) node).getName();
+ }
+ if (node instanceof ClassVarNode) {
+ return ((ClassVarNode) node).getName();
+ }
+ return "";
+ }
+
+ private boolean isInstanceVarRef(Node node) {
+ return ((node instanceof InstAsgnNode) || (node instanceof InstVarNode));
+ }
+
+ private boolean isClassVarRef(Node node) {
+ return ((node instanceof ClassVarAsgnNode) || (node instanceof ClassVarNode)|| (node instanceof ClassVarDeclNode));
+ }
+
+ private boolean isLocalVarRef(Node node) {
+ return ((node instanceof LocalAsgnNode)
+ || (node instanceof ArgumentNode) || (node instanceof LocalVarNode));
+ }
}
Modified: trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/rubyeditor/RubyEditor.java
===================================================================
--- trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/rubyeditor/RubyEditor.java 2006-11-01 15:57:28 UTC (rev 1644)
+++ trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/rubyeditor/RubyEditor.java 2006-11-01 15:57:54 UTC (rev 1645)
@@ -15,6 +15,7 @@
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Preferences;
import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
@@ -58,6 +59,7 @@
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.search.ui.IContextMenuConstants;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.custom.VerifyKeyListener;
import org.eclipse.swt.events.VerifyEvent;
@@ -95,6 +97,7 @@
import org.rubypeople.rdt.internal.ui.IRubyHelpContextIds;
import org.rubypeople.rdt.internal.ui.RubyPlugin;
import org.rubypeople.rdt.internal.ui.RubyUIMessages;
+import org.rubypeople.rdt.internal.ui.actions.CompositeActionGroup;
import org.rubypeople.rdt.internal.ui.actions.FoldingActionGroup;
import org.rubypeople.rdt.internal.ui.text.IRubyPartitions;
import org.rubypeople.rdt.internal.ui.text.RubyHeuristicScanner;
@@ -103,6 +106,7 @@
import org.rubypeople.rdt.ui.PreferenceConstants;
import org.rubypeople.rdt.ui.actions.FormatAction;
import org.rubypeople.rdt.ui.actions.IRubyEditorActionDefinitionIds;
+import org.rubypeople.rdt.ui.actions.OpenEditorActionGroup;
import org.rubypeople.rdt.ui.actions.RubyActionGroup;
import org.rubypeople.rdt.ui.actions.SurroundWithBeginRescueAction;
import org.rubypeople.rdt.ui.text.folding.IRubyFoldingStructureProvider;
@@ -110,7 +114,6 @@
public class RubyEditor extends RubyAbstractEditor {
- protected RubyActionGroup actionGroup;
private ProjectionSupport fProjectionSupport;
/** The editor's tab converter */
@@ -171,6 +174,8 @@
private FoldingActionGroup fFoldingGroup;
private BracketInserter fBracketInserter = new BracketInserter();
+ private CompositeActionGroup fActionGroups;
+ private CompositeActionGroup fContextMenuGroup;
public RubyEditor() {
@@ -189,12 +194,12 @@
* @return returns this editor's standard action group
*/
protected ActionGroup getActionGroup() {
- return actionGroup;
+ return fActionGroups;
}
protected void createActions() {
super.createActions();
-
+
Action action = new ContentAssistAction(RubyUIMessages.getResourceBundle(),
"ContentAssistProposal.", this);
action.setActionDefinitionId(IRubyEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
@@ -226,6 +231,11 @@
action.setActionDefinitionId(IRubyEditorActionDefinitionIds.FORMAT);
setAction("Format", action);
+ ActionGroup oeg;
+ fActionGroups= new CompositeActionGroup(new ActionGroup[] {
+ oeg= new OpenEditorActionGroup(this)
+ });
+ fContextMenuGroup= new CompositeActionGroup(new ActionGroup[] {oeg});
fFoldingGroup= new FoldingActionGroup(this, getViewer());
@@ -237,8 +247,8 @@
beginRescueAction.update(selection);
provider.addSelectionChangedListener(beginRescueAction);
setAction(SurroundWithBeginRescueAction.SURROUND_WTH_BEGIN_RESCUE, beginRescueAction);
-
- actionGroup = new RubyActionGroup(this, ITextEditorActionConstants.GROUP_EDIT);
+
+ fActionGroups.addGroup(new RubyActionGroup(this, ITextEditorActionConstants.GROUP_EDIT));
}
/**
@@ -619,6 +629,11 @@
fProjectionSupport.dispose();
fProjectionSupport = null;
}
+
+ if (fActionGroups != null) {
+ fActionGroups.dispose();
+ fActionGroups= null;
+ }
super.dispose();
}
@@ -714,7 +729,18 @@
}
}
- actionGroup.fillContextMenu(menu);
+
+ menu.insertAfter(IContextMenuConstants.GROUP_OPEN, new GroupMarker(IContextMenuConstants.GROUP_SHOW));
+
+ ActionContext context= new ActionContext(getSelectionProvider().getSelection());
+ fContextMenuGroup.setContext(context);
+ fContextMenuGroup.fillContextMenu(menu);
+ fContextMenuGroup.setContext(null);
+
+ // Quick views
+ // TODO Add show outline action!
+// IAction action= getAction(IRubyEditorActionDefinitionIds.SHOW_OUTLINE);
+// menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, action);
}
protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|