Skip to content

Fix for vscode-powershell issue 168. #216

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

Merged
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
46 changes: 26 additions & 20 deletions src/PowerShellEditorServices/Debugging/VariableDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq;
using System.Management.Automation;
using System.Reflection;
using Microsoft.PowerShell.EditorServices.Utility;

namespace Microsoft.PowerShell.EditorServices
{
Expand Down Expand Up @@ -159,44 +160,49 @@ private static string GetValueString(object value, bool isExpandable)
entry.Key,
GetValueString(entry.Value, GetIsExpandable(entry.Value)));
}
else if (value.ToString().Equals(objType.ToString()))
else
{
// If the ToString() matches the type name, then display the type
// name in PowerShell format.
string shortTypeName = objType.Name;
string valueToString = value.SafeToString();

// For arrays and ICollection, display the number of contained items.
if (value is Array)
if (valueToString.Equals(objType.ToString()))
{
var arr = value as Array;
if (arr.Rank == 1)
// If the ToString() matches the type name, then display the type
// name in PowerShell format.
string shortTypeName = objType.Name;

// For arrays and ICollection, display the number of contained items.
if (value is Array)
{
shortTypeName = InsertDimensionSize(shortTypeName, arr.Length);
var arr = value as Array;
if (arr.Rank == 1)
{
shortTypeName = InsertDimensionSize(shortTypeName, arr.Length);
}
}
else if (value is ICollection)
{
var collection = (ICollection)value;
shortTypeName = InsertDimensionSize(shortTypeName, collection.Count);
}

valueString = "[" + shortTypeName + "]";
}
else if (value is ICollection)
else
{
var collection = (ICollection)value;
shortTypeName = InsertDimensionSize(shortTypeName, collection.Count);
valueString = valueToString;
}

valueString = "[" + shortTypeName + "]";
}
else
{
valueString = value.ToString();
}
}
else
{
// ToString() output is not the typename, so display that as this object's value
// Value is a scalar (not expandable). If it's a string, display it directly otherwise use SafeToString()
if (value is string)
{
valueString = "\"" + value + "\"";
}
else
{
valueString = value.ToString();
valueString = value.SafeToString();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
<Compile Include="Utility\AsyncLock.cs" />
<Compile Include="Utility\AsyncQueue.cs" />
<Compile Include="Utility\AsyncContext.cs" />
<Compile Include="Utility\Extensions.cs" />
<Compile Include="Utility\Logger.cs" />
<Compile Include="Utility\ThreadSynchronizationContext.cs" />
<Compile Include="Utility\Validate.cs" />
Expand Down
34 changes: 34 additions & 0 deletions src/PowerShellEditorServices/Utility/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System;

namespace Microsoft.PowerShell.EditorServices.Utility
{
internal static class ObjectExtensions
{
/// <summary>
/// Extension to evaluate an object's ToString() method in an exception safe way. This will
/// extension method will not throw.
/// </summary>
/// <param name="obj">The object on which to call ToString()</param>
/// <returns>The ToString() return value or a suitable error message is that throws.</returns>
public static string SafeToString(this object obj)
{
string str;

try
{
str = obj.ToString();
}
catch (Exception ex)
{
str = $"<Error converting poperty value to string - {ex.Message}>";
}

return str;
}
}
}