vscode-powershell
vscode-powershell copied to clipboard
Busy formatting PowerShell Code
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
I don't think the extension really controls when formatting happens, you may want to check these relevant VS Code settings:
editor.formatOnSaveeditor.formatOnSaveModeeditor.formatOnType
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.
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.
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
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.
I am unsure if that means it's fixable or not -- please feel free to close the issue if it is unresolvable 🚒
And thank you both for looking into it 😊
@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)!
I do, thank you! I will try the modifications setting.
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
Hey @potatoqualitee, is this still an issue for you?
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!