public IReadOnlyList <DocumentChange> ReplaceCode()
        {
            var lcs = LongestCommonSequence <Line> .Find(_oldLines, _newLines, LcsEqualityComparer);

            var edits = new List <DocumentChange>();

            foreach (var diff in lcs)
            {
                if (diff.NewLength == 0)
                {
                    edits.Add(Delete(diff.OldStart, diff.OldEnd));
                }
                else if (diff.OldLength == 0)
                {
                    edits.Add(Insert(diff.OldStart, diff.NewStart, diff.NewEnd));
                }
                else
                {
                    var length = Math.Min(diff.NewLength, diff.OldLength);
                    for (var i = 0; i < length; i++)
                    {
                        edits.Add(Replace(diff.OldStart + i, diff.NewStart + i));
                    }

                    if (diff.OldLength > length)
                    {
                        edits.Add(Delete(diff.OldStart + length, diff.OldEnd));
                    }
                    else if (diff.NewLength > length)
                    {
                        edits.Add(Insert(diff.OldStart + length, diff.NewStart + length, diff.NewEnd));
                    }
                }
            }

            if (_start.Column > 1)
            {
                foreach (var e in edits)
                {
                    if (e.ReplacedSpan.End.Line == _start.Line)
                    {
                        e.ReplacedSpan = new SourceSpan(e.ReplacedSpan.Start, e.ReplacedSpan.End.AddColumns(_start.Column - 1));
                    }
                    if (e.ReplacedSpan.Start.Line == _start.Line)
                    {
                        e.ReplacedSpan = new SourceSpan(e.ReplacedSpan.Start.AddColumns(_start.Column - 1), e.ReplacedSpan.End);
                    }
                }
            }

            return(edits.AsReadOnly());
        }
Exemple #2
0
        public IReadOnlyList <DocumentChange> ReplaceCode()
        {
            var lcs = LongestCommonSequence <Line> .Find(_oldLines, _newLines, LcsEqualityComparer);

            var edits = new List <DocumentChange>();

            foreach (var diff in lcs)
            {
                if (diff.NewLength == 0)
                {
                    edits.Add(Delete(diff.OldStart, diff.OldEnd));
                }
                else if (diff.OldLength == 0)
                {
                    edits.Add(Insert(diff.OldStart, diff.NewStart, diff.NewEnd));
                }
                else
                {
                    var length = Math.Min(diff.NewLength, diff.OldLength);
                    for (var i = 0; i < length; i++)
                    {
                        edits.Add(Replace(diff.OldStart + i, diff.NewStart + i));
                    }

                    if (diff.OldLength > length)
                    {
                        edits.Add(Delete(diff.OldStart + length, diff.OldEnd));
                    }
                    else if (diff.NewLength > length)
                    {
                        edits.Add(Insert(diff.OldStart + length, diff.NewStart + length, diff.NewEnd));
                    }
                }
            }

            return(edits.AsReadOnly());
        }