~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory/Editor/TextSourceVersionProvider.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
2
 
// 
3
 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4
 
// software and associated documentation files (the "Software"), to deal in the Software
5
 
// without restriction, including without limitation the rights to use, copy, modify, merge,
6
 
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7
 
// to whom the Software is furnished to do so, subject to the following conditions:
8
 
// 
9
 
// The above copyright notice and this permission notice shall be included in all copies or
10
 
// substantial portions of the Software.
11
 
// 
12
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13
 
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14
 
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15
 
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
 
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17
 
// DEALINGS IN THE SOFTWARE.
18
 
 
19
 
using System;
20
 
using System.Collections.Generic;
21
 
using System.Linq;
22
 
 
23
 
namespace ICSharpCode.NRefactory.Editor
24
 
{
25
 
        /// <summary>
26
 
        /// Provides ITextSourceVersion instances.
27
 
        /// </summary>
28
 
        public class TextSourceVersionProvider
29
 
        {
30
 
                Version currentVersion;
31
 
                
32
 
                public TextSourceVersionProvider()
33
 
                {
34
 
                        this.currentVersion = new Version(this);
35
 
                }
36
 
                
37
 
                /// <summary>
38
 
                /// Gets the current version.
39
 
                /// </summary>
40
 
                public ITextSourceVersion CurrentVersion {
41
 
                        get { return currentVersion; }
42
 
                }
43
 
                
44
 
                /// <summary>
45
 
                /// Replaces the current version with a new version.
46
 
                /// </summary>
47
 
                /// <param name="change">Change from current version to new version</param>
48
 
                public void AppendChange(TextChangeEventArgs change)
49
 
                {
50
 
                        if (change == null)
51
 
                                throw new ArgumentNullException("change");
52
 
                        currentVersion.change = change;
53
 
                        currentVersion.next = new Version(currentVersion);
54
 
                        currentVersion = currentVersion.next;
55
 
                }
56
 
                
57
 
                sealed class Version : ITextSourceVersion
58
 
                {
59
 
                        // Reference back to the provider.
60
 
                        // Used to determine if two checkpoints belong to the same document.
61
 
                        readonly TextSourceVersionProvider provider;
62
 
                        // ID used for CompareAge()
63
 
                        readonly int id;
64
 
                        
65
 
                        // the change from this version to the next version
66
 
                        internal TextChangeEventArgs change;
67
 
                        internal Version next;
68
 
                        
69
 
                        internal Version(TextSourceVersionProvider provider)
70
 
                        {
71
 
                                this.provider = provider;
72
 
                        }
73
 
                        
74
 
                        internal Version(Version prev)
75
 
                        {
76
 
                                this.provider = prev.provider;
77
 
                                this.id = unchecked( prev.id + 1 );
78
 
                        }
79
 
                        
80
 
                        public bool BelongsToSameDocumentAs(ITextSourceVersion other)
81
 
                        {
82
 
                                if (other == null)
83
 
                                        throw new ArgumentNullException("other");
84
 
                                Version o = other as Version;
85
 
                                return o != null && provider == o.provider;
86
 
                        }
87
 
                        
88
 
                        public int CompareAge(ITextSourceVersion other)
89
 
                        {
90
 
                                if (other == null)
91
 
                                        throw new ArgumentNullException("other");
92
 
                                Version o = other as Version;
93
 
                                if (o == null || provider != o.provider)
94
 
                                        throw new ArgumentException("Versions do not belong to the same document.");
95
 
                                // We will allow overflows, but assume that the maximum distance between checkpoints is 2^31-1.
96
 
                                // This is guaranteed on x86 because so many checkpoints don't fit into memory.
97
 
                                return Math.Sign(unchecked( this.id - o.id ));
98
 
                        }
99
 
                        
100
 
                        public IEnumerable<TextChangeEventArgs> GetChangesTo(ITextSourceVersion other)
101
 
                        {
102
 
                                int result = CompareAge(other);
103
 
                                Version o = (Version)other;
104
 
                                if (result < 0)
105
 
                                        return GetForwardChanges(o);
106
 
                                else if (result > 0)
107
 
                                        return o.GetForwardChanges(this).Reverse().Select(change => change.Invert());
108
 
                                else
109
 
                                        return EmptyList<TextChangeEventArgs>.Instance;
110
 
                        }
111
 
                        
112
 
                        IEnumerable<TextChangeEventArgs> GetForwardChanges(Version other)
113
 
                        {
114
 
                                // Return changes from this(inclusive) to other(exclusive).
115
 
                                for (Version node = this; node != other; node = node.next) {
116
 
                                        yield return node.change;
117
 
                                }
118
 
                        }
119
 
                        
120
 
                        public int MoveOffsetTo(ITextSourceVersion other, int oldOffset, AnchorMovementType movement)
121
 
                        {
122
 
                                int offset = oldOffset;
123
 
                                foreach (var e in GetChangesTo(other)) {
124
 
                                        offset = e.GetNewOffset(offset, movement);
125
 
                                }
126
 
                                return offset;
127
 
                        }
128
 
                }
129
 
        }
130
 
}