Custom annotations for diff view

已回答

I am trying to open a diff window, but wish to highlight differences differently (different colors for specific parts of the code). I would like to be able to select certain ranges in specific files to be highlighted.

Is that possible? If yes, how?

0

>I am trying to open a diff window, but wish to highlight differences differently (different colors for specific parts of the code).
You can register your own com.intellij.diff.DiffTool and have full control over displayed content.
See TwosideTextDiffViewer as potential base class and SimpleDiffViewer as a sample (but somewhat complex) implementation (please, do not extend the latter explicitly - its protected methods are likely to be changed).
This approach means, that you will need to implement most of the logic/actions yourself (or copy it from platform).

There's also com.intellij.diff.util.DiffUserDataKeysEx#CUSTOM_DIFF_COMPUTER EP, but it does not allow to change highlighting dramatically - only to specify which lines are considered to be 'changed'.
You can comine it with ComparisonManagerImpl#compareLinesInner(Range, ...) to specify that "lines 12..14 from document1 and lines 16..23 from document2 should be compared with each other, any other differences must be ignored".
See com.intellij.refactoring.extractMethod.preview.PreviewDiffPanel#getDiffComputer as a sample usage.

You can also add more highlighting on top of existing using com.intellij.diff.DiffExtension EP.

0
Avatar
Permanently deleted user

Thank you for the quick response!

 

I am using an external tool to tell me at which lines there are differences so I believe just selecting the lines would suit me. Unfortunately, I can't seem to find com.intellij.diff.util.DiffUserDataKeysEx#CUSTOM_DIFF_COMPUTER and com.intellij.refactoring.extractMethod.preview.PreviewDiffPanel#getDiffComputer. Can you please provide me with links to those?

 

Edit: I did find DiffUserDataKeysEx here: https://github.com/aefimov/rustbelt/blob/master/platform/diff-impl/src/com/intellij/diff/util/DiffUserDataKeysEx.java , but can't find CUSTOM_DIFF_COMPUTER

0

https://github.com/JetBrains/intellij-community/blob/master/platform/diff-impl/src/com/intellij/diff/util/DiffUserDataKeysEx.java#L68
https://github.com/JetBrains/intellij-community/blob/master/java/java-impl/src/com/intellij/refactoring/extractMethod/preview/PreviewDiffPanel.java#L385

CUSTOM_DIFF_COMPUTER should be passed into DiffRequest.putUserData on creation.
So you need to be the one who shows diff window (and can't, for example, modify presentation for Local Changes diff without introducing a new AnAction "Show Diff using My Tool").

0
Avatar
Permanently deleted user

Yes, I managed to get this working!

Is it possible to color code different ranges in a file using this approach?

0

No, there's no API to customize highlighting style/colors.

You might try to add some customizations on top using 'DiffExtension''.
You'll need to cast DiffViewer to TwosideTextDiffViewer to get to the Editors.
Unfortunately, SimpleDiffViewer.getDiffChanges is not public, so it might be hard to get changed lines.

For example, you can add RangeHighlighter with a higher Layer for the same areas to override background color. But this approach is somewhat fragile.
Another option would be to add additional information into the gutter (area of Editor with line numbers) by adding Icon or LineMarkerRenderer for some areas.

0
Avatar
Permanently deleted user

Hi, is it possible to use a custom diff computer with the three side diff viewer? If not are there other ways to manipulate highlighting in the three side diff viewer?

0

No. Only re-implementing this DiffTool from scratch or adding highlighting in addition to existing.

0

请先登录再写评论。