Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ProjectionSpanToChangeConverter.cs « Projection « TextModel « Impl « Text « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9e626204aee87f7ac04025e383c59f7ca53908cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//  Licensed under the MIT License. See License.txt in the project root for license information.
//
// This file contain implementations details that are subject to change without notice.
// Use at your own risk.
//
namespace Microsoft.VisualStudio.Text.Projection.Implementation
{
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Text;
    using Microsoft.VisualStudio.Text.Differencing;
    using Microsoft.VisualStudio.Text.Implementation;
    using Microsoft.VisualStudio.Text.Utilities;

    internal class ProjectionSpanToNormalizedChangeConverter
    {
        private INormalizedTextChangeCollection normalizedChanges;
        private bool computed = false;
        private int textPosition;
        private ProjectionSpanDiffer differ;
        private ITextSnapshot currentSnapshot;

        public ProjectionSpanToNormalizedChangeConverter(ProjectionSpanDiffer differ, 
                                                         int textPosition, 
                                                         ITextSnapshot currentSnapshot)
        {
            this.differ = differ;
            this.textPosition = textPosition;
            this.currentSnapshot = currentSnapshot;
        }

        public INormalizedTextChangeCollection NormalizedChanges
        {
            get 
            {
                if (!computed)
                {
                    ConstructChanges();
                    computed = true;
                }
                return this.normalizedChanges; 
            }
        }

        #region Private helpers

        private void ConstructChanges()
        {
            var diffs = differ.GetDifferences();

            List<TextChange> changes = new List<TextChange>();
            int pos = this.textPosition;

            // each difference generates a text change
            foreach (Difference diff in diffs)
            {
                pos += GetMatchSize(diffs.LeftSequence, diff.Before);
                TextChange change = TextChange.Create(pos,
                                                      BufferFactoryService.StringRebuilderFromSnapshotSpans(diffs.LeftSequence, diff.Left),
                                                      BufferFactoryService.StringRebuilderFromSnapshotSpans(diffs.RightSequence, diff.Right),
                                                      this.currentSnapshot);
                changes.Add(change);
                pos += change.OldLength;
            }
            this.normalizedChanges = NormalizedTextChangeCollection.Create(changes);
        }

        private static int GetMatchSize(IList<SnapshotSpan> spans, Match match)
        {
            int size = 0;
            if (match != null)
            {
                Span extent = match.Left;
                for (int s = extent.Start; s < extent.End; ++s)
                {
                    size += spans[s].Length;
                }
            }
            return size;
        }

        #endregion
    }
}