3
3
using Microsoft . PowerShell . EditorServices . Services . PowerShell . Execution ;
4
4
using Microsoft . PowerShell . EditorServices . Services . PowerShell . Host ;
5
5
using System ;
6
+ using System . Collections . ObjectModel ;
7
+ using System . Linq ;
6
8
using System . Management . Automation ;
7
9
using System . Text ;
8
10
using System . Threading ;
@@ -16,15 +18,13 @@ public static PowerShellConsoleService CreateAndStart(
16
18
ILoggerFactory loggerFactory ,
17
19
PowerShellExecutionService executionService )
18
20
{
19
- var consoleService = new PowerShellConsoleService (
21
+ return new PowerShellConsoleService (
20
22
loggerFactory ,
21
23
executionService ,
22
24
executionService . EngineIntrinsics ,
23
25
executionService . EditorServicesHost ,
24
26
executionService . ReadLine ,
25
27
executionService . PSReadLineProxy ) ;
26
-
27
- return consoleService ;
28
28
}
29
29
30
30
private readonly ILogger _logger ;
@@ -45,6 +45,8 @@ public static PowerShellConsoleService CreateAndStart(
45
45
46
46
private CancellationTokenSource _currentCommandCancellationSource ;
47
47
48
+ private bool _canCancel ;
49
+
48
50
private PowerShellConsoleService (
49
51
ILoggerFactory loggerFactory ,
50
52
PowerShellExecutionService executionService ,
@@ -78,21 +80,36 @@ public void StartRepl()
78
80
79
81
public void CancelCurrentPrompt ( )
80
82
{
81
- _currentCommandCancellationSource ? . Cancel ( ) ;
83
+ if ( _canCancel )
84
+ {
85
+ _currentCommandCancellationSource ? . Cancel ( ) ;
86
+ }
87
+ }
88
+
89
+ public void Stop ( )
90
+ {
91
+ _replLoopCancellationSource . Cancel ( ) ;
82
92
}
83
93
84
94
private async Task RunReplLoopAsync ( )
85
95
{
86
96
while ( ! _replLoopCancellationSource . IsCancellationRequested )
87
97
{
88
98
_currentCommandCancellationSource = CancellationTokenSource . CreateLinkedTokenSource ( _replLoopCancellationSource . Token ) ;
89
-
99
+ _canCancel = true ;
90
100
try
91
101
{
92
- await InvokePromptFunctionAsync ( ) . ConfigureAwait ( false ) ;
102
+ string promptString = ( await GetPromptOutputAsync ( ) . ConfigureAwait ( false ) ) . FirstOrDefault ( ) ?? "PS> " ;
103
+
104
+ WritePrompt ( promptString ) ;
93
105
94
106
string userInput = await InvokeReadLineAsync ( ) . ConfigureAwait ( false ) ;
95
107
108
+ if ( _currentCommandCancellationSource . IsCancellationRequested )
109
+ {
110
+ continue ;
111
+ }
112
+
96
113
await InvokeInputAsync ( userInput ) . ConfigureAwait ( false ) ;
97
114
}
98
115
catch ( OperationCanceledException )
@@ -103,25 +120,28 @@ private async Task RunReplLoopAsync()
103
120
{
104
121
105
122
}
123
+ finally
124
+ {
125
+ _canCancel = false ;
126
+ _currentCommandCancellationSource . Dispose ( ) ;
127
+ _currentCommandCancellationSource = null ;
128
+ }
106
129
}
107
130
}
108
131
109
- private Task InvokePromptFunctionAsync ( )
132
+ private Task < Collection < string > > GetPromptOutputAsync ( )
110
133
{
111
- var promptCommand = new PSCommand ( )
112
- . AddCommand ( "prompt" )
113
- . AddCommand ( "Write-Host" )
114
- . AddParameter ( "NoNewline" ) ;
115
-
116
- var executionOptions = new PowerShellExecutionOptions
117
- {
118
- WriteOutputToHost = true ,
119
- } ;
134
+ var promptCommand = new PSCommand ( ) . AddCommand ( "prompt" ) ;
120
135
121
- return _executionService . ExecutePSCommandAsync (
136
+ return _executionService . ExecutePSCommandAsync < string > (
122
137
promptCommand ,
123
- executionOptions ,
124
- _currentCommandCancellationSource . Token ) ;
138
+ new PowerShellExecutionOptions ( ) ,
139
+ CancellationToken . None ) ;
140
+ }
141
+
142
+ private void WritePrompt ( string promptString )
143
+ {
144
+ _editorServicesHost . UI . Write ( promptString ) ;
125
145
}
126
146
127
147
private Task < string > InvokeReadLineAsync ( )
0 commit comments