From 66e38c8f2d1cd3eacbef9b0fc369705d3d451867 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 26 Nov 2020 14:20:51 +0800 Subject: [PATCH] optimize: rewrite two-sides diff algorithm --- src/UI/DiffViewer.xaml.cs | 62 ++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/src/UI/DiffViewer.xaml.cs b/src/UI/DiffViewer.xaml.cs index 287e20a0..f07d71f2 100644 --- a/src/UI/DiffViewer.xaml.cs +++ b/src/UI/DiffViewer.xaml.cs @@ -194,44 +194,27 @@ namespace SourceGit.UI { switch (line.Mode) { case Git.Diff.LineMode.Added: 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; case Git.Diff.LineMode.Deleted: 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; default: + FillEmptyLines(oldSideBlocks, newSideBlocks); oldSideBlocks.Add(block); newSideBlocks.Add(block); break; } } + FillEmptyLines(oldSideBlocks, newSideBlocks); + Dispatcher.Invoke(() => { loading.Visibility = Visibility.Collapsed; textChangeOptions.Visibility = Visibility.Visible; var lineNumberWidth = CalcLineNumberColWidth(lastOldLine, lastNewLine); 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" }); oldEditor.SetValue(Grid.ColumnProperty, 0); @@ -317,6 +300,43 @@ namespace SourceGit.UI { } } + /// + /// Fill empty lines to keep same line count in both old and current. + /// + /// + /// + private void FillEmptyLines(List old, List 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); + } + } + } + /// /// Find child element of type. ///