Search code examples
kotlinintellij-plugin

Intellij Idea Plugin, Create Kotlin PSI Visitor


I'm creating a visitor for parsing all methods/properties inside of a KtFile. The problem is that, I'm hitting in debugger just one @override method which is visitKtFile, although I have put some other brake-points in another methods as well, please find attached the code for visitor.

public class TestVisitor extends KtVisitorVoid {

@Override
public void visitProperty(@NotNull KtProperty property) {
    super.visitProperty(property);
}

@Override
public void visitClass(@NotNull KtClass klass) {
    super.visitClass(klass);
}

@Override
public void visitNamedFunction(@NotNull KtNamedFunction function) {
    super.visitNamedFunction(function);
}

@Override
public void visitKtElement(@NotNull KtElement element) {
    super.visitKtElement(element);
}

@Override
public void visitKtFile(@NotNull KtFile file) {
    super.visitKtFile(file);
}

@Override
public void visitDeclaration(@NotNull KtDeclaration dcl) {
    super.visitDeclaration(dcl);
}
}

Here is the code for Action,

public class HelloAction extends AnAction {
public HelloAction() {
    super("Hello");
}

public void actionPerformed(AnActionEvent event) {
    PsiFile psiFile = event.getData(PlatformDataKeys.PSI_FILE);
    TestVisitor visitor = new TestVisitor();
    psiFile.accept(visitor);
}
}

And here is the code of test class I'm trying to parse. By the structure I have I was expecting to hit the method visitProperty 2 times. Which is not happening at all now.

class TestC {
var s = true
val t = 1

fun testLog(): String {
    var test = ""
    test = "Alex"
    return test
}
}

Appreciate any help in creating a plugin for this amazing platform Intellij Idea. Thank you.


Solution

  • KtVisitorVoid is a non-recursive visitor class. If you want to visit the entire tree, you need to use KtTreeVisitorVoid as the base class for your visitor.