Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs (C#)

Code before:

                foreach (var childName in childNameList)
                {
                    if (!pattern.IsMatch(childName))
                    {
                        continue;
                    }

                    CompletionResultType resultType;
                    if (childrenInfoTable.TryGetValue(childName, out bool isContainer))
                    {
                        if (containersOnly && !isContainer)
                        {
                            continue;
                        }

                        resultType = isContainer
                            ? CompletionResultType.ProviderContainer
                            : CompletionResultType.ProviderItem;
                    }
                    else
                    {
                        resultType = CompletionResultType.Text;
                    }

                    bool leafQuotesNeeded;
                    var completionText = NewPathCompletionText(
                        basePath,
                        EscapePath(childName, stringType, literalPaths, out leafQuotesNeeded),
                        stringType,
                        containsNestedExpressions: false,
                        forceQuotes: baseQuotesNeeded || leafQuotesNeeded,
                        addAmpersand: false);
                    results.Add(new CompletionResult(completionText, childName, resultType, baseTooltip + childName));
                }
            }

            return results;
        }

        private static string GetChildNameFromPsObject(dynamic psObject, char separator)
        {
            // The obvious solution would be to use the "PSChildName" property
            // but some providers don't have it (like the Variable provider)
            // So we use a substring of "PSPath" instead.
            string childName = psObject.PSPath ?? string.Empty;
            int ProviderSeparatorIndex = childName.IndexOf("::", StringComparison.Ordinal);
            childName = childName.Substring(ProviderSeparatorIndex + 2);
            int indexOfName = childName.LastIndexOf(separator);
            if (indexOfName == -1 || indexOfName + 1 == childName.Length)
            {

Review comment (suggestion):
```suggestion
                return psObject.ToString();
```

Code after:
                    {
                        resultType = CompletionResultType.Text;
                    }

                    bool leafQuotesNeeded;
                    var completionText = NewPathCompletionText(
                        basePath,
                        EscapePath(childName, stringType, literalPaths, out leafQuotesNeeded),
                        stringType,
                        containsNestedExpressions: false,
                        forceQuotes: baseQuotesNeeded || leafQuotesNeeded,
                        addAmpersand: false);
                    results.Add(new CompletionResult(completionText, childName, resultType, baseTooltip + childName));
                }
            }

            return results;
        }

        private static string GetChildNameFromPsObject(dynamic psObject, char separator)
        {
            if (((PSObject)psObject).BaseObject is string result)
            {
                // The "Get-ChildItem" call for this provider returned a string that we assume is the child name.
                // This is what the SCCM provider returns.
                return result;
            }

            string childName = psObject.PSChildName;
            if (childName is not null)
            {
                return childName;
            }

            // Some providers (Like the variable provider) don't include a PSChildName property
            // so we get the child name from the path instead.
            childName = psObject.PSPath ?? string.Empty;
            int ProviderSeparatorIndex = childName.IndexOf("::", StringComparison.Ordinal);
            childName = childName.Substring(ProviderSeparatorIndex + 2);
            int indexOfName = childName.LastIndexOf(separator);
            if (indexOfName == -1 || indexOfName + 1 == childName.Length)
            {
                return childName;
            }

            return childName.Substring(indexOfName + 1);
        }

        /// <summary>
        /// Takes a path and rebuilds it with the specified variable replacements.
        /// Also escapes special characters as needed.