IntelliJ Platform SDK DevGuide

11. Find Usages Provider

查找使用提供程序使用单词扫描程序来构建每个文件中存在的单词索引.

扫描程序将文本分解为单词,定义每个单词的上下文并将其传递给查找使用提供程序.

11.1.

定义查找用法提供程序

package com.simpleplugin; import com.intellij.lang.cacheBuilder.*; import com.intellij.lang.findUsages.FindUsagesProvider; import com.intellij.psi.*; import com.intellij.psi.tree.TokenSet; import com.simpleplugin.psi.*; import org.jetbrains.annotations.*; public class SimpleFindUsagesProvider implements FindUsagesProvider { @Nullable @Override public WordsScanner getWordsScanner() { return new DefaultWordsScanner(new SimpleLexerAdapter(), TokenSet.create(SimpleTypes.KEY), TokenSet.create(SimpleTypes.COMMENT), TokenSet.EMPTY); } @Override public boolean canFindUsagesFor(@NotNull PsiElement psiElement) { return psiElement instanceof PsiNamedElement; } @Nullable @Override public String getHelpId(@NotNull PsiElement psiElement) { return null; } @NotNull @Override public String getType(@NotNull PsiElement element) { if (element instanceof SimpleProperty) { return "simple property"; } else { return ""; } } @NotNull @Override public String getDescriptiveName(@NotNull PsiElement element) { if (element instanceof SimpleProperty) { return ((SimpleProperty) element).getKey(); } else { return ""; } } @NotNull @Override public String getNodeText(@NotNull PsiElement element, boolean useFullName) { if (element instanceof SimpleProperty) { return ((SimpleProperty) element).getKey() + ":" + ((SimpleProperty) element).getValue(); } else { return ""; } } }

11.2.

注册查找用法提供程序

<lang.findUsagesProvider language="Simple" implementationClass="com.simpleplugin.SimpleFindUsagesProvider"/>

11.3.

运行该项目

现在我们可以通过参考调用任何属性的* Find Usages *.

查找用法

Last modified: 11 May 2019