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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
//
// NavigationHistoryService.cs
//
// Author:
// Michael Hutchinson <mhutchinson@novell.com>
// Lluis Sanchez Gual <lluis@novell.com>
//
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using MonoDevelop.Ide.Gui.Content;
using MonoDevelop.Ide.Gui;
using MonoDevelop.Projects;
using MonoDevelop.Ide.TextEditing;
namespace MonoDevelop.Ide.Navigation
{
public static class NavigationHistoryService
{
static HistoryList history = new HistoryList ();
static Stack<Tuple<NavigationPoint, int>> closedHistory = new Stack<Tuple<NavigationPoint, int>> ();
//used to prevent re-logging the current point during a switch
static bool switching;
//whethor the current node is transient. Prevents excession automatic logging when switching rapidly between
//documents
static bool currentIsTransient;
//the amount of time until a "transient" current node bevomes "permanent"
static uint TRANSIENT_TIMEOUT = 10000; //ms
static Document currentDoc;
static NavigationHistoryService ()
{
IdeApp.Workspace.LastWorkspaceItemClosed += delegate {
history.Clear ();
closedHistory.Clear ();
OnHistoryChanged ();
};
IdeApp.Workbench.DocumentClosing += delegate(object sender, DocumentEventArgs e) {
NavigationPoint point = GetNavPointForDoc (e.Document);
if (point == null)
return;
closedHistory.Push (new Tuple<NavigationPoint, int> (point, IdeApp.Workbench.Documents.IndexOf (e.Document)));
OnClosedHistoryChanged ();
};
//keep nav points up to date
TextEditorService.LineCountChanged += LineCountChanged;
TextEditorService.LineCountChangesCommitted += CommitCountChanges;
TextEditorService.LineCountChangesReset += ResetCountChanges;
IdeApp.Workspace.FileRenamedInProject += FileRenamed;
IdeApp.Workbench.ActiveDocumentChanged += ActiveDocChanged;
}
public static void LogActiveDocument ()
{
LogActiveDocument (false);
}
public static void LogActiveDocument (bool transient)
{
if (switching)
return;
NavigationPoint point = GetNavPointForActiveDoc ();
if (point == null)
return;
NavigationHistoryItem item = new NavigationHistoryItem (point);
//if the current node's transient but has been around for a while, consider making it permanent
if (Current == null ||
(currentIsTransient && DateTime.Now.Subtract (Current.Created).TotalMilliseconds > TRANSIENT_TIMEOUT))
{
currentIsTransient = false;
}
//if the current point's transient, always replace it
if (currentIsTransient)
{
//collapse down possible extra point in history
NavigationHistoryItem backOne = history[-1];
if (backOne != null && point.ShouldReplace (backOne.NavigationPoint)) {
// The new node is the same as the last permanent, so we can discard it
history.RemoveCurrent ();
currentIsTransient = false;
item.Dispose ();
} else {
currentIsTransient = transient;
history.ReplaceCurrent (item);
}
}
//if the new point wants to replace the old one, let it
else if (Current != null && !transient && point.ShouldReplace (Current.NavigationPoint))
{
history.ReplaceCurrent (item);
//but in this case, the point should not be transient -- unless the old point was,
//but that's handled earlier
currentIsTransient = false;
}
//final choice: append the the node
//BUT only if the existing current node would not want to replace the new node
else if (Current == null || !Current.NavigationPoint.ShouldReplace (point))
{
history.AddPoint (item);
currentIsTransient = transient;
}
else
point.Dispose ();
OnHistoryChanged ();
}
static NavigationPoint GetNavPointForActiveDoc ()
{
return GetNavPointForDoc (IdeApp.Workbench.ActiveDocument);
}
static NavigationPoint GetNavPointForDoc (Document doc)
{
if (doc == null)
return null;
NavigationPoint point = null;
INavigable navigable = doc.GetContent<INavigable> ();
if (navigable != null) {
point = navigable.BuildNavigationPoint ();
if (point != null)
return point;
}
IEditableTextBuffer editBuf = doc.GetContent<IEditableTextBuffer> ();
if (editBuf != null) {
point = new TextFileNavigationPoint (doc, editBuf);
if (point != null)
return point;
}
return new DocumentNavigationPoint (doc);
}
#region Navigation
public static bool CanMoveForward {
get { return history.CanMoveForward; }
}
public static bool CanMoveBack {
get { return history.CanMoveBack; }
}
public static void MoveForward ()
{
LogActiveDocument ();
if (history.CanMoveForward) {
history.MoveForward ();
SwitchToCurrent ();
OnHistoryChanged ();
}
}
public static void MoveBack ()
{
// Log current point before moving back, to make sure a MoveForward will return to the same position
LogActiveDocument ();
if (history.CanMoveBack) {
history.MoveBack ();
SwitchToCurrent ();
OnHistoryChanged ();
}
}
public static void MoveTo (NavigationHistoryItem item)
{
history.MoveTo (item);
SwitchToCurrent ();
OnHistoryChanged ();
}
static void SwitchToCurrent ()
{
currentIsTransient = false;
switching = true;
if (history.Current != null)
history.Current.Show ();
switching = false;
}
#endregion
#region Closed Document List
public static bool HasClosedDocuments {
get { return closedHistory.Count != 0; }
}
public static void OpenLastClosedDocument () {
if (HasClosedDocuments) {
var tuple = closedHistory.Pop ();
var doc = tuple.Item1.ShowDocument ();
IdeApp.Workbench.ReorderTab (IdeApp.Workbench.Documents.IndexOf (doc), tuple.Item2);
}
}
#endregion
public static IList<NavigationHistoryItem> GetNavigationList (int desiredLength)
{
return history.GetList (desiredLength);
}
public static IList<NavigationHistoryItem> GetNavigationList (int desiredLength, out int currentIndex)
{
return history.GetList (desiredLength, out currentIndex);
}
public static NavigationHistoryItem Current { get { return history.Current; } }
public static bool IsCurrent (NavigationHistoryItem point)
{
return history.IsCurrent (point);
}
public static void Clear ()
{
history.Clear ();
LogActiveDocument ();
}
public static event EventHandler HistoryChanged;
public static event EventHandler ClosedHistoryChanged;
static void OnHistoryChanged ()
{
if (HistoryChanged != null)
HistoryChanged (null, EventArgs.Empty);
}
static void OnClosedHistoryChanged ()
{
if (ClosedHistoryChanged != null)
ClosedHistoryChanged (null, EventArgs.Empty);
}
#region Handling active doc change events
static void ActiveDocChanged (object sender, EventArgs args)
{
LogActiveDocument (true);
AttachToDoc (IdeApp.Workbench.ActiveDocument);
}
static void AttachToDoc (Document document)
{
DetachFromCurrentDoc ();
if (document == null)
return;
currentDoc = document;
currentDoc.Closed += HandleCurrentDocClosed;
if (currentDoc.Editor != null) {
currentDoc.Editor.Document.TextReplaced += BufferTextChanged;
currentDoc.Editor.Caret.PositionChanged += BufferCaretPositionChanged;
}
}
static void HandleCurrentDocClosed (object sender, EventArgs e)
{
DetachFromCurrentDoc ();
}
static void DetachFromCurrentDoc ()
{
if (currentDoc == null)
return;
currentDoc.Closed -= HandleCurrentDocClosed;
if (currentDoc.Editor != null) {
currentDoc.Editor.Document.TextReplaced -= BufferTextChanged;
currentDoc.Editor.Caret.PositionChanged -= BufferCaretPositionChanged;
}
currentDoc = null;
}
static void BufferCaretPositionChanged (object sender, EventArgs args)
{
LogActiveDocument (true);
}
static void BufferTextChanged (object sender, EventArgs args)
{
LogActiveDocument ();
}
#endregion
#region Text file line number and snippet updating
static void LineCountChanged (object sender, LineCountEventArgs args)
{
// MonoDevelop.Projects.Text.ITextFile textFile = (MonoDevelop.Projects.Text.ITextFile) sender;
}
static void CommitCountChanges (object sender, TextFileEventArgs args)
{
// MonoDevelop.Projects.Text.ITextFile textFile = (MonoDevelop.Projects.Text.ITextFile) sender;
}
static void ResetCountChanges (object sender, TextFileEventArgs args)
{
// MonoDevelop.Projects.Text.ITextFile textFile = (MonoDevelop.Projects.Text.ITextFile) sender;
}
static void FileRenamed (object sender, ProjectFileRenamedEventArgs e)
{
bool historyChanged = false;
bool closedHistoryChanged = false;
foreach (ProjectFileRenamedEventInfo args in e) {
foreach (NavigationHistoryItem point in history) {
var dp = point.NavigationPoint as DocumentNavigationPoint;
historyChanged &= (dp != null && dp.HandleRenameEvent (args.OldName, args.NewName));
closedHistoryChanged &= (dp != null && dp.HandleRenameEvent (args.OldName, args.NewName));
if (historyChanged && closedHistoryChanged)
break;
}
}
if (historyChanged)
OnHistoryChanged ();
if (closedHistoryChanged)
OnClosedHistoryChanged ();
}
#endregion
}
}
|