1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-24 01:15:29 +02:00

Type Hierarchy: support for method of field as input.

This commit is contained in:
Markus Schorn 2007-01-25 16:31:04 +00:00
parent ca878d9a0b
commit 3491d11518
14 changed files with 200 additions and 125 deletions

View file

@ -76,13 +76,11 @@ public class TypeHierarchyBaseTest extends BaseUITestCase {
}
protected void openTypeHierarchy(CEditor editor) {
TypeHierarchyUI.setIsJUnitTest(true);
TypeHierarchyUI.open(editor, (ITextSelection) editor.getSelectionProvider().getSelection());
runEventQueue(200);
}
protected void openTypeHierarchy(CEditor editor, int mode) {
TypeHierarchyUI.setIsJUnitTest(true);
TypeHierarchyUI.open(editor, (ITextSelection) editor.getSelectionProvider().getSelection());
runEventQueue(0);
THViewPart th= null;

View file

@ -138,12 +138,9 @@ public class CallHierarchyUI {
else {
ICElement[] elems= IndexUI.findAllDefinitions(index, binding);
if (elems.length == 0) {
elems= IndexUI.findAllDefinitions(index, binding);
if (elems.length == 0) {
ICElement elem= IndexUI.findAnyDeclaration(index, project, binding);
if (elems != null) {
elems= new ICElement[]{elem};
}
ICElement elem= IndexUI.findAnyDeclaration(index, project, binding);
if (elems != null) {
elems= new ICElement[]{elem};
}
}
return elems;

View file

@ -86,7 +86,6 @@ public class WorkInProgressPreferencePage extends PreferencePage implements IWor
// Add your controls here
addCheckBox(result, "Use new model builder", CCorePlugin.PREF_USE_NEW_MODEL_BUILDER); //$NON-NLS-1$
addCheckBox(result, "Show context menus for type hierarchy", "showCtxMenuTypeHierarchy"); //$NON-NLS-1$ //$NON-NLS-2$
applyDialogFont(result);
return result;

View file

@ -21,18 +21,18 @@ import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.DropTargetListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.cdt.core.model.ICElement;
public class THDropTargetListener implements DropTargetListener {
private THViewPart fTypeHierarchy;
private ICElement fInput;
private boolean fEnabled= true;
private IWorkbenchWindow fWindow;
public THDropTargetListener(THViewPart view) {
fTypeHierarchy= view;
fWindow= view.getSite().getWorkbenchWindow();
}
public void setEnabled(boolean val) {
@ -87,7 +87,7 @@ public class THDropTargetListener implements DropTargetListener {
Display.getCurrent().beep();
}
else {
fTypeHierarchy.setInput(fInput);
TypeHierarchyUI.open(fInput, fWindow);
}
}

View file

@ -219,24 +219,26 @@ class THGraph {
THGraphNode graphNode= addNode(elem);
try {
IBinding binding = IndexUI.elementToBinding(index, elem);
IIndexName[] names= index.findNames(binding, IIndex.FIND_REFERENCES | IIndex.FIND_DEFINITIONS);
for (int i = 0; i < names.length; i++) {
if (monitor.isCanceled()) {
return;
}
IIndexName indexName = names[i];
if (indexName.isBaseSpecifier()) {
IIndexName subClassDef= indexName.getEnclosingDefinition();
if (subClassDef != null) {
IBinding subClass= index.findBinding(subClassDef);
ICElementHandle[] subClassElems= IndexUI.findRepresentative(index, subClass);
if (subClassElems.length > 0) {
ICElementHandle subClassElem= subClassElems[0];
THGraphNode subGraphNode= addNode(subClassElem);
addMembers(index, subGraphNode, subClass);
addEdge(subGraphNode, graphNode);
if (handled.add(subClassElem)) {
stack.add(subClassElem);
if (binding != null) {
IIndexName[] names= index.findNames(binding, IIndex.FIND_REFERENCES | IIndex.FIND_DEFINITIONS);
for (int i = 0; i < names.length; i++) {
if (monitor.isCanceled()) {
return;
}
IIndexName indexName = names[i];
if (indexName.isBaseSpecifier()) {
IIndexName subClassDef= indexName.getEnclosingDefinition();
if (subClassDef != null) {
IBinding subClass= index.findBinding(subClassDef);
ICElementHandle[] subClassElems= IndexUI.findRepresentative(index, subClass);
if (subClassElems.length > 0) {
ICElementHandle subClassElem= subClassElems[0];
THGraphNode subGraphNode= addNode(subClassElem);
addMembers(index, subGraphNode, subClass);
addEdge(subGraphNode, graphNode);
if (handled.add(subClassElem)) {
stack.add(subClassElem);
}
}
}
}

View file

@ -92,7 +92,7 @@ class THHierarchyModel {
public void setShowInheritedMembers(boolean showInheritedMembers) {
fShowInheritedMembers = showInheritedMembers;
computeSelectedMember();
updateSelectedMember();
updateImplementors();
}
@ -107,9 +107,11 @@ class THHierarchyModel {
//
// }
synchronized public void setInput(ICElement input) {
synchronized public void setInput(ICElement input, ICElement member) {
stopGraphComputation();
fInput= input;
fSelectedMember= member;
fMemberSignatureToSelect= TypeHierarchyUI.getLocalElementSignature(fSelectedMember);
fRootNodes= null;
fSelectedTypeNode= null;
fTypeToSelect= input;
@ -223,14 +225,14 @@ class THHierarchyModel {
}
if (fSelectedTypeNode != null) {
fTypeToSelect= fSelectedTypeNode.getElement();
computeSelectedMember();
updateSelectedMember();
}
fRootNodes= (THNode[]) roots.toArray(new THNode[roots.size()]);
updateImplementors();
}
private void computeSelectedMember() {
private void updateSelectedMember() {
ICElement oldSelection= fSelectedMember;
fSelectedMember= null;
if (fSelectedTypeNode != null && fMemberSignatureToSelect != null) {
@ -315,7 +317,7 @@ class THHierarchyModel {
if (node != null) {
fTypeToSelect= node.getElement();
}
computeSelectedMember();
updateSelectedMember();
updateImplementors();
}
@ -357,7 +359,7 @@ class THHierarchyModel {
}
private boolean isImplementor(ICElement element) {
if (element == null || fSelectedMember == null) {
if (element == null || fSelectedMember == null || fMemberSignatureToSelect == null) {
return false;
}
THGraphNode gnode= fGraph.getNode(element);
@ -369,8 +371,7 @@ class THHierarchyModel {
if (member == fSelectedMember) {
return true;
}
if (fMemberSignatureToSelect != null &&
fMemberSignatureToSelect.equals(TypeHierarchyUI.getLocalElementSignature(member))) {
if (fMemberSignatureToSelect.equals(TypeHierarchyUI.getLocalElementSignature(member))) {
return true;
}
}

View file

@ -53,7 +53,7 @@ public class THHistoryAction extends Action {
* @see Action#run()
*/
public void run() {
fViewPart.setInput(fElement);
fViewPart.setInput(fElement, null);
}
}

View file

@ -36,7 +36,7 @@ public class THHistoryDropDownAction extends Action implements IMenuCreator {
public void run() {
fView.setHistoryEntries(new ICElement[0]);
fView.setInput(null);
fView.setInput(null, null);
}
}

View file

@ -174,7 +174,7 @@ public class THHistoryListAction extends Action {
HistoryListDialog dialog= new HistoryListDialog(fView.getSite().getShell(), historyEntries);
if (dialog.open() == Window.OK) {
fView.setHistoryEntries(dialog.getRemaining());
fView.setInput(dialog.getResult());
fView.setInput(dialog.getResult(), null);
}
}

View file

@ -185,20 +185,20 @@ public class THViewPart extends ViewPart {
updateActionEnablement();
}
void setInput(ICElement input) {
if (input == null) {
void setInput(ICElement inputType, ICElement inputMember) {
if (inputType == null) {
setMessage(Messages.THViewPart_instruction);
fHierarchyTreeViewer.setInput(null);
fMemberViewer.setInput(null);
return;
}
fShowsMessage= false;
fModel.setInput(input);
fModel.setInput(inputType, inputMember);
fHierarchyTreeViewer.setInput(fModel);
fMemberViewer.setInput(fModel);
fPagebook.showPage(fSplitter);
updateDescription();
updateHistory(input);
updateHistory(inputType);
updateActionEnablement();
fModel.computeGraph();
}
@ -365,7 +365,7 @@ public class THViewPart extends ViewPart {
});
menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, new Action(label) {
public void run() {
setInput(elem);
setInput(elem, null);
}
});
}

View file

@ -29,9 +29,13 @@ import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IEnumeration;
import org.eclipse.cdt.core.dom.ast.IEnumerator;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMember;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexManager;
import org.eclipse.cdt.core.index.IIndexName;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject;
@ -40,32 +44,40 @@ import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.internal.ui.actions.OpenActionUtil;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.util.ExceptionHandler;
import org.eclipse.cdt.internal.ui.viewsupport.CElementLabels;
import org.eclipse.cdt.internal.ui.viewsupport.FindNameForSelectionVisitor;
import org.eclipse.cdt.internal.ui.viewsupport.IndexUI;
public class TypeHierarchyUI {
private static boolean sIsJUnitTest= false;
public static void setIsJUnitTest(boolean val) {
sIsJUnitTest= val;
}
public static THViewPart open(ICElement input, IWorkbenchWindow window) {
if (input != null) {
return openInViewPart(window, input);
if (!isValidInput(input)) {
return null;
}
ICElement memberInput= null;
if (!isValidTypeInput(input)) {
memberInput= input;
input= memberInput.getParent();
if (!isValidTypeInput(input)) {
ICElement[] inputs= findInput(memberInput);
if (inputs != null) {
input= inputs[0];
memberInput= inputs[1];
}
}
}
if (isValidTypeInput(input)) {
return openInViewPart(window, input, memberInput);
}
return null;
}
private static THViewPart openInViewPart(IWorkbenchWindow window, ICElement input) {
private static THViewPart openInViewPart(IWorkbenchWindow window, ICElement input, ICElement member) {
IWorkbenchPage page= window.getActivePage();
try {
THViewPart result= (THViewPart)page.showView(CUIPlugin.ID_TYPE_HIERARCHY);
result.setInput(input);
result.setInput(input, member);
return result;
} catch (CoreException e) {
ExceptionHandler.handle(e, window.getShell(), Messages.TypeHierarchyUI_OpenTypeHierarchy, null);
@ -73,29 +85,6 @@ public class TypeHierarchyUI {
return null;
}
private static THViewPart openInViewPart(IWorkbenchWindow window, ICElement[] input) {
ICElement elem = null;
switch (input.length) {
case 0:
break;
case 1:
elem = input[0];
break;
default:
if (sIsJUnitTest) {
throw new RuntimeException("ambigous input"); //$NON-NLS-1$
}
elem = OpenActionUtil.selectCElement(input, window.getShell(),
Messages.TypeHierarchyUI_OpenTypeHierarchy, Messages.TypeHierarchyUI_SelectFromList,
CElementLabels.ALL_DEFAULT | CElementLabels.MF_POST_FILE_QUALIFIED, 0);
break;
}
if (elem != null) {
return openInViewPart(window, elem);
}
return null;
}
public static void open(final CEditor editor, final ITextSelection sel) {
if (editor != null) {
final ICProject project= editor.getInputCElement().getCProject();
@ -105,11 +94,11 @@ public class TypeHierarchyUI {
Job job= new Job(Messages.TypeHierarchyUI_OpenTypeHierarchy) {
protected IStatus run(IProgressMonitor monitor) {
try {
final ICElement[] elems= findDefinitions(project, editorInput, sel);
if (elems != null && elems.length > 0) {
final ICElement[] elems= findInput(project, editorInput, sel);
if (elems != null && elems.length == 2) {
display.asyncExec(new Runnable() {
public void run() {
openInViewPart(editor.getSite().getWorkbenchWindow(), elems);
openInViewPart(editor.getSite().getWorkbenchWindow(), elems[0], elems[1]);
}});
}
return Status.OK_STATUS;
@ -124,7 +113,7 @@ public class TypeHierarchyUI {
}
}
private static ICElement[] findDefinitions(ICProject project, IEditorInput editorInput, ITextSelection sel) throws CoreException {
private static ICElement[] findInput(ICProject project, IEditorInput editorInput, ITextSelection sel) throws CoreException {
try {
IIndex index= CCorePlugin.getIndexManager().getIndex(project, IIndexManager.ADD_DEPENDENCIES | IIndexManager.ADD_DEPENDENT);
@ -133,25 +122,19 @@ public class TypeHierarchyUI {
IASTName name= getSelectedName(index, editorInput, sel);
if (name != null) {
IBinding binding= name.resolveBinding();
if (isValidInput(binding)) {
if (name.isDefinition()) {
ICElement elem= IndexUI.getCElementForName(project, index, name);
if (elem != null) {
return new ICElement[]{elem};
}
}
else {
ICElement[] elems= IndexUI.findAllDefinitions(index, binding);
if (elems.length == 0) {
elems= IndexUI.findAllDefinitions(index, binding);
if (elems.length == 0) {
ICElement elem= IndexUI.findAnyDeclaration(index, project, binding);
if (elems != null) {
elems= new ICElement[]{elem};
}
}
}
return elems;
if (!isValidInput(binding)) {
return null;
}
ICElement member= null;
if (!isValidTypeInput(binding)) {
member= findDeclaration(project, index, name, binding);
name= null;
binding= findTypeBinding(binding);
}
if (isValidTypeInput(binding)) {
ICElement input= findDefinition(project, index, name, binding);
if (input != null) {
return new ICElement[] {input, member};
}
}
}
@ -173,6 +156,81 @@ public class TypeHierarchyUI {
return null;
}
private static ICElement[] findInput(ICElement member) {
ICProject project= member.getCProject();
try {
IIndex index= CCorePlugin.getIndexManager().getIndex(project, IIndexManager.ADD_DEPENDENCIES | IIndexManager.ADD_DEPENDENT);
index.acquireReadLock();
try {
IIndexName name= IndexUI.elementToName(index, member);
if (name != null) {
member= IndexUI.getCElementForName(project, index, name);
IBinding binding= index.findBinding(name);
binding= findTypeBinding(binding);
if (isValidTypeInput(binding)) {
ICElement input= findDefinition(project, index, null, binding);
if (input != null) {
return new ICElement[] {input, member};
}
}
}
}
finally {
if (index != null) {
index.releaseReadLock();
}
}
}
catch (CoreException e) {
CUIPlugin.getDefault().log(e);
}
catch (DOMException e) {
CUIPlugin.getDefault().log(e);
}
catch (InterruptedException e) {
}
return null;
}
private static IBinding findTypeBinding(IBinding memberBinding) throws DOMException {
if (memberBinding instanceof IEnumerator) {
IType type= ((IEnumerator) memberBinding).getType();
if (type instanceof IBinding) {
return (IBinding) type;
}
}
else if (memberBinding instanceof ICPPMember) {
return ((ICPPMember) memberBinding).getClassOwner();
}
return null;
}
private static ICElement findDefinition(ICProject project, IIndex index,
IASTName name, IBinding binding) throws CoreException, DOMException {
if (name != null && name.isDefinition()) {
return IndexUI.getCElementForName(project, index, name);
}
ICElement[] elems= IndexUI.findAllDefinitions(index, binding);
if (elems.length > 0) {
return elems[0];
}
return IndexUI.findAnyDeclaration(index, project, binding);
}
private static ICElement findDeclaration(ICProject project, IIndex index,
IASTName name, IBinding binding) throws CoreException, DOMException {
if (name != null && name.isDefinition()) {
return IndexUI.getCElementForName(project, index, name);
}
ICElement[] elems= IndexUI.findAllDefinitions(index, binding);
if (elems.length > 0) {
return elems[0];
}
return IndexUI.findAnyDeclaration(index, project, binding);
}
private static IASTName getSelectedName(IIndex index, IEditorInput editorInput, ITextSelection selection) throws CoreException {
int selectionStart = selection.getOffset();
int selectionLength = selection.getLength();
@ -189,16 +247,43 @@ public class TypeHierarchyUI {
}
public static boolean isValidInput(IBinding binding) {
if (isValidTypeInput(binding)
|| binding instanceof ICPPMember
|| binding instanceof IEnumerator) {
return true;
}
return false;
}
public static boolean isValidTypeInput(IBinding binding) {
if (binding instanceof ICompositeType
|| binding instanceof IEnumeration
|| binding instanceof ITypedef) {
// binding instanceof IField || binding instanceof ICPPMethod) {
return true;
}
return false;
}
public static boolean isValidInput(ICElement elem) {
if (elem == null) {
return false;
}
if (isValidTypeInput(elem)) {
return true;
}
switch (elem.getElementType()) {
case ICElement.C_FIELD:
case ICElement.C_METHOD:
case ICElement.C_METHOD_DECLARATION:
case ICElement.C_TEMPLATE_METHOD:
case ICElement.C_TEMPLATE_METHOD_DECLARATION:
case ICElement.C_ENUMERATOR:
return true;
}
return false;
}
public static boolean isValidTypeInput(ICElement elem) {
if (elem == null) {
return false;
}
@ -211,11 +296,6 @@ public class TypeHierarchyUI {
case ICElement.C_UNION_DECLARATION:
case ICElement.C_ENUMERATION:
case ICElement.C_TYPEDEF:
// case ICElement.C_FIELD:
// case ICElement.C_METHOD:
// case ICElement.C_METHOD_DECLARATION:
// case ICElement.C_TEMPLATE_METHOD:
// case ICElement.C_TEMPLATE_METHOD_DECLARATION:
return true;
}
return false;

View file

@ -72,7 +72,7 @@ public class IndexUI {
String elementName= element.getElementName();
int idx= elementName.lastIndexOf(":")+1; //$NON-NLS-1$
ISourceRange pos= sf.getSourceRange();
IRegion region= new Region(pos.getIdStartPos()+idx, pos.getIdLength());
IRegion region= new Region(pos.getIdStartPos()+idx, pos.getIdLength()-idx);
IPositionConverter converter= CCorePlugin.getPositionTrackerManager().findPositionConverter(tu, file.getTimestamp());
if (converter != null) {
region= converter.actualToHistoric(region);

View file

@ -188,6 +188,7 @@ public class CElementImageDescriptor extends CompositeImageDescriptor {
drawTopRight();
drawBottomRight();
drawBottomLeft();
drawTopLeft();
}
private void drawTopRight() {
@ -208,10 +209,6 @@ public class CElementImageDescriptor extends CompositeImageDescriptor {
x-= data.width;
drawImage(data, x, 0);
}
if ((fFlags & DEFINES) != 0) {
data= CPluginImages.DESC_OVR_DEFINES.getImageData();
drawImage(data, 0, 0);
}
if ((fFlags & TEMPLATE) != 0) {
data= CPluginImages.DESC_OVR_TEMPLATE.getImageData();
x-= data.width;
@ -260,6 +257,14 @@ public class CElementImageDescriptor extends CompositeImageDescriptor {
// }
}
private void drawTopLeft() {
ImageData data= null;
if ((fFlags & DEFINES) != 0) {
data= CPluginImages.DESC_OVR_DEFINES.getImageData();
drawImage(data, 0, 0);
}
}
private void drawBottomLeft() {
Point size= getSize();
int x= 0;

View file

@ -27,7 +27,6 @@ import org.eclipse.ui.dialogs.PropertyDialogAction;
import org.eclipse.ui.part.Page;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.internal.ui.IContextMenuConstants;
import org.eclipse.cdt.internal.ui.callhierarchy.CHViewPart;
@ -101,9 +100,7 @@ public class OpenViewActionGroup extends ActionGroup {
fOpenTypeHierarchy= new OpenTypeHierarchyAction(part);
fOpenTypeHierarchy.setActionDefinitionId(ICEditorActionDefinitionIds.OPEN_TYPE_HIERARCHY);
if (useTypeHierarchy()) {
part.setAction("OpenTypeHierarchy", fOpenTypeHierarchy); //$NON-NLS-1$
}
part.setAction("OpenTypeHierarchy", fOpenTypeHierarchy); //$NON-NLS-1$
fOpenCallHierarchy= new OpenCallHierarchyAction(part);
fOpenCallHierarchy.setActionDefinitionId(ICEditorActionDefinitionIds.OPEN_CALL_HIERARCHY);
@ -112,10 +109,6 @@ public class OpenViewActionGroup extends ActionGroup {
initialize(part.getEditorSite());
}
private boolean useTypeHierarchy() {
return CUIPlugin.getDefault().getPluginPreferences().getBoolean("showCtxMenuTypeHierarchy"); //$NON-NLS-1$
}
private void createSiteActions(IWorkbenchSite site) {
// fOpenSuperImplementation= new OpenSuperImplementationAction(site);
// fOpenSuperImplementation.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_SUPER_IMPLEMENTATION);
@ -172,7 +165,7 @@ public class OpenViewActionGroup extends ActionGroup {
public void fillContextMenu(IMenuManager menu) {
super.fillContextMenu(menu);
if (!fEditorIsOwner) {
if (useTypeHierarchy() && !fIsTypeHiararchyViewerOwner && fOpenTypeHierarchy.isEnabled()) {
if (!fIsTypeHiararchyViewerOwner && fOpenTypeHierarchy.isEnabled()) {
menu.appendToGroup(fGroupName, fOpenTypeHierarchy);
}
if (!fIsCallHiararchyViewerOwner && fOpenCallHierarchy.isEnabled()) {