Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Type field for Variables. #535

Merged
merged 1 commit into from
Jun 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

/// <summary>
/// Gets or sets the type of the variable's value. Typically shown in the UI when hovering over the value.
/// </summary>
public string Type { get; set; }

/// <summary>
/// Gets or sets the evaluatable name for the variable that will be evaluated by the debugger.
/// </summary>
Expand All @@ -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 ?
Expand Down
6 changes: 6 additions & 0 deletions src/PowerShellEditorServices/Debugging/DebugService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
24 changes: 15 additions & 9 deletions src/PowerShellEditorServices/Debugging/VariableDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -208,7 +214,7 @@ private static string GetValueString(object value, bool isExpandable)
shortTypeName = InsertDimensionSize(shortTypeName, collection.Count);
}

valueString = "[" + shortTypeName + "]";
valueString = $"[{shortTypeName}]";
}
else
{
Expand Down
5 changes: 5 additions & 0 deletions src/PowerShellEditorServices/Debugging/VariableDetailsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public abstract class VariableDetailsBase
/// </summary>
public string ValueString { get; protected set; }

/// <summary>
/// Gets the type of the variable's value.
/// </summary>
public string Type { get; protected set; }

/// <summary>
/// Returns true if the variable's value is expandable, meaning
/// that it has child properties or its contents can be enumerated.
Expand Down