Skip to content

Commit d935944

Browse files
committed
Improve ConsoleService input handling for prompts
This change adds a new ReceivePromptResponse method to the ConsoleService class so that prompt input received after a prompt is cancelled isn't mistakenly executed as a command. This change improves the user experience of prompt timeouts in VS Code.
1 parent d2917a3 commit d935944

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ protected async Task HandleEvaluateRequest(
324324
if (isFromRepl)
325325
{
326326
// Send the input through the console service
327-
editorSession.ConsoleService.ReceiveInputString(
327+
editorSession.ConsoleService.ExecuteCommand(
328328
evaluateParams.Expression,
329329
false);
330330
}

src/PowerShellEditorServices/Console/ConsoleService.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ public ConsoleService(
7575
#region Public Methods
7676

7777
/// <summary>
78-
/// Called when an input string is received from the user.
78+
/// Called when a command string is received from the user.
7979
/// If a prompt is currently active, the prompt handler is
8080
/// asked to handle the string. Otherwise the string is
8181
/// executed in the PowerShellContext.
8282
/// </summary>
8383
/// <param name="inputString">The input string to evaluate.</param>
8484
/// <param name="echoToConsole">If true, the input will be echoed to the console.</param>
85-
public void ReceiveInputString(string inputString, bool echoToConsole)
85+
public void ExecuteCommand(string inputString, bool echoToConsole)
8686
{
8787
if (this.activePromptHandler != null)
8888
{
@@ -111,6 +111,25 @@ public void ReceiveInputString(string inputString, bool echoToConsole)
111111
}
112112
}
113113

114+
/// <summary>
115+
/// Provides a direct path for a caller that just wants to provide
116+
/// user response to a prompt without executing a command if there
117+
/// is no active prompt.
118+
/// </summary>
119+
/// <param name="promptResponse">The user's response to the active prompt.</param>
120+
/// <param name="echoToConsole">If true, the input will be echoed to the console.</param>
121+
/// <returns>True if there was a prompt, false otherwise.</returns>
122+
public bool ReceivePromptResponse(string promptResponse, bool echoToConsole)
123+
{
124+
if (this.activePromptHandler != null)
125+
{
126+
this.ExecuteCommand(promptResponse, echoToConsole);
127+
return true;
128+
}
129+
130+
return false;
131+
}
132+
114133
/// <summary>
115134
/// Pushes a new IPromptHandlerContext onto the stack. This
116135
/// is used when a prompt handler context is only needed for

test/PowerShellEditorServices.Test/Console/ConsoleServiceTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public async Task ReceivesChoicePrompt()
168168
await promptTask;
169169

170170
// Respond to the prompt and wait for the prompt to complete
171-
this.consoleService.ReceiveInputString("apple", false);
171+
this.consoleService.ExecuteCommand("apple", false);
172172
await executeTask;
173173

174174
string[] outputLines =
@@ -233,8 +233,8 @@ public async Task ReceivesChoicePromptHelp()
233233
await promptTask;
234234

235235
// Respond to the prompt and wait for the help prompt to appear
236-
this.consoleService.ReceiveInputString("?", false);
237-
this.consoleService.ReceiveInputString("A", false);
236+
this.consoleService.ExecuteCommand("?", false);
237+
this.consoleService.ExecuteCommand("A", false);
238238
await executeTask;
239239

240240
string[] outputLines =
@@ -265,10 +265,10 @@ public async Task ReceivesInputPrompt()
265265
await promptTask;
266266

267267
// Respond to the prompt and wait for execution to complete
268-
this.consoleService.ReceiveInputString("John", true);
269-
this.consoleService.ReceiveInputString("40", true);
270-
this.consoleService.ReceiveInputString("Windows PowerShell In Action", true);
271-
this.consoleService.ReceiveInputString("", false);
268+
this.consoleService.ExecuteCommand("John", true);
269+
this.consoleService.ExecuteCommand("40", true);
270+
this.consoleService.ExecuteCommand("Windows PowerShell In Action", true);
271+
this.consoleService.ExecuteCommand("", false);
272272
await executeTask;
273273

274274
string[] outputLines =
@@ -329,7 +329,7 @@ public async Task ReceivesReadHostPrompt()
329329
await promptTask;
330330

331331
// Respond to the prompt and wait for execution to complete
332-
this.consoleService.ReceiveInputString("John", true);
332+
this.consoleService.ExecuteCommand("John", true);
333333
await executeTask;
334334

335335
string[] outputLines =
@@ -375,7 +375,7 @@ public async Task ReceivesReadHostPromptWithFieldName()
375375
await promptTask;
376376

377377
// Respond to the prompt and wait for execution to complete
378-
this.consoleService.ReceiveInputString("John", true);
378+
this.consoleService.ExecuteCommand("John", true);
379379
await executeTask;
380380

381381
string[] outputLines =

0 commit comments

Comments
 (0)