From aaefaf5b06554140b0f5a4f476e2843a16120fe2 Mon Sep 17 00:00:00 2001 From: leo Date: Fri, 27 Nov 2020 12:45:30 +0800 Subject: [PATCH] optimize: ignore "No new line at end of file" in diff outputs; add IsContent and IsDifference methods for ChangeBlock --- src/Git/Diff.cs | 2 +- src/UI/DiffViewer.xaml.cs | 26 +++++++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/Git/Diff.cs b/src/Git/Diff.cs index 273e58f8..c4cfd8e5 100644 --- a/src/Git/Diff.cs +++ b/src/Git/Diff.cs @@ -98,7 +98,7 @@ namespace SourceGit.Git { rs.Lines.Add(new LineChange(LineMode.Added, line.Substring(1), "", $"{newLine}")); newLine++; } else if (line[0] == '\\') { - rs.Lines.Add(new LineChange(LineMode.Indicator, line.Substring(1))); + // IGNORE \ No new line end of file. } else { var match = REG_INDICATOR.Match(line); if (match.Success) { diff --git a/src/UI/DiffViewer.xaml.cs b/src/UI/DiffViewer.xaml.cs index 312440ff..274d68e4 100644 --- a/src/UI/DiffViewer.xaml.cs +++ b/src/UI/DiffViewer.xaml.cs @@ -44,6 +44,18 @@ namespace SourceGit.UI { public FontStyle Style { get; set; } public string OldLine { get; set; } public string NewLine { get; set; } + + public bool IsContent { + get { + return Mode == Git.Diff.LineMode.Added || Mode == Git.Diff.LineMode.Deleted || Mode == Git.Diff.LineMode.Normal; + } + } + + public bool IsDifference { + get { + return Mode == Git.Diff.LineMode.Added || Mode == Git.Diff.LineMode.Deleted || Mode == Git.Diff.LineMode.None; + } + } } /// @@ -148,8 +160,8 @@ namespace SourceGit.UI { block.Content = line.Content; block.Mode = line.Mode; block.BG = GetLineBackground(line); - block.FG = line.Mode == Git.Diff.LineMode.Indicator ? fgIndicator : fgCommon; - block.Style = line.Mode == Git.Diff.LineMode.Indicator ? FontStyles.Italic : FontStyles.Normal; + block.FG = block.IsContent ? fgCommon : fgIndicator; + block.Style = block.IsContent ? FontStyles.Normal : FontStyles.Italic; block.OldLine = line.OldLine; block.NewLine = line.NewLine; @@ -199,8 +211,8 @@ namespace SourceGit.UI { block.Content = line.Content; block.Mode = line.Mode; block.BG = GetLineBackground(line); - block.FG = line.Mode == Git.Diff.LineMode.Indicator ? fgIndicator : fgCommon; - block.Style = line.Mode == Git.Diff.LineMode.Indicator ? FontStyles.Italic : FontStyles.Normal; + block.FG = block.IsContent ? fgCommon : fgIndicator; + block.Style = block.IsContent ? FontStyles.Normal : FontStyles.Italic; block.OldLine = line.OldLine; block.NewLine = line.NewLine; @@ -548,7 +560,7 @@ namespace SourceGit.UI { var first = grid.Items[firstVisible] as ChangeBlock; for (int i = firstVisible + 1; i < grid.Items.Count; i++) { var next = grid.Items[i] as ChangeBlock; - if (next.Mode != Git.Diff.LineMode.Normal && next.Mode != Git.Diff.LineMode.Indicator) { + if (next.IsDifference) { if (firstModeEnded || next.Mode != first.Mode) { scroller.ScrollToVerticalOffset(i); break; @@ -576,7 +588,7 @@ namespace SourceGit.UI { var first = grid.Items[firstVisible] as ChangeBlock; for (int i = firstVisible - 1; i >= 0; i--) { var next = grid.Items[i] as ChangeBlock; - if (next.Mode != Git.Diff.LineMode.Normal && next.Mode != Git.Diff.LineMode.Indicator) { + if (next.IsDifference) { if (firstModeEnded || next.Mode != first.Mode) { scroller.ScrollToVerticalOffset(i); break; @@ -619,7 +631,7 @@ namespace SourceGit.UI { foreach (var item in items) { var block = item as ChangeBlock; if (block == null) continue; - if (block.Mode == Git.Diff.LineMode.None || block.Mode == Git.Diff.LineMode.Indicator) continue; + if (!block.IsContent) continue; builder.Append(block.Content); builder.AppendLine();