2024-03-17 18:37:06 -07:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
using Avalonia;
|
|
|
|
using Avalonia.Controls;
|
2024-02-19 23:44:26 -08:00
|
|
|
using Avalonia.Controls.Primitives;
|
2024-06-12 06:12:45 -07:00
|
|
|
using Avalonia.Data;
|
2024-04-28 01:46:39 -07:00
|
|
|
using Avalonia.Input;
|
2024-02-05 23:08:37 -08:00
|
|
|
using Avalonia.Interactivity;
|
|
|
|
using Avalonia.Media;
|
2024-07-17 01:56:16 -07:00
|
|
|
using Avalonia.Threading;
|
2024-02-27 05:13:52 -08:00
|
|
|
using Avalonia.VisualTree;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
using AvaloniaEdit;
|
|
|
|
using AvaloniaEdit.Document;
|
|
|
|
using AvaloniaEdit.Editing;
|
|
|
|
using AvaloniaEdit.Rendering;
|
|
|
|
using AvaloniaEdit.TextMate;
|
|
|
|
using AvaloniaEdit.Utils;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
|
|
|
namespace SourceGit.Views
|
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
public class TextDiffViewChunk
|
2024-07-17 01:56:16 -07:00
|
|
|
{
|
|
|
|
public double Y { get; set; } = 0.0;
|
|
|
|
public double Height { get; set; } = 0.0;
|
|
|
|
public int StartIdx { get; set; } = 0;
|
|
|
|
public int EndIdx { get; set; } = 0;
|
2024-07-18 01:16:58 -07:00
|
|
|
public bool Combined { get; set; } = true;
|
|
|
|
public bool IsOldSide { get; set; } = false;
|
2024-07-17 19:17:40 -07:00
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
public bool ShouldReplace(TextDiffViewChunk old)
|
2024-07-17 19:17:40 -07:00
|
|
|
{
|
|
|
|
if (old == null)
|
|
|
|
return true;
|
|
|
|
|
2024-07-22 22:58:57 -07:00
|
|
|
return Math.Abs(Y - old.Y) > 0.001 ||
|
|
|
|
Math.Abs(Height - old.Height) > 0.001 ||
|
|
|
|
StartIdx != old.StartIdx ||
|
2024-07-18 01:16:58 -07:00
|
|
|
EndIdx != old.EndIdx ||
|
|
|
|
Combined != Combined ||
|
|
|
|
IsOldSide != IsOldSide;
|
2024-07-17 19:17:40 -07:00
|
|
|
}
|
2024-07-17 01:56:16 -07:00
|
|
|
}
|
|
|
|
|
2024-07-14 00:55:15 -07:00
|
|
|
public class ThemedTextDiffPresenter : TextEditor
|
2024-06-12 06:12:45 -07:00
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
public class VerticalSeperatorMargin : AbstractMargin
|
|
|
|
{
|
|
|
|
public override void Render(DrawingContext context)
|
|
|
|
{
|
|
|
|
var presenter = this.FindAncestorOfType<ThemedTextDiffPresenter>();
|
|
|
|
if (presenter != null)
|
|
|
|
{
|
|
|
|
var pen = new Pen(presenter.LineBrush);
|
|
|
|
context.DrawLine(pen, new Point(0, 0), new Point(0, Bounds.Height));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override Size MeasureOverride(Size availableSize)
|
|
|
|
{
|
|
|
|
return new Size(1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class LineNumberMargin : AbstractMargin
|
|
|
|
{
|
|
|
|
public LineNumberMargin(bool usePresenter, bool isOld)
|
|
|
|
{
|
|
|
|
_usePresenter = usePresenter;
|
|
|
|
_isOld = isOld;
|
|
|
|
ClipToBounds = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Render(DrawingContext context)
|
|
|
|
{
|
|
|
|
var presenter = this.FindAncestorOfType<ThemedTextDiffPresenter>();
|
|
|
|
if (presenter == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var isOld = _isOld;
|
|
|
|
if (_usePresenter)
|
|
|
|
isOld = presenter.IsOld;
|
|
|
|
|
|
|
|
var lines = presenter.GetLines();
|
|
|
|
var view = TextView;
|
|
|
|
if (view != null && view.VisualLinesValid)
|
|
|
|
{
|
|
|
|
var typeface = view.CreateTypeface();
|
|
|
|
foreach (var line in view.VisualLines)
|
|
|
|
{
|
|
|
|
var index = line.FirstDocumentLine.LineNumber;
|
|
|
|
if (index > lines.Count)
|
|
|
|
break;
|
|
|
|
|
|
|
|
var info = lines[index - 1];
|
|
|
|
var lineNumber = isOld ? info.OldLine : info.NewLine;
|
|
|
|
if (string.IsNullOrEmpty(lineNumber))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
var y = line.GetTextLineVisualYPosition(line.TextLines[0], VisualYPosition.TextTop) - view.VerticalOffset;
|
|
|
|
var txt = new FormattedText(
|
|
|
|
lineNumber,
|
|
|
|
CultureInfo.CurrentCulture,
|
|
|
|
FlowDirection.LeftToRight,
|
|
|
|
typeface,
|
|
|
|
presenter.FontSize,
|
|
|
|
presenter.Foreground);
|
|
|
|
context.DrawText(txt, new Point(Bounds.Width - txt.Width, y));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override Size MeasureOverride(Size availableSize)
|
|
|
|
{
|
|
|
|
var presenter = this.FindAncestorOfType<ThemedTextDiffPresenter>();
|
|
|
|
if (presenter == null)
|
|
|
|
return new Size(32, 0);
|
|
|
|
|
|
|
|
var maxLineNumber = presenter.GetMaxLineNumber();
|
|
|
|
var typeface = TextView.CreateTypeface();
|
|
|
|
var test = new FormattedText(
|
|
|
|
$"{maxLineNumber}",
|
|
|
|
CultureInfo.CurrentCulture,
|
|
|
|
FlowDirection.LeftToRight,
|
|
|
|
typeface,
|
|
|
|
presenter.FontSize,
|
|
|
|
Brushes.White);
|
|
|
|
return new Size(test.Width, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnDataContextChanged(EventArgs e)
|
|
|
|
{
|
|
|
|
base.OnDataContextChanged(e);
|
|
|
|
InvalidateMeasure();
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool _usePresenter = false;
|
|
|
|
private bool _isOld = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public class LineBackgroundRenderer : IBackgroundRenderer
|
|
|
|
{
|
|
|
|
public KnownLayer Layer => KnownLayer.Background;
|
|
|
|
|
|
|
|
public LineBackgroundRenderer(ThemedTextDiffPresenter presenter)
|
|
|
|
{
|
|
|
|
_presenter = presenter;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Draw(TextView textView, DrawingContext drawingContext)
|
|
|
|
{
|
|
|
|
if (_presenter.Document == null || !textView.VisualLinesValid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var lines = _presenter.GetLines();
|
|
|
|
var width = textView.Bounds.Width;
|
|
|
|
foreach (var line in textView.VisualLines)
|
|
|
|
{
|
|
|
|
if (line.FirstDocumentLine == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
var index = line.FirstDocumentLine.LineNumber;
|
|
|
|
if (index > lines.Count)
|
|
|
|
break;
|
|
|
|
|
|
|
|
var info = lines[index - 1];
|
|
|
|
var bg = GetBrushByLineType(info.Type);
|
|
|
|
if (bg == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
var y = line.GetTextLineVisualYPosition(line.TextLines[0], VisualYPosition.TextTop) - textView.VerticalOffset;
|
|
|
|
drawingContext.DrawRectangle(bg, null, new Rect(0, y, width, line.Height));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private IBrush GetBrushByLineType(Models.TextDiffLineType type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case Models.TextDiffLineType.None:
|
|
|
|
return _presenter.EmptyContentBackground;
|
|
|
|
case Models.TextDiffLineType.Added:
|
|
|
|
return _presenter.AddedContentBackground;
|
|
|
|
case Models.TextDiffLineType.Deleted:
|
|
|
|
return _presenter.DeletedContentBackground;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private ThemedTextDiffPresenter _presenter = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public class LineStyleTransformer : DocumentColorizingTransformer
|
|
|
|
{
|
|
|
|
public LineStyleTransformer(ThemedTextDiffPresenter presenter)
|
|
|
|
{
|
|
|
|
_presenter = presenter;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void ColorizeLine(DocumentLine line)
|
|
|
|
{
|
|
|
|
var lines = _presenter.GetLines();
|
|
|
|
var idx = line.LineNumber;
|
|
|
|
if (idx > lines.Count)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var info = lines[idx - 1];
|
|
|
|
if (info.Type == Models.TextDiffLineType.Indicator)
|
|
|
|
{
|
|
|
|
ChangeLinePart(line.Offset, line.EndOffset, v =>
|
|
|
|
{
|
|
|
|
v.TextRunProperties.SetForegroundBrush(_presenter.IndicatorForeground);
|
|
|
|
v.TextRunProperties.SetTypeface(new Typeface(_presenter.FontFamily, FontStyle.Italic));
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.Highlights.Count > 0)
|
|
|
|
{
|
|
|
|
var bg = info.Type == Models.TextDiffLineType.Added ? _presenter.AddedHighlightBrush : _presenter.DeletedHighlightBrush;
|
|
|
|
foreach (var highlight in info.Highlights)
|
|
|
|
{
|
|
|
|
ChangeLinePart(line.Offset + highlight.Start, line.Offset + highlight.Start + highlight.Count, v =>
|
|
|
|
{
|
|
|
|
v.TextRunProperties.SetBackgroundBrush(bg);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private readonly ThemedTextDiffPresenter _presenter;
|
|
|
|
}
|
|
|
|
|
2024-06-12 06:12:45 -07:00
|
|
|
public static readonly StyledProperty<string> FileNameProperty =
|
2024-07-14 00:55:15 -07:00
|
|
|
AvaloniaProperty.Register<ThemedTextDiffPresenter, string>(nameof(FileName), string.Empty);
|
2024-06-12 06:12:45 -07:00
|
|
|
|
|
|
|
public string FileName
|
|
|
|
{
|
|
|
|
get => GetValue(FileNameProperty);
|
|
|
|
set => SetValue(FileNameProperty, value);
|
|
|
|
}
|
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
public static readonly StyledProperty<bool> IsOldProperty =
|
|
|
|
AvaloniaProperty.Register<ThemedTextDiffPresenter, bool>(nameof(IsOld));
|
|
|
|
|
|
|
|
public bool IsOld
|
|
|
|
{
|
|
|
|
get => GetValue(IsOldProperty);
|
|
|
|
set => SetValue(IsOldProperty, value);
|
|
|
|
}
|
|
|
|
|
2024-06-12 06:12:45 -07:00
|
|
|
public static readonly StyledProperty<IBrush> LineBrushProperty =
|
2024-07-14 00:55:15 -07:00
|
|
|
AvaloniaProperty.Register<ThemedTextDiffPresenter, IBrush>(nameof(LineBrush), new SolidColorBrush(Colors.DarkGray));
|
2024-06-12 06:12:45 -07:00
|
|
|
|
|
|
|
public IBrush LineBrush
|
|
|
|
{
|
|
|
|
get => GetValue(LineBrushProperty);
|
|
|
|
set => SetValue(LineBrushProperty, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static readonly StyledProperty<IBrush> EmptyContentBackgroundProperty =
|
2024-07-14 00:55:15 -07:00
|
|
|
AvaloniaProperty.Register<ThemedTextDiffPresenter, IBrush>(nameof(EmptyContentBackground), new SolidColorBrush(Color.FromArgb(60, 0, 0, 0)));
|
2024-06-12 06:12:45 -07:00
|
|
|
|
|
|
|
public IBrush EmptyContentBackground
|
|
|
|
{
|
|
|
|
get => GetValue(EmptyContentBackgroundProperty);
|
|
|
|
set => SetValue(EmptyContentBackgroundProperty, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static readonly StyledProperty<IBrush> AddedContentBackgroundProperty =
|
2024-07-14 00:55:15 -07:00
|
|
|
AvaloniaProperty.Register<ThemedTextDiffPresenter, IBrush>(nameof(AddedContentBackground), new SolidColorBrush(Color.FromArgb(60, 0, 255, 0)));
|
2024-06-12 06:12:45 -07:00
|
|
|
|
|
|
|
public IBrush AddedContentBackground
|
|
|
|
{
|
|
|
|
get => GetValue(AddedContentBackgroundProperty);
|
|
|
|
set => SetValue(AddedContentBackgroundProperty, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static readonly StyledProperty<IBrush> DeletedContentBackgroundProperty =
|
2024-07-14 00:55:15 -07:00
|
|
|
AvaloniaProperty.Register<ThemedTextDiffPresenter, IBrush>(nameof(DeletedContentBackground), new SolidColorBrush(Color.FromArgb(60, 255, 0, 0)));
|
2024-06-12 06:12:45 -07:00
|
|
|
|
|
|
|
public IBrush DeletedContentBackground
|
|
|
|
{
|
|
|
|
get => GetValue(DeletedContentBackgroundProperty);
|
|
|
|
set => SetValue(DeletedContentBackgroundProperty, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static readonly StyledProperty<IBrush> AddedHighlightBrushProperty =
|
2024-07-14 00:55:15 -07:00
|
|
|
AvaloniaProperty.Register<ThemedTextDiffPresenter, IBrush>(nameof(AddedHighlightBrush), new SolidColorBrush(Color.FromArgb(90, 0, 255, 0)));
|
2024-06-12 06:12:45 -07:00
|
|
|
|
|
|
|
public IBrush AddedHighlightBrush
|
|
|
|
{
|
|
|
|
get => GetValue(AddedHighlightBrushProperty);
|
|
|
|
set => SetValue(AddedHighlightBrushProperty, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static readonly StyledProperty<IBrush> DeletedHighlightBrushProperty =
|
2024-07-14 00:55:15 -07:00
|
|
|
AvaloniaProperty.Register<ThemedTextDiffPresenter, IBrush>(nameof(DeletedHighlightBrush), new SolidColorBrush(Color.FromArgb(80, 255, 0, 0)));
|
2024-06-12 06:12:45 -07:00
|
|
|
|
|
|
|
public IBrush DeletedHighlightBrush
|
|
|
|
{
|
|
|
|
get => GetValue(DeletedHighlightBrushProperty);
|
|
|
|
set => SetValue(DeletedHighlightBrushProperty, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static readonly StyledProperty<IBrush> IndicatorForegroundProperty =
|
2024-07-14 00:55:15 -07:00
|
|
|
AvaloniaProperty.Register<ThemedTextDiffPresenter, IBrush>(nameof(IndicatorForeground), Brushes.Gray);
|
2024-06-12 06:12:45 -07:00
|
|
|
|
|
|
|
public IBrush IndicatorForeground
|
|
|
|
{
|
|
|
|
get => GetValue(IndicatorForegroundProperty);
|
|
|
|
set => SetValue(IndicatorForegroundProperty, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static readonly StyledProperty<bool> UseSyntaxHighlightingProperty =
|
2024-07-14 00:55:15 -07:00
|
|
|
AvaloniaProperty.Register<ThemedTextDiffPresenter, bool>(nameof(UseSyntaxHighlighting));
|
2024-06-12 06:12:45 -07:00
|
|
|
|
|
|
|
public bool UseSyntaxHighlighting
|
|
|
|
{
|
|
|
|
get => GetValue(UseSyntaxHighlightingProperty);
|
|
|
|
set => SetValue(UseSyntaxHighlightingProperty, value);
|
|
|
|
}
|
|
|
|
|
2024-06-19 02:33:34 -07:00
|
|
|
public static readonly StyledProperty<bool> ShowHiddenSymbolsProperty =
|
2024-07-14 00:55:15 -07:00
|
|
|
AvaloniaProperty.Register<ThemedTextDiffPresenter, bool>(nameof(ShowHiddenSymbols));
|
2024-06-19 02:33:34 -07:00
|
|
|
|
|
|
|
public bool ShowHiddenSymbols
|
|
|
|
{
|
2024-06-23 00:45:54 -07:00
|
|
|
get => GetValue(ShowHiddenSymbolsProperty);
|
2024-06-19 02:33:34 -07:00
|
|
|
set => SetValue(ShowHiddenSymbolsProperty, value);
|
|
|
|
}
|
|
|
|
|
2024-07-18 20:01:31 -07:00
|
|
|
public static readonly StyledProperty<bool> EnableChunkSelectionProperty =
|
|
|
|
AvaloniaProperty.Register<ThemedTextDiffPresenter, bool>(nameof(EnableChunkSelection));
|
|
|
|
|
|
|
|
public bool EnableChunkSelection
|
|
|
|
{
|
|
|
|
get => GetValue(EnableChunkSelectionProperty);
|
|
|
|
set => SetValue(EnableChunkSelectionProperty, value);
|
|
|
|
}
|
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
public static readonly StyledProperty<TextDiffViewChunk> SelectedChunkProperty =
|
|
|
|
AvaloniaProperty.Register<ThemedTextDiffPresenter, TextDiffViewChunk>(nameof(SelectedChunk));
|
2024-07-17 01:56:16 -07:00
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
public TextDiffViewChunk SelectedChunk
|
2024-07-17 01:56:16 -07:00
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
get => GetValue(SelectedChunkProperty);
|
|
|
|
set => SetValue(SelectedChunkProperty, value);
|
2024-07-17 01:56:16 -07:00
|
|
|
}
|
|
|
|
|
2024-06-12 06:12:45 -07:00
|
|
|
protected override Type StyleKeyOverride => typeof(TextEditor);
|
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
public ThemedTextDiffPresenter(TextArea area, TextDocument doc) : base(area, doc)
|
2024-06-12 06:12:45 -07:00
|
|
|
{
|
|
|
|
IsReadOnly = true;
|
|
|
|
ShowLineNumbers = false;
|
|
|
|
BorderThickness = new Thickness(0);
|
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
_lineStyleTransformer = new LineStyleTransformer(this);
|
|
|
|
|
2024-06-12 06:12:45 -07:00
|
|
|
TextArea.TextView.Margin = new Thickness(4, 0);
|
|
|
|
TextArea.TextView.Options.EnableHyperlinks = false;
|
|
|
|
TextArea.TextView.Options.EnableEmailHyperlinks = false;
|
2024-07-18 01:16:58 -07:00
|
|
|
|
|
|
|
TextArea.TextView.BackgroundRenderers.Add(new LineBackgroundRenderer(this));
|
|
|
|
TextArea.TextView.LineTransformers.Add(_lineStyleTransformer);
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual List<Models.TextDiffLine> GetLines()
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual int GetMaxLineNumber()
|
|
|
|
{
|
|
|
|
return 0;
|
2024-06-12 06:12:45 -07:00
|
|
|
}
|
|
|
|
|
2024-07-18 19:43:19 -07:00
|
|
|
public virtual void UpdateSelectedChunk(double y)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-07-17 01:56:16 -07:00
|
|
|
public override void Render(DrawingContext context)
|
|
|
|
{
|
|
|
|
base.Render(context);
|
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
var chunk = SelectedChunk;
|
2024-07-25 19:18:28 -07:00
|
|
|
if (chunk == null || (!chunk.Combined && chunk.IsOldSide != IsOld))
|
2024-07-17 01:56:16 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
var color = (Color)this.FindResource("SystemAccentColor");
|
2024-07-17 21:13:19 -07:00
|
|
|
var brush = new SolidColorBrush(color, 0.1);
|
2024-07-17 01:56:16 -07:00
|
|
|
var pen = new Pen(color.ToUInt32());
|
2024-07-25 19:18:28 -07:00
|
|
|
var rect = new Rect(0, chunk.Y, Bounds.Width, chunk.Height);
|
2024-07-17 01:56:16 -07:00
|
|
|
|
|
|
|
context.DrawRectangle(brush, null, rect);
|
|
|
|
context.DrawLine(pen, rect.TopLeft, rect.TopRight);
|
|
|
|
context.DrawLine(pen, rect.BottomLeft, rect.BottomRight);
|
|
|
|
}
|
|
|
|
|
2024-06-12 06:12:45 -07:00
|
|
|
protected override void OnLoaded(RoutedEventArgs e)
|
|
|
|
{
|
|
|
|
base.OnLoaded(e);
|
2024-07-18 19:43:19 -07:00
|
|
|
|
|
|
|
TextArea.TextView.ContextRequested += OnTextViewContextRequested;
|
|
|
|
TextArea.TextView.PointerMoved += OnTextViewPointerMoved;
|
|
|
|
TextArea.TextView.PointerWheelChanged += OnTextViewPointerWheelChanged;
|
|
|
|
|
2024-06-12 06:12:45 -07:00
|
|
|
UpdateTextMate();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnUnloaded(RoutedEventArgs e)
|
|
|
|
{
|
|
|
|
base.OnUnloaded(e);
|
|
|
|
|
2024-07-18 19:43:19 -07:00
|
|
|
TextArea.TextView.ContextRequested -= OnTextViewContextRequested;
|
|
|
|
TextArea.TextView.PointerMoved -= OnTextViewPointerMoved;
|
|
|
|
TextArea.TextView.PointerWheelChanged -= OnTextViewPointerWheelChanged;
|
|
|
|
|
2024-06-12 06:12:45 -07:00
|
|
|
if (_textMate != null)
|
|
|
|
{
|
|
|
|
_textMate.Dispose();
|
|
|
|
_textMate = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
|
|
|
|
{
|
|
|
|
base.OnPropertyChanged(change);
|
|
|
|
|
|
|
|
if (change.Property == UseSyntaxHighlightingProperty)
|
2024-06-19 03:15:32 -07:00
|
|
|
{
|
2024-06-12 06:12:45 -07:00
|
|
|
UpdateTextMate();
|
2024-06-19 03:15:32 -07:00
|
|
|
}
|
|
|
|
else if (change.Property == ShowHiddenSymbolsProperty)
|
2024-06-19 02:33:34 -07:00
|
|
|
{
|
2024-06-19 03:15:32 -07:00
|
|
|
var val = change.NewValue is true;
|
|
|
|
Options.ShowTabs = val;
|
|
|
|
Options.ShowSpaces = val;
|
2024-06-19 02:33:34 -07:00
|
|
|
}
|
2024-06-12 06:12:45 -07:00
|
|
|
else if (change.Property == FileNameProperty)
|
2024-06-19 03:15:32 -07:00
|
|
|
{
|
2024-06-12 06:12:45 -07:00
|
|
|
Models.TextMateHelper.SetGrammarByFileName(_textMate, FileName);
|
2024-06-19 03:15:32 -07:00
|
|
|
}
|
2024-06-12 06:12:45 -07:00
|
|
|
else if (change.Property.Name == "ActualThemeVariant" && change.NewValue != null)
|
2024-06-19 03:15:32 -07:00
|
|
|
{
|
2024-06-12 06:12:45 -07:00
|
|
|
Models.TextMateHelper.SetThemeByApp(_textMate);
|
2024-06-19 03:15:32 -07:00
|
|
|
}
|
2024-07-18 01:16:58 -07:00
|
|
|
else if (change.Property == SelectedChunkProperty)
|
2024-07-17 01:56:16 -07:00
|
|
|
{
|
|
|
|
InvalidateVisual();
|
|
|
|
}
|
2024-06-12 06:12:45 -07:00
|
|
|
}
|
|
|
|
|
2024-07-18 19:43:19 -07:00
|
|
|
private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs e)
|
|
|
|
{
|
|
|
|
var selection = TextArea.Selection;
|
|
|
|
if (selection.IsEmpty)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var copy = new MenuItem();
|
|
|
|
copy.Header = App.Text("Copy");
|
|
|
|
copy.Icon = App.CreateMenuIcon("Icons.Copy");
|
|
|
|
copy.Click += (_, ev) =>
|
|
|
|
{
|
|
|
|
App.CopyText(SelectedText);
|
|
|
|
ev.Handled = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
var menu = new ContextMenu();
|
|
|
|
menu.Items.Add(copy);
|
|
|
|
|
|
|
|
TextArea.TextView.OpenContextMenu(menu);
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnTextViewPointerMoved(object sender, PointerEventArgs e)
|
|
|
|
{
|
2024-07-18 20:01:31 -07:00
|
|
|
if (EnableChunkSelection && sender is TextView view)
|
2024-07-18 19:43:19 -07:00
|
|
|
UpdateSelectedChunk(e.GetPosition(view).Y + view.VerticalOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnTextViewPointerWheelChanged(object sender, PointerWheelEventArgs e)
|
|
|
|
{
|
2024-07-18 20:01:31 -07:00
|
|
|
if (EnableChunkSelection && sender is TextView view)
|
2024-07-18 19:43:19 -07:00
|
|
|
{
|
|
|
|
var y = e.GetPosition(view).Y + view.VerticalOffset;
|
|
|
|
Dispatcher.UIThread.Post(() => UpdateSelectedChunk(y));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
protected void TrySetChunk(TextDiffViewChunk chunk)
|
2024-07-17 19:17:40 -07:00
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
var old = SelectedChunk;
|
2024-07-17 19:17:40 -07:00
|
|
|
if (chunk == null)
|
|
|
|
{
|
|
|
|
if (old != null)
|
2024-07-18 01:16:58 -07:00
|
|
|
SetCurrentValue(SelectedChunkProperty, null);
|
2024-07-17 19:17:40 -07:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chunk.ShouldReplace(old))
|
2024-07-18 01:16:58 -07:00
|
|
|
SetCurrentValue(SelectedChunkProperty, chunk);
|
2024-07-17 19:17:40 -07:00
|
|
|
}
|
|
|
|
|
2024-07-17 02:10:32 -07:00
|
|
|
protected (int, int) FindRangeByIndex(List<Models.TextDiffLine> lines, int lineIdx)
|
|
|
|
{
|
|
|
|
var startIdx = -1;
|
|
|
|
var endIdx = -1;
|
|
|
|
|
|
|
|
var normalLineCount = 0;
|
|
|
|
var modifiedLineCount = 0;
|
|
|
|
|
|
|
|
for (int i = lineIdx; i >= 0; i--)
|
|
|
|
{
|
|
|
|
var line = lines[i];
|
|
|
|
if (line.Type == Models.TextDiffLineType.Indicator)
|
|
|
|
{
|
|
|
|
startIdx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line.Type == Models.TextDiffLineType.Normal)
|
|
|
|
{
|
|
|
|
normalLineCount++;
|
|
|
|
if (normalLineCount >= 2)
|
|
|
|
{
|
|
|
|
startIdx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
normalLineCount = 0;
|
|
|
|
modifiedLineCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
normalLineCount = lines[lineIdx].Type == Models.TextDiffLineType.Normal ? 1 : 0;
|
|
|
|
for (int i = lineIdx + 1; i < lines.Count; i++)
|
|
|
|
{
|
|
|
|
var line = lines[i];
|
|
|
|
if (line.Type == Models.TextDiffLineType.Indicator)
|
|
|
|
{
|
|
|
|
endIdx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line.Type == Models.TextDiffLineType.Normal)
|
|
|
|
{
|
|
|
|
normalLineCount++;
|
|
|
|
if (normalLineCount >= 2)
|
|
|
|
{
|
|
|
|
endIdx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
normalLineCount = 0;
|
|
|
|
modifiedLineCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (endIdx == -1)
|
|
|
|
endIdx = lines.Count - 1;
|
|
|
|
|
|
|
|
return modifiedLineCount > 0 ? (startIdx, endIdx) : (-1, -1);
|
|
|
|
}
|
|
|
|
|
2024-07-14 00:55:15 -07:00
|
|
|
private void UpdateTextMate()
|
2024-06-12 06:12:45 -07:00
|
|
|
{
|
|
|
|
if (UseSyntaxHighlighting)
|
|
|
|
{
|
|
|
|
if (_textMate == null)
|
|
|
|
{
|
|
|
|
TextArea.TextView.LineTransformers.Remove(_lineStyleTransformer);
|
|
|
|
_textMate = Models.TextMateHelper.CreateForEditor(this);
|
|
|
|
TextArea.TextView.LineTransformers.Add(_lineStyleTransformer);
|
2024-06-13 18:46:30 -07:00
|
|
|
Models.TextMateHelper.SetGrammarByFileName(_textMate, FileName);
|
2024-06-12 06:12:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_textMate != null)
|
|
|
|
{
|
|
|
|
_textMate.Dispose();
|
|
|
|
_textMate = null;
|
|
|
|
GC.Collect();
|
|
|
|
|
|
|
|
TextArea.TextView.Redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private TextMate.Installation _textMate = null;
|
2024-07-18 01:16:58 -07:00
|
|
|
protected LineStyleTransformer _lineStyleTransformer = null;
|
2024-06-12 06:12:45 -07:00
|
|
|
}
|
|
|
|
|
2024-07-14 00:55:15 -07:00
|
|
|
public class CombinedTextDiffPresenter : ThemedTextDiffPresenter
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
public CombinedTextDiffPresenter() : base(new TextArea(), new TextDocument())
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
TextArea.LeftMargins.Add(new LineNumberMargin(false, true) { Margin = new Thickness(8, 0) });
|
|
|
|
TextArea.LeftMargins.Add(new VerticalSeperatorMargin());
|
|
|
|
TextArea.LeftMargins.Add(new LineNumberMargin(false, false) { Margin = new Thickness(8, 0) });
|
|
|
|
TextArea.LeftMargins.Add(new VerticalSeperatorMargin());
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
public override List<Models.TextDiffLine> GetLines()
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
if (DataContext is Models.TextDiff diff)
|
|
|
|
return diff.Lines;
|
|
|
|
return [];
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
public override int GetMaxLineNumber()
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
if (DataContext is Models.TextDiff diff)
|
|
|
|
return diff.MaxLineNumber;
|
|
|
|
return 0;
|
2024-03-20 03:27:48 -07:00
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-06-12 06:12:45 -07:00
|
|
|
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
2024-03-20 03:27:48 -07:00
|
|
|
{
|
2024-06-12 06:12:45 -07:00
|
|
|
base.OnApplyTemplate(e);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-06-12 06:12:45 -07:00
|
|
|
var scroller = (ScrollViewer)e.NameScope.Find("PART_ScrollViewer");
|
2024-07-25 02:57:28 -07:00
|
|
|
if (scroller != null)
|
|
|
|
{
|
|
|
|
scroller.Bind(ScrollViewer.OffsetProperty, new Binding("SyncScrollOffset", BindingMode.TwoWay));
|
|
|
|
scroller.GotFocus += (_, _) => TrySetChunk(null);
|
|
|
|
}
|
2024-06-12 06:12:45 -07:00
|
|
|
}
|
2024-03-04 23:53:38 -08:00
|
|
|
|
2024-07-18 19:43:19 -07:00
|
|
|
public override void UpdateSelectedChunk(double y)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
var diff = DataContext as Models.TextDiff;
|
2024-07-18 20:01:31 -07:00
|
|
|
if (diff == null)
|
2024-07-18 01:16:58 -07:00
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-07-18 19:43:19 -07:00
|
|
|
var view = TextArea.TextView;
|
2024-07-18 01:16:58 -07:00
|
|
|
var selection = TextArea.Selection;
|
|
|
|
if (!selection.IsEmpty)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
var startIdx = Math.Min(selection.StartPosition.Line - 1, diff.Lines.Count - 1);
|
|
|
|
var endIdx = Math.Min(selection.EndPosition.Line - 1, diff.Lines.Count - 1);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
if (startIdx > endIdx)
|
|
|
|
(startIdx, endIdx) = (endIdx, startIdx);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
var hasChanges = false;
|
|
|
|
for (var i = startIdx; i <= endIdx; i++)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
var line = diff.Lines[i];
|
|
|
|
if (line.Type == Models.TextDiffLineType.Added || line.Type == Models.TextDiffLineType.Deleted)
|
|
|
|
{
|
|
|
|
hasChanges = true;
|
2024-03-31 01:54:29 -07:00
|
|
|
break;
|
2024-07-18 01:16:58 -07:00
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
if (!hasChanges)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
TrySetChunk(null);
|
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-07-25 02:57:28 -07:00
|
|
|
var firstLineIdx = view.VisualLines[0].FirstDocumentLine.LineNumber - 1;
|
|
|
|
var lastLineIdx = view.VisualLines[^1].FirstDocumentLine.LineNumber - 1;
|
|
|
|
if (endIdx < firstLineIdx)
|
|
|
|
{
|
2024-07-25 19:18:28 -07:00
|
|
|
TrySetChunk(null);
|
2024-07-25 02:57:28 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (startIdx > lastLineIdx)
|
|
|
|
{
|
2024-07-25 19:18:28 -07:00
|
|
|
TrySetChunk(null);
|
2024-07-25 02:57:28 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-18 19:43:19 -07:00
|
|
|
var startLine = view.GetVisualLine(startIdx + 1);
|
|
|
|
var endLine = view.GetVisualLine(endIdx + 1);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
var rectStartY = startLine != null ?
|
2024-07-18 19:43:19 -07:00
|
|
|
startLine.GetTextLineVisualYPosition(startLine.TextLines[0], VisualYPosition.TextTop) - view.VerticalOffset :
|
2024-07-18 01:16:58 -07:00
|
|
|
0;
|
|
|
|
var rectEndY = endLine != null ?
|
2024-07-18 19:43:19 -07:00
|
|
|
endLine.GetTextLineVisualYPosition(endLine.TextLines[^1], VisualYPosition.TextBottom) - view.VerticalOffset :
|
|
|
|
view.Bounds.Height;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
TrySetChunk(new TextDiffViewChunk()
|
|
|
|
{
|
|
|
|
Y = rectStartY,
|
|
|
|
Height = rectEndY - rectStartY,
|
|
|
|
StartIdx = startIdx,
|
|
|
|
EndIdx = endIdx,
|
|
|
|
Combined = true,
|
|
|
|
IsOldSide = false,
|
|
|
|
});
|
|
|
|
}
|
2024-07-18 19:43:19 -07:00
|
|
|
else
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
var lineIdx = -1;
|
|
|
|
foreach (var line in view.VisualLines)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
var index = line.FirstDocumentLine.LineNumber;
|
|
|
|
if (index > diff.Lines.Count)
|
|
|
|
break;
|
|
|
|
|
|
|
|
var endY = line.GetTextLineVisualYPosition(line.TextLines[^1], VisualYPosition.TextBottom);
|
|
|
|
if (endY > y)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
lineIdx = index - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
if (lineIdx == -1)
|
|
|
|
{
|
|
|
|
TrySetChunk(null);
|
2024-02-05 23:08:37 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
var (startIdx, endIdx) = FindRangeByIndex(diff.Lines, lineIdx);
|
|
|
|
if (startIdx == -1)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
TrySetChunk(null);
|
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
var startLine = view.GetVisualLine(startIdx + 1);
|
|
|
|
var endLine = view.GetVisualLine(endIdx + 1);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
var rectStartY = startLine != null ?
|
2024-07-18 19:43:19 -07:00
|
|
|
startLine.GetTextLineVisualYPosition(startLine.TextLines[0], VisualYPosition.TextTop) - view.VerticalOffset :
|
2024-07-18 01:16:58 -07:00
|
|
|
0;
|
|
|
|
var rectEndY = endLine != null ?
|
2024-07-18 19:43:19 -07:00
|
|
|
endLine.GetTextLineVisualYPosition(endLine.TextLines[^1], VisualYPosition.TextBottom) - view.VerticalOffset :
|
2024-07-18 01:16:58 -07:00
|
|
|
view.Bounds.Height;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
TrySetChunk(new TextDiffViewChunk()
|
|
|
|
{
|
|
|
|
Y = rectStartY,
|
|
|
|
Height = rectEndY - rectStartY,
|
|
|
|
StartIdx = startIdx,
|
|
|
|
EndIdx = endIdx,
|
|
|
|
Combined = true,
|
|
|
|
IsOldSide = false,
|
|
|
|
});
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-06-12 06:12:45 -07:00
|
|
|
protected override void OnDataContextChanged(EventArgs e)
|
|
|
|
{
|
|
|
|
base.OnDataContextChanged(e);
|
|
|
|
|
2024-07-18 19:43:19 -07:00
|
|
|
var textDiff = DataContext as Models.TextDiff;
|
|
|
|
if (textDiff != null)
|
2024-06-12 06:12:45 -07:00
|
|
|
{
|
|
|
|
var builder = new StringBuilder();
|
2024-07-18 19:43:19 -07:00
|
|
|
foreach (var line in textDiff.Lines)
|
2024-07-03 00:34:29 -07:00
|
|
|
{
|
|
|
|
if (line.Content.Length > 10000)
|
|
|
|
{
|
|
|
|
builder.Append(line.Content.Substring(0, 1000));
|
2024-07-18 19:43:19 -07:00
|
|
|
builder.Append($"...({line.Content.Length - 1000} character trimmed)");
|
2024-07-03 00:34:29 -07:00
|
|
|
builder.AppendLine();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
builder.AppendLine(line.Content);
|
|
|
|
}
|
|
|
|
}
|
2024-06-12 06:12:45 -07:00
|
|
|
|
|
|
|
Text = builder.ToString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Text = string.Empty;
|
|
|
|
}
|
2024-07-18 19:43:19 -07:00
|
|
|
|
|
|
|
GC.Collect();
|
2024-06-12 06:12:45 -07:00
|
|
|
}
|
2024-07-18 19:43:19 -07:00
|
|
|
}
|
2024-06-12 06:12:45 -07:00
|
|
|
|
2024-07-18 19:43:19 -07:00
|
|
|
public class SingleSideTextDiffPresenter : ThemedTextDiffPresenter
|
|
|
|
{
|
|
|
|
public SingleSideTextDiffPresenter() : base(new TextArea(), new TextDocument())
|
2024-04-28 01:46:39 -07:00
|
|
|
{
|
2024-07-18 19:43:19 -07:00
|
|
|
TextArea.LeftMargins.Add(new LineNumberMargin(true, false) { Margin = new Thickness(8, 0) });
|
|
|
|
TextArea.LeftMargins.Add(new VerticalSeperatorMargin());
|
2024-04-28 01:46:39 -07:00
|
|
|
}
|
|
|
|
|
2024-07-18 19:43:19 -07:00
|
|
|
public override List<Models.TextDiffLine> GetLines()
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-18 19:43:19 -07:00
|
|
|
if (DataContext is ViewModels.TwoSideTextDiff diff)
|
|
|
|
return IsOld ? diff.Old : diff.New;
|
|
|
|
return [];
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-07-18 19:43:19 -07:00
|
|
|
public override int GetMaxLineNumber()
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-18 19:43:19 -07:00
|
|
|
if (DataContext is ViewModels.TwoSideTextDiff diff)
|
|
|
|
return diff.MaxLineNumber;
|
|
|
|
return 0;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-07-18 19:43:19 -07:00
|
|
|
public override void UpdateSelectedChunk(double y)
|
2024-07-17 01:56:16 -07:00
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
var diff = DataContext as ViewModels.TwoSideTextDiff;
|
2024-07-18 20:01:31 -07:00
|
|
|
if (diff == null)
|
2024-07-18 01:16:58 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
var parent = this.FindAncestorOfType<TextDiffView>();
|
|
|
|
if (parent == null)
|
2024-07-17 01:56:16 -07:00
|
|
|
return;
|
|
|
|
|
2024-07-18 19:43:19 -07:00
|
|
|
var view = TextArea.TextView;
|
|
|
|
var lines = IsOld ? diff.Old : diff.New;
|
2024-07-18 01:16:58 -07:00
|
|
|
var selection = TextArea.Selection;
|
|
|
|
if (!selection.IsEmpty)
|
2024-07-17 01:56:16 -07:00
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
var startIdx = Math.Min(selection.StartPosition.Line - 1, lines.Count - 1);
|
|
|
|
var endIdx = Math.Min(selection.EndPosition.Line - 1, lines.Count - 1);
|
|
|
|
|
|
|
|
if (startIdx > endIdx)
|
|
|
|
(startIdx, endIdx) = (endIdx, startIdx);
|
|
|
|
|
|
|
|
var hasChanges = false;
|
|
|
|
for (var i = startIdx; i <= endIdx; i++)
|
|
|
|
{
|
|
|
|
var line = lines[i];
|
|
|
|
if (line.Type == Models.TextDiffLineType.Added || line.Type == Models.TextDiffLineType.Deleted)
|
|
|
|
{
|
|
|
|
hasChanges = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasChanges)
|
|
|
|
{
|
|
|
|
TrySetChunk(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-25 02:57:28 -07:00
|
|
|
var firstLineIdx = view.VisualLines[0].FirstDocumentLine.LineNumber - 1;
|
|
|
|
var lastLineIdx = view.VisualLines[^1].FirstDocumentLine.LineNumber - 1;
|
|
|
|
if (endIdx < firstLineIdx)
|
|
|
|
{
|
2024-07-25 19:18:28 -07:00
|
|
|
TrySetChunk(null);
|
2024-07-25 02:57:28 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (startIdx > lastLineIdx)
|
|
|
|
{
|
2024-07-25 19:18:28 -07:00
|
|
|
TrySetChunk(null);
|
2024-07-25 02:57:28 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-18 19:43:19 -07:00
|
|
|
var startLine = view.GetVisualLine(startIdx + 1);
|
|
|
|
var endLine = view.GetVisualLine(endIdx + 1);
|
2024-07-18 01:16:58 -07:00
|
|
|
|
|
|
|
var rectStartY = startLine != null ?
|
2024-07-18 19:43:19 -07:00
|
|
|
startLine.GetTextLineVisualYPosition(startLine.TextLines[0], VisualYPosition.TextTop) - view.VerticalOffset :
|
2024-07-18 01:16:58 -07:00
|
|
|
0;
|
|
|
|
var rectEndY = endLine != null ?
|
2024-07-18 19:43:19 -07:00
|
|
|
endLine.GetTextLineVisualYPosition(endLine.TextLines[^1], VisualYPosition.TextBottom) - view.VerticalOffset :
|
|
|
|
view.Bounds.Height;
|
2024-07-18 01:16:58 -07:00
|
|
|
|
|
|
|
diff.ConvertsToCombinedRange(parent.DataContext as Models.TextDiff, ref startIdx, ref endIdx, IsOld);
|
|
|
|
|
|
|
|
TrySetChunk(new TextDiffViewChunk()
|
|
|
|
{
|
|
|
|
Y = rectStartY,
|
|
|
|
Height = rectEndY - rectStartY,
|
|
|
|
StartIdx = startIdx,
|
|
|
|
EndIdx = endIdx,
|
|
|
|
Combined = false,
|
|
|
|
IsOldSide = IsOld,
|
|
|
|
});
|
|
|
|
|
2024-07-17 01:56:16 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-18 19:43:19 -07:00
|
|
|
var textDiff = this.FindAncestorOfType<TextDiffView>()?.DataContext as Models.TextDiff;
|
|
|
|
if (textDiff != null)
|
2024-07-17 01:56:16 -07:00
|
|
|
{
|
|
|
|
var lineIdx = -1;
|
|
|
|
foreach (var line in view.VisualLines)
|
|
|
|
{
|
|
|
|
var index = line.FirstDocumentLine.LineNumber;
|
|
|
|
if (index > lines.Count)
|
|
|
|
break;
|
|
|
|
|
|
|
|
var endY = line.GetTextLineVisualYPosition(line.TextLines[^1], VisualYPosition.TextBottom);
|
|
|
|
if (endY > y)
|
|
|
|
{
|
|
|
|
lineIdx = index - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lineIdx == -1)
|
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
TrySetChunk(null);
|
2024-07-17 01:56:16 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var (startIdx, endIdx) = FindRangeByIndex(lines, lineIdx);
|
|
|
|
if (startIdx == -1)
|
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
TrySetChunk(null);
|
2024-07-17 01:56:16 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var startLine = view.GetVisualLine(startIdx + 1);
|
|
|
|
var endLine = view.GetVisualLine(endIdx + 1);
|
|
|
|
|
|
|
|
var rectStartY = startLine != null ?
|
|
|
|
startLine.GetTextLineVisualYPosition(startLine.TextLines[0], VisualYPosition.TextTop) - view.VerticalOffset :
|
|
|
|
0;
|
|
|
|
var rectEndY = endLine != null ?
|
|
|
|
endLine.GetTextLineVisualYPosition(endLine.TextLines[^1], VisualYPosition.TextBottom) - view.VerticalOffset :
|
|
|
|
view.Bounds.Height;
|
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
TrySetChunk(new TextDiffViewChunk()
|
2024-07-17 01:56:16 -07:00
|
|
|
{
|
|
|
|
Y = rectStartY,
|
|
|
|
Height = rectEndY - rectStartY,
|
|
|
|
StartIdx = textDiff.Lines.IndexOf(lines[startIdx]),
|
2024-07-17 19:17:40 -07:00
|
|
|
EndIdx = endIdx == lines.Count - 1 ? textDiff.Lines.Count - 1 : textDiff.Lines.IndexOf(lines[endIdx]),
|
2024-07-18 01:16:58 -07:00
|
|
|
Combined = true,
|
|
|
|
IsOldSide = false,
|
2024-07-17 19:17:40 -07:00
|
|
|
});
|
2024-07-17 01:56:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 19:43:19 -07:00
|
|
|
protected override void OnLoaded(RoutedEventArgs e)
|
2024-07-17 01:56:16 -07:00
|
|
|
{
|
2024-07-18 19:43:19 -07:00
|
|
|
base.OnLoaded(e);
|
|
|
|
|
|
|
|
_scrollViewer = this.FindDescendantOfType<ScrollViewer>();
|
|
|
|
if (_scrollViewer != null)
|
|
|
|
{
|
2024-07-25 02:57:28 -07:00
|
|
|
_scrollViewer.GotFocus += OnTextViewScrollGotFocus;
|
2024-07-18 19:43:19 -07:00
|
|
|
_scrollViewer.ScrollChanged += OnTextViewScrollChanged;
|
|
|
|
_scrollViewer.Bind(ScrollViewer.OffsetProperty, new Binding("SyncScrollOffset", BindingMode.OneWay));
|
|
|
|
}
|
|
|
|
|
|
|
|
TextArea.PointerWheelChanged += OnTextAreaPointerWheelChanged;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnUnloaded(RoutedEventArgs e)
|
|
|
|
{
|
|
|
|
base.OnUnloaded(e);
|
|
|
|
|
|
|
|
if (_scrollViewer != null)
|
|
|
|
{
|
|
|
|
_scrollViewer.ScrollChanged -= OnTextViewScrollChanged;
|
2024-07-25 02:57:28 -07:00
|
|
|
_scrollViewer.GotFocus -= OnTextViewScrollGotFocus;
|
2024-07-18 19:43:19 -07:00
|
|
|
_scrollViewer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextArea.PointerWheelChanged -= OnTextAreaPointerWheelChanged;
|
|
|
|
|
|
|
|
GC.Collect();
|
|
|
|
}
|
2024-07-17 01:56:16 -07:00
|
|
|
|
2024-07-18 19:43:19 -07:00
|
|
|
protected override void OnDataContextChanged(EventArgs e)
|
|
|
|
{
|
|
|
|
base.OnDataContextChanged(e);
|
|
|
|
|
|
|
|
if (DataContext is ViewModels.TwoSideTextDiff diff)
|
|
|
|
{
|
|
|
|
var builder = new StringBuilder();
|
|
|
|
var lines = IsOld ? diff.Old : diff.New;
|
|
|
|
foreach (var line in lines)
|
|
|
|
{
|
|
|
|
if (line.Content.Length > 10000)
|
|
|
|
{
|
|
|
|
builder.Append(line.Content.Substring(0, 1000));
|
|
|
|
builder.Append($"...({line.Content.Length - 1000} characters trimmed)");
|
|
|
|
builder.AppendLine();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
builder.AppendLine(line.Content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Text = builder.ToString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Text = string.Empty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-25 02:57:28 -07:00
|
|
|
private void OnTextViewScrollGotFocus(object sender, GotFocusEventArgs e)
|
|
|
|
{
|
|
|
|
TrySetChunk(null);
|
|
|
|
}
|
|
|
|
|
2024-07-18 19:43:19 -07:00
|
|
|
private void OnTextViewScrollChanged(object sender, ScrollChangedEventArgs e)
|
|
|
|
{
|
|
|
|
if (TextArea.IsFocused && DataContext is ViewModels.TwoSideTextDiff diff)
|
|
|
|
diff.SyncScrollOffset = _scrollViewer.Offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnTextAreaPointerWheelChanged(object sender, PointerWheelEventArgs e)
|
|
|
|
{
|
|
|
|
if (!TextArea.IsFocused)
|
|
|
|
Focus();
|
2024-07-17 01:56:16 -07:00
|
|
|
}
|
|
|
|
|
2024-03-12 00:50:00 -07:00
|
|
|
private ScrollViewer _scrollViewer = null;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public partial class TextDiffView : UserControl
|
|
|
|
{
|
2024-03-19 22:08:01 -07:00
|
|
|
public static readonly StyledProperty<bool> UseSideBySideDiffProperty =
|
2024-07-14 00:55:15 -07:00
|
|
|
AvaloniaProperty.Register<TextDiffView, bool>(nameof(UseSideBySideDiff));
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-03-19 22:08:01 -07:00
|
|
|
public bool UseSideBySideDiff
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-03-19 22:08:01 -07:00
|
|
|
get => GetValue(UseSideBySideDiffProperty);
|
|
|
|
set => SetValue(UseSideBySideDiffProperty, value);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
public static readonly StyledProperty<TextDiffViewChunk> SelectedChunkProperty =
|
|
|
|
AvaloniaProperty.Register<TextDiffView, TextDiffViewChunk>(nameof(SelectedChunk));
|
2024-07-17 01:56:16 -07:00
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
public TextDiffViewChunk SelectedChunk
|
2024-07-17 01:56:16 -07:00
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
get => GetValue(SelectedChunkProperty);
|
|
|
|
set => SetValue(SelectedChunkProperty, value);
|
2024-07-17 01:56:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public static readonly StyledProperty<bool> IsUnstagedChangeProperty =
|
|
|
|
AvaloniaProperty.Register<TextDiffView, bool>(nameof(IsUnstagedChange));
|
|
|
|
|
|
|
|
public bool IsUnstagedChange
|
|
|
|
{
|
|
|
|
get => GetValue(IsUnstagedChangeProperty);
|
|
|
|
set => SetValue(IsUnstagedChangeProperty, value);
|
|
|
|
}
|
|
|
|
|
2024-07-18 20:01:31 -07:00
|
|
|
public static readonly StyledProperty<bool> EnableChunkSelectionProperty =
|
|
|
|
AvaloniaProperty.Register<TextDiffView, bool>(nameof(EnableChunkSelection));
|
|
|
|
|
|
|
|
public bool EnableChunkSelection
|
|
|
|
{
|
|
|
|
get => GetValue(EnableChunkSelectionProperty);
|
|
|
|
set => SetValue(EnableChunkSelectionProperty, value);
|
|
|
|
}
|
|
|
|
|
2024-06-12 06:12:45 -07:00
|
|
|
static TextDiffView()
|
2024-06-05 05:33:33 -07:00
|
|
|
{
|
2024-07-14 00:55:15 -07:00
|
|
|
UseSideBySideDiffProperty.Changed.AddClassHandler<TextDiffView>((v, _) =>
|
2024-06-12 06:12:45 -07:00
|
|
|
{
|
|
|
|
if (v.DataContext is Models.TextDiff diff)
|
|
|
|
{
|
|
|
|
diff.SyncScrollOffset = Vector.Zero;
|
|
|
|
|
|
|
|
if (v.UseSideBySideDiff)
|
2024-07-17 01:56:16 -07:00
|
|
|
v.Editor.Content = new ViewModels.TwoSideTextDiff(diff);
|
2024-06-12 06:12:45 -07:00
|
|
|
else
|
2024-07-17 01:56:16 -07:00
|
|
|
v.Editor.Content = diff;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
SelectedChunkProperty.Changed.AddClassHandler<TextDiffView>((v, _) =>
|
2024-07-17 01:56:16 -07:00
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
var chunk = v.SelectedChunk;
|
2024-07-25 19:18:28 -07:00
|
|
|
if (chunk == null)
|
2024-07-17 01:56:16 -07:00
|
|
|
{
|
|
|
|
v.Popup.IsVisible = false;
|
|
|
|
return;
|
2024-06-12 06:12:45 -07:00
|
|
|
}
|
2024-07-17 01:56:16 -07:00
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
var top = chunk.Y + 16;
|
|
|
|
var right = (chunk.Combined || !chunk.IsOldSide) ? 16 : v.Bounds.Width * 0.5f + 16;
|
|
|
|
v.Popup.Margin = new Thickness(0, top, right, 0);
|
2024-07-17 01:56:16 -07:00
|
|
|
v.Popup.IsVisible = true;
|
2024-06-12 06:12:45 -07:00
|
|
|
});
|
2024-06-05 05:33:33 -07:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public TextDiffView()
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
InitializeComponent();
|
|
|
|
}
|
|
|
|
|
2024-06-12 06:12:45 -07:00
|
|
|
protected override void OnDataContextChanged(EventArgs e)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-06-12 06:12:45 -07:00
|
|
|
base.OnDataContextChanged(e);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
if (SelectedChunk != null)
|
|
|
|
SetCurrentValue(SelectedChunkProperty, null);
|
2024-07-17 01:56:16 -07:00
|
|
|
|
2024-06-12 06:12:45 -07:00
|
|
|
var diff = DataContext as Models.TextDiff;
|
|
|
|
if (diff == null)
|
2024-06-06 05:25:16 -07:00
|
|
|
{
|
2024-07-17 01:56:16 -07:00
|
|
|
Editor.Content = null;
|
2024-06-12 06:12:45 -07:00
|
|
|
GC.Collect();
|
2024-06-06 05:25:16 -07:00
|
|
|
return;
|
2024-02-27 05:13:52 -08:00
|
|
|
}
|
2024-06-06 05:25:16 -07:00
|
|
|
|
2024-06-12 06:12:45 -07:00
|
|
|
if (UseSideBySideDiff)
|
2024-07-17 01:56:16 -07:00
|
|
|
Editor.Content = new ViewModels.TwoSideTextDiff(diff, Editor.Content as ViewModels.TwoSideTextDiff);
|
2024-06-12 06:12:45 -07:00
|
|
|
else
|
2024-07-17 01:56:16 -07:00
|
|
|
Editor.Content = diff;
|
|
|
|
|
|
|
|
IsUnstagedChange = diff.Option.IsUnstaged;
|
2024-07-18 20:01:31 -07:00
|
|
|
EnableChunkSelection = diff.Option.WorkingCopyChange != null;
|
2024-02-27 05:13:52 -08:00
|
|
|
}
|
|
|
|
|
2024-07-17 01:56:16 -07:00
|
|
|
protected override void OnPointerExited(PointerEventArgs e)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-17 01:56:16 -07:00
|
|
|
base.OnPointerExited(e);
|
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
if (SelectedChunk != null)
|
|
|
|
SetCurrentValue(SelectedChunkProperty, null);
|
2024-07-17 01:56:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private void OnStageChunk(object sender, RoutedEventArgs e)
|
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
var chunk = SelectedChunk;
|
2024-07-17 01:56:16 -07:00
|
|
|
if (chunk == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var diff = DataContext as Models.TextDiff;
|
|
|
|
if (diff == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var change = diff.Option.WorkingCopyChange;
|
|
|
|
if (change == null)
|
|
|
|
return;
|
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
var selection = diff.MakeSelection(chunk.StartIdx + 1, chunk.EndIdx + 1, chunk.Combined, chunk.IsOldSide);
|
2024-07-17 01:56:16 -07:00
|
|
|
if (!selection.HasChanges)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!selection.HasLeftChanges)
|
|
|
|
{
|
|
|
|
var workcopyView = this.FindAncestorOfType<WorkingCopy>();
|
|
|
|
if (workcopyView == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var workcopy = workcopyView.DataContext as ViewModels.WorkingCopy;
|
|
|
|
workcopy?.StageChanges(new List<Models.Change> { change });
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var repoView = this.FindAncestorOfType<Repository>();
|
|
|
|
if (repoView == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var repo = repoView.DataContext as ViewModels.Repository;
|
|
|
|
if (repo == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
repo.SetWatcherEnabled(false);
|
|
|
|
|
|
|
|
var tmpFile = Path.GetTempFileName();
|
|
|
|
if (change.WorkTree == Models.ChangeState.Untracked)
|
|
|
|
{
|
|
|
|
diff.GenerateNewPatchFromSelection(change, null, selection, false, tmpFile);
|
|
|
|
}
|
2024-07-18 01:16:58 -07:00
|
|
|
else if (chunk.Combined)
|
2024-07-17 01:56:16 -07:00
|
|
|
{
|
|
|
|
var treeGuid = new Commands.QueryStagedFileBlobGuid(diff.Repo, change.Path).Result();
|
|
|
|
diff.GeneratePatchFromSelection(change, treeGuid, selection, false, tmpFile);
|
|
|
|
}
|
2024-07-18 01:16:58 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
var treeGuid = new Commands.QueryStagedFileBlobGuid(diff.Repo, change.Path).Result();
|
|
|
|
diff.GeneratePatchFromSelectionSingleSide(change, treeGuid, selection, false, chunk.IsOldSide, tmpFile);
|
|
|
|
}
|
2024-07-17 01:56:16 -07:00
|
|
|
|
|
|
|
new Commands.Apply(diff.Repo, tmpFile, true, "nowarn", "--cache --index").Exec();
|
|
|
|
File.Delete(tmpFile);
|
|
|
|
|
|
|
|
repo.MarkWorkingCopyDirtyManually();
|
|
|
|
repo.SetWatcherEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnUnstageChunk(object sender, RoutedEventArgs e)
|
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
var chunk = SelectedChunk;
|
2024-07-17 01:56:16 -07:00
|
|
|
if (chunk == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var diff = DataContext as Models.TextDiff;
|
|
|
|
if (diff == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var change = diff.Option.WorkingCopyChange;
|
|
|
|
if (change == null)
|
|
|
|
return;
|
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
var selection = diff.MakeSelection(chunk.StartIdx + 1, chunk.EndIdx + 1, chunk.Combined, chunk.IsOldSide);
|
2024-07-17 01:56:16 -07:00
|
|
|
if (!selection.HasChanges)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!selection.HasLeftChanges)
|
|
|
|
{
|
|
|
|
var workcopyView = this.FindAncestorOfType<WorkingCopy>();
|
|
|
|
if (workcopyView == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var workcopy = workcopyView.DataContext as ViewModels.WorkingCopy;
|
|
|
|
workcopy?.UnstageChanges(new List<Models.Change> { change });
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var repoView = this.FindAncestorOfType<Repository>();
|
|
|
|
if (repoView == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var repo = repoView.DataContext as ViewModels.Repository;
|
|
|
|
if (repo == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
repo.SetWatcherEnabled(false);
|
|
|
|
|
|
|
|
var treeGuid = new Commands.QueryStagedFileBlobGuid(diff.Repo, change.Path).Result();
|
|
|
|
var tmpFile = Path.GetTempFileName();
|
|
|
|
if (change.Index == Models.ChangeState.Added)
|
|
|
|
diff.GenerateNewPatchFromSelection(change, treeGuid, selection, true, tmpFile);
|
2024-07-18 01:16:58 -07:00
|
|
|
else if (chunk.Combined)
|
2024-07-17 01:56:16 -07:00
|
|
|
diff.GeneratePatchFromSelection(change, treeGuid, selection, true, tmpFile);
|
2024-07-18 01:16:58 -07:00
|
|
|
else
|
|
|
|
diff.GeneratePatchFromSelectionSingleSide(change, treeGuid, selection, true, chunk.IsOldSide, tmpFile);
|
2024-07-17 01:56:16 -07:00
|
|
|
|
|
|
|
new Commands.Apply(diff.Repo, tmpFile, true, "nowarn", "--cache --index --reverse").Exec();
|
|
|
|
File.Delete(tmpFile);
|
|
|
|
|
|
|
|
repo.MarkWorkingCopyDirtyManually();
|
|
|
|
repo.SetWatcherEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-22 22:58:57 -07:00
|
|
|
private void OnDiscardChunk(object sender, RoutedEventArgs e)
|
2024-07-17 01:56:16 -07:00
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
var chunk = SelectedChunk;
|
2024-07-17 01:56:16 -07:00
|
|
|
if (chunk == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var diff = DataContext as Models.TextDiff;
|
|
|
|
if (diff == null)
|
|
|
|
return;
|
2024-02-27 05:13:52 -08:00
|
|
|
|
2024-07-17 01:56:16 -07:00
|
|
|
var change = diff.Option.WorkingCopyChange;
|
|
|
|
if (change == null)
|
|
|
|
return;
|
|
|
|
|
2024-07-18 01:16:58 -07:00
|
|
|
var selection = diff.MakeSelection(chunk.StartIdx + 1, chunk.EndIdx + 1, chunk.Combined, chunk.IsOldSide);
|
2024-07-17 01:56:16 -07:00
|
|
|
if (!selection.HasChanges)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!selection.HasLeftChanges)
|
|
|
|
{
|
|
|
|
var workcopyView = this.FindAncestorOfType<WorkingCopy>();
|
|
|
|
if (workcopyView == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var workcopy = workcopyView.DataContext as ViewModels.WorkingCopy;
|
|
|
|
workcopy?.Discard(new List<Models.Change> { change }, diff.Option.IsUnstaged);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var repoView = this.FindAncestorOfType<Repository>();
|
|
|
|
if (repoView == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var repo = repoView.DataContext as ViewModels.Repository;
|
|
|
|
if (repo == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
repo.SetWatcherEnabled(false);
|
|
|
|
|
|
|
|
var tmpFile = Path.GetTempFileName();
|
|
|
|
if (change.Index == Models.ChangeState.Added)
|
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
diff.GenerateNewPatchFromSelection(change, null, selection, true, tmpFile);
|
2024-07-22 22:58:57 -07:00
|
|
|
}
|
2024-07-18 01:16:58 -07:00
|
|
|
else if (chunk.Combined)
|
2024-07-17 01:56:16 -07:00
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
var treeGuid = new Commands.QueryStagedFileBlobGuid(diff.Repo, change.Path).Result();
|
2024-07-17 01:56:16 -07:00
|
|
|
diff.GeneratePatchFromSelection(change, treeGuid, selection, true, tmpFile);
|
2024-07-22 22:58:57 -07:00
|
|
|
}
|
2024-07-18 01:16:58 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
var treeGuid = new Commands.QueryStagedFileBlobGuid(diff.Repo, change.Path).Result();
|
|
|
|
diff.GeneratePatchFromSelectionSingleSide(change, treeGuid, selection, true, chunk.IsOldSide, tmpFile);
|
2024-07-17 01:56:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
new Commands.Apply(diff.Repo, tmpFile, true, "nowarn", diff.Option.IsUnstaged ? "--reverse" : "--index --reverse").Exec();
|
|
|
|
File.Delete(tmpFile);
|
|
|
|
|
|
|
|
repo.MarkWorkingCopyDirtyManually();
|
|
|
|
repo.SetWatcherEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
}
|