Repository navigation
Expand file tree
/
Copy pathInteractiveWindow.cs
More file actions
597 lines (482 loc) · 19.2 KB
/
Copy pathInteractiveWindow.cs
File metadata and controls
597 lines (482 loc) · 19.2 KB
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Dumps commands in QueryStatus and Exec.
// #define DUMP_COMMANDS
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Language.Intellisense.Utilities;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Formatting;
using Microsoft.VisualStudio.Text.Operations;
using Microsoft.VisualStudio.Text.Projection;
using Microsoft.VisualStudio.Utilities;
using Roslyn.Utilities;
namespace Microsoft.VisualStudio.InteractiveWindow
{
// TODO: We should condense committed language buffers into a single language buffer and save the
// classifications from the previous language buffer if the perf of having individual buffers
// starts having problems w/ a large number of inputs.
/// <summary>
/// Provides implementation of a Repl Window built on top of the VS editor using projection buffers.
/// </summary>
internal partial class InteractiveWindow : IInteractiveWindow, IInteractiveWindowOperations2
{
internal const string ClipboardFormat = "89344A36-9821-495A-8255-99A63969F87D";
// The following two field definitions have to stay in sync with VS editor implementation
/// <summary>
/// A data format used to tag the contents of the clipboard so that it's clear
/// the data has been put in the clipboard by our editor
/// </summary>
internal const string ClipboardLineBasedCutCopyTag = "VisualStudioEditorOperationsLineCutCopyClipboardTag";
/// <summary>
/// A data format used to tag the contents of the clipboard as a box selection.
/// This is the same string that was used in VS9 and previous versions.
/// </summary>
internal const string BoxSelectionCutCopyTag = "MSDEVColumnSelect";
internal int LanguageBufferCounter = 0;
public event EventHandler<SubmissionBufferAddedEventArgs> SubmissionBufferAdded;
PropertyCollection IPropertyOwner.Properties { get; } = new PropertyCollection();
private readonly SemaphoreSlim _inputReaderSemaphore = new SemaphoreSlim(initialCount: 1, maxCount: 1);
/// <remarks>
/// WARNING: Members of this object should only be accessed from the UI thread.
/// </remarks>
private readonly UIThreadOnly _uiOnly;
// Setter for InteractiveWindowClipboard is a test hook.
internal InteractiveWindowClipboard InteractiveWindowClipboard { get; set; } = new SystemClipboard();
#region Initialization
public InteractiveWindow(
IInteractiveWindowEditorFactoryService host,
IContentTypeRegistryService contentTypeRegistry,
ITextBufferFactoryService bufferFactory,
IProjectionBufferFactoryService projectionBufferFactory,
IEditorOperationsFactoryService editorOperationsFactory,
ITextBufferUndoManagerProvider textBufferUndoManagerProvider,
ITextEditorFactoryService editorFactory,
IRtfBuilderService rtfBuilderService,
IIntellisenseSessionStackMapService intellisenseSessionStackMap,
ISmartIndentationService smartIndenterService,
IInteractiveEvaluator evaluator,
IWaitIndicator waitIndicator)
{
if (evaluator == null)
{
throw new ArgumentNullException(nameof(evaluator));
}
_uiOnly = new UIThreadOnly(
this,
host,
contentTypeRegistry,
bufferFactory,
projectionBufferFactory,
editorOperationsFactory,
textBufferUndoManagerProvider,
editorFactory,
rtfBuilderService,
intellisenseSessionStackMap,
smartIndenterService,
evaluator,
waitIndicator);
evaluator.CurrentWindow = this;
RequiresUIThread();
}
async Task<ExecutionResult> IInteractiveWindow.InitializeAsync()
{
try
{
RequiresUIThread();
var uiOnly = _uiOnly; // Verified above.
if (uiOnly.State != State.Starting)
{
throw new InvalidOperationException(InteractiveWindowResources.AlreadyInitialized);
}
uiOnly.State = State.Initializing;
// Anything that reads options should wait until after this call so the evaluator can set the options first
ExecutionResult result = await uiOnly.Evaluator.InitializeAsync().ConfigureAwait(continueOnCapturedContext: true);
Debug.Assert(OnUIThread()); // ConfigureAwait should bring us back to the UI thread.
if (result.IsSuccessful)
{
uiOnly.PrepareForInput();
}
return result;
}
catch (Exception e) when (ReportAndPropagateException(e))
{
throw ExceptionUtilities.Unreachable;
}
}
private bool ReportAndPropagateException(Exception e)
{
FatalError.ReportWithoutCrashUnlessCanceled(e); // Drop return value.
((IInteractiveWindow)this).WriteErrorLine(InteractiveWindowResources.InternalError);
return false; // Never consider the exception handled.
}
#endregion
void IInteractiveWindow.Close()
{
UIThread(uiOnly => uiOnly.Close());
}
#region Misc Helpers
/// <remarks>
/// The caller is responsible for using the buffer in a thread-safe manner.
/// </remarks>
public ITextBuffer CurrentLanguageBuffer => _uiOnly.CurrentLanguageBuffer;
void IDisposable.Dispose()
{
UIThread(uiOnly => ((IDisposable)uiOnly).Dispose());
}
public static InteractiveWindow FromBuffer(ITextBuffer buffer)
{
object result;
buffer.Properties.TryGetProperty(typeof(InteractiveWindow), out result);
return result as InteractiveWindow;
}
#endregion
#region IInteractiveWindow
public event Action ReadyForInput;
/// <remarks>
/// The caller is responsible for using the text view in a thread-safe manner.
/// </remarks>
IWpfTextView IInteractiveWindow.TextView => _uiOnly.TextView;
/// <remarks>
/// The caller is responsible for using the buffer in a thread-safe manner.
/// </remarks>
ITextBuffer IInteractiveWindow.OutputBuffer => _uiOnly.OutputBuffer;
/// <remarks>
/// The caller is responsible for using the writer in a thread-safe manner.
/// </remarks>
TextWriter IInteractiveWindow.OutputWriter => _uiOnly.OutputWriter;
/// <remarks>
/// The caller is responsible for using the writer in a thread-safe manner.
/// </remarks>
TextWriter IInteractiveWindow.ErrorOutputWriter => _uiOnly.ErrorOutputWriter;
/// <remarks>
/// The caller is responsible for using the evaluator in a thread-safe manner.
/// </remarks>
IInteractiveEvaluator IInteractiveWindow.Evaluator => _uiOnly.Evaluator;
/// <remarks>
/// Normally, an async method would have an NFW exception filter. This
/// one doesn't because it just calls other async methods that already
/// have filters.
/// </remarks>
async Task IInteractiveWindow.SubmitAsync(IEnumerable<string> inputs)
{
var completion = new TaskCompletionSource<object>();
var submissions = inputs.ToArray();
var numSubmissions = submissions.Length;
PendingSubmission[] pendingSubmissions = new PendingSubmission[numSubmissions];
if (numSubmissions == 0)
{
completion.SetResult(null);
}
else
{
for (int i = 0; i < numSubmissions; i++)
{
pendingSubmissions[i] = new PendingSubmission(submissions[i], i == numSubmissions - 1 ? completion : null);
}
}
UIThread(uiOnly => uiOnly.Submit(pendingSubmissions));
// This indicates that the last submission has completed.
await completion.Task.ConfigureAwait(false);
// These should all have finished already, but we'll await them so that their
// statuses are folded into the task we return.
await Task.WhenAll(pendingSubmissions.Select(p => p.Task)).ConfigureAwait(false);
}
void IInteractiveWindow.AddInput(string command)
{
UIThread(uiOnly => uiOnly.AddInput(command));
}
void IInteractiveWindow.FlushOutput()
{
UIThread(uiOnly => uiOnly.FlushOutput());
}
void IInteractiveWindow.InsertCode(string text)
{
UIThread(uiOnly => uiOnly.InsertCode(text));
}
#endregion
#region Commands
Task<ExecutionResult> IInteractiveWindowOperations.ResetAsync(bool initialize)
{
return UIThread(uiOnly => uiOnly.ResetAsync(initialize));
}
void IInteractiveWindowOperations.ClearHistory()
{
UIThread(uiOnly => uiOnly.ClearHistory());
}
void IInteractiveWindowOperations.ClearView()
{
UIThread(uiOnly => uiOnly.ClearView());
}
/// <summary>
/// Pastes from the clipboard into the text view
/// </summary>
bool IInteractiveWindowOperations.Paste()
{
return UIThread(uiOnly => uiOnly.Paste());
}
void IInteractiveWindowOperations.ExecuteInput()
{
UIThread(uiOnly => uiOnly.ExecuteInputAsync());
}
/// <remarks>
/// Test hook.
/// </remarks>
internal Task ExecuteInputAsync()
{
return UIThread(uiOnly => uiOnly.ExecuteInputAsync());
}
/// <summary>
/// Appends text to the output buffer and updates projection buffer to include it.
/// WARNING: this has to be the only method that writes to the output buffer so that
/// the output buffering counters are kept in sync.
/// </summary>
internal void AppendOutput(IEnumerable<string> output)
{
RequiresUIThread();
_uiOnly.AppendOutput(output); // Verified above.
}
/// <summary>
/// Clears the current input
/// </summary>
void IInteractiveWindowOperations.Cancel()
{
UIThread(uiOnly => uiOnly.Cancel());
}
void IInteractiveWindowOperations.HistoryPrevious(string search)
{
UIThread(uiOnly => uiOnly.HistoryPrevious(search));
}
void IInteractiveWindowOperations.HistoryNext(string search)
{
UIThread(uiOnly => uiOnly.HistoryNext(search));
}
void IInteractiveWindowOperations.HistorySearchNext()
{
UIThread(uiOnly => uiOnly.HistorySearchNext());
}
void IInteractiveWindowOperations.HistorySearchPrevious()
{
UIThread(uiOnly => uiOnly.HistorySearchPrevious());
}
/// <summary>
/// Moves to the beginning of the line.
/// </summary>
void IInteractiveWindowOperations.Home(bool extendSelection)
{
UIThread(uiOnly => uiOnly.Home(extendSelection));
}
/// <summary>
/// Moves to the end of the line.
/// </summary>
void IInteractiveWindowOperations.End(bool extendSelection)
{
UIThread(uiOnly => uiOnly.End(extendSelection));
}
void IInteractiveWindowOperations.SelectAll()
{
UIThread(uiOnly => uiOnly.SelectAll());
}
#endregion
#region Keyboard Commands
/// <remarks>Only consistent on the UI thread.</remarks>
bool IInteractiveWindow.IsRunning => _uiOnly.State != State.WaitingForInput;
/// <remarks>Only consistent on the UI thread.</remarks>
bool IInteractiveWindow.IsResetting => _uiOnly.State == State.Resetting || _uiOnly.State == State.ResettingAndReadingStandardInput;
/// <remarks>Only consistent on the UI thread.</remarks>
bool IInteractiveWindow.IsInitializing => _uiOnly.State == State.Starting || _uiOnly.State == State.Initializing;
IInteractiveWindowOperations IInteractiveWindow.Operations => this;
bool IInteractiveWindowOperations.Delete()
{
return UIThread(uiOnly => uiOnly.Delete());
}
void IInteractiveWindowOperations.Cut()
{
UIThread(uiOnly => uiOnly.Cut());
}
void IInteractiveWindowOperations2.Copy()
{
UIThread(uiOnly => uiOnly.Copy());
}
void IInteractiveWindowOperations2.CopyCode()
{
UIThread(uiOnly => uiOnly.CopyCode());
}
bool IInteractiveWindowOperations.Backspace()
{
return UIThread(uiOnly => uiOnly.Backspace());
}
bool IInteractiveWindowOperations.TrySubmitStandardInput()
{
return UIThread(uiOnly => uiOnly.TrySubmitStandardInput());
}
bool IInteractiveWindowOperations.BreakLine()
{
return UIThread(uiOnly => uiOnly.BreakLine());
}
bool IInteractiveWindowOperations.Return()
{
return UIThread(uiOnly => uiOnly.Return());
}
void IInteractiveWindowOperations2.DeleteLine()
{
UIThread(uiOnly => uiOnly.DeleteLine());
}
void IInteractiveWindowOperations2.CutLine()
{
UIThread(uiOnly => uiOnly.CutLine());
}
void IInteractiveWindowOperations2.TypeChar(char typedChar)
{
UIThread(uiOnly => uiOnly.TypeChar(typedChar));
}
#endregion
#region Command Debugging
#if DUMP_COMMANDS
private static void DumpCmd(string prefix, int result, ref Guid pguidCmdGroup, uint cmd, uint cmdf)
{
string cmdName;
if (pguidCmdGroup == VSConstants.GUID_VSStandardCommandSet97)
{
cmdName = ((VSConstants.VSStd97CmdID)cmd).ToString();
}
else if (pguidCmdGroup == VSConstants.VSStd2K)
{
cmdName = ((VSConstants.VSStd2KCmdID)cmd).ToString();
}
else if (pguidCmdGroup == VSConstants.VsStd2010)
{
cmdName = ((VSConstants.VSStd2010CmdID)cmd).ToString();
}
else if (pguidCmdGroup == GuidList.guidReplWindowCmdSet)
{
cmdName = ((ReplCommandId)cmd).ToString();
}
else
{
return;
}
Debug.WriteLine("{3}({0}) -> {1} {2}", cmdName, Enum.Format(typeof(OLECMDF), (OLECMDF)cmdf, "F"), result, prefix);
}
#endif
#endregion
#region Active Code and Standard Input
TextReader IInteractiveWindow.ReadStandardInput()
{
// shouldn't be called on the UI thread because we'll hang
RequiresNonUIThread();
return ReadStandardInputAsync().GetAwaiter().GetResult();
}
private async Task<TextReader> ReadStandardInputAsync()
{
try
{
// True because this is a public API and we want to use the same
// thread as the caller (esp for blocking).
await _inputReaderSemaphore.WaitAsync().ConfigureAwait(true); // Only one thread can read from standard input at a time.
try
{
return await UIThread(uiOnly => uiOnly.ReadStandardInputAsync()).ConfigureAwait(true);
}
finally
{
_inputReaderSemaphore.Release();
}
}
catch (Exception e) when (ReportAndPropagateException(e))
{
throw ExceptionUtilities.Unreachable;
}
}
#endregion
#region Output
Span IInteractiveWindow.Write(string text)
{
return UIThread(uiOnly => uiOnly.Write(text));
}
Span IInteractiveWindow.WriteLine(string text)
{
return UIThread(uiOnly => uiOnly.WriteLine(text));
}
Span IInteractiveWindow.WriteError(string text)
{
return UIThread(uiOnly => uiOnly.WriteError(text));
}
Span IInteractiveWindow.WriteErrorLine(string text)
{
return UIThread(uiOnly => uiOnly.WriteErrorLine(text));
}
void IInteractiveWindow.Write(UIElement element)
{
UIThread(uiOnly => uiOnly.Write(element));
}
#endregion
#region UI Dispatcher Helpers
private Dispatcher Dispatcher => ((FrameworkElement)_uiOnly.TextView).Dispatcher; // Always safe to access the dispatcher.
internal bool OnUIThread()
{
return Dispatcher.CheckAccess();
}
private T UIThread<T>(Func<UIThreadOnly, T> func)
{
if (!OnUIThread())
{
return (T)Dispatcher.Invoke(func, _uiOnly); // Safe because of dispatch.
}
return func(_uiOnly); // Safe because of check.
}
private void UIThread(Action<UIThreadOnly> action)
{
if (!OnUIThread())
{
Dispatcher.Invoke(action, _uiOnly); // Safe because of dispatch.
return;
}
action(_uiOnly); // Safe because of check.
}
private void RequiresUIThread()
{
if (!OnUIThread())
{
throw new InvalidOperationException(InteractiveWindowResources.RequireUIThread);
}
}
private void RequiresNonUIThread()
{
if (OnUIThread())
{
throw new InvalidOperationException(InteractiveWindowResources.RequireNonUIThread);
}
}
private static void DoEvents()
{
var frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(
DispatcherPriority.Background,
new Action<DispatcherFrame>(f => f.Continue = false),
frame);
Dispatcher.PushFrame(frame);
}
#endregion
#region Testing
internal event Action<State> StateChanged;
internal void Undo_TestOnly(int count)
{
UIThread(uiOnly => uiOnly.UndoHistory?.Undo(count));
}
internal void Redo_TestOnly(int count)
{
UIThread(uiOnly => uiOnly.UndoHistory?.Redo(count));
}
#endregion
}
}