Is there a PsiElementFinder for kotlin?


In java, we can provide additional parsing for the IDE via ShortNameCache and PsiElementFinder, and I noticed that kotlin idea plugin can be called by java in this way through kotlin IDEA plugin.

Now I want to implement additional parsing in Kotlin, for example to let the IDE call a kotlin class that doesn't actually exist (just in memory):

  1. In lombok, it is possible to trick the IDE into letting a class have additional methods, such as setXXX/getXXX.
  2. In Android ViewBinding, the LayoutBinding class is also generated by the layout xml file

But they are both generating java classes, and I want to generate a kotlin class that will be used in kotlin analyzing, but when I try to insert my in-memory generated class through the IDE plugin by PsiElementFinder, the IDE plugin show me the error:

Kotlin light classes should not be found by JavaPsiFacade

I would like to know what is the replacement for the PsiElementFinder extension point in kotlin?kotlin SyntheticResolveExtension might be an option, but this extension point is difficult to use and unstable. And after taking a general look at the api for SyntheticResolveExtension, I noticed that it can't seem to generate new packages, but Java's PsiElementFinder can. Java plugin's PsiElementFinder is very easy to use. Is there an extension point like java?


Comment actions Permalink

No, there is no specific PsiElementFinder for Kotlin - no reason for that as Kotlin is compiled into java bytecode. 

To maintain java-kotlin interop plugin uses so called LightClasses to mimic Java PsiClass - under the hood it delegates calls to Kotlin functions, properties and other constructions.

Could you please explain in details what do you want to achieve ?

Comment actions Permalink

When using annotation processors, we tend to generate some classes, but this relies on the gradle build task (ksp/kapt), which tends to be very slow

If we could use the IDE plug-in to read the annotated element (by KotlinAnnotatedElementsSearcher) and generate the corresponding placeholder classes (without having to have a real implementation, just so that the IDE parses them without errors), we could achieve a live preview without waiting for the annotation processor to run out of time

I even wrote a framework to do this, but unfortunately, I could only generate 'fake' java psi: 

Comment actions Permalink

Kotlin looks up classes from `KotlinJavaPsiFacade` (it is visible on your screenshot) that effectively delegates requests to PsiElementFinder extension points (e.g. one special hack is `KotlinScriptDependenciesClassFinder` to support script files). 

So, it could help if you add your own PsiElementFinder impl


Please sign in to leave a comment.