Skip to content

Commit 03d8989

Browse files
Revert "Consolidate InterruptCurrentForeground and MustRunInForeground (PowerShell#1777)"
This reverts commit 39c9ed4.
1 parent 381e7fe commit 03d8989

File tree

8 files changed

+43
-65
lines changed

8 files changed

+43
-65
lines changed

src/PowerShellEditorServices/Services/DebugAdapter/Handlers/ConfigurationDoneHandler.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@ internal class ConfigurationDoneHandler : IConfigurationDoneHandler
2525
// TODO: We currently set `WriteInputToHost` as true, which writes our debugged commands'
2626
// `GetInvocationText` and that reveals some obscure implementation details we should
2727
// instead hide from the user with pretty strings (or perhaps not write out at all).
28-
//
29-
// This API is mostly used for F5 execution so it requires the foreground.
3028
private static readonly PowerShellExecutionOptions s_debuggerExecutionOptions = new()
3129
{
32-
RequiresForeground = true,
30+
MustRunInForeground = true,
3331
WriteInputToHost = true,
3432
WriteOutputToHost = true,
3533
ThrowOnError = false,

src/PowerShellEditorServices/Services/Extension/ExtensionService.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,17 @@ public Task InvokeCommandAsync(string commandName, EditorContext editorContext,
129129
.AddParameter("ScriptBlock", editorCommand.ScriptBlock)
130130
.AddParameter("ArgumentList", new object[] { editorContext });
131131

132-
// This API is used for editor command execution so it requires the foreground.
132+
// This API is used for editor command execution, so it needs to interrupt the
133+
// current prompt (or other foreground task).
133134
return ExecutionService.ExecutePSCommandAsync(
134135
executeCommand,
135136
cancellationToken,
136137
new PowerShellExecutionOptions
137138
{
138-
RequiresForeground = true,
139139
WriteOutputToHost = !editorCommand.SuppressOutput,
140140
AddToHistory = !editorCommand.SuppressOutput,
141141
ThrowOnError = false,
142+
InterruptCurrentForeground = true
142143
});
143144
}
144145

src/PowerShellEditorServices/Services/PowerShell/Console/PsrlReadLine.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ public PsrlReadLine(
3232
_psrlProxy.OverrideIdleHandler(onIdleAction);
3333
}
3434

35-
public override string ReadLine(CancellationToken cancellationToken) => _psesHost.InvokeDelegate(
36-
representation: "ReadLine",
37-
new ExecutionOptions { RequiresForeground = true },
38-
InvokePSReadLine,
39-
cancellationToken);
35+
public override string ReadLine(CancellationToken cancellationToken) => _psesHost.InvokeDelegate(representation: "ReadLine", new ExecutionOptions { MustRunInForeground = true }, InvokePSReadLine, cancellationToken);
4036

4137
protected override ConsoleKeyInfo ReadKey(CancellationToken cancellationToken) => _psesHost.ReadKey(intercept: true, cancellationToken);
4238

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public bool TryTake(out T item)
6565

6666
public IDisposable BlockConsumers() => PriorityQueueBlockLifetime.StartBlocking(_blockConsumersEvent);
6767

68-
public void Dispose() => _blockConsumersEvent.Dispose();
68+
public void Dispose() => ((IDisposable)_blockConsumersEvent).Dispose();
6969

7070
private class PriorityQueueBlockLifetime : IDisposable
7171
{

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ public enum ExecutionPriority
1616
public record ExecutionOptions
1717
{
1818
public ExecutionPriority Priority { get; init; } = ExecutionPriority.Normal;
19-
public bool RequiresForeground { get; init; }
19+
public bool MustRunInForeground { get; init; }
20+
public bool InterruptCurrentForeground { get; init; }
2021
}
2122

2223
public record PowerShellExecutionOptions : ExecutionOptions
2324
{
2425
internal static PowerShellExecutionOptions ImmediateInteractive = new()
2526
{
2627
Priority = ExecutionPriority.Next,
27-
RequiresForeground = true,
28+
MustRunInForeground = true,
29+
InterruptCurrentForeground = true,
2830
};
2931

3032
public bool WriteOutputToHost { get; init; }

src/PowerShellEditorServices/Services/PowerShell/Handlers/EvaluateHandler.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@ internal class EvaluateHandler : IEvaluateHandler
2121

2222
public async Task<EvaluateResponseBody> Handle(EvaluateRequestArguments request, CancellationToken cancellationToken)
2323
{
24-
// This API is mostly used for F8 execution so it requires the foreground.
24+
// This API is mostly used for F8 execution, so it needs to interrupt the command prompt
25+
// (or other foreground task).
2526
await _executionService.ExecutePSCommandAsync(
2627
new PSCommand().AddScript(request.Expression),
2728
CancellationToken.None,
2829
new PowerShellExecutionOptions
2930
{
30-
RequiresForeground = true,
3131
WriteInputToHost = true,
3232
WriteOutputToHost = true,
3333
AddToHistory = true,
3434
ThrowOnError = false,
35+
InterruptCurrentForeground = true
3536
}).ConfigureAwait(false);
3637

3738
// TODO: Should we return a more informative result?

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

+25-39
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation.
1+
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

44
using System;
@@ -277,7 +277,7 @@ public void SetExit()
277277
public Task<T> InvokeTaskOnPipelineThreadAsync<T>(
278278
SynchronousTask<T> task)
279279
{
280-
if (task.ExecutionOptions.RequiresForeground)
280+
if (task.ExecutionOptions.InterruptCurrentForeground)
281281
{
282282
// When a task must displace the current foreground command,
283283
// we must:
@@ -404,14 +404,9 @@ public void InvokePSDelegate(string representation, ExecutionOptions executionOp
404404

405405
internal Task LoadHostProfilesAsync(CancellationToken cancellationToken)
406406
{
407-
// TODO: Why exactly does loading profiles require the foreground?
408407
return ExecuteDelegateAsync(
409408
"LoadProfiles",
410-
new PowerShellExecutionOptions
411-
{
412-
RequiresForeground = true,
413-
ThrowOnError = false
414-
},
409+
new PowerShellExecutionOptions { MustRunInForeground = true, ThrowOnError = false },
415410
(pwsh, _) => pwsh.LoadProfiles(_hostInfo.ProfilePaths),
416411
cancellationToken);
417412
}
@@ -855,15 +850,7 @@ private string InvokeReadLine(CancellationToken cancellationToken)
855850
private void InvokeInput(string input, CancellationToken cancellationToken)
856851
{
857852
PSCommand command = new PSCommand().AddScript(input, useLocalScope: false);
858-
InvokePSCommand(
859-
command,
860-
new PowerShellExecutionOptions
861-
{
862-
AddToHistory = true,
863-
ThrowOnError = false,
864-
WriteOutputToHost = true
865-
},
866-
cancellationToken);
853+
InvokePSCommand(command, new PowerShellExecutionOptions { AddToHistory = true, ThrowOnError = false, WriteOutputToHost = true }, cancellationToken);
867854
}
868855

869856
private void AddRunspaceEventHandlers(Runspace runspace)
@@ -957,7 +944,6 @@ private Runspace CreateInitialRunspace(InitialSessionState initialSessionState)
957944
return runspace;
958945
}
959946

960-
// NOTE: This token is received from PSReadLine, and it _is_ the ReadKey cancellation token!
961947
private void OnPowerShellIdle(CancellationToken idleCancellationToken)
962948
{
963949
IReadOnlyList<PSEventSubscriber> eventSubscribers = _mainRunspaceEngineIntrinsics.Events.Subscribers;
@@ -990,7 +976,7 @@ private void OnPowerShellIdle(CancellationToken idleCancellationToken)
990976
while (!cancellationScope.CancellationToken.IsCancellationRequested
991977
&& _taskQueue.TryTake(out ISynchronousTask task))
992978
{
993-
if (task.ExecutionOptions.RequiresForeground)
979+
if (task.ExecutionOptions.MustRunInForeground)
994980
{
995981
// If we have a task that is queued, but cannot be run under readline
996982
// we place it back at the front of the queue, and cancel the readline task
@@ -1111,27 +1097,27 @@ private void OnDebuggerStopped(object sender, DebuggerStopEventArgs debuggerStop
11111097

11121098
void OnDebuggerStoppedImpl(object sender, DebuggerStopEventArgs debuggerStopEventArgs)
11131099
{
1114-
// If the debug server is NOT active, we need to synchronize state and start it.
1115-
if (!DebugContext.IsDebugServerActive)
1116-
{
1117-
_languageServer?.SendNotification("powerShell/startDebugger");
1118-
}
1100+
// If the debug server is NOT active, we need to synchronize state and start it.
1101+
if (!DebugContext.IsDebugServerActive)
1102+
{
1103+
_languageServer?.SendNotification("powerShell/startDebugger");
1104+
}
11191105

1120-
DebugContext.SetDebuggerStopped(debuggerStopEventArgs);
1106+
DebugContext.SetDebuggerStopped(debuggerStopEventArgs);
11211107

1122-
try
1123-
{
1124-
CurrentPowerShell.WaitForRemoteOutputIfNeeded();
1125-
PowerShellFrameType frameBase = CurrentFrame.FrameType & PowerShellFrameType.Remote;
1126-
PushPowerShellAndRunLoop(
1127-
CreateNestedPowerShell(CurrentRunspace),
1128-
frameBase | PowerShellFrameType.Debug | PowerShellFrameType.Nested | PowerShellFrameType.Repl);
1129-
CurrentPowerShell.ResumeRemoteOutputIfNeeded();
1130-
}
1131-
finally
1132-
{
1133-
DebugContext.SetDebuggerResumed();
1134-
}
1108+
try
1109+
{
1110+
CurrentPowerShell.WaitForRemoteOutputIfNeeded();
1111+
PowerShellFrameType frameBase = CurrentFrame.FrameType & PowerShellFrameType.Remote;
1112+
PushPowerShellAndRunLoop(
1113+
CreateNestedPowerShell(CurrentRunspace),
1114+
frameBase | PowerShellFrameType.Debug | PowerShellFrameType.Nested | PowerShellFrameType.Repl);
1115+
CurrentPowerShell.ResumeRemoteOutputIfNeeded();
1116+
}
1117+
finally
1118+
{
1119+
DebugContext.SetDebuggerResumed();
1120+
}
11351121
}
11361122
}
11371123

@@ -1155,7 +1141,7 @@ private Task PopOrReinitializeRunspaceAsync()
11551141
// we simply run this on its thread, guaranteeing that no other action can occur
11561142
return ExecuteDelegateAsync(
11571143
nameof(PopOrReinitializeRunspaceAsync),
1158-
new ExecutionOptions { RequiresForeground = true },
1144+
new ExecutionOptions { InterruptCurrentForeground = true },
11591145
(_) =>
11601146
{
11611147
while (_psFrameStack.Count > 0

src/PowerShellEditorServices/Services/Template/TemplateService.cs

+5-11
Original file line numberDiff line numberDiff line change
@@ -158,21 +158,15 @@ public async Task<bool> CreateFromTemplateAsync(
158158
_logger.LogTrace(
159159
$"Invoking Plaster...\n\n TemplatePath: {templatePath}\n DestinationPath: {destinationPath}");
160160

161-
PSCommand command = new PSCommand()
162-
.AddCommand("Invoke-Plaster")
163-
.AddParameter("TemplatePath", templatePath)
164-
.AddParameter("DestinationPath", destinationPath);
161+
PSCommand command = new();
162+
command.AddCommand("Invoke-Plaster");
163+
command.AddParameter("TemplatePath", templatePath);
164+
command.AddParameter("DestinationPath", destinationPath);
165165

166-
// This command is interactive so it requires the foreground.
167166
await _executionService.ExecutePSCommandAsync(
168167
command,
169168
CancellationToken.None,
170-
new PowerShellExecutionOptions
171-
{
172-
RequiresForeground = true,
173-
WriteOutputToHost = true,
174-
ThrowOnError = false
175-
}).ConfigureAwait(false);
169+
new PowerShellExecutionOptions { WriteOutputToHost = true, InterruptCurrentForeground = true, ThrowOnError = false }).ConfigureAwait(false);
176170

177171
// If any errors were written out, creation was not successful
178172
return true;

0 commit comments

Comments
 (0)