@@ -23,33 +23,38 @@ namespace Microsoft.PowerShell.EditorServices.Protocol.Server
2323 public class DebugAdapter : DebugAdapterBase
2424 {
2525 private EditorSession editorSession ;
26- private OutputDebouncer outputDebouncer ;
2726
2827 private bool noDebug ;
2928 private bool waitingForAttach ;
3029 private string scriptToLaunch ;
30+ private bool ownsEditorSession ;
3131 private string arguments ;
3232
3333 public DebugAdapter ( HostDetails hostDetails , ProfilePaths profilePaths )
3434 : this ( hostDetails , profilePaths , new StdioServerChannel ( ) , null )
3535 {
3636 }
3737
38+ public DebugAdapter ( EditorSession editorSession , ChannelBase serverChannel )
39+ : base ( serverChannel )
40+ {
41+ this . editorSession = editorSession ;
42+ this . editorSession . PowerShellContext . RunspaceChanged += this . powerShellContext_RunspaceChanged ;
43+ this . editorSession . DebugService . DebuggerStopped += this . DebugService_DebuggerStopped ;
44+ }
45+
3846 public DebugAdapter (
3947 HostDetails hostDetails ,
4048 ProfilePaths profilePaths ,
4149 ChannelBase serverChannel ,
4250 IEditorOperations editorOperations )
4351 : base ( serverChannel )
4452 {
53+ this . ownsEditorSession = true ;
4554 this . editorSession = new EditorSession ( ) ;
4655 this . editorSession . StartDebugSession ( hostDetails , profilePaths , editorOperations ) ;
4756 this . editorSession . PowerShellContext . RunspaceChanged += this . powerShellContext_RunspaceChanged ;
4857 this . editorSession . DebugService . DebuggerStopped += this . DebugService_DebuggerStopped ;
49- this . editorSession . ConsoleService . OutputWritten += this . powerShellContext_OutputWritten ;
50-
51- // Set up the output debouncer to throttle output event writes
52- this . outputDebouncer = new OutputDebouncer ( this ) ;
5358 }
5459
5560 protected override void Initialize ( )
@@ -88,9 +93,6 @@ protected Task LaunchScript(RequestContext<object> requestContext)
8893 async ( t ) => {
8994 Logger . Write ( LogLevel . Verbose , "Execution completed, flushing output then terminating..." ) ;
9095
91- // Make sure remaining output is flushed before exiting
92- await this . outputDebouncer . Flush ( ) ;
93-
9496 await this . SendEvent (
9597 TerminatedEvent . Type ,
9698 new TerminatedEvent ( ) ) ;
@@ -102,14 +104,18 @@ await this.SendEvent(
102104
103105 protected override void Shutdown ( )
104106 {
105- // Make sure remaining output is flushed before exiting
106- this . outputDebouncer . Flush ( ) . Wait ( ) ;
107-
108107 Logger . Write ( LogLevel . Normal , "Debug adapter is shutting down..." ) ;
109108
110109 if ( this . editorSession != null )
111110 {
112- this . editorSession . Dispose ( ) ;
111+ this . editorSession . PowerShellContext . RunspaceChanged -= this . powerShellContext_RunspaceChanged ;
112+ this . editorSession . DebugService . DebuggerStopped -= this . DebugService_DebuggerStopped ;
113+
114+ if ( this . ownsEditorSession )
115+ {
116+ this . editorSession . Dispose ( ) ;
117+ }
118+
113119 this . editorSession = null ;
114120 }
115121 }
@@ -657,26 +663,7 @@ protected async Task HandleEvaluateRequest(
657663 "repl" ,
658664 StringComparison . CurrentCultureIgnoreCase ) ;
659665
660- if ( isFromRepl )
661- {
662- // Check for special commands
663- if ( string . Equals ( "!ctrlc" , evaluateParams . Expression , StringComparison . CurrentCultureIgnoreCase ) )
664- {
665- editorSession . PowerShellContext . AbortExecution ( ) ;
666- }
667- else if ( string . Equals ( "!break" , evaluateParams . Expression , StringComparison . CurrentCultureIgnoreCase ) )
668- {
669- editorSession . DebugService . Break ( ) ;
670- }
671- else
672- {
673- // Send the input through the console service
674- editorSession . ConsoleService . ExecuteCommand (
675- evaluateParams . Expression ,
676- false ) ;
677- }
678- }
679- else
666+ if ( ! isFromRepl )
680667 {
681668 VariableDetails result =
682669 await editorSession . DebugService . EvaluateExpression (
@@ -692,6 +679,12 @@ await editorSession.DebugService.EvaluateExpression(
692679 result . Id : 0 ;
693680 }
694681 }
682+ else
683+ {
684+ Logger . Write (
685+ LogLevel . Verbose ,
686+ $ "Debug adapter client attempted to evaluate command in REPL: { evaluateParams . Expression } ") ;
687+ }
695688
696689 await requestContext . SendResult (
697690 new EvaluateResponseBody
@@ -707,9 +700,6 @@ await requestContext.SendResult(
707700
708701 async void DebugService_DebuggerStopped ( object sender , DebuggerStoppedEventArgs e )
709702 {
710- // Flush pending output before sending the event
711- await this . outputDebouncer . Flush ( ) ;
712-
713703 // Provide the reason for why the debugger has stopped script execution.
714704 // See https://github.com/Microsoft/vscode/issues/3648
715705 // The reason is displayed in the breakpoints viewlet. Some recommended reasons are:
@@ -766,12 +756,6 @@ await this.SendEvent<ContinuedEvent>(
766756 }
767757 }
768758
769- async void powerShellContext_OutputWritten ( object sender , OutputWrittenEventArgs e )
770- {
771- // Queue the output for writing
772- await this . outputDebouncer . Invoke ( e ) ;
773- }
774-
775759 #endregion
776760 }
777761}
0 commit comments