Skip to content

Commit 5816987

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

File tree

3 files changed

+64
-73
lines changed

3 files changed

+64
-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

+9-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@
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+
void MaybeAddToHistory();
24+
}
25+
26+
internal class SynchronousPowerShellTask<TResult> : SynchronousTask<IReadOnlyList<TResult>>, ISynchronousPowerShellTask
2027
{
2128
private static readonly PowerShellExecutionOptions s_defaultPowerShellExecutionOptions = new();
2229

@@ -353,7 +360,7 @@ private void CancelNormalExecution()
353360
}
354361
}
355362

356-
internal void MaybeAddToHistory()
363+
public void MaybeAddToHistory()
357364
{
358365
// Do not add PSES internal commands to history. Also exclude input that came from the
359366
// REPL (e.g. PSReadLine) as it handles history itself in that scenario.

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

+54-19
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,9 @@ private bool CancelForegroundAndPrepend(ISynchronousTask task, bool isIdle = fal
313313

314314
_skipNextPrompt = true;
315315

316-
if (task is SynchronousPowerShellTask<PSObject> psTask)
316+
if (task is ISynchronousPowerShellTask t)
317317
{
318-
psTask.MaybeAddToHistory();
318+
t.MaybeAddToHistory();
319319
}
320320

321321
using (_taskQueue.BlockConsumers())
@@ -334,6 +334,26 @@ private bool CancelForegroundAndPrepend(ISynchronousTask task, bool isIdle = fal
334334
return true;
335335
}
336336

337+
// This handles executing the task while also notifying the client that the pipeline is
338+
// currently busy with a PowerShell task. The extension indicates this with a spinner.
339+
private void ExecuteTaskSynchronously(ISynchronousTask task, CancellationToken cancellationToken)
340+
{
341+
if (task is ISynchronousPowerShellTask t
342+
&& (t.PowerShellExecutionOptions.AddToHistory
343+
|| t.PowerShellExecutionOptions.FromRepl))
344+
{
345+
_languageServer?.SendNotification("powerShell/executionBusyStatus", true);
346+
}
347+
try
348+
{
349+
task.ExecuteSynchronously(cancellationToken);
350+
}
351+
finally
352+
{
353+
_languageServer?.SendNotification("powerShell/executionBusyStatus", false);
354+
}
355+
}
356+
337357
public Task<T> InvokeTaskOnPipelineThreadAsync<T>(SynchronousTask<T> task)
338358
{
339359
if (CancelForegroundAndPrepend(task))
@@ -769,8 +789,13 @@ private void RunExecutionLoop(bool isForDebug = false)
769789
{
770790
try
771791
{
772-
task.ExecuteSynchronously(cancellationScope.CancellationToken);
792+
ExecuteTaskSynchronously(task, cancellationScope.CancellationToken);
773793
}
794+
// Our flaky extension command test seems to be such because sometimes another
795+
// task gets queued, and since it runs in the foreground it cancels that task.
796+
// Interactively, this happens in the first loop (with DoOneRepl) which catches
797+
// the cancellation exception, but when under test that is a no-op, so it
798+
// happens in this second loop. Hence we need to catch it here too.
774799
catch (OperationCanceledException e)
775800
{
776801
_logger.LogDebug(e, "Task {Task} was canceled!", task);
@@ -935,19 +960,27 @@ private string InvokeReadLine(CancellationToken cancellationToken)
935960
}
936961
}
937962

963+
// TODO: Should we actually be directly invoking input versus queueing it as a task like everything else?
938964
private void InvokeInput(string input, CancellationToken cancellationToken)
939965
{
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);
966+
_languageServer?.SendNotification("powerShell/executionBusyStatus", true);
967+
try
968+
{
969+
InvokePSCommand(
970+
new PSCommand().AddScript(input, useLocalScope: false),
971+
new PowerShellExecutionOptions
972+
{
973+
AddToHistory = true,
974+
ThrowOnError = false,
975+
WriteOutputToHost = true,
976+
FromRepl = true,
977+
},
978+
cancellationToken);
979+
}
980+
finally
981+
{
982+
_languageServer?.SendNotification("powerShell/executionBusyStatus", false);
983+
}
951984
}
952985

953986
private void AddRunspaceEventHandlers(Runspace runspace)
@@ -1076,16 +1109,18 @@ private void OnPowerShellIdle(CancellationToken idleCancellationToken)
10761109
while (!cancellationScope.CancellationToken.IsCancellationRequested
10771110
&& _taskQueue.TryTake(out ISynchronousTask task))
10781111
{
1112+
// Tasks which require the foreground cannot run under this idle handler, so the
1113+
// current foreground tasks gets canceled, the new task gets prepended, and this
1114+
// handler returns.
10791115
if (CancelForegroundAndPrepend(task, isIdle: true))
10801116
{
10811117
return;
10821118
}
10831119

1084-
// If we're executing a task, we don't need to run an extra pipeline later for events
1085-
// TODO: This may not be a PowerShell task, so ideally we can differentiate that here.
1086-
// For now it's mostly true and an easy assumption to make.
1087-
runPipelineForEventProcessing = false;
1088-
task.ExecuteSynchronously(cancellationScope.CancellationToken);
1120+
// If we're executing a PowerShell task, we don't need to run an extra pipeline
1121+
// later for events.
1122+
runPipelineForEventProcessing = task is not ISynchronousPowerShellTask;
1123+
ExecuteTaskSynchronously(task, cancellationScope.CancellationToken);
10891124
}
10901125
}
10911126

0 commit comments

Comments
 (0)