How to obtain the embedded editor that shows when using VcsDiffUtil.showDiffFor()
I am trying to get the editor in two-sided diff window tool that pops-up when using VcsDiffUtil.showDiffFor(). I would like to add in a mouse listener to that specific, and also add in a JPopup at various line-numbers of that editor.
If that is not possible, what is the best way to create a custom diffPanel that compares two branches for which I can get a hook into the editor and add custom components?
Thanks
请先登录再写评论。
You can register `com.intellij.diff.DiffExtension`, cast passed `DiffViewer` to `TwosideTextDiffViewer` and use `getEditor1()/getEditor2()` methods to get these editors.
If you need to differentiate this particular diff window from others, you'll have to create it yourself.
For example, using `com.intellij.openapi.vcs.changes.actions.diff.ShowDiffAction#showDiffForChange` and passing extra data via `ShowDiffContext.putChainContext` (that can be later retrieved in `DiffExtension` via `DiffContext.getUserData`).
That was very helpful! I will look to incorporate your suggestions. Thanks!
So after looking into registering DiffExtensions how do I actually do it? I have seen other projects using diffManager.registerDiffTool() but that class seem to have been deprecated.
>how do I actually do it
How do you register it? Same way as with other extensions.
https://www.jetbrains.org/intellij/sdk/docs/basics/plugin_structure/plugin_extensions_and_extension_points.html
Thank you Aleksey! For anyone else searching the forum and trying to figure out how to get a hook into the editors in the diff window this is how you do it (implementation in Scala)
object MyDiffExtension {
val MY_CONTEXT_KEY: Key[Boolean] = KeyWithDefaultValue.create("MY_CONTEXT_KEY", false)
}
class MYDiffExtension extends DiffExtension {
override def onViewerCreated(frameDiff: FrameDiffTool.DiffViewer,
diffContext: DiffContext,
diffRequest: DiffRequest): Unit = {
val isMyDiffTool: Boolean =
diffContext.getUserData(CruxDiffExtension.MY_CONTEXT_KEY)
// Return if the context in which the diffExtension was
// called with my custom context
if(!isMyDiffTool)
return
println("MYExtensionInitiated")
val editor: Editor = frameDiff.asInstanceOf[TwosideTextDiffViewer]
.getEditor2
}}}
Elsewhere in your plugin.xml file you merely register this extension as follows:
Now you can do
This will trigger the Diff View with your appropriate extension hooked in when it matches the context.
Hope this helps others!