-
Notifications
You must be signed in to change notification settings - Fork 6
/
InformationBar.cs
404 lines (326 loc) · 12.9 KB
/
InformationBar.cs
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Operations;
namespace FixMixedTabs
{
sealed class InformationBarMargin : ContentControl, IWpfTextViewMargin
{
public const string MarginName = "InformationBar";
private IWpfTextView _textView;
private ITextDocument _document;
private IEditorOperations _operations;
private ITextUndoHistory _undoHistory;
private bool _isDisposed = false;
bool _dontShowAgain = false;
public InformationBarMargin(IWpfTextView textView, ITextDocument document, IEditorOperations editorOperations, ITextUndoHistory undoHistory)
{
_textView = textView;
_document = document;
_operations = editorOperations;
_undoHistory = undoHistory;
var informationBar = new InformationBarControl();
informationBar.Tabify.Click += Tabify;
informationBar.Untabify.Click += Untabify;
informationBar.Hide.Click += Hide;
informationBar.DontShowAgain.Click += DontShowAgain;
this.Height = 0;
this.Content = informationBar;
this.Name = MarginName;
document.FileActionOccurred += FileActionOccurred;
textView.Closed += TextViewClosed;
// Delay the initial check until the view gets focus
textView.GotAggregateFocus += GotAggregateFocus;
}
void DisableInformationBar()
{
this.CloseInformationBar();
_dontShowAgain = true;
if (_document != null)
{
_document.FileActionOccurred -= FileActionOccurred;
_document = null;
}
if (_textView != null)
{
_textView.GotAggregateFocus -= GotAggregateFocus;
_textView.Closed -= TextViewClosed;
_textView = null;
}
}
void CheckTabsAndSpaces()
{
if (_dontShowAgain)
return;
ITextSnapshot snapshot = _textView.TextDataModel.DocumentBuffer.CurrentSnapshot;
int tabSize = _textView.Options.GetOptionValue(DefaultOptions.TabSizeOptionId);
bool startsWithSpaces = false;
bool startsWithTabs = false;
foreach (var line in snapshot.Lines)
{
if (line.Length > 0)
{
char firstChar = line.Start.GetChar();
if (firstChar == '\t')
startsWithTabs = true;
else if (firstChar == ' ')
{
// We need to count to make sure there are enough spaces to go into a tab or a tab that follows the spaces
int countOfSpaces = 1;
for (int i = line.Start + 1; i < line.End; i++)
{
char ch = snapshot[i];
if (ch == ' ')
{
countOfSpaces++;
if (countOfSpaces >= tabSize)
{
startsWithSpaces = true;
break;
}
}
else if (ch == '\t')
{
startsWithSpaces = true;
break;
}
else
{
break;
}
}
}
if (startsWithSpaces && startsWithTabs)
break;
}
}
if (startsWithTabs && startsWithSpaces)
ShowInformationBar();
}
#region Event Handlers
void FileActionOccurred(object sender, TextDocumentFileActionEventArgs e)
{
if (_dontShowAgain)
return;
if ((e.FileActionType & FileActionTypes.ContentLoadedFromDisk) != 0 ||
(e.FileActionType & FileActionTypes.ContentSavedToDisk) != 0)
{
CheckTabsAndSpaces();
}
}
void GotAggregateFocus(object sender, EventArgs e)
{
_textView.GotAggregateFocus -= GotAggregateFocus;
CheckTabsAndSpaces();
}
void TextViewClosed(object sender, EventArgs e)
{
DisableInformationBar();
}
#endregion
#region Hiding and showing the information bar
void Hide(object sender, RoutedEventArgs e)
{
this.CloseInformationBar();
}
void DontShowAgain(object sender, RoutedEventArgs e)
{
this.DisableInformationBar();
}
void CloseInformationBar()
{
if (this.Height == 0 || _dontShowAgain)
return;
// Since we're going to be closing, make sure focus is back in the editor
_textView.VisualElement.Focus();
ChangeHeightTo(0);
}
void ShowInformationBar()
{
if (this.Height > 0 || _dontShowAgain)
return;
ChangeHeightTo(27);
}
void ChangeHeightTo(double newHeight)
{
if (_dontShowAgain)
return;
if (_textView.Options.GetOptionValue(DefaultWpfViewOptions.EnableSimpleGraphicsId))
{
this.Height = newHeight;
}
else
{
DoubleAnimation animation = new DoubleAnimation(this.Height, newHeight, new Duration(TimeSpan.FromMilliseconds(175)));
Storyboard.SetTarget(animation, this);
Storyboard.SetTargetProperty(animation, new PropertyPath(StackPanel.HeightProperty));
Storyboard storyboard = new Storyboard();
storyboard.Children.Add(animation);
storyboard.Begin(this);
}
}
#endregion
#region Performing Tabify and Untabify
void Tabify(object sender, RoutedEventArgs e)
{
PerformActionInUndo(() =>
{
int tabSize = _textView.Options.GetOptionValue(DefaultOptions.TabSizeOptionId);
using (ITextEdit edit = _textView.TextBuffer.CreateEdit())
{
foreach (var line in edit.Snapshot.Lines)
{
bool tabsAfterSpaces = false;
int column = 0;
int spanLength = 0;
int countOfLargestRunOfSpaces = 0;
int countOfCurrentRunOfSpaces = 0;
for (int i = line.Start; i < line.End; i++)
{
char ch = edit.Snapshot[i];
// Increment column or break, depending on the character
if (ch == ' ')
{
countOfCurrentRunOfSpaces++;
countOfLargestRunOfSpaces = Math.Max(countOfLargestRunOfSpaces, countOfCurrentRunOfSpaces);
column++;
spanLength++;
}
else if (ch == '\t')
{
if (countOfLargestRunOfSpaces > 0)
tabsAfterSpaces = true;
countOfCurrentRunOfSpaces = 0;
column += tabSize - (column % tabSize);
spanLength++;
}
else
{
break;
}
}
// Only do a replace if this will have any effect
if (tabsAfterSpaces || countOfLargestRunOfSpaces >= tabSize)
{
int tabCount = column / tabSize;
int spaceCount = column % tabSize;
string newWhitespace = string.Format("{0}{1}",
new string('\t', tabCount),
new string(' ', spaceCount));
if (!edit.Replace(new Span(line.Start, spanLength), newWhitespace))
return false;
}
}
edit.Apply();
return !edit.Canceled;
}
});
this.CloseInformationBar();
}
void Untabify(object sender, RoutedEventArgs e)
{
PerformActionInUndo(() =>
{
int tabSize = _textView.Options.GetOptionValue(DefaultOptions.TabSizeOptionId);
using (ITextEdit edit = _textView.TextBuffer.CreateEdit())
{
foreach (var line in edit.Snapshot.Lines)
{
bool hasTabs = false;
int column = 0;
int spanLength = 0;
for (int i = line.Start; i < line.End; i++)
{
char ch = edit.Snapshot[i];
if (ch == '\t')
{
hasTabs = true;
column += tabSize - (column % tabSize);
spanLength++;
}
else if (ch == ' ')
{
spanLength++;
column++;
}
else
{
break;
}
}
// Only do a replace if this will have any effect
if (hasTabs)
{
string newWhitespace = new string(' ', column);
if (!edit.Replace(new Span(line.Start, spanLength), newWhitespace))
return false;
}
}
edit.Apply();
return !edit.Canceled;
}
});
this.CloseInformationBar();
}
void PerformActionInUndo(Func<bool> action)
{
ITrackingPoint anchor = _textView.TextSnapshot.CreateTrackingPoint(_textView.Selection.AnchorPoint.Position, PointTrackingMode.Positive);
ITrackingPoint active = _textView.TextSnapshot.CreateTrackingPoint(_textView.Selection.ActivePoint.Position, PointTrackingMode.Positive);
bool empty = _textView.Selection.IsEmpty;
TextSelectionMode mode = _textView.Selection.Mode;
using (var undo = _undoHistory.CreateTransaction("Untabify"))
{
_operations.AddBeforeTextBufferChangePrimitive();
if (!action())
{
undo.Cancel();
return;
}
ITextSnapshot after = _textView.TextSnapshot;
_operations.SelectAndMoveCaret(new VirtualSnapshotPoint(anchor.GetPoint(after)),
new VirtualSnapshotPoint(active.GetPoint(after)),
mode,
EnsureSpanVisibleOptions.ShowStart);
_operations.AddAfterTextBufferChangePrimitive();
undo.Complete();
}
}
#endregion
#region IWpfTextViewMargin Members
public FrameworkElement VisualElement
{
get
{
return this;
}
}
#endregion
#region ITextViewMargin Members
public double MarginSize
{
get
{
return this.ActualHeight;
}
}
public bool Enabled
{
get
{
return !_dontShowAgain;
}
}
public ITextViewMargin GetTextViewMargin(string marginName)
{
return (marginName == InformationBarMargin.MarginName) ? (IWpfTextViewMargin)this : null;
}
public void Dispose()
{
this.DisableInformationBar();
}
#endregion
}
}