Search code examples
c#wpfavalonedit

Whole line highlighting in AvalonEdit


I'm creating a view for patch files using AvalonEdit, and I want to make it so that diffs are highlighted across the entire line, not just the text background - similar to what GitHub for Windows does today:

I'm new to AvalonEdit, so I'm not sure the best way to do this. Here's what I've found so far:

  • Override VisualLineElementGenerator in order to create an additional TextSpan that is the length of the control. This seems Tricky.

  • Create a new control to add to TextView.Layers in the background and OnRender in the green/red by hand - this seems more promising, but it's not super clear what event I need to hook in order to detect when to re-render.

  • Override TextView - this seems like overkill.

Edit: Here's what happens with a simple syntax highlighter, which is what I don't want:


Solution

  • Create a new control to add to TextView.Layers in the background and OnRender in the green/red by hand

    You don't have to create a new layer to render in something in the background: you can add your implementation of IBackgroundRenderer to textView.BackgroundRenderers to render something in the background of an existing layer.

    it's not super clear what event I need to hook in order to detect when to re-render

    That would be: textView.VisualLinesChanged. But you don't need that if you use IBackgroundRenderer as the existing layers are already re-rendered by AvalonEdit.