Skip to content

Commit 2ec7d59

Browse files
committed
WIP: Generalized busy indicator
1 parent bbdf98d commit 2ec7d59

File tree

3 files changed

+45
-73
lines changed

3 files changed

+45
-73
lines changed

src/PowerShellEditorServices/Services/Extension/ExtensionService.cs

+1-52
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,6 @@
1414

1515
namespace 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-
4817
/// <summary>
4918
/// Provides a high-level service which enables PowerShell scripts
5019
/// and modules to extend the behavior of the host editor.
@@ -154,7 +123,6 @@ internal Task InitializeAsync()
154123
/// <exception cref="KeyNotFoundException">The command being invoked was not registered.</exception>
155124
public Task InvokeCommandAsync(string commandName, EditorContext editorContext, CancellationToken cancellationToken)
156125
{
157-
_languageServer?.SendNotification("powerShell/executionStatusChanged", ExecutionStatus.Pending);
158126
if (editorCommands.TryGetValue(commandName, out EditorCommand editorCommand))
159127
{
160128
PSCommand executeCommand = new PSCommand()
@@ -163,7 +131,6 @@ public Task InvokeCommandAsync(string commandName, EditorContext editorContext,
163131
.AddParameter("ArgumentList", new object[] { editorContext });
164132

165133
// This API is used for editor command execution so it requires the foreground.
166-
_languageServer?.SendNotification("powerShell/executionStatusChanged", ExecutionStatus.Running);
167134
return ExecutionService.ExecutePSCommandAsync(
168135
executeCommand,
169136
cancellationToken,
@@ -173,27 +140,9 @@ public Task InvokeCommandAsync(string commandName, EditorContext editorContext,
173140
WriteOutputToHost = !editorCommand.SuppressOutput,
174141
AddToHistory = !editorCommand.SuppressOutput,
175142
ThrowOnError = false,
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);
143+
});
194144
}
195145

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

src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616

1717
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution
1818
{
19-
internal class SynchronousPowerShellTask<TResult> : SynchronousTask<IReadOnlyList<TResult>>
19+
internal interface ISynchronousPowerShellTask
20+
{
21+
PowerShellExecutionOptions PowerShellExecutionOptions { get; }
22+
}
23+
24+
internal class SynchronousPowerShellTask<TResult> : SynchronousTask<IReadOnlyList<TResult>>, ISynchronousPowerShellTask
2025
{
2126
private static readonly PowerShellExecutionOptions s_defaultPowerShellExecutionOptions = new();
2227

src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs

+38-20
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,23 @@ private bool CancelForegroundAndPrepend(ISynchronousTask task, bool isIdle = fal
334334
return true;
335335
}
336336

337+
private void ExecuteTaskSynchronously(ISynchronousTask task, CancellationToken cancellationToken)
338+
{
339+
if (task is ISynchronousPowerShellTask t
340+
&& (t.PowerShellExecutionOptions.AddToHistory
341+
|| t.PowerShellExecutionOptions.FromRepl))
342+
{
343+
_languageServer?.SendNotification("powerShell/executionBusyStatus", true);
344+
}
345+
try
346+
{
347+
task.ExecuteSynchronously(cancellationToken);
348+
}
349+
finally
350+
{
351+
_languageServer?.SendNotification("powerShell/executionBusyStatus", false);
352+
}
353+
}
337354
public Task<T> InvokeTaskOnPipelineThreadAsync<T>(SynchronousTask<T> task)
338355
{
339356
if (CancelForegroundAndPrepend(task))
@@ -767,14 +784,7 @@ private void RunExecutionLoop(bool isForDebug = false)
767784
&& !cancellationScope.CancellationToken.IsCancellationRequested
768785
&& _taskQueue.TryTake(out ISynchronousTask task))
769786
{
770-
try
771-
{
772-
task.ExecuteSynchronously(cancellationScope.CancellationToken);
773-
}
774-
catch (OperationCanceledException e)
775-
{
776-
_logger.LogDebug(e, "Task {Task} was canceled!", task);
777-
}
787+
ExecuteTaskSynchronously(task, cancellationScope.CancellationToken);
778788
}
779789

780790
if (_shouldExit
@@ -935,19 +945,27 @@ private string InvokeReadLine(CancellationToken cancellationToken)
935945
}
936946
}
937947

948+
// TODO: Should we actually be directly invoking input versus queueing it as a task like everything else?
938949
private void InvokeInput(string input, CancellationToken cancellationToken)
939950
{
940-
PSCommand command = new PSCommand().AddScript(input, useLocalScope: false);
941-
InvokePSCommand(
942-
command,
943-
new PowerShellExecutionOptions
944-
{
945-
AddToHistory = true,
946-
ThrowOnError = false,
947-
WriteOutputToHost = true,
948-
FromRepl = true,
949-
},
950-
cancellationToken);
951+
_languageServer?.SendNotification("powerShell/executionBusyStatus", true);
952+
try
953+
{
954+
InvokePSCommand(
955+
new PSCommand().AddScript(input, useLocalScope: false),
956+
new PowerShellExecutionOptions
957+
{
958+
AddToHistory = true,
959+
ThrowOnError = false,
960+
WriteOutputToHost = true,
961+
FromRepl = true,
962+
},
963+
cancellationToken);
964+
}
965+
finally
966+
{
967+
_languageServer?.SendNotification("powerShell/executionBusyStatus", false);
968+
}
951969
}
952970

953971
private void AddRunspaceEventHandlers(Runspace runspace)
@@ -1085,7 +1103,7 @@ private void OnPowerShellIdle(CancellationToken idleCancellationToken)
10851103
// TODO: This may not be a PowerShell task, so ideally we can differentiate that here.
10861104
// For now it's mostly true and an easy assumption to make.
10871105
runPipelineForEventProcessing = false;
1088-
task.ExecuteSynchronously(cancellationScope.CancellationToken);
1106+
ExecuteTaskSynchronously(task, cancellationScope.CancellationToken);
10891107
}
10901108
}
10911109

0 commit comments

Comments
 (0)