This page is part of multi-step Custom Language Support Tutorial. All previous steps must be executed in sequence for the code to work.
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 for easy navigation between items in the current editor.
Define a Structure View FactoryThe SimpleStructureViewFactory
implements PsiStructureViewFactory
. The getStructureViewBuilder()
implementation reuses the IntelliJ Platform class TreeBasedStructureViewBuilder
. At this point the project will not compile until SimpleStructureViewModel
is implemented below.
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); } }; } }
Define a Structure View ModelThe SimpleStructureViewModel
is created by implementing StructureViewModel
, which defines the model for data displayed in the standard structure view. It also extends StructureViewModelBase
, an implementation that links the model to a text editor.
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 ElementThe 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 FactoryThe SimpleStructureViewFactory
implementation is registered with the IntelliJ Platform in the plugin configuration file using the com.intellij.lang.structureViewExtension
extension point.
<extensions defaultExtensionNs="com.intellij"> <lang.psiStructureViewFactory language="Simple" implementationClass="org.intellij.sdk.language.SimpleStructureViewFactory"/> </extensions>
Run the ProjectRun the project by using the Gradle runIde
task.
Open the test.simple file and open . The IDE now supports a structure view of the Simple Language:
19 March 2025
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4