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

View file

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

View file

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