-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
OutputWindowPane.cs
300 lines (255 loc) · 10.6 KB
/
OutputWindowPane.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
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Threading;
using Task = System.Threading.Tasks.Task;
namespace Community.VisualStudio.Toolkit
{
/// <summary>
/// A pane in the Output window.
/// </summary>
/// <remarks>
/// OutputWindowPane allows an extension to create a new Output window pane or get an existing one.
/// A pane can be activated (shown), hidden and cleared. Text can be written to the pane via methods
/// like <see cref="WriteLineAsync(string)"/> or with a <see cref="TextWriter"/> returned
/// from <see cref="CreateOutputPaneTextWriterAsync"/>.
/// </remarks>
/// <example>
/// <code>
/// Guid myPaneGuid;
/// {
/// OutputWindowPane pane = await VS.Windows.CreateOutputWindowPaneAsync("My Pane");
/// myPaneGuid = pane.Guid;
/// await pane.WriteLineAsync("My message");
/// }
///
/// // Elsewhere:
/// {
/// OutputWindowPane pane = await VS.Windows.GetOutputWindowPaneAsync(myPaneGuid);
/// Debug.Assert(pane.Name == "My Pane");
/// using (TextWriter writer = await pane.CreateOutputPaneTextWriterAsync())
/// {
/// char[] buffer = GetSomeChars();
/// await writer.WriteLineAsync("This is a more efficient way to write lots of text.");
/// await writer.WriteAsync(buffer, 0, buffer.Length);
/// await writer.WriteLineAsync();
/// }
/// }
/// </code>
/// </example>
public class OutputWindowPane
{
private IVsOutputWindowPane? _pane;
private string _paneName;
private OutputWindowPane(string newPaneName, Guid paneGuid)
{
_paneName = newPaneName;
Guid = paneGuid;
}
/// <summary>
/// Uniquely identifies the Output window pane.
/// After creating a pane, you can cache this Guid and later obtain the same pane from <see cref="GetAsync(Guid)"/>.
/// </summary>
public Guid Guid { get; }
/// <summary>
/// The name (title) of the Output window pane.
/// </summary>
public string Name
{
get
{
// We may already have the pane's name either from CreateOutputWindowPaneAsync, or a previous call to this getter.
if (!string.IsNullOrEmpty(_paneName))
return _paneName;
// Query the pane's name and then cache it for future use.
_paneName = ThreadHelper.JoinableTaskFactory.Run(async () =>
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
await EnsurePaneAsync();
if (_pane == null)
throw new InvalidOperationException("IVsOutputWindowPane should exist");
string name = string.Empty;
_pane.GetName(ref name);
return name;
});
return _paneName;
}
}
/// <summary>
/// The underlying OutputWindow Pane object.
/// </summary>
public IVsOutputWindowPane? Pane => _pane;
/// <summary>
/// Creates a new Output window pane with the given name (title).
/// The new pane can be created at construction time or lazily upon first write.
/// </summary>
/// <param name="name">The name (title) of the new pane.</param>
/// <param name="lazyCreate">Whether to lazily create the pane upon first write.</param>
/// <returns>A new OutputWindowPane.</returns>
public static async Task<OutputWindowPane> CreateAsync(string name, bool lazyCreate = true)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentException(nameof(name));
OutputWindowPane pane = new OutputWindowPane(name, Guid.NewGuid());
if (!lazyCreate)
{
await pane.EnsurePaneAsync();
}
return pane;
}
/// <summary>
/// Gets an existing Visual Studio Output window pane (General, Build, Debug).
/// If the General pane does not already exist then it will be created, but that is not
/// the case for Build or Debug, in which case the method returns null.
/// </summary>
/// <param name="pane">The Visual Studio pane to get.</param>
/// <returns>A new OutputWindowPane or null.</returns>
public static Task<OutputWindowPane?> GetAsync(Windows.VSOutputWindowPane pane)
{
return pane switch
{
Windows.VSOutputWindowPane.General => GetAsync(VSConstants.OutputWindowPaneGuid.GeneralPane_guid),
Windows.VSOutputWindowPane.Build => GetAsync(VSConstants.OutputWindowPaneGuid.BuildOutputPane_guid),
Windows.VSOutputWindowPane.SortedBuild => GetAsync(VSConstants.OutputWindowPaneGuid.SortedBuildOutputPane_guid),
Windows.VSOutputWindowPane.Debug => GetAsync(VSConstants.OutputWindowPaneGuid.DebugPane_guid),
_ => throw new InvalidOperationException("Unexpected VisualStudioPane"),
};
}
/// <summary>
/// Gets an existing Output window pane.
/// Returns null if a pane with the specified guid does not exist.
/// </summary>
/// <param name="guid">The pane's unique identifier.</param>
/// <returns>A new OutputWindowPane or null.</returns>
public static async Task<OutputWindowPane?> GetAsync(Guid guid)
{
// Empty string for `newPaneName` signals to EnsurePaneAsync that we want to get an existing pane.
OutputWindowPane pane = new OutputWindowPane(string.Empty, guid);
try
{
await pane.EnsurePaneAsync();
return pane;
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// Shows and activates the Output window pane.
/// </summary>
public async Task ActivateAsync()
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
await EnsurePaneAsync();
if (_pane == null)
throw new InvalidOperationException("IVsOutputWindowPane should exist");
_pane.Activate();
}
/// <summary>
/// Hides the Output window pane.
/// </summary>
public async Task HideAsync()
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
await EnsurePaneAsync();
if (_pane == null)
throw new InvalidOperationException("IVsOutputWindowPane should exist");
_pane.Hide();
}
/// <summary>
/// Clears the Output window pane.
/// </summary>
public async Task ClearAsync()
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
await EnsurePaneAsync();
if (_pane == null)
throw new InvalidOperationException("IVsOutputWindowPane should exist");
_pane.Clear();
}
/// <summary>
/// Writes a new line to the Output window pane.
/// </summary>
public void WriteLine()
{
ThreadHelper.JoinableTaskFactory.Run(async () =>
{
await WriteLineAsync();
});
}
/// <summary>
/// Writes the given text followed by a new line to the Output window pane.
/// </summary>
/// <param name="value">The text value to write.</param>
public void WriteLine(string value)
{
ThreadHelper.JoinableTaskFactory.Run(async () =>
{
await WriteLineAsync(value);
});
}
/// <summary>
/// Writes a new line to the Output window pane.
/// </summary>
public Task WriteLineAsync()
{
return WriteLineAsync(string.Empty);
}
/// <summary>
/// Writes the given text followed by a new line to the Output window pane.
/// </summary>
/// <param name="value">The text value to write. May be an empty string, in which case a newline is written.</param>
public async Task WriteLineAsync(string value)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
await EnsurePaneAsync();
if (_pane == null)
throw new InvalidOperationException("IVsOutputWindowPane should exist");
_pane.OutputString(value + Environment.NewLine);
}
/// <summary>
/// Returns a new <see cref="TextWriter"/> that can be used to write text to the Output window pane.
/// For newer versions of Visual Studio this provides a more efficient way to write lots
/// of text to the pane.
/// </summary>
public async Task<TextWriter> CreateOutputPaneTextWriterAsync()
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
await EnsurePaneAsync();
if (_pane == null)
throw new InvalidOperationException("IVsOutputWindowPane should exist");
#if VS16 || VS17
return new OutputWindowTextWriter(_pane);
#else
return new OutputWindowTextWriterVS14(_pane); // For VS15 too
#endif
}
private async Task EnsurePaneAsync()
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
if (_pane == null)
{
// Special case for Visual Studio's General pane
if (Guid == VSConstants.OutputWindowPaneGuid.GeneralPane_guid)
{
_pane = await VS.GetRequiredServiceAsync<SVsGeneralOutputWindowPane, IVsOutputWindowPane>();
return;
}
IVsOutputWindow outputWindow = await VS.Services.GetOutputWindowAsync();
Guid paneGuid = Guid;
// Only create the pane if we were constructed with a non-empty `_paneName`.
if (!string.IsNullOrEmpty(_paneName))
{
const int visible = 1;
const int clearWithSolution = 1;
ErrorHandler.ThrowOnFailure(outputWindow.CreatePane(ref paneGuid, _paneName, visible, clearWithSolution));
}
ErrorHandler.ThrowOnFailure(outputWindow.GetPane(ref paneGuid, out _pane));
}
}
}
}