public void TextDiff(string source, bool sourceIsFile, string destination, bool destinationIsFile) { using (new DisposableState(this, null)) { DiffListText sLf = null; DiffListText dLf = null; try { sLf = new DiffListText(source, sourceIsFile); dLf = new DiffListText(destination, destinationIsFile); } catch (Exception ex) { MessageBox.Show(ex.Message, "File Error"); return; } try { double time = 0; var de = new DiffEngine(); time = de.ProcessDiff(sLf, dLf, DiffEngineLevel.SlowPerfect); var rep = de.DiffReport(); ShowDiff(sLf, dLf, rep, time); } catch (Exception ex) { var tmp = string.Format("{0}{1}{1}***STACK***{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace); MessageBox.Show(tmp, "Compare Error"); } } }
private void ShowDiff(DiffListText source, DiffListText destination, List<DiffResultSpan> diffLines, double seconds) { _currentIndex = 0; _cmdDifferenceNext.Enabled = true; _cmdDifferencePrevious.Enabled = true; _differences = new List<int>(); _rtbSourceScript.BeginUpdate(); _rtbDestinationScript.BeginUpdate(); var sourceLines = new List<KeyValuePair<string, Color>>(); var destinationLines = new List<KeyValuePair<string, Color>>(); int i; foreach (var drs in diffLines) { switch (drs.Status) { case DiffResultSpanStatus.DeleteSource: _differences.Add(sourceLines.Count); for (i = 0; i < drs.Length; i++) { sourceLines.Add(new KeyValuePair<string, Color>(((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line, Color.Red)); destinationLines.Add(new KeyValuePair<string, Color>(new string(' ', ((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line.Length), Color.Blue)); } break; case DiffResultSpanStatus.NoChange: for (i = 0; i < drs.Length; i++) { sourceLines.Add(new KeyValuePair<string, Color>(((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line, Color.Empty)); destinationLines.Add(new KeyValuePair<string, Color>(((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line, Color.Empty)); } break; case DiffResultSpanStatus.AddDestination: _differences.Add(sourceLines.Count); for (i = 0; i < drs.Length; i++) { sourceLines.Add(new KeyValuePair<string, Color>(new string(' ', ((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line.Length), Color.Blue)); destinationLines.Add(new KeyValuePair<string, Color>(((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line, Color.Blue)); } break; case DiffResultSpanStatus.Replace: _differences.Add(sourceLines.Count); for (i = 0; i < drs.Length; i++) { sourceLines.Add(new KeyValuePair<string, Color>(((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line, Color.Red)); destinationLines.Add(new KeyValuePair<string, Color>(((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line, Color.Green)); } break; } } FillScript(sourceLines, _rtbSourceScript); FillScript(destinationLines, _rtbDestinationScript); _rtbSourceScript.EndUpdate(); _rtbDestinationScript.EndUpdate(); this.Text = _differences.Count + " differences."; }
private void ShowDiff(DiffListText source, DiffListText destination, List <DiffResultSpan> diffLines, double seconds) { _currentIndex = 0; _cmdDifferenceNext.Enabled = true; _cmdDifferencePrevious.Enabled = true; _differences = new List <int>(); _rtbSourceScript.BeginUpdate(); _rtbDestinationScript.BeginUpdate(); var sourceLines = new List <KeyValuePair <string, Color> >(); var destinationLines = new List <KeyValuePair <string, Color> >(); int i; foreach (var drs in diffLines) { switch (drs.Status) { case DiffResultSpanStatus.DeleteSource: _differences.Add(sourceLines.Count); for (i = 0; i < drs.Length; i++) { sourceLines.Add(new KeyValuePair <string, Color>(((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line, Color.Red)); destinationLines.Add(new KeyValuePair <string, Color>(new string(' ', ((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line.Length), Color.Blue)); } break; case DiffResultSpanStatus.NoChange: for (i = 0; i < drs.Length; i++) { sourceLines.Add(new KeyValuePair <string, Color>(((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line, Color.Empty)); destinationLines.Add(new KeyValuePair <string, Color>(((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line, Color.Empty)); } break; case DiffResultSpanStatus.AddDestination: _differences.Add(sourceLines.Count); for (i = 0; i < drs.Length; i++) { sourceLines.Add(new KeyValuePair <string, Color>(new string(' ', ((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line.Length), Color.Blue)); destinationLines.Add(new KeyValuePair <string, Color>(((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line, Color.Blue)); } break; case DiffResultSpanStatus.Replace: _differences.Add(sourceLines.Count); for (i = 0; i < drs.Length; i++) { sourceLines.Add(new KeyValuePair <string, Color>(((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line, Color.Red)); destinationLines.Add(new KeyValuePair <string, Color>(((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line, Color.Green)); } break; } } FillScript(sourceLines, _rtbSourceScript); FillScript(destinationLines, _rtbDestinationScript); _rtbSourceScript.EndUpdate(); _rtbDestinationScript.EndUpdate(); this.Text = _differences.Count + " differences."; }