vscode-powershell icon indicating copy to clipboard operation
vscode-powershell copied to clipboard

Busy formatting PowerShell Code

Open potatoqualitee opened this issue 3 years ago • 2 comments

Prerequisites

  • [X] I have written a descriptive issue title.
  • [X] I have searched all open and closed issues to ensure it has not already been reported.
  • [X] I have read the troubleshooting guide.
  • [X] I am sure this issue is with the extension itself and does not reproduce in a standalone PowerShell instance.
  • [X] I have verified that I am using the latest version of Visual Studio Code and the PowerShell extension.
  • [X] If this is a security issue, I have read the security issue reporting guidance.

Summary

Hello, I continue to get the prompt where I have to wait for my PowerShell file to save because it's formatting. But I don't notice it's happening because I quickly switch to my powershell.exe window and I wonder why my command isn't reflecting the changes. Can you perhaps save the file first, unformatted, then try with the formatting in the background?

Thank you

PowerShell Version

Name                           Value                                                         
----                           -----                                                         
PSVersion                      5.1.22621.169                                                 
PSEdition                      Desktop                                                       
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                       
BuildVersion                   10.0.22621.169                                                
CLRVersion                     4.0.30319.42000                                               
WSManStackVersion              3.0                                                           
PSRemotingProtocolVersion      2.3                                                           
SerializationVersion           1.1.0.1

Visual Studio Code Version

1.71.1
e7f30e38c5a4efafeec8ad52861eb772a9ee4dfb
x64

Extension Version

[email protected]

Steps to Reproduce

spend a while editing ps1 files in vs code

Visuals

No response

Logs

No response

potatoqualitee avatar Sep 15 '22 09:09 potatoqualitee

I don't think the extension really controls when formatting happens, you may want to check these relevant VS Code settings:

  • editor.formatOnSave
  • editor.formatOnSaveMode
  • editor.formatOnType

andyleejordan avatar Sep 15 '22 16:09 andyleejordan

thank you. that makes sense and I do have formatOnSave checked, which is my preference. do you know if this can be fixed? I've encountered it for years.

potatoqualitee avatar Sep 15 '22 18:09 potatoqualitee

I'm not sure, it would have to be upstream in VS Code itself, the extension doesn't have any control over that, it just runs the formatter when asked and returns the results.

andyleejordan avatar Sep 30 '22 20:09 andyleejordan

I get from a high level that the editor must not allow to make changes any more once formatting has started but one should still be able to read/browser the code, not sure if it's a lack of control on their side. @andschwa could you give a pointer please where formatting is handled here before it calls PowerShellEditorServices to make the Invoke-Formatter request? Maybe a better API could be called, the below blog is quite old but shows better and worse ways of calling the vs-code API: https://code.visualstudio.com/blogs/2016/11/15/formatters-best-practices

bergmeister avatar Oct 03 '22 15:10 bergmeister

I spent the morning finding an answer to your question @bergmeister, and there isn't anywhere client-side (e.g. in this repository) that formatting is setup. Instead, the formatting capability is both registered and handled in by PSES here: https://github.com/PowerShell/PowerShellEditorServices/blob/main/src/PowerShellEditorServices/Services/TextDocument/Handlers/FormattingHandlers.cs

What happens is that when the client connects to the server, this capability gets registered by the server, informing the client that formatting is supported. In this manner, it is essentially the same as the "right" way documented in that best practices document; that is, the client does not create a manual formatting command. It is a proper LSP capability.

Though it doesn't explicitly use the client-side registerDocumentFormattingEditProvider API, the end result, to my understanding, is the same.

andyleejordan avatar Oct 03 '22 22:10 andyleejordan

I am unsure if that means it's fixable or not -- please feel free to close the issue if it is unresolvable 🚒

potatoqualitee avatar Oct 04 '22 09:10 potatoqualitee

And thank you both for looking into it 😊

potatoqualitee avatar Oct 04 '22 09:10 potatoqualitee

@potatoqualitee what do you have set for editor.formatOnSaveMode? I think (but am not certain) that we support formatting just modifications, which should be quicker (if it works)!

andyleejordan avatar Oct 06 '22 19:10 andyleejordan

I do, thank you! I will try the modifications setting.

potatoqualitee avatar Oct 07 '22 01:10 potatoqualitee

Maybe related, I've noticed the formatter seems to hang a lot more often than it used to, noticed it in the last month or so. I do format on save only on modifications as well

JustinGrote avatar Nov 18 '22 21:11 JustinGrote

Hey @potatoqualitee, is this still an issue for you?

andyleejordan avatar Feb 10 '23 18:02 andyleejordan

This issue was closed automatically as author feedback was indicated as needed, but there has been no activity in over a week. Please feel free to reopen with any available information!

ghost avatar Feb 18 '23 21:02 ghost