4
// Mike Krüger <mkrueger@novell.com>
6
// Copyright (c) 2007 Novell, Inc (http://www.novell.com)
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:
15
// The above copyright notice and this permission notice shall be included in
16
// all copies or substantial portions of the Software.
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
30
using System.Collections.Generic;
31
using Mono.TextEditor.Highlighting;
34
namespace Mono.TextEditor
36
public abstract class LineSegment : ISegment
38
List<TextMarker> markers;
39
public IEnumerable<TextMarker> Markers {
41
return markers ?? Enumerable.Empty<TextMarker> ();
44
public int MarkerCount {
46
return markers != null ? markers.Count : 0;
50
public int EditableLength {
52
return Length - DelimiterLength;
56
public int DelimiterLength {
62
public bool WasChanged {
67
CloneableStack<Span> startSpan;
68
static readonly CloneableStack<Span> EmptySpan = new CloneableStack<Span> ();
70
public CloneableStack<Span> StartSpan {
72
return startSpan ?? EmptySpan;
75
startSpan = value != null && value.Count == 0 ? null : value;
79
public abstract int Offset { get; set; }
86
public int EndOffset {
88
return Offset + Length;
92
public bool IsBookmarked {
96
return markers.Contains (BookmarkMarker.Instance);
101
AddMarker (BookmarkMarker.Instance);
104
markers.Remove (BookmarkMarker.Instance);
109
protected LineSegment (int length, int delimiterLength)
112
DelimiterLength = delimiterLength;
115
internal void AddMarker (TextMarker marker)
118
markers = new List<TextMarker> ();
119
marker.LineSegment = this;
120
markers.Add (marker);
123
public void ClearMarker ()
125
if (markers != null) {
131
internal void RemoveMarker (TextMarker marker)
133
marker.LineSegment = null;
136
markers.Remove (marker);
137
if (markers.Count == 0)
141
internal TextMarker GetMarker (Type type)
145
return markers.Find (m => m.GetType () == type);
148
internal void RemoveMarker (Type type)
150
for (int i = 0; markers != null && i < markers.Count; i++) {
151
if (markers[i].GetType () == type) {
152
RemoveMarker (markers[i]);
159
/// This method gets the line indentation.
161
/// <param name="doc">
162
/// The <see cref="Document"/> the line belongs to.
165
/// The indentation of the line (all whitespace chars up to the first non ws char).
167
public string GetIndentation (Document doc)
169
var result = new StringBuilder ();
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')
178
return result.ToString ();
181
public int GetLogicalColumn (TextEditorData editor, int visualColumn)
183
int curVisualColumn = 1;
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);
192
if (curVisualColumn > visualColumn)
193
return i - offset + 1;
195
return EditableLength + (visualColumn - curVisualColumn) + 1;
198
public int GetVisualColumn (TextEditorData editor, int logicalColumn)
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);
212
public bool Contains (int offset)
215
return o <= offset && offset < o + Length;
218
public bool Contains (ISegment segment)
220
return segment != null && Offset <= segment.Offset && segment.EndOffset <= EndOffset;
223
public override string ToString ()
225
return String.Format ("[LineSegment: Offset={0}, Length={1}, DelimiterLength={2}, StartSpan={3}]", Offset, Length, DelimiterLength, StartSpan == null ? "null" : StartSpan.Count.ToString());