Skip to content

Commit

Permalink
Do not expand same class twice in (quick) type hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
fedejeanne committed Jan 20, 2025
1 parent 7e83160 commit d96a6cc
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 10 deletions.
8 changes: 8 additions & 0 deletions org.eclipse.jdt.ui/.settings/.api_filters
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@
</message_arguments>
</filter>
</resource>
<resource path="ui/org/eclipse/jdt/internal/ui/typehierarchy/HierarchyInformationControl.java" type="org.eclipse.jdt.internal.ui.typehierarchy.HierarchyInformationControl">
<filter id="571519004">
<message_arguments>
<message_argument value="org.eclipse.jdt.internal.ui.typehierarchy.HierarchyInformationControl.createTreeViewer(Composite, int)"/>
<message_argument value="TreeViewer"/>
</message_arguments>
</filter>
</resource>
<resource path="ui/org/eclipse/jdt/internal/ui/viewsupport/ProblemTableViewer.java" type="org.eclipse.jdt.internal.ui.viewsupport.ProblemTableViewer">
<filter id="571473929">
<message_arguments>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package org.eclipse.jdt.internal.ui.typehierarchy;

import java.lang.reflect.InvocationTargetException;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
Expand All @@ -23,6 +25,7 @@
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.Widget;

import org.eclipse.jface.bindings.TriggerSequence;
import org.eclipse.jface.bindings.keys.KeySequence;
Expand Down Expand Up @@ -130,7 +133,32 @@ protected TreeViewer createTreeViewer(Composite parent, int style) {
gd.heightHint= tree.getItemHeight() * 12;
tree.setLayoutData(gd);

TreeViewer treeViewer= new TreeViewer(tree);
TreeViewer treeViewer= new TreeViewer(tree) {
private Set<Object> visited;

@Override
protected void inputChanged(Object input, Object oldInput) {
visited= new HashSet<>();
super.inputChanged(input, oldInput);
visited= null;
}

@Override
protected void internalExpandToLevel(Widget node, int level) {
if (!shouldExpand(node))
return;

super.internalExpandToLevel(node, level);
}

private boolean shouldExpand(Widget widget) {
if (widget == null) {
return false;
}
Object data= widget.getData();
return data == null || visited.add(data);
}
};
treeViewer.addFilter(new ViewerFilter() {
@Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,12 @@ private void updateHierarchyViewer(final boolean doExpand) {
fPagebook.showPage(fNoHierarchyShownLabel);
} else {
if (getCurrentViewer().containsElements() != null) {
Runnable runnable= () -> JavaCore.runReadOnly(() -> getCurrentViewer().updateContent(doExpand));
Runnable runnable= () -> JavaCore.runReadOnly(() -> {
TypeHierarchyViewer viewer= getCurrentViewer();
viewer.preUpdateContent();
viewer.updateContent(doExpand);
viewer.postUpdateContent();
});
BusyIndicator.showWhile(getDisplay(), runnable);
if (!isChildVisible(fViewerbook, getCurrentViewer().getControl())) {
setViewerVisibility(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
*******************************************************************************/
package org.eclipse.jdt.internal.ui.typehierarchy;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.Widget;

import org.eclipse.core.runtime.Assert;

Expand All @@ -42,6 +46,8 @@ public abstract class TypeHierarchyViewer extends ProblemTreeViewer {

private HierarchyLabelProvider fLabelProvider;

private Set<Object> visited;


public TypeHierarchyViewer(Composite parent, IContentProvider contentProvider, TypeHierarchyLifeCycle lifeCycle) {
super(new Tree(parent, SWT.SINGLE));
Expand Down Expand Up @@ -83,15 +89,17 @@ public void initContextMenu(IMenuListener menuListener, String popupId, IWorkben
}

/**
* Fills up the context menu with items for the hierarchy viewer
* Should be called by the creator of the context menu
* Fills up the context menu with items for the hierarchy viewer Should be called by the creator
* of the context menu
*
* @param menu the menu manager
*/
public void contributeToContextMenu(IMenuManager menu) {
}

/**
* Set the member filter
*
* @param memberFilter the member filters to set
*/
public void setMemberFilter(IMember[] memberFilter) {
Expand All @@ -103,6 +111,7 @@ public void setMemberFilter(IMember[] memberFilter) {

/**
* Returns if method filtering is enabled.
*
* @return <code>true</code>if method filtering is enabled.
*/
public boolean isMethodFiltering() {
Expand All @@ -122,8 +131,9 @@ public void setWorkingSetFilter(ViewerFilter filter) {
}

/**
* Returns true if the hierarchy contains elements. Returns one of them
* With member filtering it is possible that no elements are visible
* Returns true if the hierarchy contains elements. Returns one of them With member filtering it
* is possible that no elements are visible
*
* @return one of the elements contained
*/
public Object containsElements() {
Expand All @@ -138,14 +148,15 @@ public Object containsElements() {
}

/**
* Returns true if the hierarchy contains elements. Returns one of them
* With member filtering it is possible that no elements are visible
* Returns true if the hierarchy contains elements. Returns one of them With member filtering it
* is possible that no elements are visible
*
* @return the tree root
*/
public IType getTreeRootType() {
TypeHierarchyContentProvider contentProvider= getHierarchyContentProvider();
if (contentProvider != null) {
Object[] elements= contentProvider.getElements(null);
Object[] elements= contentProvider.getElements(null);
if (elements.length > 0 && elements[0] instanceof IType) {
return (IType) elements[0];
}
Expand All @@ -155,6 +166,7 @@ public IType getTreeRootType() {

/**
* Returns true if the hierarchy contains element the element.
*
* @param element the element
* @return <code>true</code> if element is shown
*/
Expand All @@ -164,6 +176,7 @@ public boolean isElementShown(Object element) {

/**
* Updates the content of this viewer: refresh and expanding the tree in the way wanted.
*
* @param doExpand if set, update should expand
*/
public abstract void updateContent(boolean doExpand);
Expand All @@ -179,7 +192,37 @@ public void setContentProvider(IContentProvider cp) {
}

protected TypeHierarchyContentProvider getHierarchyContentProvider() {
return (TypeHierarchyContentProvider)getContentProvider();
return (TypeHierarchyContentProvider) getContentProvider();
}

@Override
protected void inputChanged(Object input, Object oldInput) {
preUpdateContent();
super.inputChanged(input, oldInput);
postUpdateContent();
}

@Override
protected void internalExpandToLevel(Widget node, int level) {
if (!shouldExpand(node))
return;

super.internalExpandToLevel(node, level);
}

private boolean shouldExpand(Widget widget) {
if (widget == null) {
return false;
}
Object data= widget.getData();
return data == null || visited.add(data);
}

void preUpdateContent() {
visited= new HashSet<>();
}

void postUpdateContent() {
visited= null;
}
}

0 comments on commit d96a6cc

Please sign in to comment.