Skip to content

Commit 91e209f

Browse files
rkeithhilldaviwil
authored andcommitted
Initial start of using Type field on Variables.
1 parent edca175 commit 91e209f

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

src/PowerShellEditorServices.Protocol/DebugAdapter/Variable.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public class Variable
1212
// /** The variable's value. For structured objects this can be a multi line text, e.g. for a function the body of a function. */
1313
public string Value { get; set; }
1414

15+
/// <summary>
16+
/// Gets or sets the type of the variable's value. Typically shown in the UI when hovering over the value.
17+
/// </summary>
18+
public string Type { get; set; }
19+
1520
/// <summary>
1621
/// Gets or sets the evaluatable name for the variable that will be evaluated by the debugger.
1722
/// </summary>
@@ -26,6 +31,7 @@ public static Variable Create(VariableDetailsBase variable)
2631
{
2732
Name = variable.Name,
2833
Value = variable.ValueString ?? string.Empty,
34+
Type = variable.Type,
2935
EvaluateName = variable.Name,
3036
VariablesReference =
3137
variable.IsExpandable ?

src/PowerShellEditorServices/Debugging/DebugService.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,12 @@ public VariableDetailsBase[] GetVariables(int variableReferenceId)
363363
{
364364
VariableDetailsBase[] childVariables;
365365

366+
if ((variableReferenceId < 0) || (variableReferenceId >= this.variables.Count))
367+
{
368+
logger.Write(LogLevel.Warning, $"Received request for variableReferenceId {variableReferenceId} that is out of range of valid indices.");
369+
return new VariableDetailsBase[0];
370+
}
371+
366372
VariableDetailsBase parentVariable = this.variables[variableReferenceId];
367373
if (parentVariable.IsExpandable)
368374
{

src/PowerShellEditorServices/Debugging/VariableDetails.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ public VariableDetails(string name, object value)
8787
this.Id = -1; // Not been assigned a variable reference id yet
8888
this.Name = name;
8989
this.IsExpandable = GetIsExpandable(value);
90-
this.ValueString = GetValueString(value, this.IsExpandable);
90+
91+
string typeName;
92+
this.ValueString = GetValueStringAndType(value, this.IsExpandable, out typeName);
93+
this.Type = typeName;
9194
}
9295

9396
#endregion
@@ -154,23 +157,27 @@ private static bool GetIsExpandable(object valueObject)
154157
!(valueObject is UnableToRetrievePropertyMessage);
155158
}
156159

157-
private static string GetValueString(object value, bool isExpandable)
160+
private static string GetValueStringAndType(object value, bool isExpandable, out string typeName)
158161
{
159-
string valueString;
162+
string valueString = null;
163+
typeName = null;
160164

161165
if (value == null)
162166
{
163167
// Set to identifier recognized by PowerShell to make setVariable from the debug UI more natural.
164-
valueString = "$null";
168+
return "$null";
165169
}
166-
else if (value is bool)
170+
171+
Type objType = value.GetType();
172+
typeName = $"[{objType.FullName}]";
173+
174+
if (value is bool)
167175
{
168176
// Set to identifier recognized by PowerShell to make setVariable from the debug UI more natural.
169177
valueString = (bool) value ? "$true" : "$false";
170178
}
171179
else if (isExpandable)
172180
{
173-
Type objType = value.GetType();
174181

175182
// Get the "value" for an expandable object.
176183
if (value is DictionaryEntry)
@@ -181,12 +188,11 @@ private static string GetValueString(object value, bool isExpandable)
181188
string.Format(
182189
"[{0}, {1}]",
183190
entry.Key,
184-
GetValueString(entry.Value, GetIsExpandable(entry.Value)));
191+
GetValueStringAndType(entry.Value, GetIsExpandable(entry.Value), out typeName));
185192
}
186193
else
187194
{
188195
string valueToString = value.SafeToString();
189-
190196
if (valueToString.Equals(objType.ToString()))
191197
{
192198
// If the ToString() matches the type name, then display the type
@@ -208,7 +214,7 @@ private static string GetValueString(object value, bool isExpandable)
208214
shortTypeName = InsertDimensionSize(shortTypeName, collection.Count);
209215
}
210216

211-
valueString = "[" + shortTypeName + "]";
217+
valueString = $"[{shortTypeName}]";
212218
}
213219
else
214220
{

src/PowerShellEditorServices/Debugging/VariableDetailsBase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public abstract class VariableDetailsBase
3838
/// </summary>
3939
public string ValueString { get; protected set; }
4040

41+
/// <summary>
42+
/// Gets the type of the variable's value.
43+
/// </summary>
44+
public string Type { get; protected set; }
45+
4146
/// <summary>
4247
/// Returns true if the variable's value is expandable, meaning
4348
/// that it has child properties or its contents can be enumerated.

0 commit comments

Comments
 (0)