Skip to content

Commit feec280

Browse files
committed
Set and verify the caret position
1 parent eca989b commit feec280

File tree

7 files changed

+97
-0
lines changed

7 files changed

+97
-0
lines changed

Tvl.VisualStudio.MouseFastScroll.IntegrationTests/Editor_InProc.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,22 @@ public void SetText(string text)
6363
view.TextBuffer.Replace(replacementSpan, text);
6464
});
6565
}
66+
67+
public void MoveCaret(int position)
68+
{
69+
ExecuteOnActiveView(
70+
view =>
71+
{
72+
var subjectBuffer = view.GetBufferContainingCaret();
73+
var point = new SnapshotPoint(subjectBuffer.CurrentSnapshot, position);
74+
75+
view.Caret.MoveTo(point);
76+
});
77+
}
78+
79+
protected override ITextBuffer GetBufferContainingCaret(IWpfTextView view)
80+
{
81+
return view.GetBufferContainingCaret();
82+
}
6683
}
6784
}

Tvl.VisualStudio.MouseFastScroll.IntegrationTests/Editor_OutOfProc.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ internal string GetText()
2828
internal void SetText(string value)
2929
=> EditorInProc.SetText(value);
3030

31+
public void MoveCaret(int position)
32+
=> EditorInProc.MoveCaret(position);
33+
3134
/// <summary>
3235
/// Sends key strokes to the active editor in Visual Studio. Various types are supported by this method:
3336
/// <see cref="string"/> (each character will be sent separately, <see cref="char"/>, and
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE.txt in the project root for license information.
3+
4+
namespace Tvl.VisualStudio.MouseFastScroll.IntegrationTests
5+
{
6+
using System;
7+
using System.Linq;
8+
using Microsoft.VisualStudio.Text;
9+
using Microsoft.VisualStudio.Text.Projection;
10+
11+
internal static class IBufferGraphExtensions
12+
{
13+
public static SnapshotSpan? MapUpOrDownToFirstMatch(this IBufferGraph bufferGraph, SnapshotSpan span, Predicate<ITextSnapshot> match)
14+
{
15+
var spans = bufferGraph.MapDownToFirstMatch(span, SpanTrackingMode.EdgeExclusive, match);
16+
if (!spans.Any())
17+
{
18+
spans = bufferGraph.MapUpToFirstMatch(span, SpanTrackingMode.EdgeExclusive, match);
19+
}
20+
21+
return spans.Select(s => (SnapshotSpan?)s).FirstOrDefault();
22+
}
23+
}
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE.txt in the project root for license information.
3+
4+
namespace Tvl.VisualStudio.MouseFastScroll.IntegrationTests
5+
{
6+
using System;
7+
using Microsoft.VisualStudio.Text;
8+
using Microsoft.VisualStudio.Text.Editor;
9+
10+
internal static class ITextViewExtensions
11+
{
12+
public static SnapshotPoint? GetCaretPoint(this ITextView textView, Predicate<ITextSnapshot> match)
13+
{
14+
var caret = textView.Caret.Position;
15+
var span = textView.BufferGraph.MapUpOrDownToFirstMatch(new SnapshotSpan(caret.BufferPosition, 0), match);
16+
if (span.HasValue)
17+
{
18+
return span.Value.Start;
19+
}
20+
else
21+
{
22+
return null;
23+
}
24+
}
25+
26+
public static ITextBuffer GetBufferContainingCaret(this ITextView textView, string contentType = "text")
27+
{
28+
var point = GetCaretPoint(textView, s => s.ContentType.IsOfType(contentType));
29+
return point.HasValue ? point.Value.Snapshot.TextBuffer : null;
30+
}
31+
}
32+
}

Tvl.VisualStudio.MouseFastScroll.IntegrationTests/TextViewWindow_InProc.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,26 @@
44
namespace Tvl.VisualStudio.MouseFastScroll.IntegrationTests
55
{
66
using System;
7+
using Microsoft.VisualStudio.Text;
78
using Microsoft.VisualStudio.Text.Editor;
89

910
internal abstract class TextViewWindow_InProc : InProcComponent
1011
{
1112
protected abstract IWpfTextView GetActiveTextView();
1213

14+
protected abstract ITextBuffer GetBufferContainingCaret(IWpfTextView view);
15+
16+
public int GetCaretPosition()
17+
{
18+
return ExecuteOnActiveView(
19+
view =>
20+
{
21+
var subjectBuffer = GetBufferContainingCaret(view);
22+
var bufferPosition = view.Caret.Position.BufferPosition;
23+
return bufferPosition.Position;
24+
});
25+
}
26+
1327
protected T ExecuteOnActiveView<T>(Func<IWpfTextView, T> action)
1428
{
1529
return InvokeOnUIThread(

Tvl.VisualStudio.MouseFastScroll.IntegrationTests/TextViewWindow_OutOfProc.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ internal TextViewWindow_InProc TextViewWindowInProc
1717
}
1818

1919
internal abstract TextViewWindow_InProc CreateInProcComponent(VisualStudioInstance visualStudioInstance);
20+
21+
public int GetCaretPosition()
22+
=> TextViewWindowInProc.GetCaretPosition();
2023
}
2124
}

Tvl.VisualStudio.MouseFastScroll.IntegrationTests/TrivialIntegrationTest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public void OpenDocumentAndType()
4343
string expected = initialText + additionalTypedText.Replace("\n", Environment.NewLine);
4444
Assert.Equal(expected, VisualStudio.Editor.GetText());
4545

46+
Assert.Equal(expected.Length, VisualStudio.Editor.GetCaretPosition());
47+
VisualStudio.Editor.MoveCaret(0);
48+
Assert.Equal(0, VisualStudio.Editor.GetCaretPosition());
49+
4650
VisualStudioInstance.RetryRpcCall(() => window.Close(vsSaveChanges.vsSaveChangesNo));
4751
}
4852

0 commit comments

Comments
 (0)