1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +02:00

Bug 507285 - Open Declaration for assembly editors

Currently, it only does something useful for include statements.
It uses the C model to resolve the include, much like clicking
on the include in the Outline view.

Change-Id: I4c50ff4b477721795d58f44acc5c0389c05d4768
This commit is contained in:
Nathan Ridge 2016-12-06 00:19:53 -05:00
parent 5d0e9dc625
commit 5b86da58ad
13 changed files with 137 additions and 27 deletions

View file

@ -73,6 +73,9 @@ NewWizards.class.description = Create a new C++ class
scope.cEditor.name=C/C++ Editor
cEditor.description=Editor for C/C++ Source Files
scope.asmEditor.name=Assembly Editor
asmEditor.description=Editor for Assembly Source Files
category.source.name=C/C++ Source
category.source.description= C/C++ Source Actions

View file

@ -2243,6 +2243,12 @@
description="%cEditor.description"
id="org.eclipse.cdt.ui.cEditorScope">
</context>
<context
name="%scope.asmEditor.name"
parentId="org.eclipse.ui.textEditorScope"
description="%asmEditor.description"
id="org.eclipse.cdt.ui.asmEditorScope">
</context>
<context
name="%cViewScope.name"
description="%cViewScope.description"
@ -2456,6 +2462,11 @@
contextId="org.eclipse.cdt.ui.cEditorScope"
commandId="org.eclipse.cdt.ui.edit.opendecl"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"/>
<key
sequence="F3"
contextId="org.eclipse.cdt.ui.asmEditorScope"
commandId="org.eclipse.cdt.ui.edit.opendecl"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"/>
<key
sequence="F3"
contextId="org.eclipse.cdt.ui.cViewScope"

View file

@ -195,6 +195,7 @@ import org.eclipse.cdt.core.model.ITranslationUnitHolder;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.ICEditor;
import org.eclipse.cdt.ui.ICModelBasedEditor;
import org.eclipse.cdt.ui.IWorkingCopyManager;
import org.eclipse.cdt.ui.PreferenceConstants;
import org.eclipse.cdt.ui.actions.GenerateActionGroup;
@ -249,7 +250,8 @@ import org.eclipse.cdt.internal.ui.viewsupport.SelectionListenerWithASTManager;
/**
* C/C++ source editor.
*/
public class CEditor extends TextEditor implements ICEditor, ISelectionChangedListener, ICReconcilingListener, ITranslationUnitHolder {
public class CEditor extends TextEditor implements ICEditor, ISelectionChangedListener, ICReconcilingListener,
ICModelBasedEditor {
/** Marker used for synchronization from Problems View to the editor on double-click. */
private IMarker fSyncProblemsViewMarker;

View file

@ -54,6 +54,10 @@ public class ASMEditorActionContributor extends TextEditorActionContributor {
textEditor= (ITextEditor) part;
fTogglePresentation.setEditor(textEditor);
if (part instanceof AsmTextEditor) {
((AsmTextEditor) part).fillActionBars(getActionBars());
}
}
/*

View file

@ -37,10 +37,12 @@ import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorActionBarContributor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPartService;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionGroup;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.navigator.ICommonMenuConstants;
import org.eclipse.ui.part.EditorActionBarContributor;
@ -55,7 +57,9 @@ import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.ui.CDTUITools;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.ICModelBasedEditor;
import org.eclipse.cdt.ui.IWorkingCopyManager;
import org.eclipse.cdt.ui.actions.OpenViewActionGroup;
import org.eclipse.cdt.ui.text.AsmSourceViewerConfiguration;
import org.eclipse.cdt.ui.text.ICPartitions;
import org.eclipse.cdt.ui.text.IColorManager;
@ -69,7 +73,7 @@ import org.eclipse.cdt.internal.ui.editor.ICAnnotation;
/**
* Assembly text editor.
*/
public class AsmTextEditor extends TextEditor implements ISelectionChangedListener {
public class AsmTextEditor extends TextEditor implements ISelectionChangedListener, ICModelBasedEditor {
/**
* Updates the outline page selection and this editor's range indicator.
@ -87,6 +91,9 @@ public class AsmTextEditor extends TextEditor implements ISelectionChangedListen
private AbstractCModelOutlinePage fOutlinePage;
private EditorSelectionChangedListener fEditorSelectionChangedListener;
// Search actions
private ActionGroup fOpenInViewGroup;
/**
* Creates a new assembly text editor.
*/
@ -163,6 +170,10 @@ public class AsmTextEditor extends TextEditor implements ISelectionChangedListen
fEditorSelectionChangedListener.uninstall(getSelectionProvider());
fEditorSelectionChangedListener = null;
}
if (fOpenInViewGroup != null) {
fOpenInViewGroup.dispose();
fOpenInViewGroup = null;
}
super.dispose();
}
@ -202,7 +213,15 @@ public class AsmTextEditor extends TextEditor implements ISelectionChangedListen
menu.add(new Separator(IContextMenuConstants.GROUP_DEBUG));
menu.add(new GroupMarker(IContextMenuConstants.GROUP_DEBUG+".end")); //$NON-NLS-1$
// The base implementation creates GROUP_OPEN, so actions added
// to GROUP_OPEN need to come after this.
super.editorContextMenuAboutToShow(menu);
boolean hasCElement = getInputCElement() != null;
if (hasCElement) {
addAction(menu, IContextMenuConstants.GROUP_OPEN, "OpenDeclarations"); //$NON-NLS-1$
fOpenInViewGroup.fillContextMenu(menu);
}
}
/**
@ -358,7 +377,7 @@ public class AsmTextEditor extends TextEditor implements ISelectionChangedListen
* @return the most narrow element which includes the given offset
*/
protected ICElement getElementAt(int offset, boolean reconcile) {
ITranslationUnit unit= (ITranslationUnit)getInputCElement();
ITranslationUnit unit= getInputCElement();
if (unit != null) {
try {
@ -384,7 +403,7 @@ public class AsmTextEditor extends TextEditor implements ISelectionChangedListen
*
* @return the C element wrapped by this editors input.
*/
public ICElement getInputCElement () {
public IWorkingCopy getInputCElement () {
return CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(getEditorInput());
}
@ -553,4 +572,24 @@ public class AsmTextEditor extends TextEditor implements ISelectionChangedListen
}
}
@Override
public ITranslationUnit getTranslationUnit() {
return getInputCElement();
}
@Override
protected void initializeKeyBindingScopes() {
setKeyBindingScopes(new String [] { "org.eclipse.cdt.ui.asmEditorScope" }); //$NON-NLS-1$
}
@Override
protected void createActions() {
super.createActions();
fOpenInViewGroup = new OpenViewActionGroup(this);
}
public void fillActionBars(IActionBars actionBars) {
fOpenInViewGroup.fillActionBars(actionBars);
}
}

View file

@ -53,7 +53,7 @@ public abstract class FindAction extends SelectionParseAction {
searchJob = createQuery((ISourceReference) object);
} else if (selection instanceof ITextSelection) {
ITextSelection selNode = (ITextSelection) selection;
ICElement element = fEditor.getInputCElement();
ICElement element = fEditor.getTranslationUnit();
while (element != null && !(element instanceof ITranslationUnit))
element = element.getParent();
if (element != null) {

View file

@ -51,7 +51,7 @@ public class FindDeclarationsProjectAction extends FindAction {
protected ICElement[] getScope() {
ICProject project = null;
if (fEditor != null) {
project = fEditor.getInputCElement().getCProject();
project = fEditor.getTranslationUnit().getCProject();
} else if (fSite != null){
ISelection selection = getSelection();
if (selection instanceof IStructuredSelection) {

View file

@ -51,7 +51,7 @@ public class FindRefsProjectAction extends FindAction {
protected ICElement[] getScope() {
ICProject project = null;
if (fEditor != null) {
project = fEditor.getInputCElement().getCProject();
project = fEditor.getTranslationUnit().getCProject();
} else if (fSite != null){
ISelection selection = getSelection();
if (selection instanceof IStructuredSelection) {

View file

@ -24,9 +24,9 @@ import org.eclipse.jface.text.ITextSelection;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.ICModelBasedEditor;
import org.eclipse.cdt.internal.ui.actions.OpenActionUtil;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.editor.CEditorMessages;
import org.eclipse.cdt.internal.ui.text.CWordFinder;
import org.eclipse.cdt.internal.ui.viewsupport.CElementLabels;
@ -42,7 +42,7 @@ public class OpenDeclarationsAction extends SelectionParseAction {
/**
* Creates a new action with the given editor
*/
public OpenDeclarationsAction(CEditor editor) {
public OpenDeclarationsAction(ICModelBasedEditor editor) {
super(editor);
setText(CEditorMessages.OpenDeclarations_label);
setToolTipText(CEditorMessages.OpenDeclarations_tooltip);
@ -73,7 +73,7 @@ public class OpenDeclarationsAction extends SelectionParseAction {
private OpenDeclarationsJob createJob(ITargetDisambiguator targetDisambiguator) {
String text= computeSelectedWord();
OpenDeclarationsJob job= null;
ICElement elem= fEditor.getInputCElement();
ICElement elem= fEditor.getTranslationUnit();
if (elem instanceof ITranslationUnit && fTextSelection != null) {
job= new OpenDeclarationsJob(this, (ITranslationUnit) elem, fTextSelection, text,
targetDisambiguator);

View file

@ -76,8 +76,10 @@ import org.eclipse.cdt.core.index.IIndexMacro;
import org.eclipse.cdt.core.index.IIndexManager;
import org.eclipse.cdt.core.index.IIndexName;
import org.eclipse.cdt.core.index.IndexFilter;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.IInclude;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.IMethodDeclaration;
import org.eclipse.cdt.core.model.ISourceRange;
@ -102,8 +104,10 @@ import org.eclipse.cdt.internal.core.model.ASTCache.ASTRunnable;
import org.eclipse.cdt.internal.core.model.ext.CElementHandleFactory;
import org.eclipse.cdt.internal.core.model.ext.ICElementHandle;
import org.eclipse.cdt.internal.ui.actions.SelectionConverter;
import org.eclipse.cdt.internal.ui.editor.ASTProvider;
import org.eclipse.cdt.internal.ui.editor.CEditorMessages;
import org.eclipse.cdt.internal.ui.editor.CElementIncludeResolver;
import org.eclipse.cdt.internal.ui.search.actions.OpenDeclarationsAction.ITargetDisambiguator;
import org.eclipse.cdt.internal.ui.viewsupport.IndexUI;
@ -161,10 +165,32 @@ class OpenDeclarationsJob extends Job implements ASTRunnable {
}
}
// Navigation without an AST. Used for assembly files which do not have an AST.
// TODO(nathanridge): Can we avoid using ASTProvider altogether in this case?
private IStatus navigateWithoutAst() {
try {
ICElement element = SelectionConverter.getElementAtOffset(fTranslationUnit, fTextSelection);
if (element instanceof IInclude) {
IInclude include = (IInclude) element;
IPath fileToOpen = CElementIncludeResolver.resolveInclude(include);
if (fileToOpen != null) {
openInclude(fileToOpen);
return Status.OK_STATUS;
}
fAction.reportIncludeLookupFailure(include.getFullFileName());
return Status.OK_STATUS;
}
} catch (CModelException e) {
} catch (CoreException e) {
}
fAction.reportSelectionMatchFailure();
return Status.OK_STATUS;
}
@Override
public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) throws CoreException {
if (ast == null) {
return Status.OK_STATUS;
return navigateWithoutAst();
}
int selectionStart = fTextSelection.getOffset();
int selectionLength = fTextSelection.getLength();
@ -685,6 +711,19 @@ class OpenDeclarationsJob extends Job implements ASTRunnable {
}
}
private void openInclude(IPath path) {
runInUIThread(new Runnable() {
@Override
public void run() {
try {
fAction.open(path, 0, 0);
} catch (CoreException e) {
CUIPlugin.log(e);
}
}
});
}
private void openInclude(IASTPreprocessorIncludeStatement incStmt) {
String name = null;
if (incStmt.isResolved())
@ -692,16 +731,7 @@ class OpenDeclarationsJob extends Job implements ASTRunnable {
if (name != null) {
final IPath path = new Path(name);
runInUIThread(new Runnable() {
@Override
public void run() {
try {
fAction.open(path, 0, 0);
} catch (CoreException e) {
CUIPlugin.log(e);
}
}
});
openInclude(path);
} else {
fAction.reportIncludeLookupFailure(new String(incStmt.getName().toCharArray()));
}

View file

@ -26,8 +26,8 @@ import org.eclipse.ui.texteditor.ITextEditor;
import com.ibm.icu.text.MessageFormat;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.ui.ICModelBasedEditor;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.search.CSearchMessages;
import org.eclipse.cdt.internal.ui.util.EditorUtility;
import org.eclipse.cdt.internal.ui.util.StatusLineHandler;
@ -39,13 +39,13 @@ import org.eclipse.cdt.internal.ui.util.StatusLineHandler;
public class SelectionParseAction extends Action {
protected IWorkbenchSite fSite;
protected CEditor fEditor;
protected ICModelBasedEditor fEditor;
public SelectionParseAction() {
super();
}
public SelectionParseAction(CEditor editor) {
public SelectionParseAction(ICModelBasedEditor editor) {
super();
fEditor= editor;
fSite= editor.getSite();
@ -88,7 +88,7 @@ public class SelectionParseAction extends Action {
protected void open(IPath path, int currentOffset, int currentLength) throws CoreException {
clearStatusLine();
IEditorPart editor = EditorUtility.openInEditor(path, fEditor.getInputCElement());
IEditorPart editor = EditorUtility.openInEditor(path, fEditor.getTranslationUnit());
ITextEditor textEditor = EditorUtility.getTextEditor(editor);
if (textEditor != null) {
textEditor.selectAndReveal(currentOffset, currentLength);

View file

@ -0,0 +1,20 @@
/*******************************************************************************
* Copyright (c) 2016 Nathan Ridge.
* 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.cdt.ui;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.cdt.core.model.ITranslationUnitHolder;
/**
* A text editor whose contents are represented by the C model.
* Implementations include CEditor and AsmTextEditor.
* @since 6.1
*/
public interface ICModelBasedEditor extends ITextEditor, ITranslationUnitHolder {
}

View file

@ -29,6 +29,7 @@ import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.IInclude;
import org.eclipse.cdt.ui.ICModelBasedEditor;
import org.eclipse.cdt.internal.ui.IContextMenuConstants;
import org.eclipse.cdt.internal.ui.callhierarchy.OpenCallHierarchyAction;
@ -128,8 +129,8 @@ public class OpenViewActionGroup extends ActionGroup {
fOpenIncludeBrowser.setActionDefinitionId(ICEditorActionDefinitionIds.OPEN_INCLUDE_BROWSER);
part.setAction("OpenIncludeBrowser", fOpenIncludeBrowser); //$NON-NLS-1$
if (part instanceof CEditor) {
fOpenDeclaration= new OpenDeclarationsAction((CEditor) part);
if (part instanceof ICModelBasedEditor) {
fOpenDeclaration= new OpenDeclarationsAction((ICModelBasedEditor) part);
fOpenDeclaration.setActionDefinitionId(ICEditorActionDefinitionIds.OPEN_DECL);
part.setAction("OpenDeclarations", fOpenDeclaration); //$NON-NLS-1$
}