themishkun
05/25/2020, 3:31 PM// foo.kt
package foo
data class Bar(val x: Int)
// foox.kt
package foox
import foo.Bar
data class Buzz(val bar: Bar)
I want to jump from KtClass
of Buzz
to the KtClass
of Bar
, emulating jump to declaration
functionality of the IDE.
What is a right way to approach this?Imran/Malic
05/26/2020, 12:29 PMImran/Malic
05/26/2020, 12:32 PMImran/Malic
05/26/2020, 12:34 PMImran/Malic
05/26/2020, 12:46 PMBar
the cellrenderer and popUptitle are optional. This can be pretty minimalistic if that is desired.themishkun
05/27/2020, 8:45 AMthemishkun
05/27/2020, 10:47 AMdata class
use either a primitive type or another data class. This is a part of my project to bring transitive immutablility checker.
So my question is really: “how can I go from a one data class constructor to declaration of the type of one of its parameters to check if all of them are data classes”themishkun
05/27/2020, 1:02 PMval context = constructor.analyze()
for (parameter in constructor.parameters) {
val typeDescriptor = context.get(BindingContext.TYPE, parameter.typeReference)
val classDescriptor = typeDescriptor?.let { DescriptorUtils.getClassDescriptorForType(it) }
val psi = classDescriptor?.source?.getPsi()
}
And that’s how we can visit psi of each parameter’s type declarationImran/Malic
05/27/2020, 1:04 PM{element: PsiElement -> PsiNavigateUtil.navigate(element)
}
Or some other variant. The only thing it does it checks the PsiReference and moves the editor and your caret to that place.Imran/Malic
05/27/2020, 1:05 PMImran/Malic
05/27/2020, 1:06 PMImran/Malic
05/27/2020, 2:54 PMval IdeMetaPlugin.fromBuzzToBar: ExtensionPhase
get() = addApplicableInspection(
defaultFixText= "Jump to Bar",
kClass = KtClass::class.java,
inspectionText= { "Some meaningful text for your users" },
isApplicable= { it.name == "Buzz" },
inspectionHighlightType = { ProblemHighlightType.INFORMATION },
level = HighlightDisplayLevel.WEAK_WARNING,
applyTo = {
val bar: KtClass = TODO("Find Bar depending on your use-case")
PsiNavigateUtil.navigate(bar)
},
groupPath = arrayOf("Your Path")
)
to illustrate what I am saying.
The same thing happens when user’s click on GotoRelatedItems or LineMarkers, only that those methods are not called by plugin devs explicitly. Rather it solely asks for the PsiElement and then it calls that internally.