1 | // Copyright (c) 2010-2013 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 | |
---|
21 | namespace ICSharpCode.NRefactory.Editor |
---|
22 | { |
---|
23 | /// <summary> |
---|
24 | /// Describes a change of the document text. |
---|
25 | /// This class is thread-safe. |
---|
26 | /// </summary> |
---|
27 | [Serializable] |
---|
28 | public class TextChangeEventArgs : EventArgs |
---|
29 | { |
---|
30 | readonly int offset; |
---|
31 | readonly ITextSource removedText; |
---|
32 | readonly ITextSource insertedText; |
---|
33 | |
---|
34 | /// <summary> |
---|
35 | /// The offset at which the change occurs. |
---|
36 | /// </summary> |
---|
37 | public int Offset { |
---|
38 | get { return offset; } |
---|
39 | } |
---|
40 | |
---|
41 | /// <summary> |
---|
42 | /// The text that was removed. |
---|
43 | /// </summary> |
---|
44 | public ITextSource RemovedText { |
---|
45 | get { return removedText; } |
---|
46 | } |
---|
47 | |
---|
48 | /// <summary> |
---|
49 | /// The number of characters removed. |
---|
50 | /// </summary> |
---|
51 | public int RemovalLength { |
---|
52 | get { return removedText.TextLength; } |
---|
53 | } |
---|
54 | |
---|
55 | /// <summary> |
---|
56 | /// The text that was inserted. |
---|
57 | /// </summary> |
---|
58 | public ITextSource InsertedText { |
---|
59 | get { return insertedText; } |
---|
60 | } |
---|
61 | |
---|
62 | /// <summary> |
---|
63 | /// The number of characters inserted. |
---|
64 | /// </summary> |
---|
65 | public int InsertionLength { |
---|
66 | get { return insertedText.TextLength; } |
---|
67 | } |
---|
68 | |
---|
69 | /// <summary> |
---|
70 | /// Creates a new TextChangeEventArgs object. |
---|
71 | /// </summary> |
---|
72 | public TextChangeEventArgs(int offset, string removedText, string insertedText) |
---|
73 | { |
---|
74 | if (offset < 0) |
---|
75 | throw new ArgumentOutOfRangeException("offset", offset, "offset must not be negative"); |
---|
76 | this.offset = offset; |
---|
77 | this.removedText = removedText != null ? new StringTextSource(removedText) : StringTextSource.Empty; |
---|
78 | this.insertedText = insertedText != null ? new StringTextSource(insertedText) : StringTextSource.Empty; |
---|
79 | } |
---|
80 | |
---|
81 | /// <summary> |
---|
82 | /// Creates a new TextChangeEventArgs object. |
---|
83 | /// </summary> |
---|
84 | public TextChangeEventArgs(int offset, ITextSource removedText, ITextSource insertedText) |
---|
85 | { |
---|
86 | if (offset < 0) |
---|
87 | throw new ArgumentOutOfRangeException("offset", offset, "offset must not be negative"); |
---|
88 | this.offset = offset; |
---|
89 | this.removedText = removedText ?? StringTextSource.Empty; |
---|
90 | this.insertedText = insertedText ?? StringTextSource.Empty; |
---|
91 | } |
---|
92 | |
---|
93 | /// <summary> |
---|
94 | /// Gets the new offset where the specified offset moves after this document change. |
---|
95 | /// </summary> |
---|
96 | public virtual int GetNewOffset(int offset, AnchorMovementType movementType = AnchorMovementType.Default) |
---|
97 | { |
---|
98 | if (offset >= this.Offset && offset <= this.Offset + this.RemovalLength) { |
---|
99 | if (movementType == AnchorMovementType.BeforeInsertion) |
---|
100 | return this.Offset; |
---|
101 | else |
---|
102 | return this.Offset + this.InsertionLength; |
---|
103 | } else if (offset > this.Offset) { |
---|
104 | return offset + this.InsertionLength - this.RemovalLength; |
---|
105 | } else { |
---|
106 | return offset; |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | /// <summary> |
---|
111 | /// Creates TextChangeEventArgs for the reverse change. |
---|
112 | /// </summary> |
---|
113 | public virtual TextChangeEventArgs Invert() |
---|
114 | { |
---|
115 | return new TextChangeEventArgs(offset, insertedText, removedText); |
---|
116 | } |
---|
117 | } |
---|
118 | } |
---|