private void UpdateCompareContent() { if (Left != null && Right != null) { var diff = DiffModule.DiffMain(Left.RawContents, Right.RawContents); DiffModule.DiffCleanupSemantic(diff); if (Comparison != null) { Comparison.RebuildRequested -= Comparison_RebuildRequested; } Comparison = new Comparison(diff); Comparison.RebuildRequested += Comparison_RebuildRequested; LeftDocument.Text = Comparison.Root?.GetAsString(Side.Left); RightDocument.Text = Comparison.Root?.GetAsString(Side.Right); ResultDocument.Changed -= _resultDocument_Changed; ResultDocument.Text = Comparison.Root?.GetAsString(Side.Result); ResultDocument.Changed += _resultDocument_Changed; } }
private static string GetBody(Base o) { var diffMatchPatch = new DiffMatchPatch.DiffMatchPatch(); var differents = diffMatchPatch.DiffMain(o.Changes.Body.From, o.Issue.Body); diffMatchPatch.DiffCleanupSemantic(differents); //diffMatchPatch.DiffCleanupEfficiency(differents); return(diffMatchPatch.DiffEmbedBody(differents)); //return "```diff\n" + diffMatchPatch.DiffEmbedBody(differents) + "```"; /* var strBuilder = new StringBuilder(); * if (differents.Any(d => d.operation == DiffMatchPatch.Operation.INSERT)) * { * strBuilder.AppendLine("Added:"); * foreach (var diff in differents.Where(d => d.operation == DiffMatchPatch.Operation.INSERT).Select(d => d.text)) * { * strBuilder.AppendLine(diff); * } * } * * if (differents.Any(d => d.operation == DiffMatchPatch.Operation.DELETE)) * { * if (strBuilder.Length > 0) * strBuilder.AppendLine(); * strBuilder.AppendLine("Removed:"); * foreach (var diff in differents.Where(d => d.operation == DiffMatchPatch.Operation.DELETE).Select(d => d.text)) * { * strBuilder.AppendLine(diff); * } * } * * return strBuilder.ToString();*/ }
public static List <LinePatch> MakePatchRaw(string text1, string text2) { var mapping = new Dictionary <string, char>(); var lineText1 = MapLines(text1, mapping); var lineText2 = MapLines(text2, mapping); var lineArray = mapping.ToDictionary(x => x.Value, x => x.Key); var diffs = DMP.DiffMain(lineText1, lineText2, true); var patches = DMP.Patchmake(diffs); var starts1 = new int[lineText1.Length + 1]; var starts2 = new int[lineText2.Length + 1]; int sum = 0; for (int i = 0; i < lineText1.Length; i++) { starts1[i] = sum; sum += lineArray[lineText1[i]].Length; } starts1[starts1.Length - 1] = sum; sum = 0; for (int i = 0; i < lineText2.Length; i++) { starts2[i] = sum; sum += lineArray[lineText2[i]].Length; } starts2[starts2.Length - 1] = sum; var linePatches = new List <LinePatch>(patches.Count); foreach (var patch in patches) { var lineDiffs = patch.Diffs.Select(x => new LineDiff(x.Operation, x.Text.Select(l => lineArray[l]).ToList())).ToList(); var linePatch = new LinePatch(patch.Start1, patch.Start2, lineDiffs); linePatches.Add(linePatch); } return(linePatches); }