예제 #1
0
        static void WritePiece(StringBuilder html, DiffPiece piece)
        {
            if (piece.Type == DiffPlex.DiffBuilder.Model.ChangeType.Unchanged)
            {
                WriteSpan(html, "diff-unchanged", piece.Text);
            }
            else if (piece.Type == DiffPlex.DiffBuilder.Model.ChangeType.Inserted)
            {
                WriteSpan(html, "diff-inserted", piece.Text);
            }
            else if (piece.Type == DiffPlex.DiffBuilder.Model.ChangeType.Deleted)
            {
                WriteSpan(html, "diff-deleted", piece.Text);
            }
            else if (piece.Type == DiffPlex.DiffBuilder.Model.ChangeType.Modified)
            {
                html.Append("<span class=\"diff-modified\">");

                foreach (var subpiece in piece.SubPieces)
                {
                    WritePiece(html, subpiece);
                }

                html.Append("</span>");
            }
        }
예제 #2
0
 private static string FormatText(DiffPiece diffPiece)
 {
     return new StringBuilder(diffPiece.Text)
         .Replace(' ', '·')
         .Replace("\t", "→   ")
         .ToString();
 }
예제 #3
0
 private TextBlock Format(DiffPiece diffPiece)
 {
     return new TextBlock() {
         Text = FormatText(diffPiece),
         TextWrapping = TextWrapping.NoWrap,
         Background = GetLineBackgroundBrush(diffPiece.Type)
     };
 }
예제 #4
0
        private static string RenderDiffCharacter(DiffPiece word, ref ChangeType lastAction)
        {
            StringBuilder result = new StringBuilder ();
            foreach (var characters in word.SubPieces)
            {
                if (characters.Type == ChangeType.Imaginary)
                    continue;

                if (lastAction != ChangeType.Unchanged && lastAction != characters.Type)
                    result.Append (PangoEnd);

                result.Append (StartSpan (characters.Type, lastAction)).Append (characters.Text);
                lastAction = characters.Type;
            }
            return result.ToString ();
        }
예제 #5
0
        private static void AppendLine(StringBuilder sb, DiffPiece line)
        {
            switch (line.Type)
              {
            case ChangeType.Inserted:
              sb.Append("+ ");
              break;
            case ChangeType.Deleted:
              sb.Append("- ");
              break;
            default:
              sb.Append("  ");
              break;
              }

              sb.AppendLine(line.Text);
        }
예제 #6
0
    private static string PrintDiffLine(DiffPiece line)
    {
        StringBuilder sb = new StringBuilder();
        if (line.Type == ChangeType.Unchanged)
        {
            sb.Append(line.Text);
        }
        else
        {
            foreach (var piece in line.SubPieces)
            {
                if (string.IsNullOrEmpty(piece.Text))
                    continue;

                if (piece.Type == ChangeType.Unchanged)
                {
                    sb.Append(string.Format("{0}", HttpUtility.HtmlEncode(piece.Text)));
                }
                else if (piece.Type == ChangeType.Deleted || piece.Type == ChangeType.Inserted)
                {
                    sb.Append(string.Format("<span style='color:red'>{0}</span>", HttpUtility.HtmlEncode(piece.Text)));
                }
                else if (!IsNumber(piece.Text))
                {
                    sb.Append(string.Format("<span style='color:red'>{0}</span>", HttpUtility.HtmlEncode(piece.Text)));
                }
                else
                {
                    foreach (var character in piece.SubPieces)
                    {
                        if (character.Type == ChangeType.Unchanged)
                        {
                            sb.Append(string.Format("{0}", HttpUtility.HtmlEncode(character.Text)));
                        }
                        else
                        {
                            sb.Append(string.Format("<span style='color:red'>{0}</span>", HttpUtility.HtmlEncode(character.Text)));
                        }
                    }
                }
            }
        }

        return sb.ToString();
    }
예제 #7
0
 public static double LineDiff(DiffPiece Line, int FullLength)
 {
     double OverAll = 0;
     foreach (DiffPiece Word in Line.SubPieces)
     {
         switch (Word.Type)
         {
             case ChangeType.Deleted:
                 OverAll = OverAll + GetWeight(Word.Text.Length, FullLength);
                 break;
             case ChangeType.Inserted:
                 OverAll = OverAll + GetWeight(Word.Text.Length, FullLength);
                 break;
             case ChangeType.Imaginary:
             case ChangeType.Unchanged:
             case ChangeType.Modified:
                 break;
         }
     }
     return OverAll;
 }
예제 #8
0
        private static string RenderDiffWords(DiffPiece line)
        {
            StringBuilder result = new StringBuilder ();
            ChangeType lastAction = ChangeType.Unchanged;
            foreach (var word in line.SubPieces) {
                if (word.Type == ChangeType.Imaginary)
                    continue;

                if (word.Type == ChangeType.Modified)
                    result.Append (RenderDiffCharacter (word, ref lastAction));
                else
                {
                    if (lastAction != ChangeType.Unchanged && lastAction != word.Type)
                        result.Append (PangoEnd);
                    result.Append (StartSpan (word.Type, lastAction)).Append (word.Text);
                    lastAction = word.Type;
                }
            }
            if(lastAction != ChangeType.Unchanged)
                result.Append (PangoEnd);
            return result.ToString ();
        }
예제 #9
0
        private static void BuildDiffPieces(DiffResult diffResult, List<DiffPiece> oldPieces, List<DiffPiece> newPieces, PieceBuilder subPieceBuilder)
        {
            int aPos = 0;
            int bPos = 0;

            foreach (var diffBlock in diffResult.DiffBlocks)
            {
                while (bPos < diffBlock.InsertStartB && aPos < diffBlock.DeleteStartA)
                {
                    oldPieces.Add(new DiffPiece(diffResult.PiecesOld[aPos], ChangeType.Unchanged, aPos + 1));
                    newPieces.Add(new DiffPiece(diffResult.PiecesNew[bPos], ChangeType.Unchanged, bPos + 1));
                    aPos++;
                    bPos++;
                }

                int i = 0;
                for (; i < Math.Min(diffBlock.DeleteCountA, diffBlock.InsertCountB); i++)
                {
                    var oldPiece = new DiffPiece(diffResult.PiecesOld[i + diffBlock.DeleteStartA], ChangeType.Deleted, aPos + 1);
                    var newPiece = new DiffPiece(diffResult.PiecesNew[i + diffBlock.InsertStartB], ChangeType.Inserted, bPos + 1);

                    if (subPieceBuilder != null)
                    {
                        subPieceBuilder(diffResult.PiecesOld[aPos], diffResult.PiecesNew[bPos], oldPiece.SubPieces, newPiece.SubPieces);
                        newPiece.Type = oldPiece.Type = ChangeType.Modified;
                    }

                    oldPieces.Add(oldPiece);
                    newPieces.Add(newPiece);
                    aPos++;
                    bPos++;
                }

                if (diffBlock.DeleteCountA > diffBlock.InsertCountB)
                {
                    for (; i < diffBlock.DeleteCountA; i++)
                    {
                        oldPieces.Add(new DiffPiece(diffResult.PiecesOld[i + diffBlock.DeleteStartA], ChangeType.Deleted, aPos + 1));
                        newPieces.Add(new DiffPiece());
                        aPos++;
                    }
                }
                else
                {
                    for (; i < diffBlock.InsertCountB; i++)
                    {
                        newPieces.Add(new DiffPiece(diffResult.PiecesNew[i + diffBlock.InsertStartB], ChangeType.Inserted, bPos + 1));
                        oldPieces.Add(new DiffPiece());
                        bPos++;
                    }
                }
            }

            while (bPos < diffResult.PiecesNew.Length && aPos < diffResult.PiecesOld.Length)
            {
                oldPieces.Add(new DiffPiece(diffResult.PiecesOld[aPos], ChangeType.Unchanged, aPos + 1));
                newPieces.Add(new DiffPiece(diffResult.PiecesNew[bPos], ChangeType.Unchanged, bPos + 1));
                aPos++;
                bPos++;
            }
        }
예제 #10
0
        private void RenderDiffWords(Grid grid, TextBox textBox, DiffPiece line, int lineNumber)
        {
            var charPos = 0;
            var characterWidth = CharacterWidthOverride ?? currentFont.CharacterWidth;
            var leftOffset = LeftOffsetOverride ?? currentFont.LeftOffset;
            foreach (var word in line.SubPieces)
            {
                SolidColorBrush fillColor;
                if (word.Type == ChangeType.Deleted)
                    fillColor = new SolidColorBrush(Color.FromArgb(255, 200, 100, 100));
                else if (word.Type == ChangeType.Inserted)
                    fillColor = new SolidColorBrush(Color.FromArgb(255, 255, 255, 150));
                else if (word.Type == ChangeType.Imaginary)
                    continue;
                else
                    fillColor = new SolidColorBrush(Colors.Transparent);

                var left = characterWidth * charPos + leftOffset;
                var wordWidth = characterWidth * word.Text.Length;
                PlaceRectangleInGrid(textBox, grid, lineNumber, fillColor, left, wordWidth);

                charPos += word.Text.Length;
            }
        }
예제 #11
0
 internal static string LineDiff(DiffPiece Line)
 {
     StringBuilder DR = new StringBuilder();
     foreach (DiffPiece Word in Line.SubPieces)
     {
         switch (Word.Type)
         {
             case ChangeType.Deleted:
                 DR.Append(@"\highlight2 "); DR.Append(Tools.RtfSafe(Word.Text)); DR.Append(@" \highlight0 ");
                 break;
             case ChangeType.Inserted:
                 DR.Append(@"\highlight1 "); DR.Append(Tools.RtfSafe(Word.Text)); DR.Append(@" \highlight0 ");
                 break;
             case ChangeType.Imaginary:
                 break;
             case ChangeType.Unchanged:
                 DR.Append(Tools.RtfSafe(Word.Text));
                 break;
             case ChangeType.Modified:
                 DR.Append(Tools.RtfSafe(Word.Text));
                 break;
         }
     }
     return DR.ToString();
 }
 private Paragraph RenderDiffWords(DiffPiece line)
 {
     var paragraph = new Paragraph();
     foreach (var word in line.SubPieces)
     {
         if (word.Type == ChangeType.Imaginary) continue;
         var run = new Run(word.Text);
         switch (word.Type)
         {
             case ChangeType.Deleted:
                 run.Background = _deletedWordFillColor;
                 break;
             case ChangeType.Inserted:
                 run.Background = _insertedWordFillColor;
                 break;
             default :
                 run.Background = _unchangedWordFillColor;
                 break;
         }
         paragraph.Inlines.Add(run);
     }
     return paragraph;
 }
        private Paragraph GetParagraph(StringBuilder stringBuilder, ChangeType? lineType, DiffPiece line)
        {
            Run run;
            Paragraph paragraph = null;

            switch (lineType)
            {
                case ChangeType.Unchanged:

                    run = new Run {Text = stringBuilder.ToString()};
                    paragraph = new Paragraph(run);
                    break;
                case ChangeType.Modified:
                    paragraph = RenderDiffWords(line);
                    paragraph.Background = _modifiedFillColor;
                    break;
                case ChangeType.Deleted:
                    run = new Run { Text = stringBuilder.ToString() };
                    paragraph = new Paragraph(run) {Background = _deletedFillColor};
                    break;
                case ChangeType.Inserted:
                    run = new Run { Text = stringBuilder.ToString() };
                    paragraph = new Paragraph(run) {Background = _insertedFillColor};
                    break;
                case ChangeType.Imaginary:
                    run = new Run { Text = stringBuilder.ToString() };
                    paragraph = new Paragraph(run) {Background = _imaginaryFillColor};
                    break;
            }

            if (paragraph != null)
            {
                paragraph.FontFamily = _fontFamily;
                paragraph.FontSize = FontSize;
                paragraph.Margin = _zeroThickness;
                paragraph.LineHeight = _lineHeight;
            }

            return paragraph;
        }