PowerShell / vscode-powershell

Provides PowerShell language and debugging support for Visual Studio Code
https://marketplace.visualstudio.com/items/ms-vscode.PowerShell
MIT License
1.7k stars 488 forks source link

Support Advanced Hovers #3026

Open JustinGrote opened 3 years ago

JustinGrote commented 3 years ago

Summary of the new feature

The existing powershell hover for commands/functions is very basic and only provides the synopsis. VScode now supports colorized advanced hovers, and Omnisharp C# extension already implements this as a reference. https://code.visualstudio.com/api/language-extensions/programmatic-language-features#show-hovers image

Powershell should support the same for functions and methods, displaying colorized syntax info about the function, and ideally not just the synopsis but parameter descriptions. The verbosity could be controlled via a setting.

Proposed technical implementation details (optional)

This should be done after the parser rewrite. It should probably be tagged a good-first-issue as well, I'd be willing to give it a shot once the parser rewrite is done to avoid a moving target.

This could also be a good candidate as a separate extension for the extension API that @TylerLeonhardt was working on, though it may be difficult to make sure the two don't clobber each other.

MartinGC94 commented 9 months ago

It would also be nice if hovering over a variable/property would show the type like Visual Studio does. If the variable/property doesn't currently have a value the internal PowerShell type inference code could be used.

JustinGrote commented 9 months ago

@MartinGC94 that feature would be better suited as an inlay hint but I agree, we could definitely offer a high confidence and best effort inference here, it's one of my to-dos after inline breakpointing.

https://www.youtube.com/watch?v=uvrIFZYW7eg

Also I'm going to use this opportunity to MASSIVELY thank you for all of your completion and autocomplete fixups you've done over the past year or two, they make my quality of life so much better :)

MartinGC94 commented 9 months ago

I hadn't heard about inlay hints before. They seem neat, though I'd still prefer the hover tooltips but maybe I'll change my mind if I try to use those hints.

Also I'm going to use this opportunity to MASSIVELY thank you for all of your completion and autocomplete fixups you've done over the past year or two, they make my quality of life so much better :)

Thanks, it's been a great way for me to sharpen my C# skills and I of course also love the UX improvements myself.

JustinGrote commented 9 months ago

@MartinGC94 they are awesome and fully implemented in Typescript/F#/C# extensions. Try opening a file and hitting ctrl-alt. You can also configure them to be persistently visible for various types.

JustinGrote commented 8 months ago

@MartinGC94 I have a basic prototype wired up here: https://github.com/PowerShell/PowerShellEditorServices/pull/2133

Can you point me to docs or the API that is the best way to find the inferred types of variables and parameters in PowerShell? Ideally one that does a single pass of a whole document/script as efficiently as possible rather than a ton of individual AST visits.

EDIT: I realized I can sort of look at how the current completion handler works for properties/etc. to determine the symbol type and steal that for now for a proof of concept.

JustinGrote commented 8 months ago

OK, after some experimentation and discussion with @SeeminglyScience, doing inlay hints is probably not feasible as of PS7.4, as the current implementation would basically have to get a CompletionResult for pretty much every token (we could potentially do some smart scope deduplication) that is on the screen and refresh all of it either when the document changes or the view changes (if we limit completions to just what is on screen)

Until PowerShell has a performant API that can provide a visitor for gathering this kind of info, it's doable but it would be slow AF and not super useful.

Enhancing the hovers however is definitely doable and something I'll explore some more. I may scaffold out the inlay hover further as experimental for both variables and parameters and that way the completion engine can just get swapped out when we have a better/faster way.

andyleejordan commented 8 months ago

I would love to see the existing hovers made better. Not sure if you saw https://github.com/PowerShell/PowerShellEditorServices/pull/2127. There's a lot we could do.

JustinGrote commented 8 months ago

@andyleejordan you probably sent it to me and it fell off my to-look list :)

JustinGrote commented 1 day ago

I was investigating this a but further today.

While we could theoretically offer this today, unfortunately to do inline hints "simply", requires running completion for every variable/parameter symbol in the document, which even if we defer in the pipeline, will result in a lot of compute and while fine for relatively small scripts, would end up being very slow for large scripts. We would be able to optimize by potentially only doing deltas after that, but it's still really heavyweight by virtue of that it would have to run within the runspace.

The alternative is utilize several of the additions from the rename provider to do static AST analysis. For example:

[int]$x

$x <-- can show int as inlay hint

We effectively expand on the rename FindVariableDefinition functions and, if there is a hard type defined, we can relatively safely display inlay hints for subsequent references. This would be another "best effort" feature, and we could still implement the inlay hint feature but maybe cut it off at only the visible document and refresh when requested.

I'll experiment with this more.