Beispiel #1
0
        /// <summary>
        /// Gets the side-by-side textual diffs.
        /// </summary>
        /// <param name="differ">The differ instance.</param>
        /// <param name="oldText">The old text to diff.</param>
        /// <param name="newText">The new text.</param>
        /// <param name="ignoreWhiteSpace">true if ignore the white space; othewise, false.</param>
        /// <param name="ignoreCase">true if case-insensitive; otherwise, false.</param>
        /// <param name="lineChunker">The line chunker.</param>
        /// <param name="wordChunker">The word chunker.</param>
        /// <returns>The diffs result.</returns>
        public static SideBySideDiffModel Diff(IDiffer differ, string oldText, string newText, bool ignoreWhiteSpace = true, bool ignoreCase = false, IChunker lineChunker = null, IChunker wordChunker = null)
        {
            if (oldText == null)
            {
                throw new ArgumentNullException(nameof(oldText));
            }
            if (newText == null)
            {
                throw new ArgumentNullException(nameof(newText));
            }

            if (differ == null)
            {
                return(Diff(oldText, newText, ignoreWhiteSpace, ignoreCase));
            }

            var model      = new SideBySideDiffModel();
            var diffResult = differ.CreateDiffs(oldText, newText, ignoreWhiteSpace, ignoreCase, lineChunker ?? LineChunker.Instance);

            BuildDiffPieces(diffResult, model.OldText.Lines, model.NewText.Lines, (ot, nt, op, np, iw, ic) =>
            {
                var r = differ.CreateDiffs(ot, nt, iw, ic, wordChunker ?? WordChunker.Instance);
                return(BuildDiffPieces(r, op, np, null, iw, ic));
            }, ignoreWhiteSpace, ignoreCase);

            return(model);
        }
        private SideBySideDiffModel BuildLineDiff(string oldText, string newText, bool ignoreWhitespace)
        {
            var model      = new SideBySideDiffModel();
            var diffResult = differ.CreateDiffs(oldText, newText, ignoreWhitespace, false, lineChunker);

            BuildDiffPieces(diffResult, model.OldText.Lines, model.NewText.Lines, BuildWordDiffPieces);
            return(model);
        }
Beispiel #3
0
        public DiffPaneModel BuildDiffModel(string oldText, string newText, bool ignoreWhitespace, bool ignoreCase, IChunker chunker)
        {
            if (oldText == null)
            {
                throw new ArgumentNullException(nameof(oldText));
            }
            if (newText == null)
            {
                throw new ArgumentNullException(nameof(newText));
            }

            var model = new DiffPaneModel();

            var diffResult = differ.CreateDiffs(oldText, newText, ignoreWhitespace, ignoreCase: ignoreCase, chunker);

            BuildDiffPieces(diffResult, model.Lines);
            return(model);
        }
 /// <summary>
 /// Finds the total size of diff (sum or all inserts and deletes) between old and new texts in an enumerable of text replacements.
 /// </summary>
 private int GetTotalDiffSize(IEnumerable <MappedTextReplacement> replacements) =>
 replacements.Sum(r =>
 {
     var diff = _differ.CreateDiffs(r.OriginalText.ToString(), r.NewText.ToString(), true, false, _chunker);
     return(diff.DiffBlocks.Any() ? diff.DiffBlocks.Select(b => b.DeleteCountA + b.InsertCountB).Sum() : 0);
 });