1414
1515namespace Microsoft . PowerShell . EditorServices . Services . Extension
1616{
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+
1748 /// <summary>
1849 /// Provides a high-level service which enables PowerShell scripts
1950 /// and modules to extend the behavior of the host editor.
@@ -123,6 +154,7 @@ internal Task InitializeAsync()
123154 /// <exception cref="KeyNotFoundException">The command being invoked was not registered.</exception>
124155 public Task InvokeCommandAsync ( string commandName , EditorContext editorContext , CancellationToken cancellationToken )
125156 {
157+ _languageServer ? . SendNotification ( "powerShell/executionStatusChanged" , ExecutionStatus . Pending ) ;
126158 if ( editorCommands . TryGetValue ( commandName , out EditorCommand editorCommand ) )
127159 {
128160 PSCommand executeCommand = new PSCommand ( )
@@ -131,6 +163,7 @@ public Task InvokeCommandAsync(string commandName, EditorContext editorContext,
131163 . AddParameter ( "ArgumentList" , new object [ ] { editorContext } ) ;
132164
133165 // This API is used for editor command execution so it requires the foreground.
166+ _languageServer ? . SendNotification ( "powerShell/executionStatusChanged" , ExecutionStatus . Running ) ;
134167 return ExecutionService . ExecutePSCommandAsync (
135168 executeCommand ,
136169 cancellationToken ,
@@ -140,9 +173,27 @@ public Task InvokeCommandAsync(string commandName, EditorContext editorContext,
140173 WriteOutputToHost = ! editorCommand . SuppressOutput ,
141174 AddToHistory = ! editorCommand . SuppressOutput ,
142175 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 ) ;
144194 }
145195
196+ _languageServer ? . SendNotification ( "powerShell/executionStatusChanged" , ExecutionStatus . Failed ) ;
146197 throw new KeyNotFoundException ( $ "Editor command not found: '{ commandName } '") ;
147198 }
148199
0 commit comments