vscode-powershell
vscode-powershell copied to clipboard
Intellisense for "[PSCustomObject]" leaves an extra close square bracket causing a syntax error
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.
- [ ] If this is a security issue, I have read the security issue reporting guidance.
Summary
Using Intellisesnse while typing-out [PSCustomObject], before I type the closing square brackets, an extra square bracket is appended, causing a syntax error.
PowerShell Version
$psversiontable ;host
Name Value
---- -----
PSVersion 7.4.2
PSEdition Core
GitCommitId 7.4.2
OS Microsoft Windows 10.0.20348
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Name : Visual Studio Code Host
Version : 2024.2.2
InstanceId : 795c2dfd-55fe-4258-b089-29937135df2b
UI : System.Management.Automation.Internal.Host.InternalHos
tUserInterface
CurrentCulture : en-US
CurrentUICulture : en-US
PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled : True
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace
Visual Studio Code Version
code --version
1.90.0
89de5a8d4d6205e5b11647eb6a74844ca23d2573
x64
Extension Version
code --list-extensions --show-versions | select-string powershell
[email protected]
Steps to Reproduce
Type "[PSCustom" and then press the tab key
Visuals
Pressing TAB at this point:
Causes this extra square bracket at this point:
Logs
No response
Thanks for the report! This is a snippet and will need to be addressed there, should be fairly simple. In the meantime, if you choose the option to sort snippet suggestions to bottom, the normal intellisense will apply.
Relevant snippet to fix https://github.com/PowerShell/vscode-powershell/blob/ef038c80a261b79ac18593c0ec0b218129d0bc64/snippets/PowerShell.json#L529-L540
As far as i can see, it seems the external close square bracket isnt really a bug. The snippet does not add any brackets, as far i understand the logic.
I think you have an extension that adds the right closing bracket but the cursor position doesnt go beyond it when you tab.
Try the following:
- Type an opening bracket only. If you see a closing bracket appear, start typing just ps
- Now the cursor will be positioned after the s character but before the closing bracket
- Press the tab key and you will see the issue you had in your image.
Now repeat but before you press the tab for completion, move the character one position to the right so now it is positioned on the closing bracket.
If you press the tab now, it will be expanded correctly without "extra" closing bracket.
You can also test it without using brackets, but you just can use ps by itself, but if you type psc and then tab, it will expand correctly and again not show "extra" closing bracket.
Hope this made sense.
Yes, that approach makes sense as a workaround, although it's unique compared to Intellisense's behavior in other contexts. Typically, when the suggestion box appears, pressing TAB fills in the command as expected. However, with [PSCustomObject], Intellisense's performance is inconsistent. I'll make a mental note of how to manage it in these situations. Thank you for the information; it was useful, even if not quite what I was looking for.
The intention was not necessarily to provide a workaround, but rather to show why its not really a bug. But if it helps you get the results , that's cool as well.
What you're seeing, and i do as well, is actually the expected behavior in my eyes.
You can replace the opening bracket with say @@ and typing in between them pscustomobject and then press tab for expansion and you will see that only the inside was expanded. So what we could potentially argue that the front square bracket should probably not have been replaced, however it counters line 532.
as for consistency, i think its actually behaves the same in other places where the prefix has at least 2 options, one with square brackets and one without. An example is in lines 500-512 using [SuppressMessageAttribute] And if you start with opening bracket, ending appears automatically, start typing the inside of it and tab and the extra closing bracket appear, so id say it is consistent.
Can always try to run VSCode without anything else other than the PowerShell extension and verify that nothing adds a closing brackets and test again and you will not see that closing bracket as the closing one will not appear immediately after you typed the opening one.