forked from NoahRic/FixMixedTabs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInformationBar.cs
264 lines (208 loc) · 7.56 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
using System;
using System.Linq;
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 bool _isDisposed = false;
private ITextDocument _document;
private IEditorOperations _operations;
private ITextUndoHistory _undoHistory;
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;
// Delay the initial check until the view gets focus
textView.GotAggregateFocus += GotAggregateFocus;
}
void CheckTabsAndSpaces()
{
if (dontShowAgain)
return;
ITextSnapshot snapshot = _textView.TextDataModel.DocumentBuffer.CurrentSnapshot;
bool startsWithSpaces = false;
bool startsWithTabs = false;
int lineNumber = 1;
foreach (var line in snapshot.Lines)
{
if (line.Length > 0)
{
char firstChar = line.Start.GetChar();
if (firstChar == '\t')
{
startsWithTabs = true;
}
else if (firstChar == ' ')
{
// TODO: don't use '4', instead get the configured tab size.
if (line.GetText().TakeWhile(c => c == ' ').Count() >= 4)
{
startsWithSpaces = true;
}
}
if (startsWithSpaces && startsWithTabs)
break;
lineNumber += 1;
}
}
if (startsWithTabs && startsWithSpaces)
ShowInformationBar();
}
#region Event Handlers
void FileActionOccurred(object sender, TextDocumentFileActionEventArgs e)
{
if ((e.FileActionType & FileActionTypes.ContentLoadedFromDisk) != 0 ||
(e.FileActionType & FileActionTypes.ContentSavedToDisk) != 0)
{
CheckTabsAndSpaces();
}
}
void GotAggregateFocus(object sender, EventArgs e)
{
_textView.GotAggregateFocus -= GotAggregateFocus;
CheckTabsAndSpaces();
}
#endregion
#region Hiding and showing the information bar
void Hide(object sender, RoutedEventArgs e)
{
this.CloseInformationBar();
}
void DontShowAgain(object sender, RoutedEventArgs e)
{
this.dontShowAgain = true;
this.CloseInformationBar();
}
void CloseInformationBar()
{
if (this.Height == 0)
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 (_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(() =>
{
_operations.SelectAll();
_operations.Tabify();
});
this.CloseInformationBar();
}
void Untabify(object sender, RoutedEventArgs e)
{
PerformActionInUndo(() =>
{
_operations.SelectAll();
_operations.Untabify();
});
this.CloseInformationBar();
}
void PerformActionInUndo(Action 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();
action();
ITextSnapshot after = _textView.TextSnapshot;
_operations.SelectAndMoveCaret(new VirtualSnapshotPoint(anchor.GetPoint(after)),
new VirtualSnapshotPoint(active.GetPoint(after)),
mode,
EnsureSpanVisibleOptions.ShowStart);
_operations.AddAfterTextBufferChangePrimitive();
undo.Complete();
}
}
#endregion
private void ThrowIfDisposed()
{
if (_isDisposed)
throw new ObjectDisposedException(MarginName);
}
#region IWpfTextViewMargin Members
public FrameworkElement VisualElement
{
get
{
ThrowIfDisposed();
return this;
}
}
#endregion
#region ITextViewMargin Members
public double MarginSize
{
get
{
ThrowIfDisposed();
return this.ActualHeight;
}
}
public bool Enabled
{
get
{
ThrowIfDisposed();
return true;
}
}
public ITextViewMargin GetTextViewMargin(string marginName)
{
return (marginName == InformationBarMargin.MarginName) ? (IWpfTextViewMargin)this : null;
}
public void Dispose()
{
// Nothing to do here.
}
#endregion
}
}