~ubuntu-branches/ubuntu/wily/monodevelop/wily

« back to all changes in this revision

Viewing changes to src/core/Mono.Texteditor/Mono.TextEditor/LineSegment.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2012-05-27 18:08:20 UTC
  • mfrom: (19.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20120527180820-fydl21qnbnfr8w2t
Tags: 3.0.2+dfsg-3
* [fcecfe7] Fix monodevelop-core-addins.pc.in to point to actual 
  installed location of assemblies.
* [26e1a07] DebSrc 3.0 does not support Quilt's -p parameter, so 
  manually adjust the path in the patch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// LineSegment.cs
2
 
//
3
 
// Author:
4
 
//   Mike Krüger <mkrueger@novell.com>
5
 
//
6
 
// Copyright (c) 2007 Novell, Inc (http://www.novell.com)
7
 
//
8
 
// Permission is hereby granted, free of charge, to any person obtaining a copy
9
 
// of this software and associated documentation files (the "Software"), to deal
10
 
// in the Software without restriction, including without limitation the rights
11
 
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 
// copies of the Software, and to permit persons to whom the Software is
13
 
// furnished to do so, subject to the following conditions:
14
 
//
15
 
// The above copyright notice and this permission notice shall be included in
16
 
// all copies or substantial portions of the Software.
17
 
//
18
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 
// THE SOFTWARE.
25
 
//
26
 
//
27
 
 
28
 
using System;
29
 
using System.Text;
30
 
using System.Collections.Generic;
31
 
using Mono.TextEditor.Highlighting;
32
 
using System.Linq;
33
 
 
34
 
namespace Mono.TextEditor
35
 
{
36
 
        public abstract class LineSegment : ISegment
37
 
        {
38
 
                List<TextMarker> markers;
39
 
                public IEnumerable<TextMarker> Markers {
40
 
                        get {
41
 
                                return markers ?? Enumerable.Empty<TextMarker> ();
42
 
                        }
43
 
                }
44
 
                public int MarkerCount {
45
 
                        get {
46
 
                                return markers != null ? markers.Count : 0;
47
 
                        }
48
 
                }
49
 
 
50
 
                public int EditableLength {
51
 
                        get {
52
 
                                return Length - DelimiterLength;
53
 
                        }
54
 
                }
55
 
 
56
 
                public int DelimiterLength {
57
 
                        get;
58
 
                        set;
59
 
                }
60
 
 
61
 
 
62
 
                public bool WasChanged {
63
 
                        get;
64
 
                        set;
65
 
                }
66
 
 
67
 
                CloneableStack<Span> startSpan;
68
 
                static readonly CloneableStack<Span> EmptySpan = new CloneableStack<Span> ();
69
 
 
70
 
                public CloneableStack<Span> StartSpan {
71
 
                        get {
72
 
                                return startSpan ?? EmptySpan;
73
 
                        }
74
 
                        set {
75
 
                                startSpan = value != null && value.Count == 0 ? null : value;
76
 
                        }
77
 
                }
78
 
 
79
 
                public abstract int Offset { get; set; }
80
 
 
81
 
                public int Length {
82
 
                        get;
83
 
                        set;
84
 
                }
85
 
 
86
 
                public int EndOffset {
87
 
                        get {
88
 
                                return Offset + Length;
89
 
                        }
90
 
                }
91
 
 
92
 
                public bool IsBookmarked  {
93
 
                        get {
94
 
                                if (markers == null)
95
 
                                        return false;
96
 
                                return markers.Contains (BookmarkMarker.Instance);
97
 
                        }
98
 
                        set {
99
 
                                if (value) {
100
 
                                        if (!IsBookmarked)
101
 
                                                AddMarker (BookmarkMarker.Instance);
102
 
                                } else {
103
 
                                        if (markers != null)
104
 
                                                markers.Remove (BookmarkMarker.Instance);
105
 
                                }
106
 
                        }
107
 
                }
108
 
 
109
 
                protected LineSegment (int length, int delimiterLength)
110
 
                {
111
 
                        Length          = length;
112
 
                        DelimiterLength = delimiterLength;
113
 
                }
114
 
 
115
 
                internal void AddMarker (TextMarker marker)
116
 
                {
117
 
                        if (markers == null)
118
 
                                markers = new List<TextMarker> ();
119
 
                        marker.LineSegment = this;
120
 
                        markers.Add (marker);
121
 
                }
122
 
 
123
 
                public void ClearMarker ()
124
 
                {
125
 
                        if (markers != null) {
126
 
                                markers.Clear ();
127
 
                                markers = null;
128
 
                        }
129
 
                }
130
 
 
131
 
                internal void RemoveMarker (TextMarker marker)
132
 
                {
133
 
                        marker.LineSegment = null;
134
 
                        if (markers == null)
135
 
                                return;
136
 
                        markers.Remove (marker);
137
 
                        if (markers.Count == 0)
138
 
                                markers = null;
139
 
                }
140
 
 
141
 
                internal TextMarker GetMarker (Type type)
142
 
                {
143
 
                        if (markers == null)
144
 
                                return null;
145
 
                        return markers.Find (m => m.GetType () == type);
146
 
                }
147
 
 
148
 
                internal void RemoveMarker (Type type)
149
 
                {
150
 
                        for (int i = 0; markers != null && i < markers.Count; i++) {
151
 
                                if (markers[i].GetType () == type) {
152
 
                                        RemoveMarker (markers[i]);
153
 
                                        i--;
154
 
                                }
155
 
                        }
156
 
                }
157
 
 
158
 
                /// <summary>
159
 
                /// This method gets the line indentation.
160
 
                /// </summary>
161
 
                /// <param name="doc">
162
 
                /// The <see cref="Document"/> the line belongs to.
163
 
                /// </param>
164
 
                /// <returns>
165
 
                /// The indentation of the line (all whitespace chars up to the first non ws char).
166
 
                /// </returns>
167
 
                public string GetIndentation (Document doc)
168
 
                {
169
 
                        var result = new StringBuilder ();
170
 
                        int offset = Offset;
171
 
                        int max = System.Math.Min (offset + Length, doc.Length);
172
 
                        for (int i = offset; i < max; i++) {
173
 
                                char ch = doc.GetCharAt (i);
174
 
                                if (ch != ' ' && ch != '\t')
175
 
                                        break;
176
 
                                result.Append (ch);
177
 
                        }
178
 
                        return result.ToString ();
179
 
                }
180
 
 
181
 
                public int GetLogicalColumn (TextEditorData editor, int visualColumn)
182
 
                {
183
 
                        int curVisualColumn = 1;
184
 
                        int offset = Offset;
185
 
                        int max = offset + EditableLength;
186
 
                        for (int i = offset; i < max; i++) {
187
 
                                if (i < editor.Document.Length && editor.Document.GetCharAt (i) == '\t') {
188
 
                                        curVisualColumn = TextViewMargin.GetNextTabstop (editor, curVisualColumn);
189
 
                                } else {
190
 
                                        curVisualColumn++;
191
 
                                }
192
 
                                if (curVisualColumn > visualColumn)
193
 
                                        return i - offset + 1;
194
 
                        }
195
 
                        return EditableLength + (visualColumn - curVisualColumn) + 1;
196
 
                }
197
 
 
198
 
                public int GetVisualColumn (TextEditorData editor, int logicalColumn)
199
 
                {
200
 
                        int result = 1;
201
 
                        int offset = Offset;
202
 
                        for (int i = 0; i < logicalColumn - 1; i++) {
203
 
                                if (i < EditableLength && editor.Document.GetCharAt (offset + i) == '\t') {
204
 
                                        result = TextViewMargin.GetNextTabstop (editor, result);
205
 
                                } else {
206
 
                                        result++;
207
 
                                }
208
 
                        }
209
 
                        return result;
210
 
                }
211
 
 
212
 
                public bool Contains (int offset)
213
 
                {
214
 
                        int o = Offset;
215
 
                        return o <= offset && offset < o + Length;
216
 
                }
217
 
 
218
 
                public bool Contains (ISegment segment)
219
 
                {
220
 
                        return segment != null && Offset <= segment.Offset && segment.EndOffset <= EndOffset;
221
 
                }
222
 
 
223
 
                public override string ToString ()
224
 
                {
225
 
                        return String.Format ("[LineSegment: Offset={0}, Length={1}, DelimiterLength={2}, StartSpan={3}]", Offset, Length, DelimiterLength, StartSpan == null ? "null" : StartSpan.Count.ToString());
226
 
                }
227
 
        }
228
 
}