From aa96a1769d28bb55211480597a92a691b3f7633d Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Sat, 17 Jun 2017 11:56:01 -0600 Subject: [PATCH] Initial start of using Type field on Variables. --- .../DebugAdapter/Variable.cs | 6 +++++ .../Debugging/DebugService.cs | 6 +++++ .../Debugging/VariableDetails.cs | 24 ++++++++++++------- .../Debugging/VariableDetailsBase.cs | 5 ++++ 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/PowerShellEditorServices.Protocol/DebugAdapter/Variable.cs b/src/PowerShellEditorServices.Protocol/DebugAdapter/Variable.cs index cf944b506..a335b0975 100644 --- a/src/PowerShellEditorServices.Protocol/DebugAdapter/Variable.cs +++ b/src/PowerShellEditorServices.Protocol/DebugAdapter/Variable.cs @@ -12,6 +12,11 @@ public class Variable // /** The variable's value. For structured objects this can be a multi line text, e.g. for a function the body of a function. */ public string Value { get; set; } + /// + /// Gets or sets the type of the variable's value. Typically shown in the UI when hovering over the value. + /// + public string Type { get; set; } + /// /// Gets or sets the evaluatable name for the variable that will be evaluated by the debugger. /// @@ -26,6 +31,7 @@ public static Variable Create(VariableDetailsBase variable) { Name = variable.Name, Value = variable.ValueString ?? string.Empty, + Type = variable.Type, EvaluateName = variable.Name, VariablesReference = variable.IsExpandable ? diff --git a/src/PowerShellEditorServices/Debugging/DebugService.cs b/src/PowerShellEditorServices/Debugging/DebugService.cs index ceb83bc28..74c053bad 100644 --- a/src/PowerShellEditorServices/Debugging/DebugService.cs +++ b/src/PowerShellEditorServices/Debugging/DebugService.cs @@ -363,6 +363,12 @@ public VariableDetailsBase[] GetVariables(int variableReferenceId) { VariableDetailsBase[] childVariables; + if ((variableReferenceId < 0) || (variableReferenceId >= this.variables.Count)) + { + logger.Write(LogLevel.Warning, $"Received request for variableReferenceId {variableReferenceId} that is out of range of valid indices."); + return new VariableDetailsBase[0]; + } + VariableDetailsBase parentVariable = this.variables[variableReferenceId]; if (parentVariable.IsExpandable) { diff --git a/src/PowerShellEditorServices/Debugging/VariableDetails.cs b/src/PowerShellEditorServices/Debugging/VariableDetails.cs index 342f045a5..25ee71057 100644 --- a/src/PowerShellEditorServices/Debugging/VariableDetails.cs +++ b/src/PowerShellEditorServices/Debugging/VariableDetails.cs @@ -87,7 +87,10 @@ public VariableDetails(string name, object value) this.Id = -1; // Not been assigned a variable reference id yet this.Name = name; this.IsExpandable = GetIsExpandable(value); - this.ValueString = GetValueString(value, this.IsExpandable); + + string typeName; + this.ValueString = GetValueStringAndType(value, this.IsExpandable, out typeName); + this.Type = typeName; } #endregion @@ -154,23 +157,27 @@ private static bool GetIsExpandable(object valueObject) !(valueObject is UnableToRetrievePropertyMessage); } - private static string GetValueString(object value, bool isExpandable) + private static string GetValueStringAndType(object value, bool isExpandable, out string typeName) { - string valueString; + string valueString = null; + typeName = null; if (value == null) { // Set to identifier recognized by PowerShell to make setVariable from the debug UI more natural. - valueString = "$null"; + return "$null"; } - else if (value is bool) + + Type objType = value.GetType(); + typeName = $"[{objType.FullName}]"; + + if (value is bool) { // Set to identifier recognized by PowerShell to make setVariable from the debug UI more natural. valueString = (bool) value ? "$true" : "$false"; } else if (isExpandable) { - Type objType = value.GetType(); // Get the "value" for an expandable object. if (value is DictionaryEntry) @@ -181,12 +188,11 @@ private static string GetValueString(object value, bool isExpandable) string.Format( "[{0}, {1}]", entry.Key, - GetValueString(entry.Value, GetIsExpandable(entry.Value))); + GetValueStringAndType(entry.Value, GetIsExpandable(entry.Value), out typeName)); } else { string valueToString = value.SafeToString(); - if (valueToString.Equals(objType.ToString())) { // If the ToString() matches the type name, then display the type @@ -208,7 +214,7 @@ private static string GetValueString(object value, bool isExpandable) shortTypeName = InsertDimensionSize(shortTypeName, collection.Count); } - valueString = "[" + shortTypeName + "]"; + valueString = $"[{shortTypeName}]"; } else { diff --git a/src/PowerShellEditorServices/Debugging/VariableDetailsBase.cs b/src/PowerShellEditorServices/Debugging/VariableDetailsBase.cs index eb1f27c8f..aada558fa 100644 --- a/src/PowerShellEditorServices/Debugging/VariableDetailsBase.cs +++ b/src/PowerShellEditorServices/Debugging/VariableDetailsBase.cs @@ -38,6 +38,11 @@ public abstract class VariableDetailsBase /// public string ValueString { get; protected set; } + /// + /// Gets the type of the variable's value. + /// + public string Type { get; protected set; } + /// /// Returns true if the variable's value is expandable, meaning /// that it has child properties or its contents can be enumerated.