|
From: <caw...@us...> - 2007-07-07 00:29:36
|
Revision: 2725
http://svn.sourceforge.net/rubyeclipse/?rev=2725&view=rev
Author: cawilliams
Date: 2007-07-06 17:29:33 -0700 (Fri, 06 Jul 2007)
Log Message:
-----------
more Quick Outline groundwork
Modified Paths:
--------------
trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/actions/SelectionConverter.java
Added Paths:
-----------
trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/text/RubyElementProvider.java
Modified: trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/actions/SelectionConverter.java
===================================================================
--- trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/actions/SelectionConverter.java 2007-07-06 20:45:34 UTC (rev 2724)
+++ trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/actions/SelectionConverter.java 2007-07-07 00:29:33 UTC (rev 2725)
@@ -5,8 +5,13 @@
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.text.ITextSelection;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.ISelectionProvider;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.rubypeople.rdt.core.ICodeAssist;
import org.rubypeople.rdt.core.IRubyElement;
@@ -20,6 +25,54 @@
import org.rubypeople.rdt.ui.IWorkingCopyManager;
public class SelectionConverter {
+
+ /**
+ * Converts the selection provided by the given part into a structured selection.
+ * The following conversion rules are used:
+ * <ul>
+ * <li><code>part instanceof RubyEditor</code>: returns a structured selection
+ * using code resolve to convert the editor's text selection.</li>
+ * <li><code>part instanceof IWorkbenchPart</code>: returns the part's selection
+ * if it is a structured selection.</li>
+ * <li><code>default</code>: returns an empty structured selection.</li>
+ * </ul>
+ */
+ public static IStructuredSelection getStructuredSelection(IWorkbenchPart part) throws RubyModelException {
+ if (part instanceof RubyEditor)
+ return new StructuredSelection(codeResolve((RubyEditor)part));
+ ISelectionProvider provider= part.getSite().getSelectionProvider();
+ if (provider != null) {
+ ISelection selection= provider.getSelection();
+ if (selection instanceof IStructuredSelection)
+ return (IStructuredSelection)selection;
+ }
+ return StructuredSelection.EMPTY;
+ }
+
+ public static IRubyElement getElementAtOffset(RubyEditor editor) throws RubyModelException {
+ return getElementAtOffset(editor, true);
+ }
+
+ /**
+ * @param primaryOnly if <code>true</code> only primary working copies will be returned
+ * @since 3.2
+ */
+ private static IRubyElement getElementAtOffset(RubyEditor editor, boolean primaryOnly) throws RubyModelException {
+ return getElementAtOffset(getInput(editor, primaryOnly), (ITextSelection)editor.getSelectionProvider().getSelection());
+ }
+
+ public static IRubyElement getElementAtOffset(IRubyElement input, ITextSelection selection) throws RubyModelException {
+ if (input instanceof IRubyScript) {
+ IRubyScript cunit= (IRubyScript) input;
+ RubyModelUtil.reconcile(cunit);
+ IRubyElement ref= cunit.getElementAt(selection.getOffset());
+ if (ref == null)
+ return input;
+ else
+ return ref;
+ }
+ return null;
+ }
public static IRubyScript getInputAsRubyScript(RubyEditor editor) {
Object editorInput = SelectionConverter.getInput(editor);
Added: trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/text/RubyElementProvider.java
===================================================================
--- trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/text/RubyElementProvider.java (rev 0)
+++ trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/text/RubyElementProvider.java 2007-07-07 00:29:33 UTC (rev 2725)
@@ -0,0 +1,89 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2006 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.rubypeople.rdt.internal.ui.text;
+
+
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.Region;
+import org.eclipse.jface.text.information.IInformationProvider;
+import org.eclipse.jface.text.information.IInformationProviderExtension;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.IEditorPart;
+import org.rubypeople.rdt.core.IRubyElement;
+import org.rubypeople.rdt.core.RubyModelException;
+import org.rubypeople.rdt.internal.ui.actions.SelectionConverter;
+import org.rubypeople.rdt.internal.ui.rubyeditor.EditorUtility;
+import org.rubypeople.rdt.internal.ui.rubyeditor.RubyEditor;
+
+/**
+ * Provides a Ruby element to be displayed in by an information presenter.
+ */
+public class RubyElementProvider implements IInformationProvider, IInformationProviderExtension {
+
+ private RubyEditor fEditor;
+ private boolean fUseCodeResolve;
+
+ public RubyElementProvider(IEditorPart editor) {
+ fUseCodeResolve= false;
+ if (editor instanceof RubyEditor)
+ fEditor= (RubyEditor)editor;
+ }
+
+ public RubyElementProvider(IEditorPart editor, boolean useCodeResolve) {
+ this(editor);
+ fUseCodeResolve= useCodeResolve;
+ }
+
+ /*
+ * @see IInformationProvider#getSubject(ITextViewer, int)
+ */
+ public IRegion getSubject(ITextViewer textViewer, int offset) {
+ if (textViewer != null && fEditor != null) {
+ IRegion region= RubyWordFinder.findWord(textViewer.getDocument(), offset);
+ if (region != null)
+ return region;
+ else
+ return new Region(offset, 0);
+ }
+ return null;
+ }
+
+ /*
+ * @see IInformationProvider#getInformation(ITextViewer, IRegion)
+ */
+ public String getInformation(ITextViewer textViewer, IRegion subject) {
+ return getInformation2(textViewer, subject).toString();
+ }
+
+ /*
+ * @see IInformationProviderExtension#getElement(ITextViewer, IRegion)
+ */
+ public Object getInformation2(ITextViewer textViewer, IRegion subject) {
+ if (fEditor == null)
+ return null;
+
+ try {
+ if (fUseCodeResolve) {
+ IStructuredSelection sel= SelectionConverter.getStructuredSelection(fEditor);
+ if (!sel.isEmpty())
+ return sel.getFirstElement();
+ }
+ IRubyElement element= SelectionConverter.getElementAtOffset(fEditor);
+ if (element != null)
+ return element;
+
+ return EditorUtility.getEditorInputRubyElement(fEditor, false);
+ } catch (RubyModelException e) {
+ return null;
+ }
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|