Skip to content

Commit 6a83d00

Browse files
committed
Merge pull request #74 from rkeithhill/rkeithhill/variable-typename
Addresses proposal #72 - for expandable objects, display something us…
2 parents 44a5af4 + 07dd830 commit 6a83d00

File tree

1 file changed

+76
-9
lines changed

1 file changed

+76
-9
lines changed

src/PowerShellEditorServices/Debugging/VariableDetails.cs

+76-9
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ public VariableDetails(string name, object value)
7171
this.Id = -1; // Not been assigned a variable reference id yet
7272
this.Name = name;
7373
this.IsExpandable = GetIsExpandable(value);
74-
this.ValueString =
75-
this.IsExpandable == false ?
76-
GetValueString(value) :
77-
" "; // An empty string isn't enough due to a temporary bug in VS Code.
74+
this.ValueString = GetValueString(value, this.IsExpandable);
7875
}
7976

8077
#endregion
@@ -136,12 +133,82 @@ private static bool GetIsExpandable(object valueObject)
136133
!(valueObject is string); // Strings get treated as IEnumerables
137134
}
138135

139-
private static string GetValueString(object value)
136+
private static string GetValueString(object value, bool isExpandable)
140137
{
141-
return
142-
value != null ?
143-
value.ToString() :
144-
"null";
138+
string valueString;
139+
140+
if (value == null)
141+
{
142+
valueString = "null";
143+
}
144+
else if (isExpandable)
145+
{
146+
Type objType = value.GetType();
147+
148+
// Get the "value" for an expandable object. This will either
149+
// be the short type name or the ToString() response if ToString()
150+
// responds with something other than the type name.
151+
if (value.ToString().Equals(objType.FullName))
152+
{
153+
string shortTypeName = objType.Name;
154+
155+
// For arrays and ICollection, display the number of contained items.
156+
if (value is Array)
157+
{
158+
var arr = value as Array;
159+
if (arr.Rank == 1)
160+
{
161+
shortTypeName = InsertDimensionSize(shortTypeName, arr.Length);
162+
}
163+
}
164+
else if (value is ICollection)
165+
{
166+
var collection = (ICollection)value;
167+
shortTypeName = InsertDimensionSize(shortTypeName, collection.Count);
168+
}
169+
170+
valueString = "[" + shortTypeName + "]";
171+
}
172+
else
173+
{
174+
valueString = value.ToString();
175+
}
176+
}
177+
else
178+
{
179+
if (value.GetType() == typeof(string))
180+
{
181+
valueString = "\"" + value + "\"";
182+
}
183+
else
184+
{
185+
valueString = value.ToString();
186+
}
187+
}
188+
189+
return valueString;
190+
}
191+
192+
private static string InsertDimensionSize(string value, int dimensionSize)
193+
{
194+
string result = value;
195+
196+
int indexLastRBracket = value.LastIndexOf("]");
197+
if (indexLastRBracket > 0)
198+
{
199+
result =
200+
value.Substring(0, indexLastRBracket) +
201+
dimensionSize +
202+
value.Substring(indexLastRBracket);
203+
}
204+
else
205+
{
206+
// Types like ArrayList don't use [] in type name so
207+
// display value like so - [ArrayList: 5]
208+
result = value + ": " + dimensionSize;
209+
}
210+
211+
return result;
145212
}
146213

147214
private static VariableDetails[] GetChildren(object obj)

0 commit comments

Comments
 (0)