Skip to content

Commit 6213d1b

Browse files
authored
Merge pull request #401 from daviwil/debugger-polish
A few improvements to the interactive debugging experience
2 parents 8efdcc3 + 28617c6 commit 6213d1b

File tree

6 files changed

+91
-28
lines changed

6 files changed

+91
-28
lines changed

src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs

+54-17
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public DebugAdapter(EditorSession editorSession, ChannelBase serverChannel)
4646
this.editorSession = editorSession;
4747
this.editorSession.PowerShellContext.RunspaceChanged += this.powerShellContext_RunspaceChanged;
4848
this.editorSession.DebugService.DebuggerStopped += this.DebugService_DebuggerStopped;
49+
this.editorSession.PowerShellContext.DebuggerResumed += this.powerShellContext_DebuggerResumed;
4950
}
5051

5152
public DebugAdapter(
@@ -60,6 +61,7 @@ public DebugAdapter(
6061
this.editorSession.StartDebugSession(hostDetails, profilePaths, editorOperations);
6162
this.editorSession.PowerShellContext.RunspaceChanged += this.powerShellContext_RunspaceChanged;
6263
this.editorSession.DebugService.DebuggerStopped += this.DebugService_DebuggerStopped;
64+
this.editorSession.PowerShellContext.DebuggerResumed += this.powerShellContext_DebuggerResumed;
6365

6466
// The assumption in this overload is that the debugger
6567
// is running in UI-hosted mode, no terminal interface
@@ -295,6 +297,11 @@ await this.editorSession.PowerShellContext.ExecuteScriptString(
295297
"", false, true);
296298
}
297299

300+
if (this.editorSession.ConsoleService.EnableConsoleRepl)
301+
{
302+
await this.WriteUseIntegratedConsoleMessage();
303+
}
304+
298305
// Send the InitializedEvent so that the debugger will continue
299306
// sending configuration requests
300307
await this.SendEvent(
@@ -743,23 +750,30 @@ protected async Task HandleEvaluateRequest(
743750

744751
if (isFromRepl)
745752
{
746-
// Check for special commands
747-
if (string.Equals("!ctrlc", evaluateParams.Expression, StringComparison.CurrentCultureIgnoreCase))
753+
if (!this.editorSession.ConsoleService.EnableConsoleRepl)
748754
{
749-
editorSession.PowerShellContext.AbortExecution();
750-
}
751-
else if (string.Equals("!break", evaluateParams.Expression, StringComparison.CurrentCultureIgnoreCase))
752-
{
753-
editorSession.DebugService.Break();
755+
// Check for special commands
756+
if (string.Equals("!ctrlc", evaluateParams.Expression, StringComparison.CurrentCultureIgnoreCase))
757+
{
758+
editorSession.PowerShellContext.AbortExecution();
759+
}
760+
else if (string.Equals("!break", evaluateParams.Expression, StringComparison.CurrentCultureIgnoreCase))
761+
{
762+
editorSession.DebugService.Break();
763+
}
764+
else
765+
{
766+
// Send the input through the console service
767+
var notAwaited =
768+
this.editorSession
769+
.PowerShellContext
770+
.ExecuteScriptString(evaluateParams.Expression, false, true)
771+
.ConfigureAwait(false);
772+
}
754773
}
755774
else
756775
{
757-
// Send the input through the console service
758-
var notAwaited =
759-
this.editorSession
760-
.PowerShellContext
761-
.ExecuteScriptString(evaluateParams.Expression, false, true)
762-
.ConfigureAwait(false);
776+
await this.WriteUseIntegratedConsoleMessage();
763777
}
764778
}
765779
else
@@ -770,10 +784,11 @@ protected async Task HandleEvaluateRequest(
770784
// has been resumed, return an empty result in this case.
771785
if (editorSession.PowerShellContext.IsDebuggerStopped)
772786
{
773-
await editorSession.DebugService.EvaluateExpression(
774-
evaluateParams.Expression,
775-
evaluateParams.FrameId,
776-
isFromRepl);
787+
result =
788+
await editorSession.DebugService.EvaluateExpression(
789+
evaluateParams.Expression,
790+
evaluateParams.FrameId,
791+
isFromRepl);
777792
}
778793

779794
if (result != null)
@@ -793,6 +808,17 @@ await requestContext.SendResult(
793808
});
794809
}
795810

811+
private async Task WriteUseIntegratedConsoleMessage()
812+
{
813+
await this.SendEvent(
814+
OutputEvent.Type,
815+
new OutputEventBody
816+
{
817+
Output = "\nThe Debug Console is no longer used for PowerShell debugging. Please use the 'PowerShell Integrated Console' to execute commands in the debugger. Run the 'PowerShell: Show Integrated Console' command to open it.",
818+
Category = "stderr"
819+
});
820+
}
821+
796822
#endregion
797823

798824
#region Event Handlers
@@ -867,6 +893,17 @@ await this.SendEvent<ContinuedEvent>(
867893
}
868894
}
869895

896+
private async void powerShellContext_DebuggerResumed(object sender, DebuggerResumeAction e)
897+
{
898+
await this.SendEvent(
899+
ContinuedEvent.Type,
900+
new ContinuedEvent
901+
{
902+
AllThreadsContinued = true,
903+
ThreadId = 1
904+
});
905+
}
906+
870907
#endregion
871908
}
872909
}

src/PowerShellEditorServices/Console/ConsoleService.cs

+4
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ public void CancelReadLoop()
131131
{
132132
if (this.readLineCancellationToken != null)
133133
{
134+
// Set this to false so that Ctrl+C isn't trapped by any
135+
// lingering ReadKey
136+
Console.TreatControlCAsInput = false;
137+
134138
this.readLineCancellationToken.Cancel();
135139
this.readLineCancellationToken = null;
136140
}

src/PowerShellEditorServices/Session/IVersionSpecificOperations.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ IEnumerable<TResult> ExecuteCommandInDebugger<TResult>(
1919
PowerShellContext powerShellContext,
2020
Runspace currentRunspace,
2121
PSCommand psCommand,
22-
bool sendOutputToHost);
22+
bool sendOutputToHost,
23+
out DebuggerResumeAction? debuggerResumeAction);
2324
}
2425
}
2526

src/PowerShellEditorServices/Session/PowerShell3Operations.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public IEnumerable<TResult> ExecuteCommandInDebugger<TResult>(
2828
PowerShellContext powerShellContext,
2929
Runspace currentRunspace,
3030
PSCommand psCommand,
31-
bool sendOutputToHost)
31+
bool sendOutputToHost,
32+
out DebuggerResumeAction? debuggerResumeAction)
3233
{
3334
IEnumerable<TResult> executionResult = null;
3435

@@ -63,6 +64,9 @@ public IEnumerable<TResult> ExecuteCommandInDebugger<TResult>(
6364
}
6465
}
6566

67+
// No DebuggerResumeAction result for PowerShell v3
68+
debuggerResumeAction = null;
69+
6670
return executionResult;
6771
}
6872
}

src/PowerShellEditorServices/Session/PowerShell4Operations.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ public IEnumerable<TResult> ExecuteCommandInDebugger<TResult>(
3333
PowerShellContext powerShellContext,
3434
Runspace currentRunspace,
3535
PSCommand psCommand,
36-
bool sendOutputToHost)
36+
bool sendOutputToHost,
37+
out DebuggerResumeAction? debuggerResumeAction)
3738
{
39+
debuggerResumeAction = null;
3840
PSDataCollection<PSObject> outputCollection = new PSDataCollection<PSObject>();
3941

4042
#if !PowerShellv3
@@ -56,6 +58,10 @@ public IEnumerable<TResult> ExecuteCommandInDebugger<TResult>(
5658
currentRunspace.Debugger.ProcessCommand(
5759
psCommand,
5860
outputCollection);
61+
62+
// Pass along the debugger's resume action if the user's
63+
// command caused one to be returned
64+
debuggerResumeAction = commandResults.ResumeAction;
5965
#endif
6066

6167
IEnumerable<TResult> results = null;

src/PowerShellEditorServices/Session/PowerShellContext.cs

+19-8
Original file line numberDiff line numberDiff line change
@@ -699,15 +699,16 @@ public async Task ExecuteScriptWithArgs(string script, string arguments = null,
699699
// Related to issue #123.
700700
if (File.Exists(script) || File.Exists(scriptAbsPath))
701701
{
702-
script = EscapePath(script, escapeSpaces: true);
702+
// Dot-source the launched script path
703+
script = ". " + EscapePath(script, escapeSpaces: true);
703704
}
704705

705706
launchedScript = script + " " + arguments;
706-
command.AddScript(launchedScript);
707+
command.AddScript(launchedScript, false);
707708
}
708709
else
709710
{
710-
command.AddCommand(script);
711+
command.AddCommand(script, false);
711712
}
712713

713714
if (writeInputToHost)
@@ -1088,11 +1089,21 @@ private IEnumerable<TResult> ExecuteCommandInDebugger<TResult>(PSCommand psComma
10881089
"Attempting to execute command(s) in the debugger:\r\n\r\n{0}",
10891090
GetStringForPSCommand(psCommand)));
10901091

1091-
return this.versionSpecificOperations.ExecuteCommandInDebugger<TResult>(
1092-
this,
1093-
this.CurrentRunspace.Runspace,
1094-
psCommand,
1095-
sendOutputToHost);
1092+
IEnumerable<TResult> output =
1093+
this.versionSpecificOperations.ExecuteCommandInDebugger<TResult>(
1094+
this,
1095+
this.CurrentRunspace.Runspace,
1096+
psCommand,
1097+
sendOutputToHost,
1098+
out DebuggerResumeAction? debuggerResumeAction);
1099+
1100+
if (debuggerResumeAction.HasValue)
1101+
{
1102+
// Resume the debugger with the specificed action
1103+
this.ResumeDebugger(debuggerResumeAction.Value);
1104+
}
1105+
1106+
return output;
10961107
}
10971108

10981109
internal void WriteOutput(string outputString, bool includeNewLine)

0 commit comments

Comments
 (0)