Пример #1
0
        private static string FormatDiff(DiffPaneModel diff)
        {
            var sb = new StringBuilder();
              foreach (var line in diff.Lines)
            AppendLine(sb, line);

              return sb.ToString();
        }
Пример #2
0
        public DiffPaneModel BuildDiffModel(string oldText, string newText)
        {
            if (oldText == null) throw new ArgumentNullException("oldText");
            if (newText == null) throw new ArgumentNullException("newText");

            var model = new DiffPaneModel();
            var diffResult = differ.CreateLineDiffs(oldText, newText, true);
            BuildDiffPieces(diffResult, model.Lines);
            return model;
        }
		protected override async void OnViewLoaded(object view) {
			base.OnViewLoaded(view);

			IsLoading = true;

			var diff = await GenerateDiffAsync();

			NewText = diff.NewText;
			OldText = diff.OldText;

			IsLoading = false;
		}
        private FlowDocument GenerateFlowDocument(DiffPaneModel diffPaneModel)
        {
            var flowDocument = new FlowDocument();
            var lineWidths = new List<double>();

            var stringBuilder = new StringBuilder();
            bool isFirstLine = true;
            ChangeType? previousLineType = null;
            DiffPiece previousLine = null;
            foreach (var line in diffPaneModel.Lines)
            {
                if (line.Type != ChangeType.Modified && isFirstLine)
                {
                    stringBuilder.Append(line.Text);
                }
                else if (line.Type != ChangeType.Modified && line.Type == previousLineType)
                {
                    stringBuilder.Append(Environment.NewLine + line.Text);
                }
                else if (!isFirstLine && (line.Type != previousLineType || previousLineType == ChangeType.Modified))
                {
                    Paragraph paragraph = GetParagraph(stringBuilder, previousLineType, previousLine);
                    flowDocument.Blocks.Add(paragraph);

                    stringBuilder.Clear();
                    if (line.Type != ChangeType.Modified)
                    {
                        stringBuilder.Append(line.Text);
                    }
                }

                isFirstLine = false;
                previousLineType = line.Type;
                previousLine = line;
                lineWidths.Add(CalculateLineWidth(line.Text));
            }

            // process last line
            if (previousLine != null)
            {
                Paragraph lastParagraph = GetParagraph(stringBuilder, previousLineType, previousLine);
                flowDocument.Blocks.Add(lastParagraph);
            }

            flowDocument.LineStackingStrategy = LineStackingStrategy.BlockLineHeight;
            flowDocument.PagePadding = _zeroThickness;
            flowDocument.PageWidth = Math.Min(lineWidths.DefaultIfEmpty(0).Max(), 1000000); // Throws an ArgumentException if value is too big. I think the maximum allowed is 1 million.
            return flowDocument;
        }
Пример #5
0
 public static string RenderDiffLines(DiffPaneModel diffModel)
 {
     StringBuilder result = new StringBuilder ();
     foreach (var line in diffModel.Lines)
     {
         if (line.Type == ChangeType.Deleted)
             result.AppendLine (PangoDeleteFormat + line.Text + PangoEnd);
         else if (line.Type == ChangeType.Inserted)
             result.AppendLine (PangoInsertFormat + line.Text + PangoEnd);
         else if (line.Type == ChangeType.Unchanged)
             result.AppendLine (line.Text);
         else if (line.Type == ChangeType.Modified)
             result.AppendLine (RenderDiffWords(line));
         else if (line.Type == ChangeType.Imaginary)
         {
             result.AppendLine ();
             logger.Debug ("Imaginary line {0}", line.Text);
         }
     }
     return result.ToString ().TrimEnd (Environment.NewLine.ToCharArray ());
 }
Пример #6
0
        private void RenderDiffLines(Grid grid, TextBox textBox, DiffPaneModel diffModel)
        {
            ClearDiffLines(grid);

            var lineNumber = 0;
            foreach (var line in diffModel.Lines)
            {
                var fillColor = new SolidColorBrush(Colors.Transparent);
                if (line.Type == ChangeType.Deleted)
                    fillColor = new SolidColorBrush(Color.FromArgb(255, 255, 200, 100));
                else if (line.Type == ChangeType.Inserted)
                    fillColor = new SolidColorBrush(Color.FromArgb(255, 255, 255, 0));
                else if (line.Type == ChangeType.Unchanged)
                    fillColor = new SolidColorBrush(Colors.White);
                else if (line.Type == ChangeType.Modified)
                {
                    if (currentFont.IsMonoSpaced)
                        RenderDiffWords(grid, textBox, line, lineNumber);
                    fillColor = new SolidColorBrush(Color.FromArgb(255, 220, 220, 255));
                }
                else if (line.Type == ChangeType.Imaginary)
                {
                    fillColor = new SolidColorBrush(Color.FromArgb(255, 200, 200, 200));

                    AddImaginaryLine(textBox, lineNumber);
                }

                if (ShowVisualAids)
                {
                    if (lineNumber % 2 == 0)
                        fillColor = new SolidColorBrush(Colors.Cyan);
                    else
                    {
                        fillColor = new SolidColorBrush(Colors.Gray);
                    }
                }

                PlaceRectangleInGrid(textBox, grid, lineNumber, fillColor, 0, null);
                lineNumber++;
            }
        }
Пример #7
0
 private void GenerateDiffPanes(DiffPaneModel leftDiff, DiffPaneModel rightDiff)
 {
     RenderDiffLines(leftGrid, leftBox, leftDiff);
     RenderDiffLines(rightGrid, rightBox, rightDiff);
 }
Пример #8
0
        private void OnDiffChange(DiffPaneModel oldValue, DiffPaneModel newValue)
        {
            var textBlocks = newValue.Lines.Select(Format).ToList();

            var paragraph = new Paragraph {
                KeepTogether = true
            };
            paragraph.Inlines.AddRange(textBlocks.Select(t => new Span() {
                Inlines = { t, new LineBreak() }
            }));

            var document = new FlowDocument {
                FontFamily = new FontFamily("Consolas"),
                FontSize = 11,
                Blocks = {paragraph},
                PageWidth = textBlocks.Max(block => {
                    block.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                    return block.DesiredSize.Width;
                }) + 50,
            };

            textBlocks.ForEach(tb => tb.Width = document.PageWidth);

            viewer.Document = document;
        }
Пример #9
0
        private void RenderDiffLines(Grid grid, DiffPaneModel diffModel)
        {
            ClearDiffLines(grid);

            var transparentFillColor = new SolidColorBrush(Colors.Transparent);
            var deletedFillColor = new SolidColorBrush(Color.FromArgb(255, 255, 200, 100));
            var insertedFillColor = new SolidColorBrush(Color.FromArgb(255, 255, 255, 0));
            var unchangedFillColor = new SolidColorBrush(Colors.White);
            var modifiedFillColor = new SolidColorBrush(Color.FromArgb(255, 220, 220, 255));
            var imaginaryFillColor = new SolidColorBrush(Color.FromArgb(255, 200, 200, 200));

            var lineNumber = 0;
            foreach (var line in diffModel.Lines)
            {
                var fillColor = transparentFillColor;

                switch (line.Type)
                {
                    case ChangeType.Deleted:
                        fillColor = deletedFillColor;
                        break;
                    case ChangeType.Inserted:
                        fillColor = insertedFillColor;
                        break;
                    case ChangeType.Unchanged:
                        fillColor = unchangedFillColor;
                        break;
                    case ChangeType.Modified:
                        fillColor = modifiedFillColor;
                        break;
                    case ChangeType.Imaginary:
                        fillColor = imaginaryFillColor;
                        break;
                }

                if (line.Type != ChangeType.Unchanged)
                {
                    PlaceRectangleInGrid(grid, lineNumber, fillColor, 0, null);
                }

                if (line.Type == ChangeType.Modified)
                    RenderDiffWords(grid, line, lineNumber);

                lineNumber++;
            }
        }
Пример #10
0
 public Different(DiffPlex.DiffBuilder.Model.DiffPaneModel diff)
 {
     Diff = diff;
 }