Implementing `Navigate -> Declaration` action going to declaration for runtime type instead of the compile-time type while debugging

Answered

I'd like to investigate implementing https://youtrack.jetbrains.com/issue/IDEA-86569, i.e., while debugging, the `Navigate -> Declaration` (⌘B) action should go to the declaration for the actual runtime type instead of the compile-time static type.

Could this feature be implemented as a plugin, or must I modify the IntelliJ codebase?

I'd prefer to make this work for all supported languages (e.g., Java, Kotlin, JavaScript, etc.), but, if support is different for different languages, I'll start with Java, unless another one is much easier.

It seems that `Navigate -> Declaration` is implemented by `com.intellij.codeInsight.navigation.actions.GotoDeclarationAction$invoke(…)`, but, when I debugged IntelliJ to track things down, it used a cache of usage-to-declaration mappings.

What IntelliJ IDEA code from the GitHub repo (https://github.com/JetBrains/intellij-community) initially determines the declaration for a usage?

I'll need to implement a `GotoDeclarationHandler` to avoid the existing cached static usage-to-declaration mappings, right?

It seems like I'll need to investigate `com.intellij.debugger.codeinsight.RuntimeTypeEvaluator` to find out the runtime type of an object, correct?

FYI, I haven't previously written any IntelliJ IDEA plugins, or worked with the IntelliJ code base.

Thanks for any help.

7 comments
Comment actions Permalink

Yes, implementing GotoDeclarationHandler should do the trick.

0
Comment actions Permalink

RuntimeTypeEvaluator should help determing the real expression type, but you'll have to extract the parent expression

0
Comment actions Permalink

I'm getting back to looking into this.

Can you point out an exact line of code, where, for normal circumstances in the java debugger:

1) the declaration for a usage is determined

2) the runtime type of an object is known

Once I know these 2 lines of code, I can backtrack to figure out how to implement the functionality described in the initial post of this thread.

Please let me know if any of this doesn't make sense, and then I'll attempt to clarify.

Thanks.

0
Comment actions Permalink

I'm getting back to looking into this.

Can someone point out an exact line of code, where, for normal circumstances in the java debugger:

1) the declaration for a reference usage is determined

2) the runtime type of an object is known

Once I know these 2 lines of code, I can backtrack to figure out how to implement the functionality described in the initial post of this thread.

Please let me know if any of this doesn't make sense, and then I'll attempt to clarify.

Thanks.

0
Comment actions Permalink

1) the declaration for a reference usage is determined here:
com.intellij.codeInsight.navigation.actions.GotoDeclarationAction#findTargetElementsNoVS
which calls com.intellij.codeInsight.navigation.actions.GotoDeclarationAction#findTargetElementsFromProviders
which goes over implementations of com.intellij.codeInsight.navigation.actions.GotoDeclarationHandler
which you want to implement.

0
Comment actions Permalink

2) RuntimeTypeEvaluator is the closest to what you need right now

0
Comment actions Permalink

Thanks for the info.  Will investigate in a bit.

0

Please sign in to leave a comment.