forked from dimven/NavisPythonShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPythonConsole.cs
749 lines (679 loc) · 27 KB
/
PythonConsole.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
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
// Copyright (c) 2010 Joe Moorhouse
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using Microsoft.Scripting.Hosting.Shell;
using System.Windows.Input;
using Microsoft.Scripting;
using IronPython.Hosting;
using IronPython.Runtime;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting.Hosting.Providers;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Threading;
using System.Windows.Documents;
using ICSharpCode.AvalonEdit.Editing;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Utils;
using Style = Microsoft.Scripting.Hosting.Shell.Style;
using System.Runtime.Remoting;
namespace PythonConsoleControl
{
public delegate void ConsoleInitializedEventHandler(object sender, EventArgs e);
/// <summary>
/// Custom IronPython console. The command dispacher runs on a separate UI thread from the REPL
/// and also from the WPF control.
/// </summary>
public class PythonConsole : IConsole, IDisposable
{
bool allowFullAutocompletion = true;
public bool AllowFullAutocompletion
{
get { return allowFullAutocompletion; }
set { allowFullAutocompletion = value; }
}
bool disableAutocompletionForCallables = true;
public bool DisableAutocompletionForCallables
{
get { return disableAutocompletionForCallables; }
set
{
if (textEditor.CompletionProvider != null) textEditor.CompletionProvider.ExcludeCallables = value;
disableAutocompletionForCallables = value;
}
}
bool allowCtrlSpaceAutocompletion = false;
public bool AllowCtrlSpaceAutocompletion
{
get { return allowCtrlSpaceAutocompletion; }
set { allowCtrlSpaceAutocompletion = value; }
}
PythonTextEditor textEditor;
int lineReceivedEventIndex = 0; // The index into the waitHandles array where the lineReceivedEvent is stored.
ManualResetEvent lineReceivedEvent = new ManualResetEvent(false);
ManualResetEvent disposedEvent = new ManualResetEvent(false);
AutoResetEvent statementsExecutionRequestedEvent = new AutoResetEvent(false);
WaitHandle[] waitHandles;
int promptLength = 4;
List<string> previousLines = new List<string>();
CommandLine commandLine;
CommandLineHistory commandLineHistory = new CommandLineHistory();
volatile bool executing = false;
// This is the thread upon which all commands execute unless the dipatcher is overridden.
Thread dispatcherThread;
public Dispatcher dispatcher;
string scriptText = String.Empty;
bool consoleInitialized = false;
string prompt;
public event ConsoleInitializedEventHandler ConsoleInitialized;
public ScriptScope ScriptScope
{
get { return commandLine.ScriptScope; }
}
public PythonConsole(PythonTextEditor textEditor, CommandLine commandLine)
{
waitHandles = new WaitHandle[] { lineReceivedEvent, disposedEvent };
this.commandLine = commandLine;
this.textEditor = textEditor;
textEditor.CompletionProvider = new PythonConsoleCompletionDataProvider(commandLine) { ExcludeCallables = disableAutocompletionForCallables };
textEditor.PreviewKeyDown += textEditor_PreviewKeyDown;
textEditor.TextEntering += textEditor_TextEntering;
dispatcherThread = new Thread(new ThreadStart(DispatcherThreadStartingPoint));
dispatcherThread.SetApartmentState(ApartmentState.STA);
dispatcherThread.IsBackground = true;
dispatcherThread.Start();
// Only required when running outside REP loop.
prompt = ">>> ";
// Set commands:
this.textEditor.textArea.Dispatcher.Invoke(new Action(delegate()
{
CommandBinding pasteBinding = null;
CommandBinding copyBinding = null;
CommandBinding cutBinding = null;
CommandBinding undoBinding = null;
CommandBinding deleteBinding = null;
foreach (CommandBinding commandBinding in (this.textEditor.textArea.CommandBindings))
{
if (commandBinding.Command == ApplicationCommands.Paste) pasteBinding = commandBinding;
if (commandBinding.Command == ApplicationCommands.Copy) copyBinding = commandBinding;
if (commandBinding.Command == ApplicationCommands.Cut) cutBinding = commandBinding;
if (commandBinding.Command == ApplicationCommands.Undo) undoBinding = commandBinding;
if (commandBinding.Command == ApplicationCommands.Delete) deleteBinding = commandBinding;
}
// Remove current bindings completely from control. These are static so modifying them will cause other
// controls' behaviour to change too.
if (pasteBinding != null) this.textEditor.textArea.CommandBindings.Remove(pasteBinding);
if (copyBinding != null) this.textEditor.textArea.CommandBindings.Remove(copyBinding);
if (cutBinding != null) this.textEditor.textArea.CommandBindings.Remove(cutBinding);
if (undoBinding != null) this.textEditor.textArea.CommandBindings.Remove(undoBinding);
if (deleteBinding != null) this.textEditor.textArea.CommandBindings.Remove(deleteBinding);
this.textEditor.textArea.CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste, OnPaste, CanPaste));
this.textEditor.textArea.CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, OnCopy, PythonEditingCommandHandler.CanCutOrCopy));
this.textEditor.textArea.CommandBindings.Add(new CommandBinding(ApplicationCommands.Cut, PythonEditingCommandHandler.OnCut, CanCut));
this.textEditor.textArea.CommandBindings.Add(new CommandBinding(ApplicationCommands.Undo, OnUndo, CanUndo));
this.textEditor.textArea.CommandBindings.Add(new CommandBinding(ApplicationCommands.Delete, PythonEditingCommandHandler.OnDelete(ApplicationCommands.NotACommand), CanDeleteCommand));
}));
// Set dispatcher to run on a UI thread independent of both the Control UI thread and thread running the REPL.
WhenConsoleInitialized(delegate
{
SetCommandDispatcher(DispatchCommand);
});
}
public Action<Action> GetCommandDispatcher()
{
var languageContext = Microsoft.Scripting.Hosting.Providers.HostingHelpers.GetLanguageContext(commandLine.ScriptScope.Engine);
var pythonContext = (IronPython.Runtime.PythonContext)languageContext;
var result = pythonContext.GetSetCommandDispatcher(null);
pythonContext.GetSetCommandDispatcher(result);
return result;
}
public void SetCommandDispatcher(Action<Action> newDispatcher)
{
var languageContext = Microsoft.Scripting.Hosting.Providers.HostingHelpers.GetLanguageContext(commandLine.ScriptScope.Engine);
var pythonContext = (IronPython.Runtime.PythonContext)languageContext;
pythonContext.GetSetCommandDispatcher(newDispatcher);
}
protected void DispatchCommand(Delegate command)
{
if (command != null)
{
// Slightly involved form to enable keyboard interrupt to work.
executing = true;
var operation = dispatcher.BeginInvoke(DispatcherPriority.Normal, command);
while (executing)
{
if (operation.Status != DispatcherOperationStatus.Completed)
operation.Wait(TimeSpan.FromSeconds(1));
if (operation.Status == DispatcherOperationStatus.Completed)
executing = false;
}
}
}
/// <summary>
/// Perform action only after the console was initialized.
/// </summary>
public void WhenConsoleInitialized(Action action)
{
if (consoleInitialized)
{
action();
}
else
{
ConsoleInitialized += (sender, args) => action();
}
}
private void DispatcherThreadStartingPoint()
{
dispatcher = new Window().Dispatcher;
while (true)
{
try
{
System.Windows.Threading.Dispatcher.Run();
}
catch (ThreadAbortException tae)
{
if (tae.ExceptionState is Microsoft.Scripting.KeyboardInterruptException)
{
Thread.ResetAbort();
executing = false;
}
}
}
}
public void SetDispatcher(Dispatcher dispatcher)
{
this.dispatcher = dispatcher;
}
public void Dispose()
{
disposedEvent.Set();
textEditor.PreviewKeyDown -= textEditor_PreviewKeyDown;
textEditor.TextEntering -= textEditor_TextEntering;
}
public TextWriter Output
{
get { return null; }
set { }
}
public TextWriter ErrorOutput
{
get { return null; }
set { }
}
#region CommandHandling
protected void CanPaste(object target, CanExecuteRoutedEventArgs args)
{
if (IsInReadOnlyRegion)
{
args.CanExecute = false;
}
else
args.CanExecute = true;
}
protected void CanCut(object target, CanExecuteRoutedEventArgs args)
{
if (!CanDelete)
{
args.CanExecute = false;
}
else
PythonEditingCommandHandler.CanCutOrCopy(target, args);
}
protected void CanDeleteCommand(object target, CanExecuteRoutedEventArgs args)
{
if (!CanDelete)
{
args.CanExecute = false;
}
else
PythonEditingCommandHandler.CanDelete(target, args);
}
protected void CanUndo(object target, CanExecuteRoutedEventArgs args)
{
args.CanExecute = false;
}
protected void OnPaste(object target, ExecutedRoutedEventArgs args)
{
if (target != textEditor.textArea) return;
TextArea textArea = textEditor.textArea;
if (textArea != null && textArea.Document != null)
{
Debug.WriteLine(Clipboard.GetText(TextDataFormat.Html));
// convert text back to correct newlines for this document
string newLine = TextUtilities.GetNewLineFromDocument(textArea.Document, textArea.Caret.Line);
string text = TextUtilities.NormalizeNewLines(Clipboard.GetText(), newLine);
string[] commands = text.Split(new String[] { newLine }, StringSplitOptions.None);
string scriptText = "";
if (commands.Length > 1)
{
text = newLine;
foreach (string command in commands)
{
text += "... " + command + newLine;
scriptText += command.Replace("\t", " ") + newLine;
}
}
if (!string.IsNullOrEmpty(text))
{
bool fullLine = textArea.Options.CutCopyWholeLine && Clipboard.ContainsData(LineSelectedType);
bool rectangular = Clipboard.ContainsData(RectangleSelection.RectangularSelectionDataType);
if (fullLine)
{
DocumentLine currentLine = textArea.Document.GetLineByNumber(textArea.Caret.Line);
if (textArea.ReadOnlySectionProvider.CanInsert(currentLine.Offset))
{
textArea.Document.Insert(currentLine.Offset, text);
}
}
else if (rectangular && textArea.Selection.IsEmpty)
{
if (!RectangleSelection.PerformRectangularPaste(textArea, textArea.Caret.Position, text, false))
textEditor.Write(text, false);
}
else
{
textEditor.Write(text, false);
}
}
textArea.Caret.BringCaretToView();
args.Handled = true;
if (commands.Length > 1)
{
lock (this.scriptText)
{
this.scriptText = scriptText;
}
dispatcher.BeginInvoke(new Action(delegate() { ExecuteStatements(); }));
}
}
}
protected void OnCopy(object target, ExecutedRoutedEventArgs args)
{
if (target != textEditor.textArea) return;
if (textEditor.SelectionLength == 0 && executing)
{
// Send the 'Ctrl-C' abort
//if (!IsInReadOnlyRegion)
//{
MoveToHomePosition();
//textEditor.Column = GetLastTextEditorLine().Length + 1;
//textEditor.Write(Environment.NewLine);
//}
dispatcherThread.Abort(new Microsoft.Scripting.KeyboardInterruptException(""));
args.Handled = true;
}
else PythonEditingCommandHandler.OnCopy(target, args);
}
const string LineSelectedType = "MSDEVLineSelect"; // This is the type VS 2003 and 2005 use for flagging a whole line copy
protected void OnUndo(object target, ExecutedRoutedEventArgs args)
{
}
#endregion
/// <summary>
/// Run externally provided statements in the Console Engine.
/// </summary>
/// <param name="statements"></param>
public void RunStatements(string statements)
{
MoveToHomePosition();
lock (this.scriptText)
{
this.scriptText = statements;
}
dispatcher.BeginInvoke(new Action(delegate() { ExecuteStatements(); }));
}
/// <summary>
/// Run on the statement execution thread.
/// </summary>
void ExecuteStatements()
{
lock (scriptText)
{
textEditor.Write("\r\n");
ScriptSource scriptSource = commandLine.ScriptScope.Engine.CreateScriptSourceFromString(scriptText, SourceCodeKind.Statements);
string error = "";
try
{
executing = true;
var errors = new ErrorReporter();
var command = scriptSource.Compile(errors);
if (command == null)
{
// compilation failed
error = "Syntax Error: " + string.Join("\nSyntax Error: ", errors.Errors) + "\n";
}
else
{
ObjectHandle wrapexception = null;
GetCommandDispatcher()(() => scriptSource.ExecuteAndWrap(commandLine.ScriptScope, out wrapexception));
if (wrapexception != null)
{
error = "Exception : " + wrapexception.Unwrap().ToString() + "\n";
}
}
}
catch (ThreadAbortException tae)
{
if (tae.ExceptionState is Microsoft.Scripting.KeyboardInterruptException) Thread.ResetAbort();
error = "KeyboardInterrupt" + System.Environment.NewLine;
}
catch (Microsoft.Scripting.SyntaxErrorException exception)
{
ExceptionOperations eo;
eo = commandLine.ScriptScope.Engine.GetService<ExceptionOperations>();
error = eo.FormatException(exception);
}
catch (Exception exception)
{
ExceptionOperations eo;
eo = commandLine.ScriptScope.Engine.GetService<ExceptionOperations>();
error = eo.FormatException(exception) + System.Environment.NewLine;
}
executing = false;
if (error != "") textEditor.Write(error);
textEditor.Write(prompt);
}
}
/// <summary>
/// Returns the next line typed in by the console user. If no line is available this method
/// will block.
/// </summary>
public string ReadLine(int autoIndentSize)
{
string indent = String.Empty;
if (autoIndentSize > 0)
{
indent = String.Empty.PadLeft(autoIndentSize);
Write(indent, Style.Prompt);
}
string line = ReadLineFromTextEditor();
if (line != null)
{
return indent + line;
}
return null;
}
/// <summary>
/// Writes text to the console.
/// </summary>
public void Write(string text, Style style)
{
textEditor.Write(text);
if (style == Style.Prompt)
{
promptLength = text.Length;
if (!consoleInitialized)
{
consoleInitialized = true;
if (ConsoleInitialized != null) ConsoleInitialized(this, EventArgs.Empty);
}
}
}
/// <summary>
/// Writes text followed by a newline to the console.
/// </summary>
public void WriteLine(string text, Style style)
{
Write(text + Environment.NewLine, style);
}
/// <summary>
/// Writes an empty line to the console.
/// </summary>
public void WriteLine()
{
Write(Environment.NewLine, Style.Out);
}
/// <summary>
/// Indicates whether there is a line already read by the console and waiting to be processed.
/// </summary>
public bool IsLineAvailable
{
get
{
lock (previousLines)
{
return previousLines.Count > 0;
}
}
}
/// <summary>
/// Gets the text that is yet to be processed from the console. This is the text that is being
/// typed in by the user who has not yet pressed the enter key.
/// </summary>
public string GetCurrentLine()
{
string fullLine = GetLastTextEditorLine();
return fullLine.Substring(promptLength);
}
/// <summary>
/// Gets the lines that have not been returned by the ReadLine method. This does not
/// include the current line.
/// </summary>
public string[] GetUnreadLines()
{
return previousLines.ToArray();
}
string GetLastTextEditorLine()
{
return textEditor.GetLine(textEditor.TotalLines - 1);
}
string ReadLineFromTextEditor()
{
int result = WaitHandle.WaitAny(waitHandles);
if (result == lineReceivedEventIndex)
{
lock (previousLines)
{
string line = previousLines[0];
previousLines.RemoveAt(0);
if (previousLines.Count == 0)
{
lineReceivedEvent.Reset();
}
return line;
}
}
return null;
}
/// <summary>
/// Processes characters entered into the text editor by the user.
/// </summary>
void textEditor_PreviewKeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Delete:
if (!CanDelete) e.Handled = true;
return;
case Key.Tab:
if (IsInReadOnlyRegion) e.Handled = true;
return;
case Key.Back:
if (!CanBackspace) e.Handled = true;
return;
case Key.Home:
MoveToHomePosition();
e.Handled = true;
return;
case Key.Down:
if (!IsInReadOnlyRegion) MoveToNextCommandLine();
e.Handled = true;
return;
case Key.Up:
if (!IsInReadOnlyRegion) MoveToPreviousCommandLine();
e.Handled = true;
return;
}
}
/// <summary>
/// Processes characters entering into the text editor by the user.
/// </summary>
void textEditor_TextEntering(object sender, TextCompositionEventArgs e)
{
if (e.Text.Length > 0)
{
if (!char.IsLetterOrDigit(e.Text[0]))
{
// Whenever a non-letter is typed while the completion window is open,
// insert the currently selected element.
textEditor.RequestCompletioninsertion(e);
}
}
if (IsInReadOnlyRegion)
{
e.Handled = true;
}
else
{
if (e.Text[0] == '\n')
{
OnEnterKeyPressed();
}
if (e.Text[0] == '.' && allowFullAutocompletion)
{
textEditor.ShowCompletionWindow();
}
if ((e.Text[0] == ' ') && (Keyboard.Modifiers == ModifierKeys.Control))
{
e.Handled = true;
if (allowCtrlSpaceAutocompletion) textEditor.ShowCompletionWindow();
}
}
}
/// <summary>
/// Move cursor to the end of the line before retrieving the line.
/// </summary>
void OnEnterKeyPressed()
{
textEditor.StopCompletion();
if (textEditor.WriteInProgress) return;
lock (previousLines)
{
// Move cursor to the end of the line.
textEditor.Column = GetLastTextEditorLine().Length + 1;
// Append line.
string currentLine = GetCurrentLine();
previousLines.Add(currentLine);
commandLineHistory.Add(currentLine);
lineReceivedEvent.Set();
}
}
/// <summary>
/// Returns true if the cursor is in a readonly text editor region.
/// </summary>
bool IsInReadOnlyRegion
{
get { return IsCurrentLineReadOnly || IsInPrompt; }
}
/// <summary>
/// Only the last line in the text editor is not read only.
/// </summary>
bool IsCurrentLineReadOnly
{
get { return textEditor.Line < textEditor.TotalLines; }
}
/// <summary>
/// Determines whether the current cursor position is in a prompt.
/// </summary>
bool IsInPrompt
{
get { return textEditor.Column - promptLength - 1 < 0; }
}
/// <summary>
/// Returns true if the user can delete at the current cursor position.
/// </summary>
bool CanDelete
{
get
{
if (textEditor.SelectionLength > 0) return SelectionIsDeletable;
else return !IsInReadOnlyRegion;
}
}
/// <summary>
/// Returns true if the user can backspace at the current cursor position.
/// </summary>
bool CanBackspace
{
get
{
if (textEditor.SelectionLength > 0) return SelectionIsDeletable;
else
{
int cursorIndex = textEditor.Column - promptLength - 1;
return !IsCurrentLineReadOnly && (cursorIndex > 0 || (cursorIndex == 0 && textEditor.SelectionLength > 0));
}
}
}
bool SelectionIsDeletable
{
get
{
return (!textEditor.SelectionIsMultiline
&& !IsCurrentLineReadOnly
&& (textEditor.SelectionStartColumn - promptLength - 1 >= 0)
&& (textEditor.SelectionEndColumn - promptLength - 1 >= 0));
}
}
/// <summary>
/// The home position is at the start of the line after the prompt.
/// </summary>
void MoveToHomePosition()
{
textEditor.Line = textEditor.TotalLines;
textEditor.Column = promptLength + 1;
}
/// <summary>
/// Shows the previous command line in the command line history.
/// </summary>
void MoveToPreviousCommandLine()
{
if (commandLineHistory.MovePrevious())
{
ReplaceCurrentLineTextAfterPrompt(commandLineHistory.Current);
}
}
/// <summary>
/// Shows the next command line in the command line history.
/// </summary>
void MoveToNextCommandLine()
{
textEditor.Line = textEditor.TotalLines;
if (commandLineHistory.MoveNext())
{
ReplaceCurrentLineTextAfterPrompt(commandLineHistory.Current);
}
}
/// <summary>
/// Replaces the current line text after the prompt with the specified text.
/// </summary>
void ReplaceCurrentLineTextAfterPrompt(string text)
{
string currentLine = GetCurrentLine();
textEditor.Replace(promptLength, currentLine.Length, text);
// Put cursor at end.
textEditor.Column = promptLength + text.Length + 1;
}
}
public class ErrorReporter : ErrorListener
{
public List<String> Errors = new List<string>();
public override void ErrorReported(ScriptSource source, string message, SourceSpan span, int errorCode, Severity severity)
{
Errors.Add(string.Format("{0} (line {1})", message, span.Start.Line));
}
public int Count
{
get { return Errors.Count; }
}
}
}