Unable to get PsiElementVisitor to work inside of overridden buildVisitor

Answered

I am trying to implement a custom inspection that will allow me to scan the contents of a composer.json for certain values; for example, I'd like to parse the require/require-dev and prompt users based on which packages are needed for a project. From what I've read through these forums, and what I've seen in existing inspections, I have defined the localInspection in my plugin.xml:

<localInspection language="PHP" groupPath="PHP" shortName="ComposerPackageToModuleInspection"
displayName="Suggest local packages that can be attached as modules"
groupName="Composer"
enabledByDefault="true" level="WARNING"
implementationClass="dev.dohpaz.phpExtras.php.codeInspection.ComposerPackageToModuleInspection" />

Then, I've added the implementation class:

package dev.dohpaz.phpExtras.php.codeInspection;

import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.json.psi.JsonElementVisitor;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.jetbrains.php.composer.ComposerDataService;
import com.jetbrains.php.lang.inspections.PhpInspection;
import org.jetbrains.annotations.NotNull;

public class ComposerPackageToModuleInspection extends PhpInspection {
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
ComposerDataService dataService = ComposerDataService.getInstance(holder.getProject());

if (dataService != null && dataService.isConfigWellConfigured()) {
VirtualFile config = LocalFileSystem.getInstance().refreshAndFindFileByPath(dataService.getConfigPath());

if (config == null) {
return PsiElementVisitor.EMPTY_VISITOR;
}

return new PhpElementVisitor() {
@Override
public void visitElement(PsiElement element) {
System.out.println("Foo"); // never called, no matter which method I override.
}
};
}

return PsiElementVisitor.EMPTY_VISITOR;
}
}

 

I have tried extending LocalInspectionTool directly with no change, and I've tried returning various implementations of PhpElementVisitor, PsiElementVisitor, and JsonElementVisitor with no luck. No matter how I code it, no method inside of the visitor ever gets called. I've verified through debugging that the buildVisitor gets called, but once it returns it breaks down.

I've based my solution off of existing code, and I do not see what I am missing from my implementation that they haven't implemented. I really could use some help here.

Thank you!

2 comments
Comment actions Permalink

After some more debugging, it seems my issue was that I was setting the language to PHP, when it should have been set to JSON. The "existing code" I was looking at seems to be wrong in that respect. Regardless, when I changed the language from PHP to JSON in plugin.xml, the JsonElementVisitor methods now are triggered as I hoped they would be.

There wouldn't happen to be more in-depth documentation/tutorials on this that anyone could point me toward? 

1
Comment actions Permalink

Hello!

The general rule is that if you're going to extend anything related to the JSON file, the "base" language is JSON. Many language support plugins use JSON files for defining dependencies/configuration, but they don't acquire JSONs, extend their behaviors.

Regarding the supporting or extending custom languages, you can read more in our docs:

https://www.jetbrains.org/intellij/sdk/docs/reference_guide/custom_language_support/code_inspections_and_intentions.html

 

0

Please sign in to leave a comment.