optimize<DiffViewer>: rewrite two-sides diff algorithm

This commit is contained in:
leo 2020-11-26 14:20:51 +08:00
parent b3b014f84e
commit 66e38c8f2d

View file

@ -194,44 +194,27 @@ namespace SourceGit.UI {
switch (line.Mode) { switch (line.Mode) {
case Git.Diff.LineMode.Added: case Git.Diff.LineMode.Added:
newSideBlocks.Add(block); newSideBlocks.Add(block);
var oldEmpty = new ChangeBlock();
oldEmpty.Content = "";
oldEmpty.Mode = Git.Diff.LineMode.None;
oldEmpty.BG = bgEmpty;
oldEmpty.FG = fgCommon;
oldEmpty.Style = FontStyles.Normal;
oldEmpty.OldLine = block.OldLine;
oldEmpty.NewLine = block.NewLine;
oldSideBlocks.Add(oldEmpty);
break; break;
case Git.Diff.LineMode.Deleted: case Git.Diff.LineMode.Deleted:
oldSideBlocks.Add(block); oldSideBlocks.Add(block);
var newEmpty = new ChangeBlock();
newEmpty.Content = "";
newEmpty.Mode = Git.Diff.LineMode.None;
newEmpty.BG = bgEmpty;
newEmpty.FG = fgCommon;
newEmpty.Style = FontStyles.Normal;
newEmpty.OldLine = block.OldLine;
newEmpty.NewLine = block.NewLine;
newSideBlocks.Add(newEmpty);
break; break;
default: default:
FillEmptyLines(oldSideBlocks, newSideBlocks);
oldSideBlocks.Add(block); oldSideBlocks.Add(block);
newSideBlocks.Add(block); newSideBlocks.Add(block);
break; break;
} }
} }
FillEmptyLines(oldSideBlocks, newSideBlocks);
Dispatcher.Invoke(() => { Dispatcher.Invoke(() => {
loading.Visibility = Visibility.Collapsed; loading.Visibility = Visibility.Collapsed;
textChangeOptions.Visibility = Visibility.Visible; textChangeOptions.Visibility = Visibility.Visible;
var lineNumberWidth = CalcLineNumberColWidth(lastOldLine, lastNewLine); var lineNumberWidth = CalcLineNumberColWidth(lastOldLine, lastNewLine);
var minWidth = editorContainer.ActualWidth / 2 - lineNumberWidth; var minWidth = editorContainer.ActualWidth / 2 - lineNumberWidth;
if (editorContainer.ActualHeight < lineChanges.Count * 16) minWidth -= 8; if (editorContainer.ActualHeight < newSideBlocks.Count * 16) minWidth -= 8;
var oldEditor = CreateTextEditor(new string[] { "OldLine" }); var oldEditor = CreateTextEditor(new string[] { "OldLine" });
oldEditor.SetValue(Grid.ColumnProperty, 0); oldEditor.SetValue(Grid.ColumnProperty, 0);
@ -317,6 +300,43 @@ namespace SourceGit.UI {
} }
} }
/// <summary>
/// Fill empty lines to keep same line count in both old and current.
/// </summary>
/// <param name="old"></param>
/// <param name="cur"></param>
private void FillEmptyLines(List<ChangeBlock> old, List<ChangeBlock> cur) {
if (old.Count < cur.Count) {
int diff = cur.Count - old.Count;
for (int i = 0; i < diff; i++) {
var empty = new ChangeBlock();
empty.Content = "";
empty.Mode = Git.Diff.LineMode.None;
empty.BG = bgEmpty;
empty.FG = Brushes.Transparent;
empty.Style = FontStyles.Normal;
empty.OldLine = "";
empty.NewLine = "";
old.Add(empty);
}
} else if (old.Count > cur.Count) {
int diff = old.Count - cur.Count;
for (int i = 0; i < diff; i++) {
var empty = new ChangeBlock();
empty.Content = "";
empty.Mode = Git.Diff.LineMode.None;
empty.BG = bgEmpty;
empty.FG = Brushes.Transparent;
empty.Style = FontStyles.Normal;
empty.OldLine = "";
empty.NewLine = "";
cur.Add(empty);
}
}
}
/// <summary> /// <summary>
/// Find child element of type. /// Find child element of type.
/// </summary> /// </summary>