Skip to content

Re-implement indicator when running registered editor commands #1924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -123,6 +154,7 @@ internal Task InitializeAsync()
/// <exception cref="KeyNotFoundException">The command being invoked was not registered.</exception>
public Task InvokeCommandAsync(string commandName, EditorContext editorContext, CancellationToken cancellationToken)
{
_languageServer?.SendNotification("powerShell/executionStatusChanged", ExecutionStatus.Pending);
if (editorCommands.TryGetValue(commandName, out EditorCommand editorCommand))
{
PSCommand executeCommand = new PSCommand()
Expand All @@ -131,6 +163,7 @@ public Task InvokeCommandAsync(string commandName, EditorContext editorContext,
.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,9 +173,27 @@ 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);
}

_languageServer?.SendNotification("powerShell/executionStatusChanged", ExecutionStatus.Failed);
throw new KeyNotFoundException($"Editor command not found: '{commandName}'");
}

Expand Down