Skip to content

Commit eca989b

Browse files
committed
Set and verify the text in the editor
1 parent 91dd44b commit eca989b

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

Tvl.VisualStudio.MouseFastScroll.IntegrationTests/Editor_InProc.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,65 @@
33

44
namespace Tvl.VisualStudio.MouseFastScroll.IntegrationTests
55
{
6+
using System;
7+
using System.Runtime.InteropServices;
8+
using Microsoft.VisualStudio.Text;
9+
using Microsoft.VisualStudio.Text.Editor;
10+
using Microsoft.VisualStudio.TextManager.Interop;
11+
612
internal class Editor_InProc : TextViewWindow_InProc
713
{
14+
private static readonly Guid IWpfTextViewId = new Guid("8C40265E-9FDB-4F54-A0FD-EBB72B7D0476");
15+
816
private Editor_InProc()
917
{
1018
}
1119

1220
public static Editor_InProc Create()
1321
=> new Editor_InProc();
1422

23+
protected override IWpfTextView GetActiveTextView()
24+
=> GetActiveTextViewHost().TextView;
25+
26+
private static IVsTextView GetActiveVsTextView()
27+
{
28+
var vsTextManager = GetGlobalService<SVsTextManager, IVsTextManager>();
29+
30+
var hresult = vsTextManager.GetActiveView(fMustHaveFocus: 1, pBuffer: null, ppView: out var vsTextView);
31+
Marshal.ThrowExceptionForHR(hresult);
32+
33+
return vsTextView;
34+
}
35+
36+
private static IWpfTextViewHost GetActiveTextViewHost()
37+
{
38+
// The active text view might not have finished composing yet, waiting for the application to 'idle'
39+
// means that it is done pumping messages (including WM_PAINT) and the window should return the correct text view
40+
WaitForApplicationIdle();
41+
42+
var activeVsTextView = (IVsUserData)GetActiveVsTextView();
43+
44+
var hresult = activeVsTextView.GetData(IWpfTextViewId, out var wpfTextViewHost);
45+
Marshal.ThrowExceptionForHR(hresult);
46+
47+
return (IWpfTextViewHost)wpfTextViewHost;
48+
}
49+
1550
public void Activate()
1651
=> GetDTE().ActiveDocument.Activate();
52+
53+
public string GetText()
54+
=> ExecuteOnActiveView(view => view.TextSnapshot.GetText());
55+
56+
public void SetText(string text)
57+
{
58+
ExecuteOnActiveView(
59+
view =>
60+
{
61+
var textSnapshot = view.TextSnapshot;
62+
var replacementSpan = new SnapshotSpan(textSnapshot, 0, textSnapshot.Length);
63+
view.TextBuffer.Replace(replacementSpan, text);
64+
});
65+
}
1766
}
1867
}

Tvl.VisualStudio.MouseFastScroll.IntegrationTests/Editor_OutOfProc.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ internal override TextViewWindow_InProc CreateInProcComponent(VisualStudioInstan
2222
public void Activate()
2323
=> EditorInProc.Activate();
2424

25+
internal string GetText()
26+
=> EditorInProc.GetText();
27+
28+
internal void SetText(string value)
29+
=> EditorInProc.SetText(value);
30+
2531
/// <summary>
2632
/// Sends key strokes to the active editor in Visual Studio. Various types are supported by this method:
2733
/// <see cref="string"/> (each character will be sent separately, <see cref="char"/>, and

Tvl.VisualStudio.MouseFastScroll.IntegrationTests/TextViewWindow_InProc.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,33 @@
33

44
namespace Tvl.VisualStudio.MouseFastScroll.IntegrationTests
55
{
6+
using System;
7+
using Microsoft.VisualStudio.Text.Editor;
8+
69
internal abstract class TextViewWindow_InProc : InProcComponent
710
{
11+
protected abstract IWpfTextView GetActiveTextView();
12+
13+
protected T ExecuteOnActiveView<T>(Func<IWpfTextView, T> action)
14+
{
15+
return InvokeOnUIThread(
16+
() =>
17+
{
18+
var view = GetActiveTextView();
19+
return action(view);
20+
});
21+
}
22+
23+
protected void ExecuteOnActiveView(Action<IWpfTextView> action)
24+
=> InvokeOnUIThread(GetExecuteOnActionViewCallback(action));
25+
26+
protected Action GetExecuteOnActionViewCallback(Action<IWpfTextView> action)
27+
{
28+
return () =>
29+
{
30+
var view = GetActiveTextView();
31+
action(view);
32+
};
33+
}
834
}
935
}

Tvl.VisualStudio.MouseFastScroll.IntegrationTests/TrivialIntegrationTest.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace Tvl.VisualStudio.MouseFastScroll.IntegrationTests
55
{
66
using System;
7-
using System.Threading;
7+
using System.Linq;
88
using Xunit;
99
using vsSaveChanges = EnvDTE.vsSaveChanges;
1010

@@ -33,7 +33,16 @@ public void TestOpenAndCloseIDE()
3333
public void OpenDocumentAndType()
3434
{
3535
var window = VisualStudioInstance.RetryRpcCall(() => VisualStudio.Dte.ItemOperations.NewFile(Name: Guid.NewGuid() + ".txt"));
36-
VisualStudio.Editor.SendKeys(Guid.NewGuid().ToString() + "\n" + Guid.NewGuid().ToString());
36+
37+
string initialText = string.Join(string.Empty, Enumerable.Range(0, 400).Select(i => Guid.NewGuid() + Environment.NewLine));
38+
VisualStudio.Editor.SetText(initialText);
39+
40+
string additionalTypedText = Guid.NewGuid().ToString() + "\n" + Guid.NewGuid().ToString();
41+
VisualStudio.Editor.SendKeys(additionalTypedText);
42+
43+
string expected = initialText + additionalTypedText.Replace("\n", Environment.NewLine);
44+
Assert.Equal(expected, VisualStudio.Editor.GetText());
45+
3746
VisualStudioInstance.RetryRpcCall(() => window.Close(vsSaveChanges.vsSaveChangesNo));
3847
}
3948

0 commit comments

Comments
 (0)