The structure view can be customized for a specific file type. Creating a structure view factory allows showing the structure of any file in the Structure tool window or by invoking Navigate | File Structure for easy navigation between items in the current editor.
final class SimpleStructureViewFactory implements PsiStructureViewFactory {
@Override
public StructureViewBuilder getStructureViewBuilder(@NotNull final PsiFile psiFile) {
return new TreeBasedStructureViewBuilder() {
@NotNull
@Override
public StructureViewModel createStructureViewModel(@Nullable Editor editor) {
return new SimpleStructureViewModel(editor, psiFile);
}
};
}
}
public class SimpleStructureViewModel extends StructureViewModelBase implements
StructureViewModel.ElementInfoProvider {
public SimpleStructureViewModel(@Nullable Editor editor, PsiFile psiFile) {
super(psiFile, editor, new SimpleStructureViewElement(psiFile));
}
@NotNull
public Sorter @NotNull [] getSorters() {
return new Sorter[]{Sorter.ALPHA_SORTER};
}
@Override
public boolean isAlwaysShowsPlus(StructureViewTreeElement element) {
return false;
}
@Override
public boolean isAlwaysLeaf(StructureViewTreeElement element) {
return element.getValue() instanceof SimpleProperty;
}
@Override
protected Class<?> @NotNull [] getSuitableClasses() {
return new Class[]{SimpleProperty.class};
}
}
Define a Structure View Element
The SimpleStructureViewElement implements StructureViewTreeElement and SortableTreeElement. The StructureViewTreeElement represents an element in the Structure View tree model. The SortableTreeElement represents an item in a smart tree that allows using text other than the presentable text as a key for alphabetic sorting.
public class SimpleStructureViewElement implements StructureViewTreeElement, SortableTreeElement {
private final NavigatablePsiElement myElement;
public SimpleStructureViewElement(NavigatablePsiElement element) {
this.myElement = element;
}
@Override
public Object getValue() {
return myElement;
}
@Override
public void navigate(boolean requestFocus) {
myElement.navigate(requestFocus);
}
@Override
public boolean canNavigate() {
return myElement.canNavigate();
}
@Override
public boolean canNavigateToSource() {
return myElement.canNavigateToSource();
}
@NotNull
@Override
public String getAlphaSortKey() {
String name = myElement.getName();
return name != null ? name : "";
}
@NotNull
@Override
public ItemPresentation getPresentation() {
ItemPresentation presentation = myElement.getPresentation();
return presentation != null ? presentation : new PresentationData();
}
@Override
public TreeElement @NotNull [] getChildren() {
if (myElement instanceof SimpleFile) {
List<SimpleProperty> properties = PsiTreeUtil.getChildrenOfTypeAsList(myElement, SimpleProperty.class);
List<TreeElement> treeElements = new ArrayList<>(properties.size());
for (SimpleProperty property : properties) {
treeElements.add(new SimpleStructureViewElement((SimplePropertyImpl) property));
}
return treeElements.toArray(new TreeElement[0]);
}
return EMPTY_ARRAY;
}
}
Register the Structure View Factory
The SimpleStructureViewFactory implementation is registered with the IntelliJ Platform in the plugin configuration file using the com.intellij.lang.psiStructureViewFactory extension point.