public static void CompareText( FastColoredTextBoxNS.FastColoredTextBox textBox1, FastColoredTextBoxNS.FastColoredTextBox textBox2, string text1, string text2) { var differ = new DiffPlex.Differ(); var builder = new DiffPlex.DiffBuilder.SideBySideDiffBuilder(differ); var result = builder.BuildDiffModel(text1, text2); var brushInserted = new SolidBrush(ColorInserted); var brushDeleted = new SolidBrush(ColorDeleted); var brushEmpty = new SolidBrush(ColorImaginary); var brushModified = new SolidBrush(ColorModified); textBox1.Text = string.Join("\n", result.OldText.Lines.Select(x => x.Text)); textBox2.Text = string.Join("\n", result.NewText.Lines.Select(x => x.Text)); for (var index = 0; index < result.OldText.Lines.Count; index++) { var oldLine = result.OldText.Lines[index]; var newLine = result.NewText.Lines[index]; if (oldLine.Type == DiffPlex.DiffBuilder.Model.ChangeType.Inserted) { textBox1.TextSource[index].BackgroundBrush = brushInserted; } else if (oldLine.Type == DiffPlex.DiffBuilder.Model.ChangeType.Deleted) { textBox1.TextSource[index].BackgroundBrush = brushDeleted; } else if (oldLine.Type == DiffPlex.DiffBuilder.Model.ChangeType.Imaginary) { textBox1.TextSource[index].BackgroundBrush = brushEmpty; } else if (oldLine.Type == DiffPlex.DiffBuilder.Model.ChangeType.Modified) { textBox1.TextSource[index].BackgroundBrush = brushModified; } if (newLine.Type == DiffPlex.DiffBuilder.Model.ChangeType.Inserted) { textBox2.TextSource[index].BackgroundBrush = brushInserted; } else if (newLine.Type == DiffPlex.DiffBuilder.Model.ChangeType.Deleted) { textBox2.TextSource[index].BackgroundBrush = brushDeleted; } else if (newLine.Type == DiffPlex.DiffBuilder.Model.ChangeType.Imaginary) { textBox2.TextSource[index].BackgroundBrush = brushEmpty; } else if (newLine.Type == DiffPlex.DiffBuilder.Model.ChangeType.Modified) { textBox2.TextSource[index].BackgroundBrush = brushModified; } } }
public bool make(TextDocument textDocument, String orgText, String newText) { BreakpointsManager hBPMgr = new BreakpointsManager(m_serviceProvider); BookmarkManager hBMMgr = new BookmarkManager(); Document hDocument = textDocument.Parent as Document; DiffPlex.Differ diff = new DiffPlex.Differ(); DiffPlex.Model.DiffResult result = diff.CreateLineDiffs(orgText, newText, false, false); hBPMgr.Save(hDocument.FullName, false); hBMMgr.Save(textDocument); textDocument.DTE.UndoContext.Open("CodeBeautiful"); try { int lineOffset = 0; foreach (var diffBlock in result.DiffBlocks) { EnvDTE80.EditPoint2 editpoint = textDocument.CreateEditPoint() as EnvDTE80.EditPoint2; EnvDTE80.EditPoint2 endLine = textDocument.CreateEditPoint() as EnvDTE80.EditPoint2; editpoint.MoveToLineAndOffset(diffBlock.DeleteStartA + lineOffset + 1, 1); try { endLine.MoveToLineAndOffset(diffBlock.DeleteStartA + lineOffset + diffBlock.DeleteCountA + 1, 1); } catch (System.ArgumentException /*ex*/) { endLine.MoveToLineAndOffset(diffBlock.DeleteStartA + lineOffset + diffBlock.DeleteCountA, 1); endLine.EndOfLine(); } editpoint.Delete(endLine); editpoint.InsertNewLine(diffBlock.InsertCountB); for (int line = 0; line < diffBlock.InsertCountB; ++line) { editpoint.MoveToLineAndOffset(diffBlock.DeleteStartA + lineOffset + line + 1, 1); editpoint.Insert(result.PiecesNew[diffBlock.InsertStartB + line]); } lineOffset += diffBlock.InsertCountB - diffBlock.DeleteCountA; } } catch (Exception e) { textDocument.DTE.UndoContext.Close(); throw e; } textDocument.DTE.UndoContext.Close(); // Restoring break points should take into consideration that some lines was changed or added hBPMgr.Restore(); hBMMgr.Restore(); return(true); }
private bool ShowDiff( SchemaChange[] dbChanges, Dictionary <string, string> old, Dictionary <string, string> current, string title) { var changes = new List <DiffModel>(); var diff = new DiffPlex.Differ(); foreach (var k in current) { if (old.ContainsKey(k.Key)) { var oldValue = old[k.Key]; var modified = DiffModel.ModificationType.NotModified; if (oldValue != k.Value) { var ld = diff.CreateLineDiffs(oldValue, k.Value, true); if (ld.DiffBlocks.Count > 0) { modified = DiffModel.ModificationType.Modified; } } changes.Add(new DiffModel { FileName = k.Key, OriginalSource = old[k.Key], NewSource = k.Value, Modified = modified }); } else { changes.Add(new DiffModel { FileName = k.Key, NewSource = k.Value, Modified = DiffModel.ModificationType.Created }); } } foreach (var k in old) { if (!current.ContainsKey(k.Key)) { changes.Add(new DiffModel { FileName = k.Key, OriginalSource = k.Value, Modified = DiffModel.ModificationType.Deleted }); } } var hasChanges = changes.Any(it => it.Modified != DiffModel.ModificationType.NotModified); DbChanges = dbChanges; DslDiff = changes.ToArray(); if (hasChanges) { LastTool.Dispatcher.BeginInvoke((Action)(() => CreateDiffWindow(title))); } return(hasChanges); }
private void bg_DoWork(object sender, DoWorkEventArgs e) { Word.Application wordApp = e.Argument as Word.Application; BackgroundWorker bg = sender as BackgroundWorker; string lastPage = string.Empty; bool isChanged = false; DateTime dtChaged = DateTime.Now; while (true) { try { if (Application.Documents.Count > 0) { if (Application.ActiveDocument.Words.Count > 0) { var currentPage = Application.ActiveDocument.Bookmarks["\\Page"].Range.Text; if (currentPage != null && currentPage != lastPage) { var differ = new DiffPlex.Differ(); var builder = new DiffPlex.DiffBuilder.InlineDiffBuilder(differ); var difference = builder.BuildDiffModel(lastPage, currentPage); var change = from d in difference.Lines where d.Type != DiffPlex.DiffBuilder.Model.ChangeType.Unchanged select d; if (change.Any()) { string changeLastText = change.Last().Text; if (!string.IsNullOrEmpty(changeLastText)) { dtChaged = DateTime.Now; isChanged = true; } } lastPage = currentPage; } TimeSpan span = (TimeSpan)(DateTime.Now - dtChaged); if (isChanged && span.Milliseconds >= 300) { isChanged = false; bg.ReportProgress(50, ""); } } } } catch (Exception) { } if (bg.CancellationPending) { break; } System.Threading.Thread.Sleep(100); } }
private void Scroller_Loaded(object sender, RoutedEventArgs e) { var scrollViewer = (ScrollViewer)sender; var ctx = scrollViewers[0].DataContext as DiffModel; var diff = new DiffPlex.Differ(); var ld = diff.CreateLineDiffs(ctx.OriginalSource ?? string.Empty, ctx.NewSource ?? string.Empty, true); var total = Math.Max(ld.PiecesNew.Length, ld.PiecesOld.Length); if (ld.DiffBlocks.Count > 0 && total > 0) { var line = Math.Min(ld.DiffBlocks[0].InsertStartB, ld.DiffBlocks[0].DeleteStartA); var lh = scrollViewer.ExtentHeight / total; if (line > total / 2 && lh > 0) line += (int)(scrollViewer.ViewportHeight / lh) / 2; if (line > total) line = total; if (scrollViewer.ViewportHeight / 2 < lh * line) verticalScrollOffset = (scrollViewer.ExtentHeight - scrollViewer.ViewportHeight) * line / total; } scrollViewer.ScrollToVerticalOffset(verticalScrollOffset); scrollViewer.ScrollToHorizontalOffset(horizontalScrollOffset); scrollViewer.Opacity = 1; if (verticalScrollerViewers.Count > 0) scrollViewer.ScrollToVerticalOffset(verticalScrollOffset); scrollViewer.ApplyTemplate(); var scrollViewerRoot = (FrameworkElement)VisualTreeHelper.GetChild(scrollViewer, 0); var horizontalScrollBar = (ScrollBar)scrollViewerRoot.FindName("PART_HorizontalScrollBar"); var verticalScrollBar = (ScrollBar)scrollViewerRoot.FindName("PART_VerticalScrollBar"); if (!horizontalScrollerViewers.Keys.Contains(horizontalScrollBar)) { horizontalScrollerViewers.Add(horizontalScrollBar, scrollViewer); } if (!verticalScrollerViewers.Keys.Contains(verticalScrollBar)) { verticalScrollerViewers.Add(verticalScrollBar, scrollViewer); } if (horizontalScrollBar != null) { horizontalScrollBar.Scroll += HorizontalScrollBar_Scroll; horizontalScrollBar.ValueChanged += HorizontalScrollBar_ValueChanged; } if (verticalScrollBar != null) { verticalScrollBar.Scroll += VerticalScrollBar_Scroll; verticalScrollBar.ValueChanged += VerticalScrollBar_ValueChanged; } }
//сравнение private void Differ() { StringBuilder sb = new StringBuilder(); List<string> inserted = new List<string>(); List<string> deleted = new List<string>(); List<string> modified = new List<string>(); List<string> imaginary = new List<string>(); var d = new DiffPlex.Differ(); var builder = new InlineDiffBuilder(d); var result = builder.BuildDiffModel(oldText, newText); foreach (var line in result.Lines) { if (line.Type == ChangeType.Inserted) { //Color Green inserted.Add(line.Text); // sb.Append("+ "); } else if (line.Type == ChangeType.Deleted) { //Color Red deleted.Add(line.Text); // sb.Append("- "); } else if (line.Type == ChangeType.Modified) { //Color Blue modified.Add(line.Text); // sb.Append("* "); } else if (line.Type == ChangeType.Imaginary) { //Color.Orange imaginary.Add(line.Text); // sb.Append("? "); } else if (line.Type == ChangeType.Unchanged) { // sb.Append(" "); } sb.Append(line.Text + "\r\n"); } string res = sb.ToString(); richTextBoxDiff.Text = res; //проблема с подцветкой реализовать другой алгоритм //обработка цветом foreach (string str in inserted) { HighlightText(richTextBoxDiff, str, Color.GreenYellow); } foreach (string str in deleted) { HighlightText(richTextBoxDiff, str, Color.IndianRed); } foreach (string str in modified) { HighlightText(richTextBoxDiff, str, Color.Blue); } foreach (string str in imaginary) { HighlightText(richTextBoxDiff, str, Color.Orange); } }
//сравнение вариант производительнее private void DifferTable(object sender, EventArgs e) { //DateTime start = DateTime.Now; StringBuilder sb = new StringBuilder(); Hashtable insertedTable = new Hashtable(); Hashtable deletedTable = new Hashtable(); Hashtable modifiedTable = new Hashtable(); Hashtable imaginaryTable = new Hashtable(); var d = new DiffPlex.Differ(); var builder = new InlineDiffBuilder(d); var result = builder.BuildDiffModel(oldText, newText); foreach (var line in result.Lines) { if (line.Type == ChangeType.Inserted) { //Color Green //inserted.Add(line.Text); if (!insertedTable.ContainsKey(line.Text)) { insertedTable.Add(line.Text, "INSERTED"); } // sb.Append("+ "); } else if (line.Type == ChangeType.Deleted) { //Color Red // deleted.Add(line.Text); if (!deletedTable.ContainsKey(line.Text)) { deletedTable.Add(line.Text, "DELETED"); } // sb.Append("- "); } else if (line.Type == ChangeType.Modified) { //Color Blue // modified.Add(line.Text); if (!modifiedTable.ContainsKey(line.Text)) { modifiedTable.Add(line.Text, "MODIFIED"); } // sb.Append("* "); } else if (line.Type == ChangeType.Imaginary) { //Color.Orange //imaginary.Add(line.Text); if (!imaginaryTable.ContainsKey(line.Text)) { imaginaryTable.Add(line.Text, "MODIFIED"); } // sb.Append("? "); } else if (line.Type == ChangeType.Unchanged) { // sb.Append(" "); } sb.Append(line.Text + "\r\n"); } string res = sb.ToString(); richTextBoxDiff.Text = res; //проблема с подцветкой реализовать другой алгоритм //обработка цветом foreach (string str in insertedTable.Keys) { HighlightText(richTextBoxDiff, str, Color.GreenYellow); } foreach (string str in deletedTable.Keys) { HighlightText(richTextBoxDiff, str, Color.IndianRed); } foreach (string str in modifiedTable.Keys) { HighlightText(richTextBoxDiff, str, Color.Blue); } foreach (string str in imaginaryTable.Keys) { HighlightText(richTextBoxDiff, str, Color.Blue); } //TimeSpan time = DateTime.Now - start; //MessageBox.Show(time.ToString()); }
private void Scroller_Loaded(object sender, RoutedEventArgs e) { var scrollViewer = (ScrollViewer)sender; var ctx = scrollViewers[0].DataContext as DiffModel; var diff = new DiffPlex.Differ(); var ld = diff.CreateLineDiffs(ctx.OriginalSource ?? string.Empty, ctx.NewSource ?? string.Empty, true); var total = Math.Max(ld.PiecesNew.Length, ld.PiecesOld.Length); if (ld.DiffBlocks.Count > 0 && total > 0) { var line = Math.Min(ld.DiffBlocks[0].InsertStartB, ld.DiffBlocks[0].DeleteStartA); var lh = scrollViewer.ExtentHeight / total; if (line > total / 2 && lh > 0) { line += (int)(scrollViewer.ViewportHeight / lh) / 2; } if (line > total) { line = total; } if (scrollViewer.ViewportHeight / 2 < lh * line) { verticalScrollOffset = (scrollViewer.ExtentHeight - scrollViewer.ViewportHeight) * line / total; } } scrollViewer.ScrollToVerticalOffset(verticalScrollOffset); scrollViewer.ScrollToHorizontalOffset(horizontalScrollOffset); scrollViewer.Opacity = 1; if (verticalScrollerViewers.Count > 0) { scrollViewer.ScrollToVerticalOffset(verticalScrollOffset); } scrollViewer.ApplyTemplate(); var scrollViewerRoot = (FrameworkElement)VisualTreeHelper.GetChild(scrollViewer, 0); var horizontalScrollBar = (ScrollBar)scrollViewerRoot.FindName("PART_HorizontalScrollBar"); var verticalScrollBar = (ScrollBar)scrollViewerRoot.FindName("PART_VerticalScrollBar"); if (!horizontalScrollerViewers.Keys.Contains(horizontalScrollBar)) { horizontalScrollerViewers.Add(horizontalScrollBar, scrollViewer); } if (!verticalScrollerViewers.Keys.Contains(verticalScrollBar)) { verticalScrollerViewers.Add(verticalScrollBar, scrollViewer); } if (horizontalScrollBar != null) { horizontalScrollBar.Scroll += HorizontalScrollBar_Scroll; horizontalScrollBar.ValueChanged += HorizontalScrollBar_ValueChanged; } if (verticalScrollBar != null) { verticalScrollBar.Scroll += VerticalScrollBar_Scroll; verticalScrollBar.ValueChanged += VerticalScrollBar_ValueChanged; } }
/// <summary> /// Shows the schema. /// </summary> private void ShowSchema() { string fileText = string.Empty; string databaseText = string.Empty; if (this.fileSide != null) { fileText = this.GetFile(this.fileSide); } if (this.databaseSide != null) { databaseText = this.GetDBSchema(this.databaseSide); } if (this.checkBoxDiff.Active) { StyleTextLineMarker addMarker = new StyleTextLineMarker() { BackgroundColor = new Cairo.Color(0.8, 0.8, 1) }; StyleTextLineMarker deleteMarker = new StyleTextLineMarker() { BackgroundColor = new Cairo.Color(1, 0.8, 0.8) }; StyleTextLineMarker modifyMarker = new StyleTextLineMarker() { BackgroundColor = new Cairo.Color(1, 1, 0.6) }; StyleTextLineMarker grayMarker = new StyleTextLineMarker() { BackgroundColor = new Cairo.Color(0.6, 0.6, 0.6) }; DiffResult result = new DiffPlex.Differ().CreateLineDiffs(databaseText, fileText, false); Dictionary<int, TextLineMarker> databaseMarker = new Dictionary<int, TextLineMarker>(); Dictionary<int, TextLineMarker> fileMarker = new Dictionary<int, TextLineMarker>(); string[] fileTextArray = fileText.Split('\n'); string[] databaseTextArray = databaseText.Split('\n'); List<string> processedFile = new List<string>(); List<string> processedDatabase = new List<string>(); int i1 = 0; // original database line number, start from 0 int i2 = 0; // original file line number, start from 0 int d1 = -1; int d2 = -1; DiffBlock currentBlock = new DiffBlock(-1, 0, -1, 0); if (result.DiffBlocks.Count > 0) { currentBlock = result.DiffBlocks[0]; result.DiffBlocks.RemoveAt(0); } for (int i = 0; true; i++) { bool change = false; if (i1 == currentBlock.DeleteStartA) { d1 = 0; } if (d1 != -1) { change = true; if (d1 < currentBlock.DeleteCountA) { processedDatabase.Add(databaseTextArray[i1]); databaseMarker.Add(i + 1, deleteMarker); processedFile.Add(string.Empty); fileMarker.Add(i + 1, deleteMarker); i1++; d1++; } else { d1 = -1; } } if (i2 == currentBlock.InsertStartB) { d2 = 0; } if (d2 != -1) { change = true; if (d2 < currentBlock.InsertCountB) { processedDatabase.Add(string.Empty); if (databaseMarker.ContainsKey(i + 1)) { databaseMarker[i + 1] = modifyMarker; } else { databaseMarker.Add(i + 1, addMarker); } processedFile.Add(fileTextArray[i2]); if (fileMarker.ContainsKey(i + 1)) { fileMarker[i + 1] = modifyMarker; } else { fileMarker.Add(i + 1, addMarker); } i2++; d2++; } else { d2 = -1; } } // Stop the iteration if we are on the last line now if (i1 >= databaseTextArray.Length && i2 >= fileTextArray.Length) { break; } if (d1 == -1 && d2 == -1 && change) { if (result.DiffBlocks.Count > 0) { currentBlock = result.DiffBlocks[0]; result.DiffBlocks.RemoveAt(0); } else { currentBlock = new DiffBlock(-1, 0, -1, 0); } change = false; } // No changes, insert the original text without marker if (!change) { processedDatabase.Add(databaseTextArray[i1]); processedFile.Add(fileTextArray[i2]); i1++; i2++; } } this.docDB.Text = string.Join("\n", processedDatabase.ToArray()); this.docLocal.Text = string.Join("\n", processedFile.ToArray()); foreach (KeyValuePair<int, TextLineMarker> single in databaseMarker) { this.docDB.AddMarker(single.Key, single.Value); } foreach (KeyValuePair<int, TextLineMarker> single in fileMarker) { this.docLocal.AddMarker(single.Key, single.Value); } this.docDB.CommitUpdateAll(); } else { this.docDB.Text = databaseText; this.docLocal.Text = fileText; } }
//сравнение private void Differ() { StringBuilder sb = new StringBuilder(); List <string> inserted = new List <string>(); List <string> deleted = new List <string>(); List <string> modified = new List <string>(); List <string> imaginary = new List <string>(); var d = new DiffPlex.Differ(); var builder = new InlineDiffBuilder(d); var result = builder.BuildDiffModel(oldText, newText); foreach (var line in result.Lines) { if (line.Type == ChangeType.Inserted) { //Color Green inserted.Add(line.Text); // sb.Append("+ "); } else if (line.Type == ChangeType.Deleted) { //Color Red deleted.Add(line.Text); // sb.Append("- "); } else if (line.Type == ChangeType.Modified) { //Color Blue modified.Add(line.Text); // sb.Append("* "); } else if (line.Type == ChangeType.Imaginary) { //Color.Orange imaginary.Add(line.Text); // sb.Append("? "); } else if (line.Type == ChangeType.Unchanged) { // sb.Append(" "); } sb.Append(line.Text + "\r\n"); } string res = sb.ToString(); richTextBoxDiff.Text = res; //проблема с подцветкой реализовать другой алгоритм //обработка цветом foreach (string str in inserted) { HighlightText(richTextBoxDiff, str, Color.GreenYellow); } foreach (string str in deleted) { HighlightText(richTextBoxDiff, str, Color.IndianRed); } foreach (string str in modified) { HighlightText(richTextBoxDiff, str, Color.Blue); } foreach (string str in imaginary) { HighlightText(richTextBoxDiff, str, Color.Orange); } }