1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-21 21:52:10 +02:00

Bug 546395 - Added "copy qualified name" feature

Change-Id: I66b544cf7d9b05fcf1e9d5641cfd5d6060fa802c
Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
This commit is contained in:
Marco Stornelli 2019-04-13 13:20:09 +02:00 committed by Jeff Johnston
parent 98ab473c5f
commit fc25433621
13 changed files with 246 additions and 8 deletions

View file

@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.cdt.ui; singleton:=true
Bundle-Version: 6.4.200.qualifier
Bundle-Version: 6.5.0.qualifier
Bundle-Activator: org.eclipse.cdt.ui.CUIPlugin
Bundle-Vendor: %providerName
Bundle-Localization: plugin

Binary file not shown.

After

Width:  |  Height:  |  Size: 433 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 543 B

View file

@ -82,6 +82,9 @@ asmEditor.description=Editor for Assembly Source Files
category.source.name=C/C++ Source
category.source.description= C/C++ Source Actions
ActionDefinition.copyQualifiedName.name= Copy Qualified Name
ActionDefinition.copyQualifiedName.description= Copy a fully qualified name to the system clipboard
ActionDefinition.sourceQuickMenu.name= Show Source Quick Menu
ActionDefinition.sourceQuickMenu.description= Shows the source quick menu
@ -682,3 +685,5 @@ semanticHighlightingExtensionPoint = Semantic Highlighting Extension Point
UserSettingEntries.name = CDT User Setting Entries
toolchains.preferences.name = Core Build Toolchains
CopyQualifiedName.label= Cop&y Qualified Name

View file

@ -1960,6 +1960,14 @@
id="org.eclipse.cdt.ui.actions.AddInclude">
</action>
<!-- Edit group -->
<action
definitionId="org.eclipse.cdt.ui.edit.text.c.copy.qualified.name"
label="%CopyQualifiedName.label"
retarget="true"
menubarPath="org.eclipse.jdt.ui.source.menu/editGroup"
icon="$nl$/icons/elcl16/cpyqual_menu.png"
id="org.eclipse.cdt.ui.actions.CopyQualifiedName">
</action>
<action
definitionId="org.eclipse.cdt.ui.edit.text.c.format"
label="%FormatAction.label"
@ -2397,6 +2405,11 @@
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
contextId="org.eclipse.cdt.ui.cEditorScope"
commandId="org.eclipse.cdt.ui.edit.text.c.toggle.comment"/>
<key
sequence="M1+Q"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
contextId="org.eclipse.cdt.ui.cEditorScope"
commandId="org.eclipse.cdt.ui.edit.text.c.copy.qualified.name"/>
<key
sequence="M1+7"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
@ -2956,6 +2969,12 @@
description="%category.source.description"
id="org.eclipse.cdt.ui.category.source">
</category>
<command
name="%ActionDefinition.copyQualifiedName.name"
description="%ActionDefinition.copyQualifiedName.description"
categoryId="org.eclipse.cdt.ui.category.source"
id="org.eclipse.cdt.ui.edit.text.c.copy.qualified.name">
</command>
<command
name="%ActionDefinition.format.name"
description="%ActionDefinition.format.description"

View file

@ -574,6 +574,8 @@ public class CPluginImages {
public static final ImageDescriptor DESC_ELCL_NAVIGATE_FORWARD = createUnManaged(T_ELCL, "forward_nav.gif"); //$NON-NLS-1$
public static final ImageDescriptor DESC_ELCL_OPEN_DECLARATION = createUnManaged(T_ELCL, "goto_input.gif"); //$NON-NLS-1$
public static final ImageDescriptor DESC_DLCL_COPY_QUALIFIED_NAME = createUnManaged(T_DLCL, "cpyqual_menu.png"); //$NON-NLS-1$
public static final ImageDescriptor DESC_ELCL_COPY_QUALIFIED_NAME = createUnManaged(T_ELCL, "cpyqual_menu.png"); //$NON-NLS-1$
// incorrectly defined descriptors
/** @deprecated as of CDT 8.0. */
@Deprecated

View file

@ -51,6 +51,7 @@ public interface ICHelpContextIds {
public static final String TOGGLE_MARK_OCCURRENCES_ACTION = PREFIX + "toggle_mark_occurrences_action_context"; //$NON-NLS-1$
public static final String FORMAT_ALL = PREFIX + "format_all_action"; //$NON-NLS-1$
public static final String GOTO_MATCHING_BRACKET_ACTION = PREFIX + "goto_matching_bracket_action"; //$NON-NLS-1$
public static final String COPY_QUALIFIED_NAME_ACTION = PREFIX + "copy_qualified_name_action_context"; //$NON-NLS-1$
// Preference/property pages
public static final String C_PREF_PAGE = PREFIX + "c_pref"; //$NON-NLS-1$

View file

@ -105,6 +105,12 @@ public class ActionMessages extends NLS {
public static String ExpandAllAction_label;
public static String ExpandAllAction_tooltip;
public static String ExpandAllAction_description;
public static String CopyQualifiedNameAction_ToolTipText;
public static String CopyQualifiedNameAction_InfoDialogTitel;
public static String CopyQualifiedNameAction_ErrorDescription;
public static String CopyQualifiedNameAction_ActionName;
public static String CopyQualifiedNameAction_ErrorTitle;
public static String CopyQualifiedNameAction_NoElementToQualify;
static {
// Initialize resource bundle.

View file

@ -127,3 +127,10 @@ CollapseAllAction_description=Collapse All
ExpandAllAction_label=Expand All
ExpandAllAction_tooltip=Expand All
ExpandAllAction_description=Expand All
CopyQualifiedNameAction_ToolTipText=Copy to fully qualified name to the system Clipboard
CopyQualifiedNameAction_InfoDialogTitel=Copy Qualified Name
CopyQualifiedNameAction_ErrorDescription=There was a problem when accessing the system clipboard. Retry?
CopyQualifiedNameAction_ActionName=Cop&y Qualified Name
CopyQualifiedNameAction_ErrorTitle=Problem Copying to Clipboard
CopyQualifiedNameAction_NoElementToQualify=No Element to qualify found in selection.

View file

@ -0,0 +1,179 @@
/*******************************************************************************
* Copyright (c) 2019 Marco Stornelli
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.cdt.internal.ui.actions;
import org.eclipse.cdt.core.browser.IQualifiedTypeName;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.internal.ui.CPluginImages;
import org.eclipse.cdt.internal.ui.ICHelpContextIds;
import org.eclipse.cdt.internal.ui.editor.CEditorMessages;
import org.eclipse.cdt.ui.CDTUITools;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.TextEditorAction;
public class CopyQualifiedNameAction extends TextEditorAction {
private ITextEditor fEditor;
private static class NameVisitor extends ASTVisitor {
private String qualifiedName;
public NameVisitor() {
shouldVisitNames = true;
qualifiedName = null;
}
@Override
public int visit(IASTName name) {
IBinding b = name.resolveBinding();
if (b instanceof ICPPBinding) {
try {
qualifiedName = String.join(IQualifiedTypeName.QUALIFIER, ((ICPPBinding) b).getQualifiedName());
} catch (DOMException e) {
CUIPlugin.log(e);
}
}
return PROCESS_ABORT;
}
public String getQualifiedName() {
return qualifiedName;
}
}
public CopyQualifiedNameAction(ITextEditor editor) {
super(CEditorMessages.getBundleForConstructedKeys(), "CopyQualifiedName.", editor); //$NON-NLS-1$
fEditor = editor;
setText(ActionMessages.CopyQualifiedNameAction_ActionName);
setToolTipText(ActionMessages.CopyQualifiedNameAction_ToolTipText);
setDisabledImageDescriptor(CPluginImages.DESC_DLCL_COPY_QUALIFIED_NAME);
setImageDescriptor(CPluginImages.DESC_ELCL_COPY_QUALIFIED_NAME);
PlatformUI.getWorkbench().getHelpSystem().setHelp(this, ICHelpContextIds.COPY_QUALIFIED_NAME_ACTION);
}
private boolean isValidSelection(ITextSelection selection) {
if (selection == null || selection.isEmpty() || selection.getLength() <= 0)
return false;
final int offset = selection.getOffset();
final int length = selection.getLength();
ITranslationUnit translationUnit = (ITranslationUnit) CDTUITools
.getEditorInputCElement(fEditor.getEditorInput());
String qualName = null;
try {
IASTTranslationUnit ast = translationUnit.getAST(null, 0);
if (ast != null) {
IASTNode enclosingNode = ast.getNodeSelector(null).findEnclosingNode(offset, length);
NameVisitor n = new NameVisitor();
enclosingNode.accept(n);
qualName = n.getQualifiedName();
}
} catch (CoreException e) {
return false;
}
return qualName != null;
}
@Override
public void update() {
super.update();
if (isEnabled()) {
setEnabled(isValidSelection(getCurrentSelection()));
}
}
/**
* Returns the editor's selection, or <code>null</code> if no selection can be obtained or the
* editor is <code>null</code>.
*
* @return the selection of the action's editor, or <code>null</code>
*/
protected ITextSelection getCurrentSelection() {
if (fEditor != null) {
ISelectionProvider provider = fEditor.getSelectionProvider();
if (provider != null) {
ISelection selection = provider.getSelection();
if (selection instanceof ITextSelection)
return (ITextSelection) selection;
}
}
return null;
}
@Override
public void run() {
ITextSelection textSelection = getCurrentSelection();
if (textSelection == null || !isValidSelection(textSelection)) {
return;
}
final int offset = textSelection.getOffset();
final int length = textSelection.getLength();
ITranslationUnit translationUnit = (ITranslationUnit) CDTUITools
.getEditorInputCElement(fEditor.getEditorInput());
String qualName = null;
try {
IASTTranslationUnit ast = translationUnit.getAST(null, 0);
if (ast != null) {
IASTNode enclosingNode = ast.getNodeSelector(null).findEnclosingNode(offset, length);
NameVisitor n = new NameVisitor();
enclosingNode.accept(n);
qualName = n.getQualifiedName();
}
} catch (CoreException e) {
CUIPlugin.log(e);
}
final Shell shell = fEditor.getEditorSite().getShell();
if (qualName == null) {
MessageDialog.openInformation(shell, ActionMessages.CopyQualifiedNameAction_InfoDialogTitel,
ActionMessages.CopyQualifiedNameAction_NoElementToQualify);
return;
}
Clipboard clipboard = new Clipboard(shell.getDisplay());
try {
clipboard.setContents(new String[] { qualName }, new Transfer[] { TextTransfer.getInstance() });
} catch (SWTError e) {
if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) {
throw e;
}
if (MessageDialog.openQuestion(shell, ActionMessages.CopyQualifiedNameAction_ErrorTitle,
ActionMessages.CopyQualifiedNameAction_ErrorDescription)) {
clipboard.setContents(new String[] { qualName }, new Transfer[] { TextTransfer.getInstance() });
}
} finally {
clipboard.dispose();
}
}
}

View file

@ -315,4 +315,10 @@ public interface ICEditorActionDefinitionIds extends ITextEditorActionDefinition
* (value <code>"org.eclipse.cdt.ui.edit.text.c.select.last"</code>).
*/
public static final String SELECT_LAST = "org.eclipse.cdt.ui.edit.text.c.select.last"; //$NON-NLS-1$
/**
* Action definition ID of the edit -> copy qualified name
* (value <code>"org.eclipse.cdt.ui.edit.text.c.copy.qualified.name"</code>).
*/
public static final String COPY_QUALIFIED_NAME = "org.eclipse.cdt.ui.edit.text.c.copy.qualified.name"; //$NON-NLS-1$
}

View file

@ -494,4 +494,11 @@ public class CdtActionConstants {
* <code>"org.eclipse.cdt.ui.actions.ExceptionOccurrences"</code>).
*/
public static final String FIND_EXCEPTION_OCCURRENCES = "org.eclipse.cdt.ui.actions.ExceptionOccurrences"; //$NON-NLS-1$
/**
* Source menu: name of standard action to copy fully qualified names (value
* <code>"org.eclipse.cdt.ui.actions.CopyQualifiedName"</code>).
* @since 6.5
*/
public static final String COPY_QUALIFIED_NAME = "org.eclipse.cdt.ui.actions.CopyQualifiedName"; //$NON-NLS-1$
}

View file

@ -23,6 +23,7 @@ import org.eclipse.cdt.core.model.ISourceReference;
import org.eclipse.cdt.internal.ui.IContextMenuConstants;
import org.eclipse.cdt.internal.ui.actions.ActionMessages;
import org.eclipse.cdt.internal.ui.actions.CDTQuickMenuCreator;
import org.eclipse.cdt.internal.ui.actions.CopyQualifiedNameAction;
import org.eclipse.cdt.internal.ui.editor.AddIncludeAction;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.editor.ICEditorActionDefinitionIds;
@ -134,7 +135,7 @@ public class GenerateActionGroup extends ActionGroup implements ISelectionChange
// private SortMembersAction fSortMembers;
private SortLinesAction fSortLines;
private FormatAllAction fFormatAll;
// private CopyQualifiedNameAction fCopyQualifiedNameAction;
private CopyQualifiedNameAction fCopyQualifiedNameAction;
//
private static final String QUICK_MENU_ID = "org.eclipse.cdt.ui.edit.text.c.source.quickMenu"; //$NON-NLS-1$
@ -172,10 +173,10 @@ public class GenerateActionGroup extends ActionGroup implements ISelectionChange
fSortLines.setActionDefinitionId(ICEditorActionDefinitionIds.SORT_LINES);
editor.setAction("SortLines", fSortLines); //$NON-NLS-1$
// IAction pastAction= editor.getAction(ITextEditorActionConstants.PASTE);//IWorkbenchActionDefinitionIds.PASTE);
// fCopyQualifiedNameAction= new CopyQualifiedNameAction(editor, null, pastAction);
// fCopyQualifiedNameAction.setActionDefinitionId(CopyQualifiedNameAction.JAVA_EDITOR_ACTION_DEFINITIONS_ID);
// editor.setAction("CopyQualifiedName", fCopyQualifiedNameAction); //$NON-NLS-1$
fCopyQualifiedNameAction = new CopyQualifiedNameAction(editor);
fCopyQualifiedNameAction.setActionDefinitionId(ICEditorActionDefinitionIds.COPY_QUALIFIED_NAME);
editor.setAction("CopyQualifiedName", fCopyQualifiedNameAction); //$NON-NLS-1$
editor.markAsSelectionDependentAction("CopyQualifiedName", true); //$NON-NLS-1$
//
// fOverrideMethods= new OverrideMethodsAction(editor);
// fOverrideMethods.setActionDefinitionId(ICEditorActionDefinitionIds.OVERRIDE_METHODS);
@ -307,6 +308,8 @@ public class GenerateActionGroup extends ActionGroup implements ISelectionChange
//
fFormatAll = new FormatAllAction(site);
fFormatAll.setActionDefinitionId(ICEditorActionDefinitionIds.FORMAT);
// fCopyQualifiedNameAction = new CopyQualifiedNameAction(site);
// fCopyQualifiedNameAction.setActionDefinitionId(ICEditorActionDefinitionIds.COPY_QUALIFIED_NAME);
//
// fCleanUp= new CleanUpAction(site);
// fCleanUp.setActionDefinitionId(ICEditorActionDefinitionIds.CLEAN_UP);
@ -346,6 +349,7 @@ public class GenerateActionGroup extends ActionGroup implements ISelectionChange
// registerSelectionListener(fSelectionProvider, fSortMembers);
registerSelectionListener(fSelectionProvider, fAddTaskAction);
// registerSelectionListener(fSelectionProvider, fCleanUp);
// registerSelectionListener(fSelectionProvider, fCopyQualifiedNameAction);
selectionChanged(new SelectionChangedEvent(fSelectionProvider, selection));
registerSelectionListener(fSelectionProvider, this);
@ -430,6 +434,7 @@ public class GenerateActionGroup extends ActionGroup implements ISelectionChange
added += addEditorAction(source, ITextEditorActionConstants.SHIFT_LEFT);
added += addEditorAction(source, "Indent"); //$NON-NLS-1$
added += addEditorAction(source, "Format"); //$NON-NLS-1$
added += addAction(source, fCopyQualifiedNameAction);
source.add(new Separator(GROUP_ORGANIZE));
added += addAction(source, fAddInclude);
added += addAction(source, fOrganizeIncludes);
@ -456,6 +461,7 @@ public class GenerateActionGroup extends ActionGroup implements ISelectionChange
// added+= addAction(source, fAddCppDocStub);
source.add(new Separator(GROUP_EDIT));
added += addAction(source, fFormatAll);
added += addAction(source, fCopyQualifiedNameAction);
source.add(new Separator(GROUP_ORGANIZE));
added += addAction(source, fAddInclude);
added += addAction(source, fOrganizeIncludes);
@ -515,7 +521,7 @@ public class GenerateActionGroup extends ActionGroup implements ISelectionChange
actionBar.setGlobalActionHandler(IDEActionFactory.ADD_TASK.getId(), fAddTaskAction);
actionBar.setGlobalActionHandler(CdtActionConstants.FORMAT, fFormatAll);
} else {
// actionBar.setGlobalActionHandler(CopyQualifiedNameAction.ACTION_HANDLER_ID, fCopyQualifiedNameAction);
actionBar.setGlobalActionHandler(CdtActionConstants.COPY_QUALIFIED_NAME, fCopyQualifiedNameAction);
}
}