Merge pull request !19 from Jai/fix/copy-content-order

* optimix<Diff>: performance optimization
* fix<DiffViewer>: incorrect order when copying file contents
This commit is contained in:
Jai 2021-08-11 11:16:00 +00:00 committed by leo
parent eb2c07b2b6
commit 663f3237c0
3 changed files with 17 additions and 16 deletions

View file

@ -14,6 +14,7 @@ namespace SourceGit.Commands {
private List<Models.TextChanges.Line> added = new List<Models.TextChanges.Line>();
private int oldLine = 0;
private int newLine = 0;
private int lineIndex = 0;
public Diff(string repo, string args) {
Cwd = repo;
@ -24,6 +25,7 @@ namespace SourceGit.Commands {
Exec();
ProcessChanges();
if (changes.IsBinary) changes.Lines.Clear();
lineIndex = 0;
return changes;
}
@ -39,11 +41,11 @@ namespace SourceGit.Commands {
oldLine = int.Parse(match.Groups[1].Value);
newLine = int.Parse(match.Groups[2].Value);
changes.Lines.Add(new Models.TextChanges.Line(Models.TextChanges.LineMode.Indicator, line, "", ""));
changes.Lines.Add(new Models.TextChanges.Line(lineIndex++, Models.TextChanges.LineMode.Indicator, line, "", ""));
} else {
if (line.Length == 0) {
ProcessChanges();
changes.Lines.Add(new Models.TextChanges.Line(Models.TextChanges.LineMode.Normal, "", $"{oldLine}", $"{newLine}"));
changes.Lines.Add(new Models.TextChanges.Line(lineIndex++, Models.TextChanges.LineMode.Normal, "", $"{oldLine}", $"{newLine}"));
oldLine++;
newLine++;
return;
@ -51,10 +53,10 @@ namespace SourceGit.Commands {
var ch = line[0];
if (ch == '-') {
deleted.Add(new Models.TextChanges.Line(Models.TextChanges.LineMode.Deleted, line.Substring(1), $"{oldLine}", ""));
deleted.Add(new Models.TextChanges.Line(lineIndex++, Models.TextChanges.LineMode.Deleted, line.Substring(1), $"{oldLine}", ""));
oldLine++;
} else if (ch == '+') {
added.Add(new Models.TextChanges.Line(Models.TextChanges.LineMode.Added, line.Substring(1), "", $"{newLine}"));
added.Add(new Models.TextChanges.Line(lineIndex++, Models.TextChanges.LineMode.Added, line.Substring(1), "", $"{newLine}"));
newLine++;
} else if (ch != '\\') {
ProcessChanges();
@ -62,9 +64,9 @@ namespace SourceGit.Commands {
if (match.Success) {
oldLine = int.Parse(match.Groups[1].Value);
newLine = int.Parse(match.Groups[2].Value);
changes.Lines.Add(new Models.TextChanges.Line(Models.TextChanges.LineMode.Indicator, line, "", ""));
changes.Lines.Add(new Models.TextChanges.Line(lineIndex++, Models.TextChanges.LineMode.Indicator, line, "", ""));
} else {
changes.Lines.Add(new Models.TextChanges.Line(Models.TextChanges.LineMode.Normal, line.Substring(1), $"{oldLine}", $"{newLine}"));
changes.Lines.Add(new Models.TextChanges.Line(lineIndex++, Models.TextChanges.LineMode.Normal, line.Substring(1), $"{oldLine}", $"{newLine}"));
oldLine++;
newLine++;
}

View file

@ -21,6 +21,7 @@ namespace SourceGit.Models {
}
public class Line {
public int Index { get; set; } = 0;
public LineMode Mode { get; set; } = LineMode.None;
public string Content { get; set; } = "";
public string OldLine { get; set; } = "";
@ -45,7 +46,8 @@ namespace SourceGit.Models {
public Line() { }
public Line(LineMode mode, string content, string oldLine, string newLine) {
public Line(int index, LineMode mode, string content, string oldLine, string newLine) {
Index = index;
Mode = mode;
Content = content;
OldLine = oldLine;

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
@ -346,13 +347,11 @@ namespace SourceGit.Views.Widgets {
grid.ContextMenuOpening += OnTextDiffContextMenuOpening;
grid.PreviewMouseWheel += OnTextDiffPreviewMouseWheel;
grid.CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, (o, e) => {
var items = (o as DataGrid).SelectedItems;
if (items.Count == 0) return;
var items = (o as DataGrid)?.SelectedItems.OfType<Models.TextChanges.Line>().OrderBy(i => i.Index).ToList();
if (items == null || items.Count == 0) return;
var builder = new StringBuilder();
foreach (var item in items) {
var block = item as Models.TextChanges.Line;
if (block == null) continue;
foreach (var block in items) {
if (!block.IsContent) continue;
builder.Append(block.Content);
@ -466,13 +465,11 @@ namespace SourceGit.Views.Widgets {
copy.Header = App.Text("Diff.Copy");
copy.Icon = copyIcon;
copy.Click += (o, ev) => {
var items = grid.SelectedItems;
var items = grid.SelectedItems.OfType<Models.TextChanges.Line>().OrderBy(i => i.Index).ToList();
if (items.Count == 0) return;
var builder = new StringBuilder();
foreach (var item in items) {
var block = item as Models.TextChanges.Line;
if (block == null) continue;
foreach (var block in items) {
if (!block.IsContent) continue;
builder.Append(block.Content);