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

Alias expansion should move cursor to end of command name

Open quantumfrost opened this issue 10 months ago • 7 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.
  • [ ] I have read the troubleshooting guide.
  • [ ] 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

After typing an alias, if you press Alt + Shift + E to replace it with the full command name, the cursor remains at the original position. It should be moved to the end of the command so you can continue typing.

For example (| denotes the cursor): cd| > Press expansion shortcut > Se|t-Location

I cannot think of a use case where leaving the cursor in the middle of the command name would be desirable. In fact, inserting a space after the command name (if there isn't already) may be desirable too.

PowerShell Version

PowerShell 7.4.7
Ubuntu 24.04.1 LTS

Visual Studio Code Version

1.96.4

Extension Version

[email protected]

Steps to Reproduce

| denotes the cursor: cd| > Press alias expansion shortcut Alt + Shift + E > Se|t-Location

Expected: Set-Location| Perhaps also insert space after command name, if there isn't one already

Visuals

No response

Logs

No response

quantumfrost avatar Feb 03 '25 11:02 quantumfrost

Huh, yeah, you're right. That would be a bug in the expandAlias handler, most likely in the client side:

https://github.com/PowerShell/vscode-powershell/blob/31e461e3cecc0fb5b893b8dba98e2642c45b1380/src/features/ExpandAlias.ts#L50

But I'll note that the server side is in need of some TLC too:

https://github.com/PowerShell/PowerShellEditorServices/blob/e26f172efa6ee6aef1de0f64b7f2d0fbbc5d22cd/src/PowerShellEditorServices/Services/PowerShell/Handlers/ExpandAliasHandler.cs#L58

Marking this as up for grabs as it seems like a great intro bug fix that could be contributed.

Thanks for the report!

andyleejordan avatar Feb 04 '25 01:02 andyleejordan

Agreed, if anyone wants to be brave, I volunteer to help get you started :)

JustinGrote avatar Feb 04 '25 18:02 JustinGrote

Hi, I've never worked on a VSCode extension, but thought I'd give it a try.

However it looks like the command works differently from what I initially thought. I assumed that when there's no selection, it only expands the alias under the cursor. But it actually assumes you want to format the entire file. If that's the intended behavior, then it may not be a bug. Although personally, I'd prefer if it expanded the command at the cursor, so you could use it as you typed.

quantumfrost avatar Feb 06 '25 18:02 quantumfrost

I can confirm alt-shift-e expands all aliases and not just the highlighted one, that's not desired behavior and we should fix that for sure, please file a separate issue or I'll create one if I have time (I never use that command so I didn't notice)

If you want aliases expanded in general, I recommend the following settings:

"powershell.codeFormatting.autoCorrectAliases": true
"[powershell]": {
  "editor.formatOnSave": true,
  "editor.formatOnSaveMode": "modificationsIfAvailable",
  "editor.formatOnPaste": true
}

This way you can write in shorthand but all aliases will be expanded upon saving the file.

It's generally not best to mix aliases and full line in the same file, I generally recommend writing shorthand and saving longhand, which these two steps automate.

JustinGrote avatar Feb 06 '25 18:02 JustinGrote

Agreed, that's a bug. It should expand just the single alias at point. Yay bugs.

andyleejordan avatar Feb 06 '25 19:02 andyleejordan

Chatted with @SydneyhSmith, if we do fix "expand alias" to just correctly expand the alias at point, we will probably still want something akin to "expand aliases" to expand all in the document.

andyleejordan avatar Feb 11 '25 19:02 andyleejordan

Super duper funny thing: she brought up the "fix problem" from PSScriptAnalyzer...and sure enough if you use Ctrl+. to "Fix Problem" on an alias that's to be expanded, it does the one at point. So that's not even using the expand alias handler, and it's working entirely correctly (expands the alias at point, and moves the cursor). I would propose that the expand alias handler should perhaps be removed in favor of this, or at least remapped. @JustinGrote @SeeminglyScience thoughts?

andyleejordan avatar Feb 11 '25 19:02 andyleejordan