IntelliJ Platform SDK DevGuide

Edit page

6. PSI Helpers and Utilities

6.1. Define helper methods for generated PSI elements

If we want to have custom methods in PSI classes we need to define them separately and ask Grammar-Kit to embed them into generated code.

Let’s define an utility class with these helper methods.

package com.simpleplugin.psi.impl; import com.intellij.lang.ASTNode; import com.intellij.navigation.ItemPresentation; import com.intellij.psi.*; import com.simpleplugin.SimpleIcons; import com.simpleplugin.psi.*; import org.jetbrains.annotations.Nullable; import javax.swing.*; public class SimplePsiImplUtil { public static String getKey(SimpleProperty element) { ASTNode keyNode = element.getNode().findChildByType(SimpleTypes.KEY); if (keyNode != null) { // IMPORTANT: Convert embedded escaped spaces to simple spaces return keyNode.getText().replaceAll("\\\\ ", " "); } else { return null; } } public static String getValue(SimpleProperty element) { ASTNode valueNode = element.getNode().findChildByType(SimpleTypes.VALUE); if (valueNode != null) { return valueNode.getText(); } else { return null; } } }

Note that the SimpleProperty interface referenced in the code above is generated by the parser. Also note that the element.getKey method used in getPresentableText will not be defined - it’s a method generated by the parser, which we’ll fix below.

6.2. Update grammar and regenerate the parser

Now we tell to use this utility class in the grammar file via psiImplUtilClass attribute.

To tell which methods for which PSI classes must be used we specify methods for particular rule.

{ parserClass="com.simpleplugin.parser.SimpleParser" extends="com.intellij.extapi.psi.ASTWrapperPsiElement" psiClassPrefix="Simple" psiImplClassSuffix="Impl" psiPackage="com.simpleplugin.psi" psiImplPackage="com.simpleplugin.psi.impl" elementTypeHolderClass="com.simpleplugin.psi.SimpleTypes" elementTypeClass="com.simpleplugin.psi.SimpleElementType" tokenTypeClass="com.simpleplugin.psi.SimpleTokenType" psiImplUtilClass="com.simpleplugin.psi.impl.SimplePsiImplUtil" } simpleFile ::= item_* private item_ ::= (property|COMMENT|CRLF) property ::= (KEY? SEPARATOR VALUE?) | KEY {methods=[getKey getValue]}

After we made our changes to the grammar we can regenerate the parser and PSI classes.

6.3. Define an utility to search properties

Now we need an utility class to search PSI elements for defined properties over the project. We will use this utility later when implementing code assistance.

package com.simpleplugin; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiManager; import com.intellij.psi.search.*; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.indexing.FileBasedIndex; import com.simpleplugin.psi.*; import java.util.*; public class SimpleUtil { public static List<SimpleProperty> findProperties(Project project, String key) { List<SimpleProperty> result = null; Collection<VirtualFile> virtualFiles = FileBasedIndex.getInstance().getContainingFiles(FileTypeIndex.NAME, SimpleFileType.INSTANCE, GlobalSearchScope.allScope(project)); for (VirtualFile virtualFile : virtualFiles) { SimpleFile simpleFile = (SimpleFile) PsiManager.getInstance(project).findFile(virtualFile); if (simpleFile != null) { SimpleProperty[] properties = PsiTreeUtil.getChildrenOfType(simpleFile, SimpleProperty.class); if (properties != null) { for (SimpleProperty property : properties) { if (key.equals(property.getKey())) { if (result == null) { result = new ArrayList<SimpleProperty>(); } result.add(property); } } } } } return result != null ? result : Collections.<SimpleProperty>emptyList(); } public static List<SimpleProperty> findProperties(Project project) { List<SimpleProperty> result = new ArrayList<SimpleProperty>(); Collection<VirtualFile> virtualFiles = FileBasedIndex.getInstance().getContainingFiles(FileTypeIndex.NAME, SimpleFileType.INSTANCE, GlobalSearchScope.allScope(project)); for (VirtualFile virtualFile : virtualFiles) { SimpleFile simpleFile = (SimpleFile) PsiManager.getInstance(project).findFile(virtualFile); if (simpleFile != null) { SimpleProperty[] properties = PsiTreeUtil.getChildrenOfType(simpleFile, SimpleProperty.class); if (properties != null) { Collections.addAll(result, properties); } } } return result; } }
Last modified: 12 October 2017