@@ -30,6 +30,7 @@ public class DebugAdapter : DebugAdapterBase
30
30
private bool isAttachSession ;
31
31
private bool waitingForAttach ;
32
32
private string scriptToLaunch ;
33
+ private bool enableConsoleRepl ;
33
34
private bool ownsEditorSession ;
34
35
private bool executionCompleted ;
35
36
private string arguments ;
@@ -44,29 +45,29 @@ public DebugAdapter(EditorSession editorSession, ChannelBase serverChannel)
44
45
: base ( serverChannel )
45
46
{
46
47
this . editorSession = editorSession ;
47
- this . editorSession . PowerShellContext . RunspaceChanged += this . powerShellContext_RunspaceChanged ;
48
- this . editorSession . DebugService . DebuggerStopped += this . DebugService_DebuggerStopped ;
49
- this . editorSession . PowerShellContext . DebuggerResumed += this . powerShellContext_DebuggerResumed ;
50
48
}
51
49
52
50
public DebugAdapter (
53
51
HostDetails hostDetails ,
54
52
ProfilePaths profilePaths ,
55
53
ChannelBase serverChannel ,
56
54
IEditorOperations editorOperations )
55
+ : this ( hostDetails , profilePaths , serverChannel , editorOperations , false )
56
+ {
57
+ }
58
+
59
+ public DebugAdapter (
60
+ HostDetails hostDetails ,
61
+ ProfilePaths profilePaths ,
62
+ ChannelBase serverChannel ,
63
+ IEditorOperations editorOperations ,
64
+ bool enableConsoleRepl )
57
65
: base ( serverChannel )
58
66
{
59
67
this . ownsEditorSession = true ;
60
68
this . editorSession = new EditorSession ( ) ;
61
69
this . editorSession . StartDebugSession ( hostDetails , profilePaths , editorOperations ) ;
62
- this . editorSession . PowerShellContext . RunspaceChanged += this . powerShellContext_RunspaceChanged ;
63
- this . editorSession . DebugService . DebuggerStopped += this . DebugService_DebuggerStopped ;
64
- this . editorSession . PowerShellContext . DebuggerResumed += this . powerShellContext_DebuggerResumed ;
65
-
66
- // The assumption in this overload is that the debugger
67
- // is running in UI-hosted mode, no terminal interface
68
- this . editorSession . ConsoleService . OutputWritten += this . powerShellContext_OutputWritten ;
69
- this . outputDebouncer = new OutputDebouncer ( this ) ;
70
+ this . enableConsoleRepl = enableConsoleRepl ;
70
71
}
71
72
72
73
protected override void Initialize ( )
@@ -139,6 +140,8 @@ private async Task OnExecutionCompleted(Task executeTask)
139
140
await this . outputDebouncer . Flush ( ) ;
140
141
}
141
142
143
+ this . UnregisterEventHandlers ( ) ;
144
+
142
145
if ( this . isAttachSession )
143
146
{
144
147
// Ensure the read loop is stopped
@@ -198,6 +201,7 @@ protected override void Shutdown()
198
201
{
199
202
this . editorSession . PowerShellContext . RunspaceChanged -= this . powerShellContext_RunspaceChanged ;
200
203
this . editorSession . DebugService . DebuggerStopped -= this . DebugService_DebuggerStopped ;
204
+ this . editorSession . PowerShellContext . DebuggerResumed -= this . powerShellContext_DebuggerResumed ;
201
205
202
206
if ( this . ownsEditorSession )
203
207
{
@@ -238,6 +242,8 @@ protected async Task HandleLaunchRequest(
238
242
LaunchRequestArguments launchParams ,
239
243
RequestContext < object > requestContext )
240
244
{
245
+ this . RegisterEventHandlers ( ) ;
246
+
241
247
// Set the working directory for the PowerShell runspace to the cwd passed in via launch.json.
242
248
// In case that is null, use the the folder of the script to be executed. If the resulting
243
249
// working dir path is a file path then extract the directory and use that.
@@ -335,6 +341,8 @@ protected async Task HandleAttachRequest(
335
341
{
336
342
this . isAttachSession = true ;
337
343
344
+ this . RegisterEventHandlers ( ) ;
345
+
338
346
// If there are no host processes to attach to or the user cancels selection, we get a null for the process id.
339
347
// This is not an error, just a request to stop the original "attach to" request.
340
348
// Testing against "undefined" is a HACK because I don't know how to make "Cancel" on quick pick loading
@@ -454,6 +462,8 @@ protected async Task HandleDisconnectRequest(
454
462
}
455
463
else
456
464
{
465
+ this . UnregisterEventHandlers ( ) ;
466
+
457
467
await requestContext . SendResult ( null ) ;
458
468
await this . Stop ( ) ;
459
469
}
@@ -839,6 +849,31 @@ await this.SendEvent(
839
849
} ) ;
840
850
}
841
851
852
+ private void RegisterEventHandlers ( )
853
+ {
854
+ this . editorSession . PowerShellContext . RunspaceChanged += this . powerShellContext_RunspaceChanged ;
855
+ this . editorSession . DebugService . DebuggerStopped += this . DebugService_DebuggerStopped ;
856
+ this . editorSession . PowerShellContext . DebuggerResumed += this . powerShellContext_DebuggerResumed ;
857
+
858
+ if ( ! this . enableConsoleRepl )
859
+ {
860
+ this . editorSession . ConsoleService . OutputWritten += this . powerShellContext_OutputWritten ;
861
+ this . outputDebouncer = new OutputDebouncer ( this ) ;
862
+ }
863
+ }
864
+
865
+ private void UnregisterEventHandlers ( )
866
+ {
867
+ this . editorSession . PowerShellContext . RunspaceChanged -= this . powerShellContext_RunspaceChanged ;
868
+ this . editorSession . DebugService . DebuggerStopped -= this . DebugService_DebuggerStopped ;
869
+ this . editorSession . PowerShellContext . DebuggerResumed -= this . powerShellContext_DebuggerResumed ;
870
+
871
+ if ( ! this . enableConsoleRepl )
872
+ {
873
+ this . editorSession . ConsoleService . OutputWritten -= this . powerShellContext_OutputWritten ;
874
+ }
875
+ }
876
+
842
877
#endregion
843
878
844
879
#region Event Handlers
0 commit comments