Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alias expansion should move cursor to end of command name #5136

Open
3 of 6 tasks
quantumfrost opened this issue Feb 3, 2025 · 7 comments
Open
3 of 6 tasks

Alias expansion should move cursor to end of command name #5136

quantumfrost opened this issue Feb 3, 2025 · 7 comments
Labels
Issue-Bug A bug to squash. Up for Grabs Will shepherd PRs.

Comments

@quantumfrost
Copy link

quantumfrost commented Feb 3, 2025

Prerequisites

  • I have written a descriptive issue title.
  • 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.
  • 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

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 quantumfrost added Issue-Bug A bug to squash. Needs: Triage Maintainer attention needed! labels Feb 3, 2025
@andyleejordan
Copy link
Member

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

editBuilder.replace(range, result.text);

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 andyleejordan added Up for Grabs Will shepherd PRs. and removed Needs: Triage Maintainer attention needed! labels Feb 4, 2025
@andyleejordan andyleejordan moved this to Wishlist in Flying Fox Feb 4, 2025
@JustinGrote
Copy link
Collaborator

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

@quantumfrost
Copy link
Author

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.

@JustinGrote
Copy link
Collaborator

JustinGrote commented Feb 6, 2025

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.

@andyleejordan
Copy link
Member

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

@andyleejordan
Copy link
Member

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
Copy link
Member

andyleejordan commented Feb 11, 2025

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug A bug to squash. Up for Grabs Will shepherd PRs.
Projects
Status: Wishlist
Development

No branches or pull requests

3 participants