-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathBookmarkDataProvider.cs
More file actions
218 lines (168 loc) · 4.62 KB
/
BookmarkDataProvider.cs
File metadata and controls
218 lines (168 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using System.Globalization;
using LogExpert.Core.Entities;
using LogExpert.Core.Interfaces;
using NLog;
namespace LogExpert.Core.Classes.Bookmark;
public class BookmarkDataProvider : IBookmarkData
{
#region Fields
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly Lock _bookmarkListLock = new();
#endregion
#region cTor
public BookmarkDataProvider ()
{
BookmarkList = [];
}
public BookmarkDataProvider (SortedList<int, Entities.Bookmark> bookmarkList)
{
BookmarkList = bookmarkList;
}
#endregion
#region Events
public event EventHandler<EventArgs> BookmarkAdded;
public event EventHandler<EventArgs> BookmarkRemoved;
public event EventHandler<EventArgs> AllBookmarksRemoved;
#endregion
#region Properties
public BookmarkCollection Bookmarks => new(BookmarkList);
public SortedList<int, Entities.Bookmark> BookmarkList { get; private set; }
#endregion
#region Public methods
public void SetBookmarks (SortedList<int, Entities.Bookmark> bookmarkList)
{
lock (_bookmarkListLock)
{
BookmarkList = bookmarkList;
}
}
public void ToggleBookmark (int lineNum)
{
if (IsBookmarkAtLine(lineNum))
{
RemoveBookmarkForLine(lineNum);
}
else
{
AddBookmark(new Entities.Bookmark(lineNum));
}
}
public bool IsBookmarkAtLine (int lineNum)
{
lock (_bookmarkListLock)
{
return BookmarkList.ContainsKey(lineNum);
}
}
public int GetBookmarkIndexForLine (int lineNum)
{
lock (_bookmarkListLock)
{
return BookmarkList.IndexOfKey(lineNum);
}
}
public Entities.Bookmark GetBookmarkForLine (int lineNum)
{
lock (_bookmarkListLock)
{
return BookmarkList[lineNum];
}
}
#endregion
#region Internals
public void ShiftBookmarks (int offset)
{
SortedList<int, Entities.Bookmark> newBookmarkList = [];
foreach (var bookmark in BookmarkList.Values)
{
var line = bookmark.LineNum - offset;
if (line >= 0)
{
bookmark.LineNum = line;
newBookmarkList.Add(line, bookmark);
}
}
BookmarkList = newBookmarkList;
}
public int FindPrevBookmarkIndex (int lineNum)
{
var values = BookmarkList.Values;
for (var i = BookmarkList.Count - 1; i >= 0; --i)
{
if (values[i].LineNum <= lineNum)
{
return i;
}
}
return BookmarkList.Count - 1;
}
public int FindNextBookmarkIndex (int lineNum)
{
var values = BookmarkList.Values;
for (var i = 0; i < BookmarkList.Count; ++i)
{
if (values[i].LineNum >= lineNum)
{
return i;
}
}
return 0;
}
public void RemoveBookmarkForLine (int lineNum)
{
lock (_bookmarkListLock)
{
_ = BookmarkList.Remove(lineNum);
}
OnBookmarkRemoved();
}
//TOOD: check if the callers are checking for null before calling
public void RemoveBookmarksForLines (IEnumerable<int> lineNumList)
{
ArgumentNullException.ThrowIfNull(lineNumList, nameof(lineNumList));
lock (_bookmarkListLock)
{
foreach (var lineNum in lineNumList)
{
_ = BookmarkList.Remove(lineNum);
}
}
OnBookmarkRemoved();
}
//TOOD: check if the callers are checking for null before calling
public void AddBookmark (Entities.Bookmark bookmark)
{
ArgumentNullException.ThrowIfNull(bookmark, nameof(bookmark));
lock (_bookmarkListLock)
{
BookmarkList.Add(bookmark.LineNum, bookmark);
}
OnBookmarkAdded();
}
public void ClearAllBookmarks ()
{
#if DEBUG
_logger.Debug(CultureInfo.InvariantCulture, "Removing all bookmarks");
#endif
lock (_bookmarkListLock)
{
BookmarkList.Clear();
}
OnAllBookmarksRemoved();
}
#endregion
#region Event invokers
protected void OnBookmarkAdded ()
{
BookmarkAdded?.Invoke(this, EventArgs.Empty);
}
protected void OnBookmarkRemoved ()
{
BookmarkRemoved?.Invoke(this, EventArgs.Empty);
}
protected void OnAllBookmarksRemoved ()
{
AllBookmarksRemoved?.Invoke(this, EventArgs.Empty);
}
#endregion
}