summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnton Kosyakov2013-02-21 03:40:53 (EST)
committer Gerrit Code Review @ Eclipse.org2013-02-24 07:11:54 (EST)
commitb1d6a5671b4cdd4773420e2ae39a95650d8bf232 (patch)
treeaa9bdcc7aaf8e947c6bf7040c12ca35553f9adf1
parent116238f5b1b662af8a4d84924da78386e71298f7 (diff)
downloadorg.eclipse.xtext-b1d6a5671b4cdd4773420e2ae39a95650d8bf232.zip
org.eclipse.xtext-b1d6a5671b4cdd4773420e2ae39a95650d8bf232.tar.gz
org.eclipse.xtext-b1d6a5671b4cdd4773420e2ae39a95650d8bf232.tar.bz2
Added "Copy Qualified Name" command for Xtext editor and outlinerefs/changes/21/10521/6
-rwxr-xr-xplugins/org.eclipse.xtext.generator/src/org/eclipse/xtext/ui/generator/ImplicitUiFragment.xpt75
-rw-r--r--plugins/org.eclipse.xtext.ui/META-INF/MANIFEST.MF1
-rw-r--r--plugins/org.eclipse.xtext.ui/plugin.xml9
-rw-r--r--plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/copyqualifiedname/AbstractCopyQualifiedNameHandler.java73
-rw-r--r--plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/copyqualifiedname/ClipboardUtil.java39
-rw-r--r--plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/copyqualifiedname/EditorCopyQualifiedNameHandler.java55
-rw-r--r--plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/copyqualifiedname/OutlineCopyQualifiedNameHandler.java48
-rw-r--r--plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/outline/impl/OutlinePage.java29
8 files changed, 325 insertions, 4 deletions
diff --git a/plugins/org.eclipse.xtext.generator/src/org/eclipse/xtext/ui/generator/ImplicitUiFragment.xpt b/plugins/org.eclipse.xtext.generator/src/org/eclipse/xtext/ui/generator/ImplicitUiFragment.xpt
index ae49d11..7b4f892 100755
--- a/plugins/org.eclipse.xtext.generator/src/org/eclipse/xtext/ui/generator/ImplicitUiFragment.xpt
+++ b/plugins/org.eclipse.xtext.generator/src/org/eclipse/xtext/ui/generator/ImplicitUiFragment.xpt
@@ -51,7 +51,27 @@
definitionId="«name».Editor.opened">
</reference>
</activeWhen>
- </handler>
+ </handler>
+ <!-- copy qualified name -->
+ <handler
+ class="«executableExtensionFactory()»:org.eclipse.xtext.ui.editor.copyqualifiedname.EditorCopyQualifiedNameHandler"
+ commandId="org.eclipse.xtext.ui.editor.copyqualifiedname.EditorCopyQualifiedName">
+ <activeWhen>
+ <reference definitionId="«name».Editor.opened" />
+ </activeWhen>
+ </handler>
+ <handler
+ class="«executableExtensionFactory()»:org.eclipse.xtext.ui.editor.copyqualifiedname.OutlineCopyQualifiedNameHandler"
+ commandId="org.eclipse.xtext.ui.editor.copyqualifiedname.OutlineCopyQualifiedName">
+ <activeWhen>
+ <and>
+ <reference definitionId="«name».XtextEditor.opened" />
+ <iterate>
+ <adapt type="org.eclipse.xtext.ui.editor.outline.IOutlineNode" />
+ </iterate>
+ </and>
+ </activeWhen>
+ </handler>
</extension>
<extension point="org.eclipse.core.expressions.definitions">
<definition id="«name».Editor.opened">
@@ -64,6 +84,16 @@
</with>
</and>
</definition>
+ <definition id="«name».XtextEditor.opened">
+ <and>
+ <reference definitionId="isXtextEditorActive"/>
+ <with variable="activeEditor">
+ <test property="org.eclipse.xtext.ui.editor.XtextEditor.languageName"
+ value="«name»"
+ forcePluginActivation="true"/>
+ </with>
+ </and>
+ </definition>
</extension>
<extension
point="org.eclipse.ui.preferencePages">
@@ -114,6 +144,19 @@
id="«name».validate"
name="Validate">
</command>
+ <!-- copy qualified name -->
+ <command
+ id="org.eclipse.xtext.ui.editor.copyqualifiedname.EditorCopyQualifiedName"
+ categoryId="org.eclipse.ui.category.edit"
+ description="Copy the qualified name for the selected element"
+ name="Copy Qualified Name">
+ </command>
+ <command
+ id="org.eclipse.xtext.ui.editor.copyqualifiedname.OutlineCopyQualifiedName"
+ categoryId="org.eclipse.ui.category.edit"
+ description="Copy the qualified name for the selected element"
+ name="Copy Qualified Name">
+ </command>
</extension>
<extension point="org.eclipse.ui.menus">
<menuContribution
@@ -129,6 +172,36 @@
</visibleWhen>
</command>
</menuContribution>
+ <!-- copy qualified name -->
+ <menuContribution locationURI="popup:#TextEditorContext?after=copy">
+ <command commandId="org.eclipse.xtext.ui.editor.copyqualifiedname.EditorCopyQualifiedName"
+ style="push" tooltip="Copy Qualified Name">
+ <visibleWhen checkEnabled="false">
+ <reference definitionId="«name».Editor.opened" />
+ </visibleWhen>
+ </command>
+ </menuContribution>
+ <menuContribution locationURI="menu:edit?after=copy">
+ <command commandId="org.eclipse.xtext.ui.editor.copyqualifiedname.EditorCopyQualifiedName"
+ style="push" tooltip="Copy Qualified Name">
+ <visibleWhen checkEnabled="false">
+ <reference definitionId="«name».Editor.opened" />
+ </visibleWhen>
+ </command>
+ </menuContribution>
+ <menuContribution locationURI="popup:org.eclipse.xtext.ui.outline?after=additions">
+ <command commandId="org.eclipse.xtext.ui.editor.copyqualifiedname.OutlineCopyQualifiedName"
+ style="push" tooltip="Copy Qualified Name">
+ <visibleWhen checkEnabled="false">
+ <and>
+ <reference definitionId="«name».XtextEditor.opened" />
+ <iterate>
+ <adapt type="org.eclipse.xtext.ui.editor.outline.IOutlineNode" />
+ </iterate>
+ </and>
+ </visibleWhen>
+ </command>
+ </menuContribution>
</extension>
<extension point="org.eclipse.ui.menus">
<menuContribution locationURI="popup:#TextEditorContext?endof=group.find">
diff --git a/plugins/org.eclipse.xtext.ui/META-INF/MANIFEST.MF b/plugins/org.eclipse.xtext.ui/META-INF/MANIFEST.MF
index dcc6881..d0b7985 100644
--- a/plugins/org.eclipse.xtext.ui/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.xtext.ui/META-INF/MANIFEST.MF
@@ -40,6 +40,7 @@ Export-Package: org.eclipse.xtext.common.ui.contentassist,
org.eclipse.xtext.ui.editor.contentassist,
org.eclipse.xtext.ui.editor.contentassist.antlr,
org.eclipse.xtext.ui.editor.contentassist.antlr.internal,
+ org.eclipse.xtext.ui.editor.copyqualifiedname;x-internal:=true,
org.eclipse.xtext.ui.editor.doubleClicking,
org.eclipse.xtext.ui.editor.embedded;x-friends:="org.eclipse.xtext.ui.codetemplates.ui",
org.eclipse.xtext.ui.editor.findrefs;x-friends:="org.eclipse.xtext.ui.tests,org.eclipse.xtext.ui.shared",
diff --git a/plugins/org.eclipse.xtext.ui/plugin.xml b/plugins/org.eclipse.xtext.ui/plugin.xml
index 578b3e9..ab271a0 100644
--- a/plugins/org.eclipse.xtext.ui/plugin.xml
+++ b/plugins/org.eclipse.xtext.ui/plugin.xml
@@ -22,6 +22,15 @@
</adapt>
</with>
</definition>
+ <definition
+ id="isXtextEditorActive">
+ <with
+ variable="activeEditor">
+ <adapt
+ type="org.eclipse.xtext.ui.editor.XtextEditor">
+ </adapt>
+ </with>
+ </definition>
</extension>
<extension point="org.eclipse.ui.menus">
diff --git a/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/copyqualifiedname/AbstractCopyQualifiedNameHandler.java b/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/copyqualifiedname/AbstractCopyQualifiedNameHandler.java
new file mode 100644
index 0000000..122e1c2
--- /dev/null
+++ b/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/copyqualifiedname/AbstractCopyQualifiedNameHandler.java
@@ -0,0 +1,73 @@
+/*******************************************************************************
+ * Copyright (c) 2013 itemis AG (http://www.itemis.eu) 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
+ *******************************************************************************/
+package org.eclipse.xtext.ui.editor.copyqualifiedname;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.xtext.naming.IQualifiedNameConverter;
+import org.eclipse.xtext.naming.IQualifiedNameProvider;
+import org.eclipse.xtext.naming.QualifiedName;
+import org.eclipse.xtext.resource.IResourceServiceProvider;
+
+import com.google.inject.Inject;
+
+/**
+ * @author Anton Kosyakov - Initial contribution and API
+ * @since 2.4
+ */
+public abstract class AbstractCopyQualifiedNameHandler extends AbstractHandler {
+
+ @Inject
+ private IResourceServiceProvider.Registry registry;
+
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+ String qualifiedName = getQualifiedName(event);
+ return ClipboardUtil.copy(qualifiedName);
+ }
+
+ protected abstract String getQualifiedName(ExecutionEvent event);
+
+ protected String getQualifiedName(EObject selectedElement) {
+ if (selectedElement.eIsProxy()) {
+ return null;
+ }
+ QualifiedName fullyQualifiedName = getFullyQualifiedName(selectedElement);
+ return toString(selectedElement, fullyQualifiedName);
+ }
+
+ private QualifiedName getFullyQualifiedName(EObject selectedElement) {
+ if (selectedElement == null) {
+ return null;
+ }
+ return getQualifiedNameProvider(selectedElement).getFullyQualifiedName(selectedElement);
+ }
+
+ private String toString(EObject selectedElement, QualifiedName fullyQualifiedName) {
+ if (fullyQualifiedName == null) {
+ return null;
+ }
+ return getQualifiedNameConverter(selectedElement).toString(fullyQualifiedName);
+ }
+
+ private IQualifiedNameProvider getQualifiedNameProvider(EObject selectedElement) {
+ return getService(selectedElement, IQualifiedNameProvider.class);
+ }
+
+ private IQualifiedNameConverter getQualifiedNameConverter(EObject selectedElement) {
+ return getService(selectedElement, IQualifiedNameConverter.class);
+ }
+
+ private <T> T getService(EObject selectedElement, Class<T> serviceClass) {
+ URI uri = selectedElement.eResource().getURI();
+ return registry.getResourceServiceProvider(uri).get(serviceClass);
+ }
+
+}
diff --git a/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/copyqualifiedname/ClipboardUtil.java b/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/copyqualifiedname/ClipboardUtil.java
new file mode 100644
index 0000000..784f2aa
--- /dev/null
+++ b/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/copyqualifiedname/ClipboardUtil.java
@@ -0,0 +1,39 @@
+/*******************************************************************************
+ * Copyright (c) 2013 itemis AG (http://www.itemis.eu) 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
+ *******************************************************************************/
+package org.eclipse.xtext.ui.editor.copyqualifiedname;
+
+import org.eclipse.swt.dnd.Clipboard;
+import org.eclipse.swt.dnd.TextTransfer;
+import org.eclipse.swt.dnd.Transfer;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.xtext.ui.util.SWTUtil;
+
+/**
+ * @author Anton Kosyakov - Initial contribution and API
+ * @since 2.4
+ */
+public class ClipboardUtil {
+
+ public static Object copy(String data) {
+ if (data == null) {
+ return null;
+ }
+ Clipboard clipboard = null;
+ try {
+ Display display = SWTUtil.getStandardDisplay();
+ clipboard = new Clipboard(display);
+ clipboard.setContents(new Object[] { data }, new Transfer[] { TextTransfer.getInstance() });
+ } finally {
+ if (clipboard != null) {
+ clipboard.dispose();
+ }
+ }
+ return null;
+ }
+
+}
diff --git a/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/copyqualifiedname/EditorCopyQualifiedNameHandler.java b/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/copyqualifiedname/EditorCopyQualifiedNameHandler.java
new file mode 100644
index 0000000..fa41067
--- /dev/null
+++ b/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/copyqualifiedname/EditorCopyQualifiedNameHandler.java
@@ -0,0 +1,55 @@
+/*******************************************************************************
+ * Copyright (c) 2013 itemis AG (http://www.itemis.eu) 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
+ *******************************************************************************/
+package org.eclipse.xtext.ui.editor.copyqualifiedname;
+
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.jface.text.ITextSelection;
+import org.eclipse.xtext.resource.EObjectAtOffsetHelper;
+import org.eclipse.xtext.resource.XtextResource;
+import org.eclipse.xtext.ui.editor.XtextEditor;
+import org.eclipse.xtext.ui.editor.utils.EditorUtils;
+import org.eclipse.xtext.util.concurrent.IUnitOfWork;
+
+import com.google.inject.Inject;
+
+/**
+ * @author Anton Kosyakov - Initial contribution and API
+ * @since 2.4
+ */
+public class EditorCopyQualifiedNameHandler extends AbstractCopyQualifiedNameHandler {
+
+ @Inject
+ private EObjectAtOffsetHelper eObjectAtOffsetHelper;
+
+ @Override
+ protected String getQualifiedName(ExecutionEvent event) {
+ XtextEditor activeXtextEditor = EditorUtils.getActiveXtextEditor(event);
+ if (activeXtextEditor == null) {
+ return null;
+ }
+ final ITextSelection selection = getTextSelection(activeXtextEditor);
+ return activeXtextEditor.getDocument().readOnly(new IUnitOfWork<String, XtextResource>() {
+
+ public String exec(XtextResource xTextResource) throws Exception {
+ EObject selectedElement = getSelectedName(selection, xTextResource);
+ return getQualifiedName(selectedElement);
+ }
+
+ });
+ }
+
+ private EObject getSelectedName(final ITextSelection selection, XtextResource xTextResource) {
+ return eObjectAtOffsetHelper.resolveElementAt(xTextResource, selection.getOffset());
+ }
+
+ private ITextSelection getTextSelection(XtextEditor activeXtextEditor) {
+ return (ITextSelection) activeXtextEditor.getSelectionProvider().getSelection();
+ }
+
+}
diff --git a/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/copyqualifiedname/OutlineCopyQualifiedNameHandler.java b/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/copyqualifiedname/OutlineCopyQualifiedNameHandler.java
new file mode 100644
index 0000000..fc59c27
--- /dev/null
+++ b/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/copyqualifiedname/OutlineCopyQualifiedNameHandler.java
@@ -0,0 +1,48 @@
+/*******************************************************************************
+ * Copyright (c) 2013 itemis AG (http://www.itemis.eu) 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
+ *******************************************************************************/
+package org.eclipse.xtext.ui.editor.copyqualifiedname;
+
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.handlers.HandlerUtil;
+import org.eclipse.xtext.ui.editor.outline.IOutlineNode;
+import org.eclipse.xtext.util.concurrent.IUnitOfWork;
+
+/**
+ * @author Anton Kosyakov - Initial contribution and API
+ * @since 2.4
+ */
+public class OutlineCopyQualifiedNameHandler extends AbstractCopyQualifiedNameHandler {
+
+ @Override
+ protected String getQualifiedName(ExecutionEvent event) {
+ IOutlineNode outlineNode = getOutlineNode(event);
+ if (outlineNode == null) {
+ return null;
+ }
+ return outlineNode.readOnly(new IUnitOfWork<String, EObject>() {
+
+ public String exec(EObject selectedElement) throws Exception {
+ return getQualifiedName(selectedElement);
+ }
+
+ });
+ }
+
+ private IOutlineNode getOutlineNode(ExecutionEvent event) {
+ ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
+ if (!(currentSelection instanceof IStructuredSelection)) {
+ return null;
+ }
+ IStructuredSelection structuredSelection = (IStructuredSelection) currentSelection;
+ return (IOutlineNode) structuredSelection.getFirstElement();
+ }
+
+}
diff --git a/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/outline/impl/OutlinePage.java b/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/outline/impl/OutlinePage.java
index 95c1c8a..60f4a38 100644
--- a/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/outline/impl/OutlinePage.java
+++ b/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/outline/impl/OutlinePage.java
@@ -15,12 +15,16 @@ import java.util.List;
import org.apache.log4j.Logger;
import org.eclipse.core.runtime.Assert;
+import org.eclipse.jface.action.MenuManager;
+import org.eclipse.jface.action.Separator;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextInputListener;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Menu;
+import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.ISourceViewerAware;
@@ -38,11 +42,16 @@ import com.google.inject.Inject;
/**
* @author Jan Koehnlein - Initial contribution and API
+ * @author Anton Kosyakov - Added registering of a context menu
*/
public class OutlinePage extends ContentOutlinePage implements ISourceViewerAware {
+ private static final String MENU_ID = "org.eclipse.xtext.ui.outline";
+
private static final Logger LOG = Logger.getLogger(OutlinePage.class);
+ private static final String CONTEXT_MENU_ID = "OutlinePageContextMenu";
+
@Inject
private OutlineNodeLabelProvider labelProvider;
@@ -52,9 +61,9 @@ public class OutlinePage extends ContentOutlinePage implements ISourceViewerAwar
@Inject
private IOutlineTreeProvider treeProvider;
- @Inject
+ @Inject
private OutlineFilterAndSorter filterAndSorter;
-
+
@Inject
private IOutlineContribution.Composite contribution;
@@ -76,6 +85,7 @@ public class OutlinePage extends ContentOutlinePage implements ISourceViewerAwar
configureModelListener();
configureActions();
refreshJob.setOutlinePage(this);
+ configureContextMenu();
}
protected void configureTree() {
@@ -132,6 +142,19 @@ public class OutlinePage extends ContentOutlinePage implements ISourceViewerAwar
contribution.register(this);
}
+ /**
+ * @since 2.4
+ */
+ protected void configureContextMenu() {
+ MenuManager menuManager = new MenuManager(CONTEXT_MENU_ID, CONTEXT_MENU_ID);
+ menuManager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
+ menuManager.setRemoveAllWhenShown(true);
+
+ Menu contextMenu = menuManager.createContextMenu(getTreeViewer().getTree());
+ getTreeViewer().getTree().setMenu(contextMenu);
+ getSite().registerContextMenu(MENU_ID, menuManager, getTreeViewer());
+ }
+
@Override
public void dispose() {
contribution.deregister(this);
@@ -209,7 +232,7 @@ public class OutlinePage extends ContentOutlinePage implements ISourceViewerAwar
public OutlineFilterAndSorter getFilterAndSorter() {
return filterAndSorter;
}
-
+
protected void refreshViewer(final IOutlineNode rootNode, final Collection<IOutlineNode> nodesToBeExpanded,
final Collection<IOutlineNode> selectedNodes) {
DisplayRunHelper.runAsyncInDisplayThread(new Runnable() {