Esempio n. 1
0
        private void AppendParagraph(RichTextBlockDiffContext richTextBlockData, string text, ref int index, Color?background = null, Color?foreground = null)
        {
            var paragraph = new Paragraph
            {
                LineStackingStrategy = LineStackingStrategy.BlockLineHeight,
                Foreground           = foreground.HasValue
                    ? _brushFactory.GetOrCreateSolidColorBrush(foreground.Value)
                    : _defaultForeground,
            };

            paragraph.LineHeight = paragraph.FontSize + 6;

            var run = new Run {
                Text = text
            };

            paragraph.Inlines.Add(run);

            richTextBlockData.Blocks.Add(paragraph);

            if (background != null)
            {
                richTextBlockData.AddTextHighlighter(new TextRange()
                {
                    StartIndex = index, Length = text.Length
                }, background.Value);
            }
            index += text.Length;
        }
Esempio n. 2
0
        private Inline NewRun(RichTextBlockDiffContext richTextBlockData, string text, ref int index, Color?background = null, Color?foreground = null)
        {
            var run = new Run
            {
                Text       = text,
                Foreground = foreground.HasValue
                            ? _brushFactory.GetOrCreateSolidColorBrush(foreground.Value)
                            : _defaultForeground
            };

            if (background != null)
            {
                richTextBlockData.AddTextHighlighter(new TextRange()
                {
                    StartIndex = index, Length = text.Length
                }, background.Value);
            }
            index += text.Length;
            return(run);
        }
Esempio n. 3
0
        private RichTextBlockDiffContext RenderDiff(System.Collections.Generic.List <OldNew <DiffPiece> > lines, Func <OldNew <DiffPiece>, DiffPiece> lineSelector, Func <OldNew <DiffPiece>, DiffPiece> pieceSelector)
        {
            var context = new RichTextBlockDiffContext(_brushFactory);
            int index   = 0;

            foreach (var line in lines)
            {
                var lineLength    = Math.Max(line.Old.Text?.Length ?? 0, line.New.Text?.Length ?? 0);
                var lineSubPieces = line.Old.SubPieces.Zip(line.New.SubPieces, (oldPiece, newPiece) => new OldNew <DiffPiece> {
                    Old = oldPiece, New = newPiece, Length = Math.Max(oldPiece.Text?.Length ?? 0, newPiece.Text?.Length ?? 0)
                });

                var oldNewLine = lineSelector(line);
                switch (oldNewLine.Type)
                {
                case ChangeType.Unchanged:
                    AppendParagraph(context, oldNewLine.Text ?? string.Empty, ref index, null);
                    break;

                case ChangeType.Imaginary:
                    AppendParagraph(context, new string(BreakingSpace, lineLength), ref index, Colors.Gray, Colors.LightCyan);
                    break;

                case ChangeType.Inserted:
                    AppendParagraph(context, oldNewLine.Text ?? string.Empty, ref index, Colors.LightGreen);
                    break;

                case ChangeType.Deleted:
                    AppendParagraph(context, oldNewLine.Text ?? string.Empty, ref index, Colors.OrangeRed);
                    break;

                case ChangeType.Modified:
                    context.Blocks.Add(ConstructModifiedParagraph(pieceSelector, lineSubPieces, context, ref index));
                    break;
                }
            }
            return(context);
        }
Esempio n. 4
0
        private Paragraph ConstructModifiedParagraph(Func <OldNew <DiffPiece>, DiffPiece> pieceSelector, IEnumerable <OldNew <DiffPiece> > lineSubPieces, RichTextBlockDiffContext context, ref int index)
        {
            var paragraph = new Paragraph()
            {
                LineStackingStrategy = LineStackingStrategy.BlockLineHeight,
                Foreground           = _defaultForeground,
            };

            paragraph.LineHeight = paragraph.FontSize + 6;

            foreach (var subPiece in lineSubPieces)
            {
                var oldNewPiece = pieceSelector(subPiece);
                switch (oldNewPiece.Type)
                {
                case ChangeType.Unchanged: paragraph.Inlines.Add(NewRun(context, oldNewPiece.Text ?? string.Empty, ref index, Colors.Yellow)); break;

                case ChangeType.Imaginary: paragraph.Inlines.Add(NewRun(context, oldNewPiece.Text ?? string.Empty, ref index)); break;

                case ChangeType.Inserted: paragraph.Inlines.Add(NewRun(context, oldNewPiece.Text ?? string.Empty, ref index, Colors.LightGreen)); break;

                case ChangeType.Deleted: paragraph.Inlines.Add(NewRun(context, oldNewPiece.Text ?? string.Empty, ref index, Colors.OrangeRed)); break;

                case ChangeType.Modified: paragraph.Inlines.Add(NewRun(context, oldNewPiece.Text ?? string.Empty, ref index, Colors.Yellow)); break;
                }
                paragraph.Inlines.Add(NewRun(context, new string(BreakingSpace, subPiece.Length - (oldNewPiece.Text ?? string.Empty).Length), ref index, Colors.Gray, Colors.LightCyan));
            }

            return(paragraph);
        }