Commit 6817211a authored by Thomas's avatar Thomas
Browse files

Fix diff algorithm

parent 11a334a6
...@@ -372,40 +372,30 @@ namespace pEp.UI.ViewModels ...@@ -372,40 +372,30 @@ namespace pEp.UI.ViewModels
List<DiffPiece> newLines = sideBySideDiffModel.NewText.Lines; List<DiffPiece> newLines = sideBySideDiffModel.NewText.Lines;
List<DiffPiece> oldLines = sideBySideDiffModel.OldText.Lines; List<DiffPiece> oldLines = sideBySideDiffModel.OldText.Lines;
List<string> block = new List<string>(); List<string> block = new List<string>();
int unchangedCount = 0;
bool newBlock = true; bool newBlock = true;
int oldFileIndex = 0, newFileIndex = 0; int oldFileIndex = 0, newFileIndex = 0;
for (int i = 0; i < newLines.Count; i++) for (int i = 0; i < newLines.Count; i++)
{ {
// Add lines as necessary // Add lines as necessary
if ((newLines[i].Type == ChangeType.Unchanged) && if (newLines[i].Type == ChangeType.Imaginary)
(block.Count > 0))
{
block.Add(" " + newLines[i].Text);
unchangedCount++;
}
else if (newLines[i].Type == ChangeType.Imaginary)
{ {
block.Add("-" + oldLines[i].Text); block.Add("-" + oldLines[i].Text);
unchangedCount = 0;
} }
else if (newLines[i].Type == ChangeType.Inserted) else if (newLines[i].Type == ChangeType.Inserted)
{ {
block.Add("+" + newLines[i].Text); block.Add("+" + newLines[i].Text);
unchangedCount = 0;
} }
else if (newLines[i].Type == ChangeType.Modified) else if (newLines[i].Type == ChangeType.Modified)
{ {
block.Add("-" + oldLines[i].Text); block.Add("-" + oldLines[i].Text);
block.Add("+" + newLines[i].Text); block.Add("+" + newLines[i].Text);
unchangedCount = 0;
} }
// If a block is being constructed, add additional parts as necessary // If a block is being constructed, add additional parts as necessary
if (block.Count > 0) if (block.Count > 0)
{ {
// If we have a new block, add the three preceding unchanged lines // First, add the three preceding unchanged lines (if available)
if (newBlock) if (newBlock)
{ {
newBlock = false; newBlock = false;
...@@ -425,14 +415,32 @@ namespace pEp.UI.ViewModels ...@@ -425,14 +415,32 @@ namespace pEp.UI.ViewModels
newFileIndex = newLines[index].Position ?? 1; newFileIndex = newLines[index].Position ?? 1;
oldFileIndex = oldLines[index].Position ?? 1; oldFileIndex = oldLines[index].Position ?? 1;
} }
else if ((unchangedCount > 2) || (i == newLines.Count - 1))
// Then, add the three following lines (if available)
int counter = 0;
while ((++i < newLines.Count) &&
(newLines[i].Type == ChangeType.Unchanged))
{
if (counter++ < 3)
{
block.Add(" " + newLines[i].Text);
}
else
{
break;
}
}
// Close block
if ((counter > 3) ||
(i >= newLines.Count))
{ {
// Once a block is complete, add descriptor at the beginning and add lines to diff // Once a block is complete, add descriptor at the beginning and add lines to diff
int oldFileLineCount = block.Count(s => !s.StartsWith("+")); int oldFileLineCount = block.Count(s => !s.StartsWith("+"));
int newFileLineCount = block.Count(s => !s.StartsWith("-")); int newFileLineCount = block.Count(s => !s.StartsWith("-"));
diff += $"@@ -{ oldFileIndex },{ oldFileLineCount } +{ newFileIndex },{ newFileLineCount } @@\n"; diff += $"@@ -{ oldFileIndex },{ oldFileLineCount } +{ newFileIndex },{ newFileLineCount } @@\n";
foreach (var line in block) foreach (string line in block)
{ {
diff += line + '\n'; diff += line + '\n';
} }
...@@ -440,6 +448,8 @@ namespace pEp.UI.ViewModels ...@@ -440,6 +448,8 @@ namespace pEp.UI.ViewModels
block = new List<string>(); block = new List<string>();
newBlock = true; newBlock = true;
} }
i--;
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment