Skip to content

Commit 5939a29

Browse files
author
Noah Richards
committed
Updated mouse processor to work on mouse-up, and work around some of the issues that go along with that. It should now work better with drag/drop and the word selection logic. Bumped to v2.2.
1 parent e4fa098 commit 5939a29

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
lines changed

Classifier.cs

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ void SendEvent(SnapshotSpan span)
9090

9191
#region UnderlineClassification public members
9292

93+
public SnapshotSpan? CurrentUnderlineSpan { get { return _underlineSpan; } }
94+
9395
public void SetUnderlineSpan(SnapshotSpan? span)
9496
{
9597
var oldSpan = _underlineSpan;

GoToDefMouseHandler.cs

+52-9
Original file line numberDiff line numberDiff line change
@@ -180,25 +180,49 @@ public GoToDefMouseHandler(IWpfTextView view, IOleCommandTarget commandTarget, I
180180

181181
#region Mouse processor overrides
182182

183+
// Remember the location of the mouse on left button down, so we only handle left button up
184+
// if the mouse has stayed in a single location.
185+
Point? _mouseDownAnchorPoint;
186+
183187
public override void PostprocessMouseLeftButtonDown(MouseButtonEventArgs e)
184188
{
185-
if (_state.Enabled)
186-
{
187-
_state.Enabled = false;
188-
this.SetHighlightSpan(null);
189-
this.DispatchGoToDef();
190-
}
189+
_mouseDownAnchorPoint = RelativeToView(e.GetPosition(_view.VisualElement));
191190
}
192191

193192
public override void PreprocessMouseMove(MouseEventArgs e)
194193
{
195-
if (_state.Enabled)
194+
if (!_mouseDownAnchorPoint.HasValue && _state.Enabled)
196195
{
197196
TryHighlightItemUnderMouse(RelativeToView(e.GetPosition(_view.VisualElement)));
198197
}
198+
}
199+
200+
public override void PreprocessMouseLeave(MouseEventArgs e)
201+
{
202+
_mouseDownAnchorPoint = null;
203+
}
204+
205+
public override void PreprocessMouseUp(MouseButtonEventArgs e)
206+
{
207+
if (_mouseDownAnchorPoint.HasValue && _state.Enabled)
208+
{
209+
var currentMousePosition = RelativeToView(e.GetPosition(_view.VisualElement));
210+
211+
// If the mouse up was less than a drag away from the mouse down, consider this a click
212+
if (Math.Abs(_mouseDownAnchorPoint.Value.X - currentMousePosition.X) < SystemParameters.MinimumHorizontalDragDistance &&
213+
Math.Abs(_mouseDownAnchorPoint.Value.Y - currentMousePosition.Y) < SystemParameters.MinimumVerticalDragDistance)
214+
{
215+
_state.Enabled = false;
216+
217+
this.SetHighlightSpan(null);
218+
_view.Selection.Clear();
219+
this.DispatchGoToDef();
220+
221+
e.Handled = true;
222+
}
199223

200-
// Don't mark the event as handled, so other mouse processors have a chance to do their work
201-
// (such as clicking+dragging to select text)
224+
_mouseDownAnchorPoint = null;
225+
}
202226
}
203227

204228
#endregion
@@ -225,6 +249,14 @@ bool TryHighlightItemUnderMouse(Point position)
225249
if (!bufferPosition.HasValue)
226250
return false;
227251

252+
// Quick check - if the mouse is still inside the current underline span, we're already set
253+
var currentSpan = CurrentUnderlineSpan;
254+
if (currentSpan.HasValue && currentSpan.Value.Contains(bufferPosition.Value))
255+
{
256+
updated = true;
257+
return true;
258+
}
259+
228260
var extent = _navigator.GetExtentOfWord(bufferPosition.Value);
229261
if (!extent.IsSignificant)
230262
return false;
@@ -261,6 +293,17 @@ bool TryHighlightItemUnderMouse(Point position)
261293
}
262294
}
263295

296+
SnapshotSpan? CurrentUnderlineSpan
297+
{
298+
get
299+
{
300+
var classifier = UnderlineClassifierProvider.GetClassifierForView(_view);
301+
if (classifier != null && classifier.CurrentUnderlineSpan.HasValue)
302+
return classifier.CurrentUnderlineSpan.Value.TranslateTo(_view.TextSnapshot, SpanTrackingMode.EdgeExclusive);
303+
else
304+
return null;
305+
}
306+
}
264307

265308
bool SetHighlightSpan(SnapshotSpan? span)
266309
{

source.extension.vsixmanifest

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<Identifier Id="GoToDef">
44
<Name>Go To Definition</Name>
55
<Author>Noah Richards</Author>
6-
<Version>2.1</Version>
6+
<Version>2.2</Version>
77
<Description xml:space="preserve">Make ctrl+click perform a "Go To Definition" on the identifier under the cursor.
88
Also, when the ctrl key is held down, highlight identifiers that look like they have
99
definitions to navigate to.</Description>

0 commit comments

Comments
 (0)