Code Coverage Statistics for Source File

c:\Tools\SD3\src\Libraries\ICSharpCode.TextEditor\Project\Src\Document\BookmarkManager\BookmarkManager.cs

Sequence Point Coverage
N/A
0 of 0
Branch Coverage
N/A
0 of 0
Lines
254
Highlight: Uncovered Code Covered Code
L V Source
1
// <file>
2
//     <copyright see="prj:///doc/copyright.txt"/>
3
//     <license see="prj:///doc/license.txt"/>
4
//     <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
5
//     <version>$Revision: 2691 $</version>
6
// </file>
7
8
using System;
9
using System.Collections.Generic;
10
11
namespace ICSharpCode.TextEditor.Document
12
{
13
	public interface IBookmarkFactory
14
	{
15
		Bookmark CreateBookmark(IDocument document, int lineNumber);
16
	}
17
	
18
	/// <summary>
19
	/// This class handles the bookmarks for a buffer
20
	/// </summary>
21
	public class BookmarkManager
22
	{
23
		IDocument      document;
24
		List<Bookmark> bookmark = new List<Bookmark>();
25
		
26
		/// <value>
27
		/// Contains all bookmarks
28
		/// </value>
29
		public List<Bookmark> Marks {
30
			get {
31
				return bookmark;
32
			}
33
		}
34
		
35
		public IDocument Document {
36
			get {
37
				return document;
38
			}
39
		}
40
		
41
		/// <summary>
42
		/// Creates a new instance of <see cref="BookmarkManager"/>
43
		/// </summary>
44
		internal BookmarkManager(IDocument document, LineManager lineTracker)
45
		{
46
			this.document = document;
47
			lineTracker.LineDeleted += delegate(object sender, LineEventArgs e) {
48
				for (int i = 0; i < bookmark.Count; i++) {
49
					Bookmark b = bookmark[i];
50
					if (b.Line == e.LineSegment) {
51
						bookmark.RemoveAt(i--);
52
						OnRemoved(new BookmarkEventArgs(b));
53
					}
54
				}
55
			};
56
		}
57
		
58
		IBookmarkFactory factory;
59
		
60
		public IBookmarkFactory Factory {
61
			get {
62
				return factory;
63
			}
64
			set {
65
				factory = value;
66
			}
67
		}
68
		
69
		/// <summary>
70
		/// Sets the mark at the line <code>lineNr</code> if it is not set, if the
71
		/// line is already marked the mark is cleared.
72
		/// </summary>
73
		public void ToggleMarkAt(int lineNr)
74
		{
75
			Bookmark newMark;
76
			if (factory != null)
77
				newMark = factory.CreateBookmark(document, lineNr);
78
			else
79
				newMark = new Bookmark(document, lineNr);
80
			
81
			Type newMarkType = newMark.GetType();
82
			
83
			for (int i = 0; i < bookmark.Count; ++i) {
84
				Bookmark mark = bookmark[i];
85
				
86
				if (mark.LineNumber == lineNr && mark.CanToggle && mark.GetType() == newMarkType) {
87
					bookmark.RemoveAt(i);
88
					OnRemoved(new BookmarkEventArgs(mark));
89
					return;
90
				}
91
			}
92
			
93
			bookmark.Add(newMark);
94
			OnAdded(new BookmarkEventArgs(newMark));
95
		}
96
		
97
		public void AddMark(Bookmark mark)
98
		{
99
			bookmark.Add(mark);
100
			OnAdded(new BookmarkEventArgs(mark));
101
		}
102
		
103
		public void RemoveMark(Bookmark mark)
104
		{
105
			bookmark.Remove(mark);
106
			OnRemoved(new BookmarkEventArgs(mark));
107
		}
108
		
109
		public void RemoveMarks(Predicate<Bookmark> predicate)
110
		{
111
			for (int i = 0; i < bookmark.Count; ++i) {
112
				Bookmark bm = bookmark[i];
113
				if (predicate(bm)) {
114
					bookmark.RemoveAt(i--);
115
					OnRemoved(new BookmarkEventArgs(bm));
116
				}
117
			}
118
		}
119
		
120
		/// <returns>
121
		/// true, if a mark at mark exists, otherwise false
122
		/// </returns>
123
		public bool IsMarked(int lineNr)
124
		{
125
			for (int i = 0; i < bookmark.Count; ++i) {
126
				if (bookmark[i].LineNumber == lineNr) {
127
					return true;
128
				}
129
			}
130
			return false;
131
		}
132
		
133
		/// <remarks>
134
		/// Clears all bookmark
135
		/// </remarks>
136
		public void Clear()
137
		{
138
			foreach (Bookmark mark in bookmark) {
139
				OnRemoved(new BookmarkEventArgs(mark));
140
			}
141
			bookmark.Clear();
142
		}
143
		
144
		/// <value>
145
		/// The lowest mark, if no marks exists it returns -1
146
		/// </value>
147
		public Bookmark GetFirstMark(Predicate<Bookmark> predicate)
148
		{
149
			if (bookmark.Count < 1) {
150
				return null;
151
			}
152
			Bookmark first = null;
153
			for (int i = 0; i < bookmark.Count; ++i) {
154
				if (predicate(bookmark[i]) && bookmark[i].IsEnabled && (first == null || bookmark[i].LineNumber < first.LineNumber)) {
155
					first = bookmark[i];
156
				}
157
			}
158
			return first;
159
		}
160
		
161
		/// <value>
162
		/// The highest mark, if no marks exists it returns -1
163
		/// </value>
164
		public Bookmark GetLastMark(Predicate<Bookmark> predicate)
165
		{
166
			if (bookmark.Count < 1) {
167
				return null;
168
			}
169
			Bookmark last = null;
170
			for (int i = 0; i < bookmark.Count; ++i) {
171
				if (predicate(bookmark[i]) && bookmark[i].IsEnabled && (last == null || bookmark[i].LineNumber > last.LineNumber)) {
172
					last = bookmark[i];
173
				}
174
			}
175
			return last;
176
		}
177
		bool AcceptAnyMarkPredicate(Bookmark mark)
178
		{
179
			return true;
180
		}
181
		public Bookmark GetNextMark(int curLineNr)
182
		{
183
			return GetNextMark(curLineNr, AcceptAnyMarkPredicate);
184
		}
185
		
186
		/// <remarks>
187
		/// returns first mark higher than <code>lineNr</code>
188
		/// </remarks>
189
		/// <returns>
190
		/// returns the next mark > cur, if it not exists it returns FirstMark()
191
		/// </returns>
192
		public Bookmark GetNextMark(int curLineNr, Predicate<Bookmark> predicate)
193
		{
194
			if (bookmark.Count == 0) {
195
				return null;
196
			}
197
			
198
			Bookmark next = GetFirstMark(predicate);
199
			foreach (Bookmark mark in bookmark) {
200
				if (predicate(mark) && mark.IsEnabled && mark.LineNumber > curLineNr) {
201
					if (mark.LineNumber < next.LineNumber || next.LineNumber <= curLineNr) {
202
						next = mark;
203
					}
204
				}
205
			}
206
			return next;
207
		}
208
		
209
		public Bookmark GetPrevMark(int curLineNr)
210
		{
211
			return GetPrevMark(curLineNr, AcceptAnyMarkPredicate);
212
		}
213
		/// <remarks>
214
		/// returns first mark lower than <code>lineNr</code>
215
		/// </remarks>
216
		/// <returns>
217
		/// returns the next mark lower than cur, if it not exists it returns LastMark()
218
		/// </returns>
219
		public Bookmark GetPrevMark(int curLineNr, Predicate<Bookmark> predicate)
220
		{
221
			if (bookmark.Count == 0) {
222
				return null;
223
			}
224
			
225
			Bookmark prev = GetLastMark(predicate);
226
			
227
			foreach (Bookmark mark in bookmark) {
228
				if (predicate(mark) && mark.IsEnabled && mark.LineNumber < curLineNr) {
229
					if (mark.LineNumber > prev.LineNumber || prev.LineNumber >= curLineNr) {
230
						prev = mark;
231
					}
232
				}
233
			}
234
			return prev;
235
		}
236
		
237
		protected virtual void OnRemoved(BookmarkEventArgs e)
238
		{
239
			if (Removed != null) {
240
				Removed(this, e);
241
			}
242
		}
243
		
244
		protected virtual void OnAdded(BookmarkEventArgs e)
245
		{
246
			if (Added != null) {
247
				Added(this, e);
248
			}
249
		}
250
		
251
		public event BookmarkEventHandler Removed;
252
		public event BookmarkEventHandler Added;
253
	}
254
}