2024-07-17 20:40:56 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
2024-06-12 06:12:45 -07:00
|
|
|
|
using Avalonia;
|
2024-07-17 20:40:56 -07:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
|
|
|
|
namespace SourceGit.ViewModels
|
|
|
|
|
{
|
|
|
|
|
public class TwoSideTextDiff : ObservableObject
|
|
|
|
|
{
|
2024-07-14 09:30:31 -07:00
|
|
|
|
public string File { get; set; }
|
2024-02-05 23:08:37 -08:00
|
|
|
|
public List<Models.TextDiffLine> Old { get; set; } = new List<Models.TextDiffLine>();
|
|
|
|
|
public List<Models.TextDiffLine> New { get; set; } = new List<Models.TextDiffLine>();
|
|
|
|
|
public int MaxLineNumber = 0;
|
|
|
|
|
|
2024-06-12 06:12:45 -07:00
|
|
|
|
public Vector SyncScrollOffset
|
|
|
|
|
{
|
|
|
|
|
get => _syncScrollOffset;
|
|
|
|
|
set => SetProperty(ref _syncScrollOffset, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TwoSideTextDiff(Models.TextDiff diff, TwoSideTextDiff previous = null)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
File = diff.File;
|
|
|
|
|
MaxLineNumber = diff.MaxLineNumber;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var line in diff.Lines)
|
|
|
|
|
{
|
|
|
|
|
switch (line.Type)
|
|
|
|
|
{
|
|
|
|
|
case Models.TextDiffLineType.Added:
|
|
|
|
|
New.Add(line);
|
|
|
|
|
break;
|
|
|
|
|
case Models.TextDiffLineType.Deleted:
|
|
|
|
|
Old.Add(line);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
FillEmptyLines();
|
|
|
|
|
Old.Add(line);
|
|
|
|
|
New.Add(line);
|
|
|
|
|
break;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FillEmptyLines();
|
2024-06-12 06:12:45 -07:00
|
|
|
|
|
|
|
|
|
if (previous != null && previous.File == File)
|
|
|
|
|
_syncScrollOffset = previous._syncScrollOffset;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-17 20:40:56 -07:00
|
|
|
|
public void ConvertsToCombinedRange(Models.TextDiff combined, ref int startLine, ref int endLine, bool isOldSide)
|
|
|
|
|
{
|
2024-07-18 01:16:58 -07:00
|
|
|
|
endLine = Math.Min(endLine, combined.Lines.Count - 1);
|
2024-07-17 20:40:56 -07:00
|
|
|
|
|
|
|
|
|
var oneSide = isOldSide ? Old : New;
|
|
|
|
|
var firstContentLine = -1;
|
2024-07-18 01:16:58 -07:00
|
|
|
|
for (int i = startLine; i <= endLine; i++)
|
2024-07-17 20:40:56 -07:00
|
|
|
|
{
|
|
|
|
|
var line = oneSide[i];
|
|
|
|
|
if (line.Type != Models.TextDiffLineType.None)
|
|
|
|
|
{
|
|
|
|
|
firstContentLine = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (firstContentLine < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var endContentLine = -1;
|
2024-07-18 01:16:58 -07:00
|
|
|
|
for (int i = Math.Min(endLine, oneSide.Count - 1); i >= startLine; i--)
|
2024-07-17 20:40:56 -07:00
|
|
|
|
{
|
|
|
|
|
var line = oneSide[i];
|
|
|
|
|
if (line.Type != Models.TextDiffLineType.None)
|
|
|
|
|
{
|
|
|
|
|
endContentLine = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (endContentLine < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var firstContent = oneSide[firstContentLine];
|
|
|
|
|
var endContent = oneSide[endContentLine];
|
2024-07-18 01:16:58 -07:00
|
|
|
|
startLine = combined.Lines.IndexOf(firstContent);
|
|
|
|
|
endLine = combined.Lines.IndexOf(endContent);
|
2024-07-17 20:40:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private void FillEmptyLines()
|
|
|
|
|
{
|
|
|
|
|
if (Old.Count < New.Count)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
int diff = New.Count - Old.Count;
|
2024-03-31 01:54:29 -07:00
|
|
|
|
for (int i = 0; i < diff; i++)
|
|
|
|
|
Old.Add(new Models.TextDiffLine());
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else if (Old.Count > New.Count)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
int diff = Old.Count - New.Count;
|
2024-03-31 01:54:29 -07:00
|
|
|
|
for (int i = 0; i < diff; i++)
|
|
|
|
|
New.Add(new Models.TextDiffLine());
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-12 06:12:45 -07:00
|
|
|
|
|
|
|
|
|
private Vector _syncScrollOffset = Vector.Zero;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|