IntelliJ Platform SDK DevGuide

Edit page

14. Structure View Factory

A structure view factory allows to show the structure of any file in a Structure tool window for easy navigation between items.

14.1. Define a structure view factory

package com.simpleplugin; import com.intellij.ide.structureView.*; import com.intellij.lang.PsiStructureViewFactory; import com.intellij.openapi.editor.Editor; import com.intellij.psi.PsiFile; import org.jetbrains.annotations.*; public class SimpleStructureViewFactory implements PsiStructureViewFactory { @Nullable @Override public StructureViewBuilder getStructureViewBuilder(final PsiFile psiFile) { return new TreeBasedStructureViewBuilder() { @NotNull @Override public StructureViewModel createStructureViewModel(@Nullable Editor editor) { return new SimpleStructureViewModel(psiFile); } }; } }

14.2. Define a structure view model

package com.simpleplugin; import com.intellij.ide.structureView.*; import com.intellij.ide.util.treeView.smartTree.Sorter; import com.intellij.psi.PsiFile; import com.simpleplugin.psi.SimpleFile; import org.jetbrains.annotations.NotNull; public class SimpleStructureViewModel extends StructureViewModelBase implements StructureViewModel.ElementInfoProvider { public SimpleStructureViewModel(PsiFile psiFile) { super(psiFile, new SimpleStructureViewElement(psiFile)); } @NotNull public Sorter[] getSorters() { return new Sorter[]{Sorter.ALPHA_SORTER}; } @Override public boolean isAlwaysShowsPlus(StructureViewTreeElement element) { return false; } @Override public boolean isAlwaysLeaf(StructureViewTreeElement element) { return element instanceof SimpleFile; } }

14.3. Define a structure view element

package com.simpleplugin; import com.intellij.ide.projectView.PresentationData; import com.intellij.ide.structureView.StructureViewTreeElement; import com.intellij.ide.util.treeView.smartTree.SortableTreeElement; import com.intellij.ide.util.treeView.smartTree.TreeElement; import com.intellij.navigation.ItemPresentation; import com.intellij.psi.NavigatablePsiElement; import com.intellij.psi.util.PsiTreeUtil; import com.simpleplugin.psi.SimpleFile; import com.simpleplugin.psi.SimpleProperty; import com.simpleplugin.psi.impl.SimplePropertyImpl; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; public class SimpleStructureViewElement implements StructureViewTreeElement, SortableTreeElement { private NavigatablePsiElement element; public SimpleStructureViewElement(NavigatablePsiElement element) { this.element = element; } @Override public Object getValue() { return element; } @Override public void navigate(boolean requestFocus) { element.navigate(requestFocus); } @Override public boolean canNavigate() { return element.canNavigate(); } @Override public boolean canNavigateToSource() { return element.canNavigateToSource(); } @NotNull @Override public String getAlphaSortKey() { String name = element.getName(); return name != null ? name : ""; } @NotNull @Override public ItemPresentation getPresentation() { ItemPresentation presentation = element.getPresentation(); return presentation != null ? presentation : new PresentationData(); } @Override public TreeElement[] getChildren() { if (element instanceof SimpleFile) { SimpleProperty[] properties = PsiTreeUtil.getChildrenOfType(element, SimpleProperty.class); List<TreeElement> treeElements = new ArrayList<TreeElement>(properties.length); for (SimpleProperty property : properties) { treeElements.add(new SimpleStructureViewElement((SimplePropertyImpl) property)); } return treeElements.toArray(new TreeElement[treeElements.size()]); } else { return EMPTY_ARRAY; } } }

14.4. Register the structure view factory

<lang.psiStructureViewFactory language="Simple" implementationClass="com.simpleplugin.SimpleStructureViewFactory"/>

14.5. Run the project

Structure View

Last modified: 6 October 2017