IntelliJ Platform SDK DevGuide

Edit page

14. Structure View Factory

结构视图工厂允许在* Structure *工具窗口中显示任何文件的结构,以便在项目之间轻松导航。

14.1。

定义结构视图工厂

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。

定义结构视图模型

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。

定义结构视图元素

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。

注册结构视图工厂

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

14.5。

运行该项目

结构视图

Last modified: 8 May 2019