Skip to content

Commit 8034cf7

Browse files
committed
Improve debugger variable support
This changes improves debugger variable support by allowing the enumeration of the children of "expandable" variables (objects and collections) and the requesting of variable scopes. The variable scope feature will be completed in a future commit.
1 parent 44aee88 commit 8034cf7

File tree

18 files changed

+398
-188
lines changed

18 files changed

+398
-188
lines changed

src/PowerShellEditorServices.Host/MessageLoop.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,22 +182,22 @@ void PowerShellSession_BreakpointUpdated(object sender, BreakpointUpdatedEventAr
182182
{
183183
}
184184

185-
async void PowerShellSession_OutputWritten(object sender, OutputWrittenEventArgs e)
185+
void PowerShellSession_OutputWritten(object sender, OutputWrittenEventArgs e)
186186
{
187187
// TODO: change this to use the OutputEvent!
188188

189-
await this.messageWriter.WriteMessage(
190-
new ReplWriteOutputEvent
191-
{
192-
Body = new ReplWriteOutputEventBody
193-
{
194-
LineContents = e.OutputText,
195-
LineType = e.OutputType,
196-
IncludeNewLine = e.IncludeNewLine,
197-
ForegroundColor = e.ForegroundColor,
198-
BackgroundColor = e.BackgroundColor
199-
}
200-
});
189+
//await this.messageWriter.WriteMessage(
190+
// new ReplWriteOutputEvent
191+
// {
192+
// Body = new ReplWriteOutputEventBody
193+
// {
194+
// LineContents = e.OutputText,
195+
// LineType = e.OutputType,
196+
// IncludeNewLine = e.IncludeNewLine,
197+
// ForegroundColor = e.ForegroundColor,
198+
// BackgroundColor = e.BackgroundColor
199+
// }
200+
// });
201201
}
202202

203203
#endregion

src/PowerShellEditorServices.Host/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ static void Main(string[] args)
2727
"/waitForDebugger",
2828
StringComparison.InvariantCultureIgnoreCase));
2929

30-
waitForDebugger = true;
31-
3230
// Should we wait for the debugger before starting?
3331
if (waitForDebugger)
3432
{

src/PowerShellEditorServices.Transport.Stdio/Model/Scope.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Microsoft.PowerShell.EditorServices.Console;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
@@ -19,5 +20,14 @@ public class Scope
1920
// /** If true, the number of variables in this scope is large or expensive to retrieve. */
2021
// expensive: boolean;
2122
public bool Expensive { get; set; }
23+
24+
public static Scope Create(VariableScope scope)
25+
{
26+
return new Scope
27+
{
28+
Name = scope.Name,
29+
VariablesReference = scope.Id
30+
};
31+
}
2232
}
2333
}

src/PowerShellEditorServices.Transport.Stdio/Model/StackFrame.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
using Microsoft.PowerShell.EditorServices.Console;
3+
34
namespace Microsoft.PowerShell.EditorServices.Transport.Stdio.Model
45
{
56
public class StackFrame
@@ -14,8 +15,6 @@ public class StackFrame
1415

1516
public int Column { get; set; }
1617

17-
public Scope[] Scopes { get; set; }
18-
1918
// /** An identifier for the stack frame. */
2019
//id: number;
2120
///** The name of the stack frame, typically a method name */
@@ -30,25 +29,18 @@ public class StackFrame
3029
//scopes: Scope[];
3130

3231
public static StackFrame Create(
33-
StackFrameDetails stackFrame)
32+
StackFrameDetails stackFrame,
33+
int id)
3434
{
3535
return new StackFrame
3636
{
37-
Id = stackFrame.FunctionName.GetHashCode(),
37+
Id = id,
3838
Name = stackFrame.FunctionName,
3939
Line = stackFrame.LineNumber,
4040
Column = stackFrame.ColumnNumber,
4141
Source = new Source
4242
{
4343
Path = stackFrame.ScriptPath
44-
},
45-
Scopes = new Scope[]
46-
{
47-
new Scope
48-
{
49-
Name = "locals",
50-
VariablesReference = 1 // TODO: Use a contextual number
51-
}
5244
}
5345
};
5446
}

src/PowerShellEditorServices.Transport.Stdio/Model/Variable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static Variable Create(VariableDetails variable)
1919
Name = variable.Name,
2020
Value = variable.ValueString ?? string.Empty,
2121
VariablesReference =
22-
variable.HasChildren ?
22+
variable.IsExpandable ?
2323
variable.Id : 0
2424
};
2525
}

src/PowerShellEditorServices.Transport.Stdio/PowerShellEditorServices.Transport.Stdio.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
<Compile Include="Request\ReferencesRequest.cs" />
108108
<Compile Include="Request\ReplExecuteRequest.cs" />
109109
<Compile Include="Request\RequestBase.cs" />
110+
<Compile Include="Request\ScopesRequest.cs" />
110111
<Compile Include="Request\SetBreakpointsRequest.cs" />
111112
<Compile Include="Request\SetExceptionBreakpointsRequest.cs" />
112113
<Compile Include="Request\SourceRequest.cs" />
@@ -129,6 +130,7 @@
129130
<Compile Include="Response\OccurrencesResponse.cs" />
130131
<Compile Include="Response\QuickInfoResponse.cs" />
131132
<Compile Include="Response\ReferencesResponse.cs" />
133+
<Compile Include="Response\ScopesResponse.cs" />
132134
<Compile Include="Response\SetBreakpointsResponse.cs" />
133135
<Compile Include="Response\SetExceptionBreakpointsResponse.cs" />
134136
<Compile Include="Response\SignatureHelpResponse.cs" />

src/PowerShellEditorServices.Transport.Stdio/Request/DisconnectRequest.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
using Microsoft.PowerShell.EditorServices.Session;
1+
using Microsoft.PowerShell.EditorServices.Console;
2+
using Microsoft.PowerShell.EditorServices.Session;
23
using Microsoft.PowerShell.EditorServices.Transport.Stdio.Message;
4+
using Microsoft.PowerShell.EditorServices.Transport.Stdio.Response;
35
using Nito.AsyncEx;
6+
using System;
47
using System.Threading.Tasks;
58

69
namespace Microsoft.PowerShell.EditorServices.Transport.Stdio.Request
@@ -12,7 +15,23 @@ public override Task ProcessMessage(
1215
EditorSession editorSession,
1316
MessageWriter messageWriter)
1417
{
15-
// TODO: Do something with this!
18+
EventHandler<SessionStateChangedEventArgs> handler = null;
19+
20+
handler =
21+
async (o, e) =>
22+
{
23+
if (e.NewSessionState == Console.PowerShellSessionState.Ready)
24+
{
25+
await messageWriter.WriteMessage(new DisconnectResponse {});
26+
editorSession.PowerShellSession.SessionStateChanged -= handler;
27+
28+
// TODO: Find a way to exit more gracefully!
29+
Environment.Exit(0);
30+
}
31+
};
32+
33+
editorSession.PowerShellSession.SessionStateChanged += handler;
34+
editorSession.PowerShellSession.AbortExecution();
1635

1736
return TaskConstants.Completed;
1837
}

src/PowerShellEditorServices.Transport.Stdio/Request/EvaluateRequest.cs

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,30 @@ public override async Task ProcessMessage(
1414
EditorSession editorSession,
1515
MessageWriter messageWriter)
1616
{
17-
// TODO TODO TODO FIX THIS
18-
19-
//VariableDetails foundHeaders =
20-
// editorSession.ConsoleService.EvaluateExpression(
21-
// this.Arguments.Expression,
22-
// this.Arguments.FrameId);
23-
24-
//string valueString = null;
25-
//int variableId = 0;
26-
27-
//if (foundHeaders != null)
28-
//{
29-
// valueString = foundHeaders.ValueString;
30-
// variableId =
31-
// foundHeaders.HasChildren ?
32-
// foundHeaders.Id : 0;
33-
//}
34-
35-
//messageWriter.WriteMessage(
36-
// this.PrepareResponse(
37-
// new EvaluateResponse
38-
// {
39-
// Body = new EvaluateResponseBody
40-
// {
41-
// Result = valueString,
42-
// VariablesReference = variableId
43-
// }
44-
// }));
17+
VariableDetails result =
18+
editorSession.DebugService.EvaluateExpression(
19+
this.Arguments.Expression,
20+
this.Arguments.FrameId);
21+
22+
string valueString = null;
23+
int variableId = 0;
24+
25+
if (result != null)
26+
{
27+
valueString = result.ValueString;
28+
variableId =
29+
result.IsExpandable ?
30+
result.Id : 0;
31+
}
4532

4633
await messageWriter.WriteMessage(
4734
this.PrepareResponse(
4835
new EvaluateResponse
4936
{
5037
Body = new EvaluateResponseBody
5138
{
52-
Result = "",
53-
VariablesReference = 0
39+
Result = valueString,
40+
VariablesReference = variableId
5441
}
5542
}));
5643
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Microsoft.PowerShell.EditorServices.Console;
2+
using Microsoft.PowerShell.EditorServices.Session;
3+
using Microsoft.PowerShell.EditorServices.Transport.Stdio.Message;
4+
using Microsoft.PowerShell.EditorServices.Transport.Stdio.Model;
5+
using Microsoft.PowerShell.EditorServices.Transport.Stdio.Response;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
9+
namespace Microsoft.PowerShell.EditorServices.Transport.Stdio.Request
10+
{
11+
[MessageTypeName("scopes")]
12+
public class ScopesRequest : RequestBase<ScopesRequestArgs>
13+
{
14+
public override async Task ProcessMessage(
15+
EditorSession editorSession,
16+
MessageWriter messageWriter)
17+
{
18+
VariableScope[] variableScopes =
19+
editorSession.DebugService.GetVariableScopes(
20+
this.Arguments.FrameId);
21+
22+
await messageWriter.WriteMessage(
23+
this.PrepareResponse(
24+
new ScopesResponse
25+
{
26+
Body = new ScopesResponseBody
27+
{
28+
Scopes =
29+
variableScopes
30+
.Select(Scope.Create)
31+
.ToArray()
32+
}
33+
}));
34+
}
35+
}
36+
37+
public class ScopesRequestArgs
38+
{
39+
public int FrameId { get; set; }
40+
}
41+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Microsoft.PowerShell.EditorServices.Transport.Stdio.Message;
2+
using Microsoft.PowerShell.EditorServices.Transport.Stdio.Model;
3+
4+
namespace Microsoft.PowerShell.EditorServices.Transport.Stdio.Response
5+
{
6+
[MessageTypeName("scopes")]
7+
public class ScopesResponse : ResponseBase<ScopesResponseBody>
8+
{
9+
}
10+
11+
public class ScopesResponseBody
12+
{
13+
public Scope[] Scopes { get; set; }
14+
}
15+
}

0 commit comments

Comments
 (0)