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-17 01:56:16 -07:00
|
|
|
public class TextViewHighlightChunk
|
|
|
|
{
|
|
|
|
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-17 19:17:40 -07:00
|
|
|
|
|
|
|
public bool ShouldReplace(TextViewHighlightChunk old)
|
|
|
|
{
|
|
|
|
if (old == null)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return Math.Abs(Y - old.Y) > 0.001 ||
|
|
|
|
Math.Abs(Height - old.Height) > 0.001 ||
|
|
|
|
StartIdx != old.StartIdx ||
|
|
|
|
EndIdx != old.EndIdx;
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-17 01:56:16 -07:00
|
|
|
public static readonly StyledProperty<TextViewHighlightChunk> HighlightChunkProperty =
|
|
|
|
AvaloniaProperty.Register<ThemedTextDiffPresenter, TextViewHighlightChunk>(nameof(HighlightChunk));
|
|
|
|
|
|
|
|
public TextViewHighlightChunk HighlightChunk
|
|
|
|
{
|
|
|
|
get => GetValue(HighlightChunkProperty);
|
|
|
|
set => SetValue(HighlightChunkProperty, value);
|
|
|
|
}
|
|
|
|
|
2024-06-12 06:12:45 -07:00
|
|
|
protected override Type StyleKeyOverride => typeof(TextEditor);
|
|
|
|
|
2024-07-14 00:55:15 -07:00
|
|
|
protected ThemedTextDiffPresenter(TextArea area, TextDocument doc) : base(area, doc)
|
2024-06-12 06:12:45 -07:00
|
|
|
{
|
|
|
|
IsReadOnly = true;
|
|
|
|
ShowLineNumbers = false;
|
|
|
|
BorderThickness = new Thickness(0);
|
|
|
|
|
|
|
|
TextArea.TextView.Margin = new Thickness(4, 0);
|
|
|
|
TextArea.TextView.Options.EnableHyperlinks = false;
|
|
|
|
TextArea.TextView.Options.EnableEmailHyperlinks = false;
|
|
|
|
}
|
|
|
|
|
2024-07-17 01:56:16 -07:00
|
|
|
public override void Render(DrawingContext context)
|
|
|
|
{
|
|
|
|
base.Render(context);
|
|
|
|
|
|
|
|
var highlightChunk = HighlightChunk;
|
|
|
|
if (highlightChunk == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var view = TextArea.TextView;
|
|
|
|
if (view == null || !view.VisualLinesValid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var color = (Color)this.FindResource("SystemAccentColor");
|
|
|
|
var brush = new SolidColorBrush(color, 0.5);
|
|
|
|
var pen = new Pen(color.ToUInt32());
|
|
|
|
|
|
|
|
var x = ((Point)view.TranslatePoint(new Point(0, 0), this)).X;
|
2024-07-17 02:14:46 -07:00
|
|
|
var rect = new Rect(x - 4, highlightChunk.Y, view.Bounds.Width + 8, highlightChunk.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);
|
|
|
|
UpdateTextMate();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnUnloaded(RoutedEventArgs e)
|
|
|
|
{
|
|
|
|
base.OnUnloaded(e);
|
|
|
|
|
|
|
|
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-17 01:56:16 -07:00
|
|
|
else if (change.Property == HighlightChunkProperty)
|
|
|
|
{
|
|
|
|
InvalidateVisual();
|
|
|
|
}
|
2024-06-12 06:12:45 -07:00
|
|
|
}
|
|
|
|
|
2024-07-17 19:17:40 -07:00
|
|
|
protected void TrySetHighlightChunk(TextViewHighlightChunk chunk)
|
|
|
|
{
|
|
|
|
var old = HighlightChunk;
|
|
|
|
if (chunk == null)
|
|
|
|
{
|
|
|
|
if (old != null)
|
|
|
|
SetCurrentValue(HighlightChunkProperty, null);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chunk.ShouldReplace(old))
|
|
|
|
SetCurrentValue(HighlightChunkProperty, chunk);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
protected IVisualLineTransformer _lineStyleTransformer = null;
|
|
|
|
}
|
|
|
|
|
2024-07-14 00:55:15 -07:00
|
|
|
public class CombinedTextDiffPresenter : ThemedTextDiffPresenter
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-14 00:55:15 -07:00
|
|
|
private class LineNumberMargin : AbstractMargin
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
|
|
|
public LineNumberMargin(CombinedTextDiffPresenter editor, bool isOldLine)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
_editor = editor;
|
|
|
|
_isOldLine = isOldLine;
|
|
|
|
ClipToBounds = true;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public override void Render(DrawingContext context)
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
if (_editor.DiffData == null)
|
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var view = TextView;
|
2024-03-17 18:37:06 -07:00
|
|
|
if (view != null && view.VisualLinesValid)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var typeface = view.CreateTypeface();
|
2024-03-17 18:37:06 -07:00
|
|
|
foreach (var line in view.VisualLines)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var index = line.FirstDocumentLine.LineNumber;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (index > _editor.DiffData.Lines.Count)
|
|
|
|
break;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var info = _editor.DiffData.Lines[index - 1];
|
|
|
|
var lineNumber = _isOldLine ? info.OldLine : info.NewLine;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (string.IsNullOrEmpty(lineNumber))
|
|
|
|
continue;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var y = line.GetTextLineVisualYPosition(line.TextLines[0], VisualYPosition.TextTop) - view.VerticalOffset;
|
|
|
|
var txt = new FormattedText(
|
|
|
|
lineNumber,
|
|
|
|
CultureInfo.CurrentCulture,
|
|
|
|
FlowDirection.LeftToRight,
|
|
|
|
typeface,
|
|
|
|
_editor.FontSize,
|
|
|
|
_editor.Foreground);
|
|
|
|
context.DrawText(txt, new Point(Bounds.Width - txt.Width, y));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
protected override Size MeasureOverride(Size availableSize)
|
|
|
|
{
|
|
|
|
if (_editor.DiffData == null || TextView == null)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
return new Size(32, 0);
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
2024-07-14 00:55:15 -07:00
|
|
|
|
|
|
|
var typeface = TextView.CreateTypeface();
|
|
|
|
var test = new FormattedText(
|
|
|
|
$"{_editor.DiffData.MaxLineNumber}",
|
|
|
|
CultureInfo.CurrentCulture,
|
|
|
|
FlowDirection.LeftToRight,
|
|
|
|
typeface,
|
|
|
|
_editor.FontSize,
|
|
|
|
Brushes.White);
|
|
|
|
return new Size(test.Width, 0);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-05-16 19:52:28 -07:00
|
|
|
protected override void OnDataContextChanged(EventArgs e)
|
|
|
|
{
|
|
|
|
base.OnDataContextChanged(e);
|
|
|
|
InvalidateMeasure();
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private readonly CombinedTextDiffPresenter _editor;
|
|
|
|
private readonly bool _isOldLine;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-07-14 00:55:15 -07:00
|
|
|
private class VerticalSeperatorMargin : AbstractMargin
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
|
|
|
public VerticalSeperatorMargin(CombinedTextDiffPresenter editor)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
_editor = editor;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public override void Render(DrawingContext context)
|
|
|
|
{
|
2024-07-14 00:55:15 -07:00
|
|
|
var pen = new Pen(_editor.LineBrush);
|
2024-02-05 23:08:37 -08:00
|
|
|
context.DrawLine(pen, new Point(0, 0), new Point(0, Bounds.Height));
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
protected override Size MeasureOverride(Size availableSize)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
return new Size(1, 0);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private readonly CombinedTextDiffPresenter _editor = null;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-07-14 00:55:15 -07:00
|
|
|
private class LineBackgroundRenderer : IBackgroundRenderer
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
public KnownLayer Layer => KnownLayer.Background;
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public LineBackgroundRenderer(CombinedTextDiffPresenter editor)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
_editor = editor;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public void Draw(TextView textView, DrawingContext drawingContext)
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
if (_editor.Document == null || !textView.VisualLinesValid)
|
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var width = textView.Bounds.Width;
|
2024-03-17 18:37:06 -07:00
|
|
|
foreach (var line in textView.VisualLines)
|
|
|
|
{
|
2024-05-24 04:15:12 -07:00
|
|
|
if (line.FirstDocumentLine == null)
|
|
|
|
continue;
|
2024-06-05 02:24:03 -07:00
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
var index = line.FirstDocumentLine.LineNumber;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (index > _editor.DiffData.Lines.Count)
|
|
|
|
break;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var info = _editor.DiffData.Lines[index - 1];
|
|
|
|
var bg = GetBrushByLineType(info.Type);
|
2024-03-31 01:54:29 -07:00
|
|
|
if (bg == null)
|
|
|
|
continue;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var y = line.GetTextLineVisualYPosition(line.TextLines[0], VisualYPosition.TextTop) - textView.VerticalOffset;
|
|
|
|
drawingContext.DrawRectangle(bg, null, new Rect(0, y, width, line.Height));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private IBrush GetBrushByLineType(Models.TextDiffLineType type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
case Models.TextDiffLineType.None:
|
2024-06-12 06:12:45 -07:00
|
|
|
return _editor.EmptyContentBackground;
|
2024-03-31 01:54:29 -07:00
|
|
|
case Models.TextDiffLineType.Added:
|
2024-06-12 06:12:45 -07:00
|
|
|
return _editor.AddedContentBackground;
|
2024-03-31 01:54:29 -07:00
|
|
|
case Models.TextDiffLineType.Deleted:
|
2024-06-12 06:12:45 -07:00
|
|
|
return _editor.DeletedContentBackground;
|
2024-03-31 01:54:29 -07:00
|
|
|
default:
|
|
|
|
return null;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private readonly CombinedTextDiffPresenter _editor = null;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-07-14 00:55:15 -07:00
|
|
|
private class LineStyleTransformer : DocumentColorizingTransformer
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-03-20 03:27:48 -07:00
|
|
|
public LineStyleTransformer(CombinedTextDiffPresenter editor)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
_editor = editor;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
protected override void ColorizeLine(DocumentLine line)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var idx = line.LineNumber;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (idx > _editor.DiffData.Lines.Count)
|
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var info = _editor.DiffData.Lines[idx - 1];
|
2024-03-17 18:37:06 -07:00
|
|
|
if (info.Type == Models.TextDiffLineType.Indicator)
|
|
|
|
{
|
|
|
|
ChangeLinePart(line.Offset, line.EndOffset, v =>
|
|
|
|
{
|
2024-06-12 06:12:45 -07:00
|
|
|
v.TextRunProperties.SetForegroundBrush(_editor.IndicatorForeground);
|
2024-03-21 03:08:24 -07:00
|
|
|
v.TextRunProperties.SetTypeface(new Typeface(_editor.FontFamily, FontStyle.Italic));
|
2024-02-05 23:08:37 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
if (info.Highlights.Count > 0)
|
|
|
|
{
|
2024-06-12 06:12:45 -07:00
|
|
|
var bg = info.Type == Models.TextDiffLineType.Added ? _editor.AddedHighlightBrush : _editor.DeletedHighlightBrush;
|
2024-03-17 18:37:06 -07:00
|
|
|
foreach (var highlight in info.Highlights)
|
|
|
|
{
|
|
|
|
ChangeLinePart(line.Offset + highlight.Start, line.Offset + highlight.Start + highlight.Count, v =>
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
v.TextRunProperties.SetBackgroundBrush(bg);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private readonly CombinedTextDiffPresenter _editor;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-06-12 06:12:45 -07:00
|
|
|
public Models.TextDiff DiffData => DataContext as Models.TextDiff;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public CombinedTextDiffPresenter() : base(new TextArea(), new TextDocument())
|
|
|
|
{
|
2024-03-20 03:27:48 -07:00
|
|
|
_lineStyleTransformer = new LineStyleTransformer(this);
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
TextArea.LeftMargins.Add(new LineNumberMargin(this, true) { Margin = new Thickness(8, 0) });
|
|
|
|
TextArea.LeftMargins.Add(new VerticalSeperatorMargin(this));
|
|
|
|
TextArea.LeftMargins.Add(new LineNumberMargin(this, false) { Margin = new Thickness(8, 0) });
|
|
|
|
TextArea.LeftMargins.Add(new VerticalSeperatorMargin(this));
|
|
|
|
|
|
|
|
TextArea.TextView.BackgroundRenderers.Add(new LineBackgroundRenderer(this));
|
2024-03-20 05:17:20 -07:00
|
|
|
TextArea.TextView.LineTransformers.Add(_lineStyleTransformer);
|
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-14 00:55:15 -07:00
|
|
|
scroller?.Bind(ScrollViewer.OffsetProperty, new Binding("SyncScrollOffset", BindingMode.TwoWay));
|
2024-06-12 06:12:45 -07:00
|
|
|
}
|
2024-03-04 23:53:38 -08:00
|
|
|
|
2024-06-12 06:12:45 -07:00
|
|
|
protected override void OnLoaded(RoutedEventArgs e)
|
|
|
|
{
|
|
|
|
base.OnLoaded(e);
|
2024-03-20 03:27:48 -07:00
|
|
|
TextArea.TextView.ContextRequested += OnTextViewContextRequested;
|
2024-07-17 01:56:16 -07:00
|
|
|
TextArea.TextView.PointerMoved += OnTextViewPointerMoved;
|
|
|
|
TextArea.TextView.PointerWheelChanged += OnTextViewPointerWheelChanged;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
protected override void OnUnloaded(RoutedEventArgs e)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
base.OnUnloaded(e);
|
|
|
|
TextArea.TextView.ContextRequested -= OnTextViewContextRequested;
|
2024-07-17 01:56:16 -07:00
|
|
|
TextArea.TextView.PointerMoved -= OnTextViewPointerMoved;
|
|
|
|
TextArea.TextView.PointerWheelChanged -= OnTextViewPointerWheelChanged;
|
2024-06-12 06:12:45 -07:00
|
|
|
}
|
2024-03-20 03:27:48 -07:00
|
|
|
|
2024-06-12 06:12:45 -07:00
|
|
|
protected override void OnDataContextChanged(EventArgs e)
|
|
|
|
{
|
|
|
|
base.OnDataContextChanged(e);
|
|
|
|
|
|
|
|
var textDiff = DataContext as Models.TextDiff;
|
|
|
|
if (textDiff != null)
|
2024-03-20 03:27:48 -07:00
|
|
|
{
|
2024-06-12 06:12:45 -07:00
|
|
|
var builder = new StringBuilder();
|
|
|
|
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));
|
|
|
|
builder.Append($"...({line.Content.Length - 1000} character trimmed)");
|
|
|
|
builder.AppendLine();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
builder.AppendLine(line.Content);
|
|
|
|
}
|
|
|
|
}
|
2024-06-12 06:12:45 -07:00
|
|
|
|
|
|
|
Text = builder.ToString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Text = string.Empty;
|
2024-03-20 03:27:48 -07:00
|
|
|
}
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
GC.Collect();
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs e)
|
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
var selection = TextArea.Selection;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (selection.IsEmpty)
|
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-02-27 05:13:52 -08:00
|
|
|
var menu = new ContextMenu();
|
|
|
|
var parentView = this.FindAncestorOfType<TextDiffView>();
|
2024-03-17 18:37:06 -07:00
|
|
|
if (parentView != null)
|
2024-02-27 05:13:52 -08:00
|
|
|
parentView.FillContextMenuForWorkingCopyChange(menu, selection.StartPosition.Line, selection.EndPosition.Line, false);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var copy = new MenuItem();
|
|
|
|
copy.Header = App.Text("Copy");
|
2024-02-27 05:13:52 -08:00
|
|
|
copy.Icon = App.CreateMenuIcon("Icons.Copy");
|
2024-07-14 00:55:15 -07:00
|
|
|
copy.Click += (_, ev) =>
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
App.CopyText(SelectedText);
|
2024-02-05 23:08:37 -08:00
|
|
|
ev.Handled = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
menu.Items.Add(copy);
|
2024-05-23 06:24:22 -07:00
|
|
|
|
|
|
|
TextArea.TextView.OpenContextMenu(menu);
|
2024-02-05 23:08:37 -08:00
|
|
|
e.Handled = true;
|
|
|
|
}
|
2024-07-17 01:56:16 -07:00
|
|
|
|
|
|
|
private void OnTextViewPointerMoved(object sender, PointerEventArgs e)
|
|
|
|
{
|
|
|
|
if (DiffData.Option.WorkingCopyChange == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(SelectedText))
|
|
|
|
{
|
2024-07-17 19:17:40 -07:00
|
|
|
TrySetHighlightChunk(null);
|
2024-07-17 01:56:16 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sender is TextView { VisualLinesValid: true } view)
|
|
|
|
{
|
|
|
|
var y = e.GetPosition(view).Y + view.VerticalOffset;
|
|
|
|
var lineIdx = -1;
|
|
|
|
foreach (var line in view.VisualLines)
|
|
|
|
{
|
|
|
|
var index = line.FirstDocumentLine.LineNumber;
|
|
|
|
if (index > DiffData.Lines.Count)
|
|
|
|
break;
|
|
|
|
|
|
|
|
var endY = line.GetTextLineVisualYPosition(line.TextLines[^1], VisualYPosition.TextBottom);
|
|
|
|
if (endY > y)
|
|
|
|
{
|
|
|
|
lineIdx = index - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lineIdx == -1)
|
|
|
|
{
|
2024-07-17 19:17:40 -07:00
|
|
|
TrySetHighlightChunk(null);
|
2024-07-17 01:56:16 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-17 02:10:32 -07:00
|
|
|
var (startIdx, endIdx) = FindRangeByIndex(DiffData.Lines, lineIdx);
|
2024-07-17 01:56:16 -07:00
|
|
|
if (startIdx == -1)
|
|
|
|
{
|
2024-07-17 19:17:40 -07:00
|
|
|
TrySetHighlightChunk(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-17 19:17:40 -07:00
|
|
|
TrySetHighlightChunk(new TextViewHighlightChunk()
|
2024-07-17 01:56:16 -07:00
|
|
|
{
|
|
|
|
Y = rectStartY,
|
|
|
|
Height = rectEndY - rectStartY,
|
|
|
|
StartIdx = startIdx,
|
|
|
|
EndIdx = endIdx,
|
2024-07-17 19:17:40 -07:00
|
|
|
});
|
2024-07-17 01:56:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnTextViewPointerWheelChanged(object sender, PointerWheelEventArgs e)
|
|
|
|
{
|
|
|
|
if (DiffData.Option.WorkingCopyChange == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// The offset of TextView has not been updated here. Post a event to next frame.
|
|
|
|
Dispatcher.UIThread.Post(() => OnTextViewPointerMoved(sender, e));
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-07-14 00:55:15 -07:00
|
|
|
public class SingleSideTextDiffPresenter : ThemedTextDiffPresenter
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-14 00:55:15 -07:00
|
|
|
private class LineNumberMargin : AbstractMargin
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
|
|
|
public LineNumberMargin(SingleSideTextDiffPresenter editor)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
_editor = editor;
|
|
|
|
ClipToBounds = true;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public override void Render(DrawingContext context)
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
if (_editor.DiffData == null)
|
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var view = TextView;
|
2024-03-17 18:37:06 -07:00
|
|
|
if (view != null && view.VisualLinesValid)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var typeface = view.CreateTypeface();
|
|
|
|
var infos = _editor.IsOld ? _editor.DiffData.Old : _editor.DiffData.New;
|
2024-03-17 18:37:06 -07:00
|
|
|
foreach (var line in view.VisualLines)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var index = line.FirstDocumentLine.LineNumber;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (index > infos.Count)
|
|
|
|
break;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var info = infos[index - 1];
|
|
|
|
var lineNumber = _editor.IsOld ? info.OldLine : info.NewLine;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (string.IsNullOrEmpty(lineNumber))
|
|
|
|
continue;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var y = line.GetTextLineVisualYPosition(line.TextLines[0], VisualYPosition.TextTop) - view.VerticalOffset;
|
|
|
|
var txt = new FormattedText(
|
|
|
|
lineNumber,
|
|
|
|
CultureInfo.CurrentCulture,
|
|
|
|
FlowDirection.LeftToRight,
|
|
|
|
typeface,
|
|
|
|
_editor.FontSize,
|
|
|
|
_editor.Foreground);
|
|
|
|
context.DrawText(txt, new Point(Bounds.Width - txt.Width, y));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
protected override Size MeasureOverride(Size availableSize)
|
|
|
|
{
|
|
|
|
if (_editor.DiffData == null || TextView == null)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
return new Size(32, 0);
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
2024-07-14 00:55:15 -07:00
|
|
|
|
|
|
|
var typeface = TextView.CreateTypeface();
|
|
|
|
var test = new FormattedText(
|
|
|
|
$"{_editor.DiffData.MaxLineNumber}",
|
|
|
|
CultureInfo.CurrentCulture,
|
|
|
|
FlowDirection.LeftToRight,
|
|
|
|
typeface,
|
|
|
|
_editor.FontSize,
|
|
|
|
Brushes.White);
|
|
|
|
return new Size(test.Width, 0);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-05-16 19:52:28 -07:00
|
|
|
protected override void OnDataContextChanged(EventArgs e)
|
|
|
|
{
|
|
|
|
base.OnDataContextChanged(e);
|
|
|
|
InvalidateMeasure();
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private readonly SingleSideTextDiffPresenter _editor;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-07-14 00:55:15 -07:00
|
|
|
private class VerticalSeperatorMargin : AbstractMargin
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
|
|
|
public VerticalSeperatorMargin(SingleSideTextDiffPresenter editor)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
_editor = editor;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public override void Render(DrawingContext context)
|
|
|
|
{
|
2024-07-14 00:55:15 -07:00
|
|
|
var pen = new Pen(_editor.LineBrush);
|
2024-02-05 23:08:37 -08:00
|
|
|
context.DrawLine(pen, new Point(0, 0), new Point(0, Bounds.Height));
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
protected override Size MeasureOverride(Size availableSize)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
return new Size(1, 0);
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private readonly SingleSideTextDiffPresenter _editor = null;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-07-14 00:55:15 -07:00
|
|
|
private class LineBackgroundRenderer : IBackgroundRenderer
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
public KnownLayer Layer => KnownLayer.Background;
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public LineBackgroundRenderer(SingleSideTextDiffPresenter editor)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
_editor = editor;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public void Draw(TextView textView, DrawingContext drawingContext)
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
if (_editor.Document == null || !textView.VisualLinesValid)
|
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var width = textView.Bounds.Width;
|
|
|
|
var infos = _editor.IsOld ? _editor.DiffData.Old : _editor.DiffData.New;
|
2024-03-17 18:37:06 -07:00
|
|
|
foreach (var line in textView.VisualLines)
|
|
|
|
{
|
2024-05-24 04:15:12 -07:00
|
|
|
if (line.FirstDocumentLine == null)
|
|
|
|
continue;
|
2024-06-05 02:24:03 -07:00
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
var index = line.FirstDocumentLine.LineNumber;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (index > infos.Count)
|
|
|
|
break;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var info = infos[index - 1];
|
|
|
|
var bg = GetBrushByLineType(info.Type);
|
2024-03-31 01:54:29 -07:00
|
|
|
if (bg == null)
|
|
|
|
continue;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var y = line.GetTextLineVisualYPosition(line.TextLines[0], VisualYPosition.TextTop) - textView.VerticalOffset;
|
|
|
|
drawingContext.DrawRectangle(bg, null, new Rect(0, y, width, line.Height));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private IBrush GetBrushByLineType(Models.TextDiffLineType type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
case Models.TextDiffLineType.None:
|
2024-06-12 06:12:45 -07:00
|
|
|
return _editor.EmptyContentBackground;
|
2024-03-31 01:54:29 -07:00
|
|
|
case Models.TextDiffLineType.Added:
|
2024-06-12 06:12:45 -07:00
|
|
|
return _editor.AddedContentBackground;
|
2024-03-31 01:54:29 -07:00
|
|
|
case Models.TextDiffLineType.Deleted:
|
2024-06-12 06:12:45 -07:00
|
|
|
return _editor.DeletedContentBackground;
|
2024-03-31 01:54:29 -07:00
|
|
|
default:
|
|
|
|
return null;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private readonly SingleSideTextDiffPresenter _editor = null;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-07-14 00:55:15 -07:00
|
|
|
private class LineStyleTransformer : DocumentColorizingTransformer
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-03-20 03:27:48 -07:00
|
|
|
public LineStyleTransformer(SingleSideTextDiffPresenter editor)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
_editor = editor;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
protected override void ColorizeLine(DocumentLine line)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
var infos = _editor.IsOld ? _editor.DiffData.Old : _editor.DiffData.New;
|
|
|
|
var idx = line.LineNumber;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (idx > infos.Count)
|
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var info = infos[idx - 1];
|
2024-03-17 18:37:06 -07:00
|
|
|
if (info.Type == Models.TextDiffLineType.Indicator)
|
|
|
|
{
|
|
|
|
ChangeLinePart(line.Offset, line.EndOffset, v =>
|
|
|
|
{
|
2024-06-12 06:12:45 -07:00
|
|
|
v.TextRunProperties.SetForegroundBrush(_editor.IndicatorForeground);
|
2024-03-21 03:08:24 -07:00
|
|
|
v.TextRunProperties.SetTypeface(new Typeface(_editor.FontFamily, FontStyle.Italic));
|
2024-02-05 23:08:37 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
if (info.Highlights.Count > 0)
|
|
|
|
{
|
2024-06-12 06:12:45 -07:00
|
|
|
var bg = info.Type == Models.TextDiffLineType.Added ? _editor.AddedHighlightBrush : _editor.DeletedHighlightBrush;
|
2024-03-17 18:37:06 -07:00
|
|
|
foreach (var highlight in info.Highlights)
|
|
|
|
{
|
|
|
|
ChangeLinePart(line.Offset + highlight.Start, line.Offset + highlight.Start + highlight.Count, v =>
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
v.TextRunProperties.SetBackgroundBrush(bg);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private readonly SingleSideTextDiffPresenter _editor;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public static readonly StyledProperty<bool> IsOldProperty =
|
|
|
|
AvaloniaProperty.Register<SingleSideTextDiffPresenter, bool>(nameof(IsOld));
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public bool IsOld
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
get => GetValue(IsOldProperty);
|
|
|
|
set => SetValue(IsOldProperty, value);
|
|
|
|
}
|
|
|
|
|
2024-06-12 06:12:45 -07:00
|
|
|
public ViewModels.TwoSideTextDiff DiffData => DataContext as ViewModels.TwoSideTextDiff;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
public SingleSideTextDiffPresenter() : base(new TextArea(), new TextDocument())
|
|
|
|
{
|
2024-03-20 03:27:48 -07:00
|
|
|
_lineStyleTransformer = new LineStyleTransformer(this);
|
|
|
|
|
|
|
|
TextArea.LeftMargins.Add(new LineNumberMargin(this) { Margin = new Thickness(8, 0) });
|
|
|
|
TextArea.LeftMargins.Add(new VerticalSeperatorMargin(this));
|
|
|
|
TextArea.TextView.BackgroundRenderers.Add(new LineBackgroundRenderer(this));
|
2024-03-20 05:17:20 -07:00
|
|
|
TextArea.TextView.LineTransformers.Add(_lineStyleTransformer);
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
protected override void OnLoaded(RoutedEventArgs e)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
base.OnLoaded(e);
|
|
|
|
|
2024-03-12 00:50:00 -07:00
|
|
|
_scrollViewer = this.FindDescendantOfType<ScrollViewer>();
|
2024-03-17 18:37:06 -07:00
|
|
|
if (_scrollViewer != null)
|
|
|
|
{
|
2024-03-12 00:50:00 -07:00
|
|
|
_scrollViewer.ScrollChanged += OnTextViewScrollChanged;
|
2024-06-12 06:12:45 -07:00
|
|
|
_scrollViewer.Bind(ScrollViewer.OffsetProperty, new Binding("SyncScrollOffset", BindingMode.OneWay));
|
2024-03-12 00:50:00 -07:00
|
|
|
}
|
|
|
|
|
2024-04-28 01:46:39 -07:00
|
|
|
TextArea.PointerWheelChanged += OnTextAreaPointerWheelChanged;
|
2024-03-20 03:27:48 -07:00
|
|
|
TextArea.TextView.ContextRequested += OnTextViewContextRequested;
|
2024-07-17 01:56:16 -07:00
|
|
|
TextArea.TextView.PointerMoved += OnTextViewPointerMoved;
|
|
|
|
TextArea.TextView.PointerWheelChanged += OnTextViewPointerWheelChanged;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
protected override void OnUnloaded(RoutedEventArgs e)
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
base.OnUnloaded(e);
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
if (_scrollViewer != null)
|
|
|
|
{
|
2024-03-12 00:50:00 -07:00
|
|
|
_scrollViewer.ScrollChanged -= OnTextViewScrollChanged;
|
|
|
|
_scrollViewer = null;
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
2024-03-12 00:50:00 -07:00
|
|
|
|
2024-04-28 01:46:39 -07:00
|
|
|
TextArea.PointerWheelChanged -= OnTextAreaPointerWheelChanged;
|
2024-02-05 23:08:37 -08:00
|
|
|
TextArea.TextView.ContextRequested -= OnTextViewContextRequested;
|
2024-07-17 01:56:16 -07:00
|
|
|
TextArea.TextView.PointerMoved -= OnTextViewPointerMoved;
|
|
|
|
TextArea.TextView.PointerWheelChanged -= OnTextViewPointerWheelChanged;
|
2024-03-20 03:27:48 -07:00
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
GC.Collect();
|
|
|
|
}
|
|
|
|
|
2024-06-12 06:12:45 -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)
|
2024-07-03 00:34:29 -07:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2024-06-12 06:12:45 -07:00
|
|
|
|
|
|
|
Text = builder.ToString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Text = string.Empty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-28 01:46:39 -07:00
|
|
|
private void OnTextAreaPointerWheelChanged(object sender, PointerWheelEventArgs e)
|
|
|
|
{
|
2024-04-29 02:55:38 -07:00
|
|
|
if (!TextArea.IsFocused)
|
|
|
|
Focus();
|
2024-04-28 01:46:39 -07:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void OnTextViewScrollChanged(object sender, ScrollChangedEventArgs e)
|
|
|
|
{
|
2024-06-12 06:12:45 -07:00
|
|
|
if (TextArea.IsFocused && DataContext is ViewModels.TwoSideTextDiff diff)
|
|
|
|
diff.SyncScrollOffset = _scrollViewer.Offset;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs e)
|
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
var selection = TextArea.Selection;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (selection.IsEmpty)
|
|
|
|
return;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
2024-02-27 05:13:52 -08:00
|
|
|
var menu = new ContextMenu();
|
|
|
|
var parentView = this.FindAncestorOfType<TextDiffView>();
|
2024-03-17 18:37:06 -07:00
|
|
|
if (parentView != null)
|
2024-02-27 05:13:52 -08:00
|
|
|
parentView.FillContextMenuForWorkingCopyChange(menu, selection.StartPosition.Line, selection.EndPosition.Line, IsOld);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
var copy = new MenuItem();
|
|
|
|
copy.Header = App.Text("Copy");
|
2024-02-27 05:13:52 -08:00
|
|
|
copy.Icon = App.CreateMenuIcon("Icons.Copy");
|
2024-07-14 00:55:15 -07:00
|
|
|
copy.Click += (_, ev) =>
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
App.CopyText(SelectedText);
|
2024-02-05 23:08:37 -08:00
|
|
|
ev.Handled = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
menu.Items.Add(copy);
|
2024-06-05 02:24:03 -07:00
|
|
|
|
2024-05-23 06:24:22 -07:00
|
|
|
TextArea.TextView.OpenContextMenu(menu);
|
2024-02-05 23:08:37 -08:00
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
|
2024-07-17 01:56:16 -07:00
|
|
|
private void OnTextViewPointerMoved(object sender, PointerEventArgs e)
|
|
|
|
{
|
|
|
|
if (DiffData.Option.WorkingCopyChange == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(SelectedText))
|
|
|
|
{
|
2024-07-17 19:17:40 -07:00
|
|
|
TrySetHighlightChunk(null);
|
2024-07-17 01:56:16 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var parentView = this.FindAncestorOfType<TextDiffView>();
|
|
|
|
if (parentView == null || parentView.DataContext == null)
|
|
|
|
{
|
2024-07-17 19:17:40 -07:00
|
|
|
TrySetHighlightChunk(null);
|
2024-07-17 01:56:16 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var textDiff = parentView.DataContext as Models.TextDiff;
|
|
|
|
if (sender is TextView { VisualLinesValid: true } view)
|
|
|
|
{
|
|
|
|
var y = e.GetPosition(view).Y + view.VerticalOffset;
|
|
|
|
var lineIdx = -1;
|
|
|
|
var lines = IsOld ? DiffData.Old : DiffData.New;
|
|
|
|
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-17 19:17:40 -07:00
|
|
|
TrySetHighlightChunk(null);
|
2024-07-17 01:56:16 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var (startIdx, endIdx) = FindRangeByIndex(lines, lineIdx);
|
|
|
|
if (startIdx == -1)
|
|
|
|
{
|
2024-07-17 19:17:40 -07:00
|
|
|
TrySetHighlightChunk(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-17 19:17:40 -07:00
|
|
|
TrySetHighlightChunk(new TextViewHighlightChunk()
|
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-17 01:56:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnTextViewPointerWheelChanged(object sender, PointerWheelEventArgs e)
|
|
|
|
{
|
|
|
|
if (DiffData.Option.WorkingCopyChange == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// The offset of TextView has not been updated here. Post a event to next frame.
|
|
|
|
Dispatcher.UIThread.Post(() => OnTextViewPointerMoved(sender, e));
|
|
|
|
}
|
|
|
|
|
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-17 01:56:16 -07:00
|
|
|
public static readonly StyledProperty<TextViewHighlightChunk> HighlightChunkProperty =
|
|
|
|
AvaloniaProperty.Register<TextDiffView, TextViewHighlightChunk>(nameof(HighlightChunk));
|
|
|
|
|
|
|
|
public TextViewHighlightChunk HighlightChunk
|
|
|
|
{
|
|
|
|
get => GetValue(HighlightChunkProperty);
|
|
|
|
set => SetValue(HighlightChunkProperty, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static readonly StyledProperty<bool> IsUnstagedChangeProperty =
|
|
|
|
AvaloniaProperty.Register<TextDiffView, bool>(nameof(IsUnstagedChange));
|
|
|
|
|
|
|
|
public bool IsUnstagedChange
|
|
|
|
{
|
|
|
|
get => GetValue(IsUnstagedChangeProperty);
|
|
|
|
set => SetValue(IsUnstagedChangeProperty, 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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
HighlightChunkProperty.Changed.AddClassHandler<TextDiffView>((v, _) =>
|
|
|
|
{
|
|
|
|
if (v.HighlightChunk == null)
|
|
|
|
{
|
|
|
|
v.Popup.IsVisible = false;
|
|
|
|
return;
|
2024-06-12 06:12:45 -07:00
|
|
|
}
|
2024-07-17 01:56:16 -07:00
|
|
|
|
|
|
|
var y = v.HighlightChunk.Y + 16;
|
|
|
|
v.Popup.Margin = new Thickness(0, y, 16, 0);
|
|
|
|
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-03-17 18:37:06 -07:00
|
|
|
public void FillContextMenuForWorkingCopyChange(ContextMenu menu, int startLine, int endLine, bool isOldSide)
|
|
|
|
{
|
2024-06-12 06:12:45 -07:00
|
|
|
var diff = DataContext as Models.TextDiff;
|
|
|
|
if (diff == null)
|
|
|
|
return;
|
|
|
|
|
2024-07-17 01:56:16 -07:00
|
|
|
var change = diff.Option.WorkingCopyChange;
|
2024-03-31 01:54:29 -07:00
|
|
|
if (change == null)
|
|
|
|
return;
|
2024-02-27 05:13:52 -08:00
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
if (startLine > endLine)
|
2024-07-14 00:55:15 -07:00
|
|
|
(startLine, endLine) = (endLine, startLine);
|
2024-02-27 05:13:52 -08:00
|
|
|
|
2024-07-17 01:56:16 -07:00
|
|
|
if (UseSideBySideDiff)
|
|
|
|
(startLine, endLine) = GetUnifiedRange(diff, startLine, endLine, isOldSide);
|
|
|
|
|
2024-07-17 20:26:47 -07:00
|
|
|
var selection = MakeSelection(diff, startLine, endLine, !UseSideBySideDiff, isOldSide);
|
2024-03-31 01:54:29 -07:00
|
|
|
if (!selection.HasChanges)
|
|
|
|
return;
|
2024-02-27 05:13:52 -08:00
|
|
|
|
|
|
|
// If all changes has been selected the use method provided by ViewModels.WorkingCopy.
|
|
|
|
// Otherwise, use `git apply`
|
2024-03-17 18:37:06 -07:00
|
|
|
if (!selection.HasLeftChanges)
|
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
var workcopyView = this.FindAncestorOfType<WorkingCopy>();
|
2024-03-31 01:54:29 -07:00
|
|
|
if (workcopyView == null)
|
|
|
|
return;
|
2024-02-27 05:13:52 -08:00
|
|
|
|
2024-07-17 01:56:16 -07:00
|
|
|
if (diff.Option.IsUnstaged)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
var stage = new MenuItem();
|
|
|
|
stage.Header = App.Text("FileCM.StageSelectedLines");
|
|
|
|
stage.Icon = App.CreateMenuIcon("Icons.File.Add");
|
2024-03-17 18:37:06 -07:00
|
|
|
stage.Click += (_, e) =>
|
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
var workcopy = workcopyView.DataContext as ViewModels.WorkingCopy;
|
2024-07-14 00:55:15 -07:00
|
|
|
workcopy?.StageChanges(new List<Models.Change> { change });
|
2024-02-27 05:13:52 -08:00
|
|
|
e.Handled = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
var discard = new MenuItem();
|
|
|
|
discard.Header = App.Text("FileCM.DiscardSelectedLines");
|
|
|
|
discard.Icon = App.CreateMenuIcon("Icons.Undo");
|
2024-03-17 18:37:06 -07:00
|
|
|
discard.Click += (_, e) =>
|
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
var workcopy = workcopyView.DataContext as ViewModels.WorkingCopy;
|
2024-07-14 00:55:15 -07:00
|
|
|
workcopy?.Discard(new List<Models.Change> { change }, true);
|
2024-02-27 05:13:52 -08:00
|
|
|
e.Handled = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
menu.Items.Add(stage);
|
|
|
|
menu.Items.Add(discard);
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
var unstage = new MenuItem();
|
|
|
|
unstage.Header = App.Text("FileCM.UnstageSelectedLines");
|
|
|
|
unstage.Icon = App.CreateMenuIcon("Icons.File.Remove");
|
2024-03-17 18:37:06 -07:00
|
|
|
unstage.Click += (_, e) =>
|
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
var workcopy = workcopyView.DataContext as ViewModels.WorkingCopy;
|
2024-07-14 00:55:15 -07:00
|
|
|
workcopy?.UnstageChanges(new List<Models.Change> { change });
|
2024-02-27 05:13:52 -08:00
|
|
|
e.Handled = true;
|
|
|
|
};
|
2024-02-28 18:59:59 -08:00
|
|
|
|
|
|
|
var discard = new MenuItem();
|
|
|
|
discard.Header = App.Text("FileCM.DiscardSelectedLines");
|
|
|
|
discard.Icon = App.CreateMenuIcon("Icons.Undo");
|
2024-03-17 18:37:06 -07:00
|
|
|
discard.Click += (_, e) =>
|
|
|
|
{
|
2024-02-29 19:34:32 -08:00
|
|
|
var workcopy = workcopyView.DataContext as ViewModels.WorkingCopy;
|
2024-07-14 00:55:15 -07:00
|
|
|
workcopy?.Discard(new List<Models.Change> { change }, false);
|
2024-02-28 18:59:59 -08:00
|
|
|
e.Handled = true;
|
|
|
|
};
|
|
|
|
|
2024-02-27 05:13:52 -08:00
|
|
|
menu.Items.Add(unstage);
|
2024-02-28 18:59:59 -08:00
|
|
|
menu.Items.Add(discard);
|
2024-02-27 05:13:52 -08:00
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
var repoView = this.FindAncestorOfType<Repository>();
|
2024-03-31 01:54:29 -07:00
|
|
|
if (repoView == null)
|
|
|
|
return;
|
2024-02-27 05:13:52 -08:00
|
|
|
|
2024-07-17 01:56:16 -07:00
|
|
|
if (diff.Option.IsUnstaged)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
var stage = new MenuItem();
|
|
|
|
stage.Header = App.Text("FileCM.StageSelectedLines");
|
|
|
|
stage.Icon = App.CreateMenuIcon("Icons.File.Add");
|
2024-03-17 18:37:06 -07:00
|
|
|
stage.Click += (_, e) =>
|
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
var repo = repoView.DataContext as ViewModels.Repository;
|
2024-07-14 00:55:15 -07:00
|
|
|
if (repo == null)
|
|
|
|
return;
|
|
|
|
|
2024-02-27 05:13:52 -08:00
|
|
|
repo.SetWatcherEnabled(false);
|
|
|
|
|
|
|
|
var tmpFile = Path.GetTempFileName();
|
2024-03-17 18:37:06 -07:00
|
|
|
if (change.WorkTree == Models.ChangeState.Untracked)
|
|
|
|
{
|
2024-06-12 06:12:45 -07:00
|
|
|
diff.GenerateNewPatchFromSelection(change, null, selection, false, tmpFile);
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
2024-03-19 22:08:01 -07:00
|
|
|
else if (!UseSideBySideDiff)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-17 01:56:16 -07:00
|
|
|
var treeGuid = new Commands.QueryStagedFileBlobGuid(diff.Repo, change.Path).Result();
|
2024-06-12 06:12:45 -07:00
|
|
|
diff.GeneratePatchFromSelection(change, treeGuid, selection, false, tmpFile);
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-07-17 01:56:16 -07:00
|
|
|
var treeGuid = new Commands.QueryStagedFileBlobGuid(diff.Repo, change.Path).Result();
|
2024-06-12 06:12:45 -07:00
|
|
|
diff.GeneratePatchFromSelectionSingleSide(change, treeGuid, selection, false, isOldSide, tmpFile);
|
2024-02-27 05:13:52 -08:00
|
|
|
}
|
|
|
|
|
2024-07-17 01:56:16 -07:00
|
|
|
new Commands.Apply(diff.Repo, tmpFile, true, "nowarn", "--cache --index").Exec();
|
2024-02-27 05:13:52 -08:00
|
|
|
File.Delete(tmpFile);
|
|
|
|
|
2024-03-07 17:57:29 -08:00
|
|
|
repo.MarkWorkingCopyDirtyManually();
|
2024-02-27 05:13:52 -08:00
|
|
|
repo.SetWatcherEnabled(true);
|
|
|
|
e.Handled = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
var discard = new MenuItem();
|
|
|
|
discard.Header = App.Text("FileCM.DiscardSelectedLines");
|
|
|
|
discard.Icon = App.CreateMenuIcon("Icons.Undo");
|
2024-03-17 18:37:06 -07:00
|
|
|
discard.Click += (_, e) =>
|
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
var repo = repoView.DataContext as ViewModels.Repository;
|
2024-07-14 00:55:15 -07:00
|
|
|
if (repo == null)
|
|
|
|
return;
|
|
|
|
|
2024-02-27 05:13:52 -08:00
|
|
|
repo.SetWatcherEnabled(false);
|
|
|
|
|
|
|
|
var tmpFile = Path.GetTempFileName();
|
2024-03-17 18:37:06 -07:00
|
|
|
if (change.WorkTree == Models.ChangeState.Untracked)
|
|
|
|
{
|
2024-06-12 06:12:45 -07:00
|
|
|
diff.GenerateNewPatchFromSelection(change, null, selection, true, tmpFile);
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
2024-03-19 22:08:01 -07:00
|
|
|
else if (!UseSideBySideDiff)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-17 01:56:16 -07:00
|
|
|
var treeGuid = new Commands.QueryStagedFileBlobGuid(diff.Repo, change.Path).Result();
|
2024-06-12 06:12:45 -07:00
|
|
|
diff.GeneratePatchFromSelection(change, treeGuid, selection, true, tmpFile);
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-07-17 01:56:16 -07:00
|
|
|
var treeGuid = new Commands.QueryStagedFileBlobGuid(diff.Repo, change.Path).Result();
|
2024-06-12 06:12:45 -07:00
|
|
|
diff.GeneratePatchFromSelectionSingleSide(change, treeGuid, selection, true, isOldSide, tmpFile);
|
2024-02-27 05:13:52 -08:00
|
|
|
}
|
|
|
|
|
2024-07-17 01:56:16 -07:00
|
|
|
new Commands.Apply(diff.Repo, tmpFile, true, "nowarn", "--reverse").Exec();
|
2024-02-27 05:13:52 -08:00
|
|
|
File.Delete(tmpFile);
|
|
|
|
|
2024-03-07 17:57:29 -08:00
|
|
|
repo.MarkWorkingCopyDirtyManually();
|
2024-02-27 05:13:52 -08:00
|
|
|
repo.SetWatcherEnabled(true);
|
|
|
|
e.Handled = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
menu.Items.Add(stage);
|
|
|
|
menu.Items.Add(discard);
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
var unstage = new MenuItem();
|
|
|
|
unstage.Header = App.Text("FileCM.UnstageSelectedLines");
|
|
|
|
unstage.Icon = App.CreateMenuIcon("Icons.File.Remove");
|
2024-03-17 18:37:06 -07:00
|
|
|
unstage.Click += (_, e) =>
|
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
var repo = repoView.DataContext as ViewModels.Repository;
|
2024-07-14 00:55:15 -07:00
|
|
|
if (repo == null)
|
|
|
|
return;
|
|
|
|
|
2024-02-27 05:13:52 -08:00
|
|
|
repo.SetWatcherEnabled(false);
|
|
|
|
|
2024-07-17 01:56:16 -07:00
|
|
|
var treeGuid = new Commands.QueryStagedFileBlobGuid(diff.Repo, change.Path).Result();
|
2024-02-27 05:13:52 -08:00
|
|
|
var tmpFile = Path.GetTempFileName();
|
2024-03-17 18:37:06 -07:00
|
|
|
if (change.Index == Models.ChangeState.Added)
|
|
|
|
{
|
2024-06-12 06:12:45 -07:00
|
|
|
diff.GenerateNewPatchFromSelection(change, treeGuid, selection, true, tmpFile);
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
2024-03-19 22:08:01 -07:00
|
|
|
else if (!UseSideBySideDiff)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-06-12 06:12:45 -07:00
|
|
|
diff.GeneratePatchFromSelection(change, treeGuid, selection, true, tmpFile);
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-06-12 06:12:45 -07:00
|
|
|
diff.GeneratePatchFromSelectionSingleSide(change, treeGuid, selection, true, isOldSide, tmpFile);
|
2024-02-27 05:13:52 -08:00
|
|
|
}
|
|
|
|
|
2024-07-17 01:56:16 -07:00
|
|
|
new Commands.Apply(diff.Repo, tmpFile, true, "nowarn", "--cache --index --reverse").Exec();
|
2024-02-27 05:13:52 -08:00
|
|
|
File.Delete(tmpFile);
|
|
|
|
|
2024-03-07 17:57:29 -08:00
|
|
|
repo.MarkWorkingCopyDirtyManually();
|
2024-02-27 05:13:52 -08:00
|
|
|
repo.SetWatcherEnabled(true);
|
|
|
|
e.Handled = true;
|
|
|
|
};
|
2024-02-28 18:59:59 -08:00
|
|
|
|
|
|
|
var discard = new MenuItem();
|
|
|
|
discard.Header = App.Text("FileCM.DiscardSelectedLines");
|
|
|
|
discard.Icon = App.CreateMenuIcon("Icons.Undo");
|
2024-03-17 18:37:06 -07:00
|
|
|
discard.Click += (_, e) =>
|
|
|
|
{
|
2024-02-28 18:59:59 -08:00
|
|
|
var repo = repoView.DataContext as ViewModels.Repository;
|
2024-07-14 00:55:15 -07:00
|
|
|
if (repo == null)
|
|
|
|
return;
|
|
|
|
|
2024-02-28 18:59:59 -08:00
|
|
|
repo.SetWatcherEnabled(false);
|
|
|
|
|
|
|
|
var tmpFile = Path.GetTempFileName();
|
2024-07-17 01:56:16 -07:00
|
|
|
var treeGuid = new Commands.QueryStagedFileBlobGuid(diff.Repo, change.Path).Result();
|
|
|
|
if (change.Index == Models.ChangeState.Added)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-07-17 01:56:16 -07:00
|
|
|
diff.GenerateNewPatchFromSelection(change, treeGuid, selection, true, tmpFile);
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
2024-03-19 22:08:01 -07:00
|
|
|
else if (!UseSideBySideDiff)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-06-12 06:12:45 -07:00
|
|
|
diff.GeneratePatchFromSelection(change, treeGuid, selection, true, tmpFile);
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-06-12 06:12:45 -07:00
|
|
|
diff.GeneratePatchFromSelectionSingleSide(change, treeGuid, selection, true, isOldSide, tmpFile);
|
2024-02-28 18:59:59 -08:00
|
|
|
}
|
|
|
|
|
2024-07-17 01:56:16 -07:00
|
|
|
new Commands.Apply(diff.Repo, tmpFile, true, "nowarn", "--index --reverse").Exec();
|
2024-02-28 18:59:59 -08:00
|
|
|
File.Delete(tmpFile);
|
|
|
|
|
2024-03-07 17:57:29 -08:00
|
|
|
repo.MarkWorkingCopyDirtyManually();
|
2024-02-28 18:59:59 -08:00
|
|
|
repo.SetWatcherEnabled(true);
|
|
|
|
e.Handled = true;
|
|
|
|
};
|
|
|
|
|
2024-02-27 05:13:52 -08:00
|
|
|
menu.Items.Add(unstage);
|
2024-02-28 18:59:59 -08:00
|
|
|
menu.Items.Add(discard);
|
2024-02-27 05:13:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
menu.Items.Add(new MenuItem() { Header = "-" });
|
|
|
|
}
|
|
|
|
|
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-17 01:56:16 -07:00
|
|
|
if (HighlightChunk != null)
|
|
|
|
SetCurrentValue(HighlightChunkProperty, null);
|
|
|
|
|
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-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);
|
|
|
|
|
|
|
|
if (HighlightChunk != null)
|
|
|
|
SetCurrentValue(HighlightChunkProperty, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnStageChunk(object sender, RoutedEventArgs e)
|
|
|
|
{
|
|
|
|
var chunk = HighlightChunk;
|
|
|
|
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-17 20:26:47 -07:00
|
|
|
var selection = MakeSelection(diff, chunk.StartIdx + 1, chunk.EndIdx + 1, true, false);
|
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);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var treeGuid = new Commands.QueryStagedFileBlobGuid(diff.Repo, change.Path).Result();
|
|
|
|
diff.GeneratePatchFromSelection(change, treeGuid, selection, false, tmpFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
var chunk = HighlightChunk;
|
|
|
|
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-17 20:26:47 -07:00
|
|
|
var selection = MakeSelection(diff, chunk.StartIdx + 1, chunk.EndIdx + 1, true, false);
|
2024-07-17 01:56:16 -07:00
|
|
|
if (!selection.HasChanges)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If all changes has been selected the use method provided by ViewModels.WorkingCopy.
|
|
|
|
// Otherwise, use `git apply`
|
|
|
|
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);
|
|
|
|
else
|
|
|
|
diff.GeneratePatchFromSelection(change, treeGuid, selection, true, tmpFile);
|
|
|
|
|
|
|
|
new Commands.Apply(diff.Repo, tmpFile, true, "nowarn", "--cache --index --reverse").Exec();
|
|
|
|
File.Delete(tmpFile);
|
|
|
|
|
|
|
|
repo.MarkWorkingCopyDirtyManually();
|
|
|
|
repo.SetWatcherEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnDiscardChunk(object sender, RoutedEventArgs e)
|
|
|
|
{
|
|
|
|
var chunk = HighlightChunk;
|
|
|
|
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-17 20:26:47 -07:00
|
|
|
var selection = MakeSelection(diff, chunk.StartIdx + 1, chunk.EndIdx + 1, true, false);
|
2024-07-17 01:56:16 -07:00
|
|
|
if (!selection.HasChanges)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If all changes has been selected the use method provided by ViewModels.WorkingCopy.
|
|
|
|
// Otherwise, use `git apply`
|
|
|
|
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();
|
|
|
|
var treeGuid = new Commands.QueryStagedFileBlobGuid(diff.Repo, change.Path).Result();
|
|
|
|
if (change.Index == Models.ChangeState.Added)
|
|
|
|
{
|
|
|
|
diff.GenerateNewPatchFromSelection(change, treeGuid, selection, true, tmpFile);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
diff.GeneratePatchFromSelection(change, treeGuid, selection, true, tmpFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
new Commands.Apply(diff.Repo, tmpFile, true, "nowarn", diff.Option.IsUnstaged ? "--reverse" : "--index --reverse").Exec();
|
|
|
|
File.Delete(tmpFile);
|
|
|
|
|
|
|
|
repo.MarkWorkingCopyDirtyManually();
|
|
|
|
repo.SetWatcherEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private (int, int) GetUnifiedRange(Models.TextDiff diff, int startLine, int endLine, bool isOldSide)
|
|
|
|
{
|
2024-06-12 06:12:45 -07:00
|
|
|
endLine = Math.Min(endLine, diff.Lines.Count);
|
2024-07-17 01:56:16 -07:00
|
|
|
if (Editor.Content is ViewModels.TwoSideTextDiff twoSides)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-02-28 02:15:40 -08:00
|
|
|
var target = isOldSide ? twoSides.Old : twoSides.New;
|
|
|
|
var firstContentLine = -1;
|
2024-03-17 18:37:06 -07:00
|
|
|
for (int i = startLine - 1; i < endLine; i++)
|
|
|
|
{
|
2024-02-28 02:15:40 -08:00
|
|
|
var line = target[i];
|
2024-03-17 18:37:06 -07:00
|
|
|
if (line.Type != Models.TextDiffLineType.None)
|
|
|
|
{
|
2024-02-28 02:15:40 -08:00
|
|
|
firstContentLine = i;
|
2024-02-27 05:13:52 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 01:54:29 -07:00
|
|
|
if (firstContentLine < 0)
|
2024-07-17 01:56:16 -07:00
|
|
|
return (-1, -1);
|
2024-02-28 02:15:40 -08:00
|
|
|
|
|
|
|
var endContentLine = -1;
|
2024-03-17 18:37:06 -07:00
|
|
|
for (int i = Math.Min(endLine - 1, target.Count - 1); i >= startLine - 1; i--)
|
|
|
|
{
|
2024-02-28 02:15:40 -08:00
|
|
|
var line = target[i];
|
2024-03-17 18:37:06 -07:00
|
|
|
if (line.Type != Models.TextDiffLineType.None)
|
|
|
|
{
|
2024-02-28 02:15:40 -08:00
|
|
|
endContentLine = i;
|
|
|
|
break;
|
2024-02-27 05:13:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 01:54:29 -07:00
|
|
|
if (endContentLine < 0)
|
2024-07-17 01:56:16 -07:00
|
|
|
return (-1, -1);
|
2024-02-27 05:13:52 -08:00
|
|
|
|
2024-02-28 02:15:40 -08:00
|
|
|
var firstContent = target[firstContentLine];
|
|
|
|
var endContent = target[endContentLine];
|
2024-06-12 06:12:45 -07:00
|
|
|
startLine = diff.Lines.IndexOf(firstContent) + 1;
|
|
|
|
endLine = diff.Lines.IndexOf(endContent) + 1;
|
2024-02-27 05:13:52 -08:00
|
|
|
}
|
|
|
|
|
2024-07-17 01:56:16 -07:00
|
|
|
return (startLine, endLine);
|
|
|
|
}
|
|
|
|
|
2024-07-17 20:26:47 -07:00
|
|
|
private Models.TextDiffSelection MakeSelection(Models.TextDiff diff, int startLine, int endLine, bool combined, bool isOldSide)
|
2024-07-17 01:56:16 -07:00
|
|
|
{
|
|
|
|
var rs = new Models.TextDiffSelection();
|
2024-02-28 02:15:40 -08:00
|
|
|
rs.StartLine = startLine;
|
|
|
|
rs.EndLine = endLine;
|
2024-02-27 05:13:52 -08:00
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
for (int i = 0; i < startLine - 1; i++)
|
|
|
|
{
|
2024-02-28 02:15:40 -08:00
|
|
|
var line = diff.Lines[i];
|
2024-03-17 18:37:06 -07:00
|
|
|
if (line.Type == Models.TextDiffLineType.Added)
|
|
|
|
{
|
2024-02-28 02:15:40 -08:00
|
|
|
rs.HasLeftChanges = true;
|
|
|
|
rs.IgnoredAdds++;
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else if (line.Type == Models.TextDiffLineType.Deleted)
|
|
|
|
{
|
2024-02-28 02:15:40 -08:00
|
|
|
rs.HasLeftChanges = true;
|
|
|
|
rs.IgnoredDeletes++;
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
|
|
|
}
|
2024-02-27 05:13:52 -08:00
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
for (int i = startLine - 1; i < endLine; i++)
|
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
var line = diff.Lines[i];
|
2024-03-17 18:37:06 -07:00
|
|
|
if (line.Type == Models.TextDiffLineType.Added)
|
|
|
|
{
|
2024-07-17 20:26:47 -07:00
|
|
|
if (combined)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-02-28 02:15:40 -08:00
|
|
|
rs.HasChanges = true;
|
2024-02-27 05:13:52 -08:00
|
|
|
break;
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else if (isOldSide)
|
|
|
|
{
|
2024-02-28 02:15:40 -08:00
|
|
|
rs.HasLeftChanges = true;
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-28 02:15:40 -08:00
|
|
|
rs.HasChanges = true;
|
2024-02-27 05:13:52 -08:00
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else if (line.Type == Models.TextDiffLineType.Deleted)
|
|
|
|
{
|
2024-07-17 20:26:47 -07:00
|
|
|
if (combined)
|
2024-03-17 18:37:06 -07:00
|
|
|
{
|
2024-02-28 02:15:40 -08:00
|
|
|
rs.HasChanges = true;
|
|
|
|
break;
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else if (isOldSide)
|
|
|
|
{
|
2024-02-28 02:15:40 -08:00
|
|
|
rs.HasChanges = true;
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-28 02:15:40 -08:00
|
|
|
rs.HasLeftChanges = true;
|
|
|
|
}
|
2024-02-27 05:13:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
if (!rs.HasLeftChanges)
|
|
|
|
{
|
|
|
|
for (int i = endLine; i < diff.Lines.Count; i++)
|
|
|
|
{
|
2024-02-28 02:15:40 -08:00
|
|
|
var line = diff.Lines[i];
|
2024-03-17 18:37:06 -07:00
|
|
|
if (line.Type == Models.TextDiffLineType.Added || line.Type == Models.TextDiffLineType.Deleted)
|
|
|
|
{
|
2024-02-28 02:15:40 -08:00
|
|
|
rs.HasLeftChanges = true;
|
|
|
|
break;
|
2024-02-27 05:13:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-28 02:15:40 -08:00
|
|
|
return rs;
|
2024-03-17 18:37:06 -07:00
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
}
|