Skip to content

Commit

Permalink
Re-implement indicator when running registered editor commands
Browse files Browse the repository at this point in the history
  • Loading branch information
andyleejordan committed Sep 28, 2022
1 parent 6e44e35 commit 152df0e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ await _executionService.ExecutePSCommandAsync(
_debugStateService.WaitingForAttach = true;
Task nonAwaitedTask = _executionService
.ExecutePSCommandAsync(debugRunspaceCmd, CancellationToken.None, PowerShellExecutionOptions.ImmediateInteractive)
.ContinueWith( OnExecutionCompletedAsync, TaskScheduler.Default);
.ContinueWith(OnExecutionCompletedAsync, TaskScheduler.Default);

if (runspaceVersion.Version.Major >= 7)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,37 @@

namespace Microsoft.PowerShell.EditorServices.Services.Extension
{
/// Enumerates the possible execution results that can occur after
/// executing a command or script.
/// </summary>
internal enum ExecutionStatus
{
/// <summary>
/// Indicates that execution has not yet started.
/// </summary>
Pending,

/// <summary>
/// Indicates that the command is executing.
/// </summary>
Running,

/// <summary>
/// Indicates that execution has failed.
/// </summary>
Failed,

/// <summary>
/// Indicates that execution was aborted by the user.
/// </summary>
Aborted,

/// <summary>
/// Indicates that execution completed successfully.
/// </summary>
Completed
}

/// <summary>
/// Provides a high-level service which enables PowerShell scripts
/// and modules to extend the behavior of the host editor.
Expand Down Expand Up @@ -125,12 +156,14 @@ public Task InvokeCommandAsync(string commandName, EditorContext editorContext,
{
if (editorCommands.TryGetValue(commandName, out EditorCommand editorCommand))
{
_languageServer?.SendNotification("powerShell/executionStatusChanged", ExecutionStatus.Pending);
PSCommand executeCommand = new PSCommand()
.AddCommand("Invoke-Command")
.AddParameter("ScriptBlock", editorCommand.ScriptBlock)
.AddParameter("ArgumentList", new object[] { editorContext });

// This API is used for editor command execution so it requires the foreground.
_languageServer?.SendNotification("powerShell/executionStatusChanged", ExecutionStatus.Running);
return ExecutionService.ExecutePSCommandAsync(
executeCommand,
cancellationToken,
Expand All @@ -140,7 +173,24 @@ public Task InvokeCommandAsync(string commandName, EditorContext editorContext,
WriteOutputToHost = !editorCommand.SuppressOutput,
AddToHistory = !editorCommand.SuppressOutput,
ThrowOnError = false,
});
}).ContinueWith((Task executeTask) =>
{
ExecutionStatus status = ExecutionStatus.Failed;
if (executeTask.IsCompleted)
{
status = ExecutionStatus.Completed;
}
else if (executeTask.IsCanceled)
{
status = ExecutionStatus.Aborted;
}
else if (executeTask.IsFaulted)
{
status = ExecutionStatus.Failed;
}

_languageServer?.SendNotification("powerShell/executionStatusChanged", status);
}, TaskScheduler.Default);
}

throw new KeyNotFoundException($"Editor command not found: '{commandName}'");
Expand Down

0 comments on commit 152df0e

Please sign in to comment.