-
Notifications
You must be signed in to change notification settings - Fork 18
/
GoToDefMouseHandler.cs
355 lines (300 loc) · 12.7 KB
/
GoToDefMouseHandler.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
using System;
using System.ComponentModel.Composition;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Operations;
using Microsoft.VisualStudio.Utilities;
using Microsoft.VisualStudio.Shell;
namespace GoToDef
{
[Export(typeof(IKeyProcessorProvider))]
[TextViewRole(PredefinedTextViewRoles.Document)]
[ContentType("code")]
[Name("GotoDef")]
[Order(Before = "VisualStudioKeyboardProcessor")]
internal sealed class GoToDefKeyProcessorProvider : IKeyProcessorProvider
{
public KeyProcessor GetAssociatedProcessor(IWpfTextView view)
{
return view.Properties.GetOrCreateSingletonProperty(typeof(GoToDefKeyProcessor),
() => new GoToDefKeyProcessor(CtrlKeyState.GetStateForView(view)));
}
}
/// <summary>
/// The state of the control key for a given view, which is kept up-to-date by a combination of the
/// key processor and the mouse process
/// </summary>
internal sealed class CtrlKeyState
{
internal static CtrlKeyState GetStateForView(ITextView view)
{
return view.Properties.GetOrCreateSingletonProperty(typeof(CtrlKeyState), () => new CtrlKeyState());
}
bool _enabled = false;
internal bool Enabled
{
get
{
// Check and see if ctrl is down but we missed it somehow.
bool ctrlDown = (Keyboard.Modifiers & ModifierKeys.Control) != 0 &&
(Keyboard.Modifiers & ModifierKeys.Shift) == 0;
if (ctrlDown != _enabled)
Enabled = ctrlDown;
return _enabled;
}
set
{
bool oldVal = _enabled;
_enabled = value;
if (oldVal != _enabled)
{
var temp = CtrlKeyStateChanged;
if (temp != null)
temp(this, new EventArgs());
}
}
}
internal event EventHandler<EventArgs> CtrlKeyStateChanged;
}
/// <summary>
/// Listen for the control key being pressed or released to update the CtrlKeyStateChanged for a view.
/// </summary>
internal sealed class GoToDefKeyProcessor : KeyProcessor
{
CtrlKeyState _state;
public GoToDefKeyProcessor(CtrlKeyState state)
{
_state = state;
}
void UpdateState(KeyEventArgs args)
{
_state.Enabled = (args.KeyboardDevice.Modifiers & ModifierKeys.Control) != 0 &&
(args.KeyboardDevice.Modifiers & ModifierKeys.Shift) == 0;
}
public override void PreviewKeyDown(KeyEventArgs args)
{
UpdateState(args);
}
public override void PreviewKeyUp(KeyEventArgs args)
{
UpdateState(args);
}
}
[Export(typeof(IMouseProcessorProvider))]
[TextViewRole(PredefinedTextViewRoles.Document)]
[ContentType("code")]
[Name("GotoDef")]
[Order(Before = "WordSelection")]
internal sealed class GoToDefMouseHandlerProvider : IMouseProcessorProvider
{
[Import]
IClassifierAggregatorService AggregatorFactory = null;
[Import]
ITextStructureNavigatorSelectorService NavigatorService = null;
[Import]
SVsServiceProvider GlobalServiceProvider = null;
public IMouseProcessor GetAssociatedProcessor(IWpfTextView view)
{
var buffer = view.TextBuffer;
IOleCommandTarget shellCommandDispatcher = GetShellCommandDispatcher(view);
if (shellCommandDispatcher == null)
return null;
return new GoToDefMouseHandler(view,
shellCommandDispatcher,
AggregatorFactory.GetClassifier(buffer),
NavigatorService.GetTextStructureNavigator(buffer),
CtrlKeyState.GetStateForView(view));
}
#region Private helpers
/// <summary>
/// Get the SUIHostCommandDispatcher from the global service provider.
/// </summary>
IOleCommandTarget GetShellCommandDispatcher(ITextView view)
{
return GlobalServiceProvider.GetService(typeof(SUIHostCommandDispatcher)) as IOleCommandTarget;
}
#endregion
}
/// <summary>
/// Handle ctrl+click on valid elements to send GoToDefinition to the shell. Also handle mouse moves
/// (when control is pressed) to highlight references for which GoToDefinition will (likely) be valid.
/// </summary>
internal sealed class GoToDefMouseHandler : MouseProcessorBase
{
IWpfTextView _view;
CtrlKeyState _state;
IClassifier _aggregator;
ITextStructureNavigator _navigator;
IOleCommandTarget _commandTarget;
public GoToDefMouseHandler(IWpfTextView view, IOleCommandTarget commandTarget, IClassifier aggregator,
ITextStructureNavigator navigator, CtrlKeyState state)
{
_view = view;
_commandTarget = commandTarget;
_state = state;
_aggregator = aggregator;
_navigator = navigator;
_state.CtrlKeyStateChanged += (sender, args) =>
{
if (_state.Enabled)
this.TryHighlightItemUnderMouse(RelativeToView(Mouse.PrimaryDevice.GetPosition(_view.VisualElement)));
else
this.SetHighlightSpan(null);
};
// Some other points to clear the highlight span:
_view.LostAggregateFocus += (sender, args) => this.SetHighlightSpan(null);
_view.VisualElement.MouseLeave += (sender, args) => this.SetHighlightSpan(null);
}
#region Mouse processor overrides
// Remember the location of the mouse on left button down, so we only handle left button up
// if the mouse has stayed in a single location.
Point? _mouseDownAnchorPoint;
public override void PostprocessMouseLeftButtonDown(MouseButtonEventArgs e)
{
_mouseDownAnchorPoint = RelativeToView(e.GetPosition(_view.VisualElement));
}
public override void PreprocessMouseMove(MouseEventArgs e)
{
if (!_mouseDownAnchorPoint.HasValue && _state.Enabled && e.LeftButton == MouseButtonState.Released)
{
TryHighlightItemUnderMouse(RelativeToView(e.GetPosition(_view.VisualElement)));
}
else if (_mouseDownAnchorPoint.HasValue)
{
// Check and see if this is a drag; if so, clear out the highlight.
var currentMousePosition = RelativeToView(e.GetPosition(_view.VisualElement));
if (InDragOperation(_mouseDownAnchorPoint.Value, currentMousePosition))
{
_mouseDownAnchorPoint = null;
this.SetHighlightSpan(null);
}
}
}
private bool InDragOperation(Point anchorPoint, Point currentPoint)
{
// If the mouse up is more than a drag away from the mouse down, this is a drag
return Math.Abs(anchorPoint.X - currentPoint.X) >= SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(anchorPoint.Y - currentPoint.Y) >= SystemParameters.MinimumVerticalDragDistance;
}
public override void PreprocessMouseLeave(MouseEventArgs e)
{
_mouseDownAnchorPoint = null;
}
public override void PreprocessMouseUp(MouseButtonEventArgs e)
{
if (_mouseDownAnchorPoint.HasValue && _state.Enabled)
{
var currentMousePosition = RelativeToView(e.GetPosition(_view.VisualElement));
if (!InDragOperation(_mouseDownAnchorPoint.Value, currentMousePosition))
{
_state.Enabled = false;
this.SetHighlightSpan(null);
_view.Selection.Clear();
this.DispatchGoToDef();
e.Handled = true;
}
}
_mouseDownAnchorPoint = null;
}
#endregion
#region Private helpers
Point RelativeToView(Point position)
{
return new Point(position.X + _view.ViewportLeft, position.Y + _view.ViewportTop);
}
bool TryHighlightItemUnderMouse(Point position)
{
bool updated = false;
try
{
var line = _view.TextViewLines.GetTextViewLineContainingYCoordinate(position.Y);
if (line == null)
return false;
var bufferPosition = line.GetBufferPositionFromXCoordinate(position.X);
if (!bufferPosition.HasValue)
return false;
// Quick check - if the mouse is still inside the current underline span, we're already set
var currentSpan = CurrentUnderlineSpan;
if (currentSpan.HasValue && currentSpan.Value.Contains(bufferPosition.Value))
{
updated = true;
return true;
}
var extent = _navigator.GetExtentOfWord(bufferPosition.Value);
if (!extent.IsSignificant)
return false;
// For C#, we ignore namespaces after using statements - GoToDef will fail for those
if (_view.TextBuffer.ContentType.IsOfType("csharp"))
{
string lineText = bufferPosition.Value.GetContainingLine().GetText().Trim();
if (lineText.StartsWith("using", StringComparison.OrdinalIgnoreCase))
return false;
}
// Now, check for valid classification type. C# and C++ (at least) classify the things we are interested
// in as either "identifier" or "user types" (though "identifier" will yield some false positives). VB, unfortunately,
// doesn't classify identifiers.
foreach (var classification in _aggregator.GetClassificationSpans(extent.Span))
{
var name = classification.ClassificationType.Classification.ToLower();
if ((name.Contains("identifier") || name.Contains("user types")) &&
SetHighlightSpan(classification.Span))
{
updated = true;
return true;
}
}
// No update occurred, so return false
return false;
}
finally
{
if (!updated)
SetHighlightSpan(null);
}
}
SnapshotSpan? CurrentUnderlineSpan
{
get
{
var classifier = UnderlineClassifierProvider.GetClassifierForView(_view);
if (classifier != null && classifier.CurrentUnderlineSpan.HasValue)
return classifier.CurrentUnderlineSpan.Value.TranslateTo(_view.TextSnapshot, SpanTrackingMode.EdgeExclusive);
else
return null;
}
}
bool SetHighlightSpan(SnapshotSpan? span)
{
var classifier = UnderlineClassifierProvider.GetClassifierForView(_view);
if (classifier != null)
{
if (span.HasValue)
Mouse.OverrideCursor = Cursors.Hand;
else
Mouse.OverrideCursor = null;
classifier.SetUnderlineSpan(span);
return true;
}
return false;
}
bool DispatchGoToDef()
{
Guid cmdGroup = VSConstants.GUID_VSStandardCommandSet97;
int hr = _commandTarget.Exec(ref cmdGroup,
(uint)VSConstants.VSStd97CmdID.GotoDefn,
(uint)OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT,
System.IntPtr.Zero,
System.IntPtr.Zero);
return ErrorHandler.Succeeded(hr);
}
#endregion
}
}