gpt4 book ai didi

c# - AvalonEdit 中的整行高亮显示

转载 作者:行者123 更新时间:2023-11-30 20:00:02 25 4
gpt4 key购买 nike

我正在使用 AvalonEdit 为补丁文件创建一个 View ,我想让它在整行中突出显示差异,而不仅仅是文本背景 - 类似于 GitHub for Windows 今天所做的:

我是 AvalonEdit 的新手,所以我不确定执行此操作的最佳方法。到目前为止,这是我发现的内容:

  • 覆盖 VisualLineElementGenerator 以创建一个额外的 TextSpan,它是控件的长度。这看起来很棘手。

  • 手动创建一个新控件以添加到后台的 TextView.Layers 和绿色/红色的 OnRender - 这似乎更有希望,但我需要什么事件并不是很清楚 Hook 以检测何时重新渲染。

  • 重写 TextView - 这似乎有点矫枉过正。

编辑:这是一个简单的语法荧光笔会发生什么,这是我想要的:

最佳答案

正如 Daniel 提到的,我通过 Wiki 页面发现了 Background Renderers,它运行良好。这是我最终所做的 - 它可能会变得更有效率,但现在已经足够好了:

public class DiffLineBackgroundRenderer : IBackgroundRenderer
{
static Pen pen;

static SolidColorBrush removedBackground;
static SolidColorBrush addedBackground;
static SolidColorBrush headerBackground;

FileDiffView host;

static DiffLineBackgroundRenderer()
{
removedBackground = new SolidColorBrush(Color.FromRgb(0xff, 0xdd, 0xdd)); removedBackground.Freeze();
addedBackground = new SolidColorBrush(Color.FromRgb(0xdd, 0xff, 0xdd)); addedBackground.Freeze();
headerBackground = new SolidColorBrush(Color.FromRgb(0xf8, 0xf8, 0xff)); headerBackground.Freeze();

var blackBrush = new SolidColorBrush(Color.FromRgb(0, 0, 0)); blackBrush.Freeze();
pen = new Pen(blackBrush, 0.0);
}

public DiffLineBackgroundRenderer(FileDiffView host)
{
this.host = host;
}

public KnownLayer Layer
{
get { return KnownLayer.Background; }
}

public void Draw(TextView textView, DrawingContext drawingContext)
{
foreach (var v in textView.VisualLines)
{
var rc = BackgroundGeometryBuilder.GetRectsFromVisualSegment(textView, v, 0, 1000).First();
// NB: This lookup to fetch the doc line number isn't great, we could
// probably do it once then just increment.
var linenum = v.FirstDocumentLine.LineNumber - 1;
if (linenum >= host.ViewModel.Lines.Count) continue;

var diffLine = host.ViewModel.Lines[linenum];

if (diffLine.Style == DiffLineStyle.Context) continue;

var brush = default(Brush);
switch (diffLine.Style)
{
case DiffLineStyle.Header:
brush = headerBackground;
break;
case DiffLineStyle.Added:
brush = addedBackground;
break;
case DiffLineStyle.Deleted:
brush = removedBackground;
break;
}

drawingContext.DrawRectangle(brush, pen,
new Rect(0, rc.Top, textView.ActualWidth, rc.Height));
}
}
}

关于c# - AvalonEdit 中的整行高亮显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22415248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com