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

0
5 comments

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`).

0
Avatar
Permanently deleted user

That was very helpful! I will look to incorporate your suggestions. Thanks! 

0
Avatar
Permanently deleted user

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. 

0
Avatar
Permanently deleted user

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:

<extensions defaultExtensionNs="com.intellij">
<diff.DiffExtension implementation="MyDiffExtension"/>
</extensions>

Now you can do 

diffContext.putChainContext(MyDiffExtension.MY_CONTEXT_KEY, true)
ShowDiffAction.showDiffForChange(project, changes, 0, diffContext)

This will trigger the Diff View with your appropriate extension hooked in when it matches the context.

Hope this helps others!

0

Please sign in to leave a comment.