14
14
15
15
namespace Microsoft . PowerShell . EditorServices . Services . Extension
16
16
{
17
+ /// Enumerates the possible execution results that can occur after
18
+ /// executing a command or script.
19
+ /// </summary>
20
+ internal enum ExecutionStatus
21
+ {
22
+ /// <summary>
23
+ /// Indicates that execution has not yet started.
24
+ /// </summary>
25
+ Pending ,
26
+
27
+ /// <summary>
28
+ /// Indicates that the command is executing.
29
+ /// </summary>
30
+ Running ,
31
+
32
+ /// <summary>
33
+ /// Indicates that execution has failed.
34
+ /// </summary>
35
+ Failed ,
36
+
37
+ /// <summary>
38
+ /// Indicates that execution was aborted by the user.
39
+ /// </summary>
40
+ Aborted ,
41
+
42
+ /// <summary>
43
+ /// Indicates that execution completed successfully.
44
+ /// </summary>
45
+ Completed
46
+ }
47
+
17
48
/// <summary>
18
49
/// Provides a high-level service which enables PowerShell scripts
19
50
/// and modules to extend the behavior of the host editor.
@@ -123,6 +154,7 @@ internal Task InitializeAsync()
123
154
/// <exception cref="KeyNotFoundException">The command being invoked was not registered.</exception>
124
155
public Task InvokeCommandAsync ( string commandName , EditorContext editorContext , CancellationToken cancellationToken )
125
156
{
157
+ _languageServer ? . SendNotification ( "powerShell/executionStatusChanged" , ExecutionStatus . Pending ) ;
126
158
if ( editorCommands . TryGetValue ( commandName , out EditorCommand editorCommand ) )
127
159
{
128
160
PSCommand executeCommand = new PSCommand ( )
@@ -131,6 +163,7 @@ public Task InvokeCommandAsync(string commandName, EditorContext editorContext,
131
163
. AddParameter ( "ArgumentList" , new object [ ] { editorContext } ) ;
132
164
133
165
// This API is used for editor command execution so it requires the foreground.
166
+ _languageServer ? . SendNotification ( "powerShell/executionStatusChanged" , ExecutionStatus . Running ) ;
134
167
return ExecutionService . ExecutePSCommandAsync (
135
168
executeCommand ,
136
169
cancellationToken ,
@@ -140,9 +173,27 @@ public Task InvokeCommandAsync(string commandName, EditorContext editorContext,
140
173
WriteOutputToHost = ! editorCommand . SuppressOutput ,
141
174
AddToHistory = ! editorCommand . SuppressOutput ,
142
175
ThrowOnError = false ,
143
- } ) ;
176
+ } ) . ContinueWith ( ( Task executeTask ) =>
177
+ {
178
+ ExecutionStatus status = ExecutionStatus . Failed ;
179
+ if ( executeTask . IsCompleted )
180
+ {
181
+ status = ExecutionStatus . Completed ;
182
+ }
183
+ else if ( executeTask . IsCanceled )
184
+ {
185
+ status = ExecutionStatus . Aborted ;
186
+ }
187
+ else if ( executeTask . IsFaulted )
188
+ {
189
+ status = ExecutionStatus . Failed ;
190
+ }
191
+
192
+ _languageServer ? . SendNotification ( "powerShell/executionStatusChanged" , status ) ;
193
+ } , TaskScheduler . Default ) ;
144
194
}
145
195
196
+ _languageServer ? . SendNotification ( "powerShell/executionStatusChanged" , ExecutionStatus . Failed ) ;
146
197
throw new KeyNotFoundException ( $ "Editor command not found: '{ commandName } '") ;
147
198
}
148
199
0 commit comments