Example #1
0
        /// <summary>
        /// Gets th edit script of this line in comparison to its <see cref="Counterpart"/>.
        /// The change edit script can then be used to color each letter position in the line
        /// to indicate how one line can completely match the other using character based
        /// change operations (insert, delete, change, none).
        ///
        /// The method should only be invoked on demand (when a line is actually
        /// displayed) - we should wait with pulling it until we have to have it for rendering.
        ///
        /// This object will NOT cache the edit script so the caller should implement an external
        /// caching algorithm to avoid multiple computations of the same answer.
        ///
        /// Getting all intra-line diffs at once makes the whole process into an O(n^2) operation
        /// instead of just an O(n) operation for line-by-line diffs.  So we try to defer the
        /// extra work until the user requests to see the changed line.  It's still
        /// the same amount of work if the user views every line, but it makes the
        /// user interface more responsive to split it up like this.
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        public EditScript GetChangeEditScript(ChangeDiffOptions options)
        {
            EditScript _changeEditScript = null;

            if (            //_changeEditScript == null &&
                _editType == EditType.Change && this.Counterpart != null)
            {
                if (this.FromA)
                {
                    int trimCountA, trimCountB;
                    MyersDiff <char> diff = new MyersDiff <char>(
                        GetCharactersToDiff(this.text, options, out trimCountA),
                        GetCharactersToDiff(this.Counterpart.text, options, out trimCountB),
                        false);                         // We don't want Change edits; just Deletes and Inserts.

                    _changeEditScript = diff.Execute(null);

                    // If we trimmed/ignored leading whitespace, we have to offset each Edit to account for that.
                    foreach (Edit edit in _changeEditScript)
                    {
                        edit.Offset(trimCountA, trimCountB);
                    }
                }
                else if (this.Counterpart.FromA && this.Counterpart.Counterpart == this)
                {
                    // Defer to the A line because its edit script changes A into B.
                    _changeEditScript = this.Counterpart.GetChangeEditScript(options);
                }
            }

            return(_changeEditScript);
        }
Example #2
0
        /// <summary>
        /// Makes comparison of two collections of string lines
        /// </summary>
        /// <param name="listA">Lines sequence A</param>
        /// <param name="listB">Lines sequence B</param>
        /// <returns>Edit script to transform A to B</returns>
        public EditScript Execute(IList <string> listA, IList <string> listB)
        {
            int[] hashA = HashStringList(listA);
            int[] hashB = HashStringList(listB);

            MyersDiff <int> diff   = new MyersDiff <int>(hashA, hashB, _supportChangeEditType);
            EditScript      result = diff.Execute();

            return(result);
        }