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

Custom View not working as expected #91

Open
TMA-2 opened this issue Nov 2, 2024 · 0 comments
Open

Custom View not working as expected #91

TMA-2 opened this issue Nov 2, 2024 · 0 comments

Comments

@TMA-2
Copy link

TMA-2 commented Nov 2, 2024

Describe the bug

In VSCode, when attempting to use the cmdlets Register-VSCodeTreeView and New-VSCodeTreeViewItem, unexpected behavior occurs. The parent TreeView will usually be created, but it will often show up blank (see screenshots). However, any child items, whether created dynamically i.e. using the pipeline and automatic variables $_ or $PSItem, or with static values, I've never had them show up. If they do, they're almost always blank, only visible thanks to the last item having a visible collapse control (>) if -HasChildren was used. No other icon, description, or tooltip appears, however.

I realize the product has been sunsetted, and Universal is receiving more support at the moment, but the documentation around this feature is sorely lacking in explanation, only containing two example uses and a short video. Worse still, the examples provided, copied verbatim, don't work as shown. However, if support is to continue for a year, I'd very much like this fixed, as PowerShell Pro Tools is basically the best PowerShell value-add for VSCode beyond the official extension (echoing Justin Grote's comments in issue #43 ).

I really love the idea of a custom treeview, as I've wanted to create a better in-app help system for cmdlets, so short of learning Typescript and creating my own extension (a tall ask as I'm more scripter than actual programmer), this seemed like the perfect middle ground to iterate commands by module and verb, and then invoke the online link or Get-Help -ShowWindow, or possibly display the associated markdown from the PS Github in the case of about_ topics. Alas, I've gotten the same results across three devices (see below for version details).

Of course, it's entirely possible I don't really understand how these cmdlets and their associated types work, but when the examples fail, something must be wrong.

Expected behavior

Essentially, exactly what is pictured in the video at the feature's documentation page. I'm only trying to get the samples working at this point before doing anything more involved.

Actual behavior

Either a single parent item is created with no visible children (see screenshot) — sometimes with no visible text — or with blank child items, where only the last TreeItem in the list is selectable/expandable, but still blank otherwise. Clicking Refresh, or directly calling [PowerShellToolsPro.VSCode.TreeViewService]::Instance.RefreshTreeView(treeViewId) will often do nothing, and sometimes on the second attempt it clears all items from the view and will not accept further additions, rendering it unusable until the extension is restarted. This is beyond aggravating, especially because I can call [PowerShellToolsPro.VSCode.TreeViewService]::Instance.GetTreeViews() and see the view(s) I created with their correct properties listed.

Even more strangely, despite seeing the TreeView items exist as mentioned above, the log output often produces an exception on refresh, mentioning "Sequence contains no elements". Calling the same method directly produces an InvalidOperation exception:

InvalidOperation: An error occurred while enumerating through a collection: Object reference not set to an instance of an object..

Output Log when Refreshing the Custom View

[20:11:41.912] Scheduling command for main runspace: [PowerShellToolsPro.VSCode.TreeViewService]::Instance.GetTreeViews() | ConvertTo-Json -WarningAction SilentlyContinue
[20:11:41.925] Scheduling command for main runspace: [PowerShellToolsPro.VSCode.TreeViewService]::Instance.LoadChildren('TreeView1', '') | ConvertTo-Json -WarningAction SilentlyContinue
[20:11:42.249] Exception running command: $LoadChildren System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.InvalidOperationException: Sequence contains no elements
   at System.Linq.ThrowHelper.ThrowNoElementsException()
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at PowerShellToolsPro.PoshToolsServer.LoadChildren(String treeViewId, String path) in D:\a\powershell-pro-tools\powershell-pro-tools\HostInjection\PoshToolsServer.cs:line 768
   at InvokeStub_PoshToolsServer.LoadChildren(Object, Span`1)
   at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   --- End of inner exception stack trace ---
   at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at PowerShellToolsPro.PoshToolsServer.Start(String pipeName) in D:\a\powershell-pro-tools\powershell-pro-tools\HostInjection\PoshToolsServer.cs:line 130

Version

Personal Tablet

  • PowerShell v7.4.6
  • VS Code 1.95.1
  • Extension: PowerShell Extension v2024.5.0 (pre-release)
  • Extension: PowerShell Pro Tools 2024.7.6
  • Windows 11 Home 10.0.22631

Personal Desktop

  • PowerShell v7.4.6
  • VS Code 1.93.1
  • Extension: PowerShell Extension v2024.3.2 (pre-release)
  • Extension: PowerShell Pro Tools 2024.7.6
  • Windows 10 Pro 10.0.19045

Work VM

  • PowerShell v7.4.6
  • VS Code 1.95.0
  • Extension: PowerShell Extension v2024.5.0 (pre-release)
  • Extension: PowerShell Pro Tools 2024.7.6
  • Windows 10 Enterprise 10.0.19045

Steps to Reproduce

Steps to reproduce the behavior:

  1. Go to the documentation on custom tree views
  2. Copy either the first or second example.
  3. Paste into a new .ps1 file in VS Code and save.
  4. Run the script with F5.

The output I was able to get once with the following code is also pictured, which is the closest I've gotten to anything usable. However, after running it once, even after refreshing, I could no longer get it to work. Only the initial TreeView would show with no child TreeItems.

$PSCmds = Get-Command -Module PowerShellProTools.VSCode | Group-Object -Property Verb
Register-VSCodeTreeView -Label 'PowerShellProToolsVSCode' -Description "$($PSCmds.Count) items" -Icon 'archive' -Tooltip "Cmdlets in PowerShellProTools.VSCode" -LoadChildren {
    $PSCmds | ForEach-Object {
        New-VSCodeTreeItem -Label $_.Name -Description "$($_.Count) items" -Icon 'list-tree' -HasChildren -DisableInvoke
        foreach($pscmd in $_.Group) {
            New-VSCodeTreeItem -Label $pscmd.Name -Description $pscmd.CommandType -Icon 'terminal' -Tooltip $pscmd.Definition
        }
    }
} -InvokeChild {
    Get-Help $args[0].Label -ShowWindow
}

Screenshots

The typical result when trying to add a TreeView

PSPT-CustomView-BlankItems

The one time I was able to get visible, invokable child TreeItems

PSPT-CustomView-CommandList-HalfBlank

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant