private void ParseDiffs(string oldScript, string newScript) { StringBuilder textBuilder = new StringBuilder(); List <DiffItem> diffItems = Diff.DiffTexts(oldScript, newScript); foreach (DiffItem diffItem in diffItems) { switch (diffItem.type) { case DiffItem.DiffType.Deleted: case DiffItem.DiffType.Inserted: DiffSegment segment = new DiffSegment() { StartOffset = textBuilder.Length, Length = diffItem.data.Length, type = diffItem.type }; diffHighlighter.AddSegment(segment); break; } textBuilder.AppendLine(diffItem.data); } try { string newline = Environment.NewLine; textBuilder.Remove(textBuilder.Length - newline.Length, newline.Length); } catch (ArgumentOutOfRangeException) {} // pass scriptView.Text = textBuilder.ToString(); }
private void DrawSegment(DiffSegment segment, TextView textView, DrawingContext drawingContext) { BackgroundGeometryBuilder geoBuilder = new BackgroundGeometryBuilder { AlignToWholePixels = true, BorderThickness = 0, CornerRadius = 3, }; Brush markerBrush; switch (segment.type) { case DiffItem.DiffType.Deleted: markerBrush = deletedBrush; geoBuilder.AddSegment(textView, segment); break; case DiffItem.DiffType.Inserted: markerBrush = addedBrush; geoBuilder.AddSegment(textView, segment); break; default: markerBrush = null; break; } Geometry geometry = geoBuilder.CreateGeometry(); if (geometry != null) { drawingContext.DrawGeometry(markerBrush, null, geometry); } }
public DiffUsingTFS(string[] args) { Debug.Assert(args.Count() >= 2, "Usage: SyntaxDiff.exe file1 file2"); string file1 = args[0], file2 = args[1]; DiffOptions diffOptions = new DiffOptions(); diffOptions.UseThirdPartyTool = false; diffOptions.OutputType = DiffOutputType.Unified; // Wherever we want to send our text-based diff output diffOptions.StreamWriter = new System.IO.StreamWriter(Console.OpenStandardOutput()); Console.WriteLine("Difference.DiffFiles - output to console"); DiffSegment diffs = Microsoft.TeamFoundation.VersionControl.Client.Difference.DiffFiles( file1, FileType.Detect(file1, null), file2, FileType.Detect(file2, null), diffOptions); var diff = diffs; while (diff != null) { Console.WriteLine("Diff ==> {0} {1}:{2}:{3} {4}:{5}:{6}", diff.Type, diff.OriginalStart, diff.OriginalLength, diff.OriginalStartOffset, diff.ModifiedStart, diff.ModifiedLength, diff.ModifiedStartOffset); diff = diff.Next; } }
/// <summary> /// returns a list of line numbers in s and t that differ /// </summary> /// <param name="s"></param> /// <param name="t"></param> /// <returns></returns> public Tuple <List <int>, List <int> > DiffStrings(List <string> s, List <string> t) { if (s.Count == t.Count && s.Zip(t).Where(x => x.Item1 != x.Item2).Count() == 0) { Console.WriteLine("No diffs"); return(Tuple.Create(new List <int>(), new List <int>())); } //dump to a file and call DiffFiles var sFileName = "___tmp_diff_str_1"; var tFileName = "___tmp_diff_str_2"; var sFile = new StreamWriter(sFileName); var tFile = new StreamWriter(tFileName); s.Iter(l => sFile.WriteLine(l)); sFile.Close(); t.Iter(l => tFile.WriteLine(l)); tFile.Close(); DiffOptions diffOptions = new DiffOptions(); diffOptions.UseThirdPartyTool = false; diffOptions.OutputType = DiffOutputType.Unified; // Wherever we want to send our text-based diff output diffOptions.StreamWriter = new System.IO.StreamWriter(Console.OpenStandardOutput()); DiffSegment diffs = Microsoft.TeamFoundation.VersionControl.Client.Difference.DiffFiles( sFileName, FileType.Detect(sFileName, null), tFileName, FileType.Detect(tFileName, null), diffOptions); var diff = diffs; var sDiffLines = new List <int>(); var tDiffLines = new List <int>(); if (diff.Next != null) //some diff { int sLast = int.MaxValue; int tLast = int.MaxValue; while (diff != null) { /*Console.WriteLine("Diff ==> {0} {1}:{2}:{3} {4}:{5}:{6}", * diff.Type, diff.OriginalStart, diff.OriginalLength, diff.OriginalStartOffset, diff.ModifiedStart, diff.ModifiedLength, diff.ModifiedStartOffset);*/ sDiffLines.AddRange(ComputeRange(sLast, diff.OriginalStart)); sLast = diff.OriginalStart + diff.OriginalLength; tDiffLines.AddRange(ComputeRange(tLast, diff.ModifiedStart)); tLast = diff.ModifiedStart + diff.ModifiedLength; diff = diff.Next; } } return(Tuple.Create(sDiffLines, tDiffLines)); }
private static Dictionary <int, int> Diff(DiffSegment diffSegment) { var result = new Dictionary <int, int>(); while (diffSegment != null) { int originalLine = diffSegment.OriginalStart; int modifiedLine = diffSegment.ModifiedStart; for (var i = 0; i < diffSegment.OriginalLength; i++) { result.Add(originalLine, modifiedLine); originalLine++; modifiedLine++; } diffSegment = diffSegment.Next; } return(result); }
public static DiffSummary DiffFiles(String source, Int32 sourceCodePage, String target, Int32 targetCodePage, DiffOptions diffOptions) { DiffSegment currSegment = Difference.DiffFiles(source, sourceCodePage, target, targetCodePage, diffOptions); DiffSummary summary = new DiffSummary(); // Initialize a set of position markers which will be used to walk // through the files Int32 currentOriginalPosition = 0; Int32 currentModifiedPosition = 0; while (currSegment != null) { // Everything between the postiion markers and the start of the current common segment // will be either lines which were deleted or lines which were added Int32 linesDeleted = currSegment.OriginalStart - currentOriginalPosition; Int32 linesAdded = currSegment.ModifiedStart - currentModifiedPosition; summary.TotalLinesModified += Math.Min(linesDeleted, linesAdded); summary.TotalLinesAdded += Math.Max(0, linesAdded - linesDeleted); summary.TotalLinesDeleted += Math.Max(0, linesDeleted - linesAdded); // Advance the position markers to the end of the common section currentOriginalPosition = currSegment.OriginalStart + currSegment.OriginalLength; currentModifiedPosition = currSegment.ModifiedStart + currSegment.ModifiedLength; // Move to the next segment in the linked-list currSegment = currSegment.Next; } // After walking the linked-list of common sections, the position markers // will be pointing to the end of the file, thus we can infer how many lines // are in each file. summary.OriginalLineCount = currentOriginalPosition; summary.ModifiedLineCount = currentModifiedPosition; return(summary); }
private static Dictionary<int, int> Diff(DiffSegment diffSegment) { var result = new Dictionary<int, int>(); while (diffSegment != null) { int originalLine = diffSegment.OriginalStart; int modifiedLine = diffSegment.ModifiedStart; for (int i = 0; i < diffSegment.OriginalLength; i++) { result.Add(originalLine, modifiedLine); originalLine++; modifiedLine++; } diffSegment = diffSegment.Next; } return result; }
public void AddSegment(DiffSegment segment) { diffSegments.Add(segment); }