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

Tab autocomplete in a Cmdlet param context #2928

Closed
3 tasks done
mavaddat opened this issue Oct 22, 2021 · 4 comments · Fixed by #2949
Closed
3 tasks done

Tab autocomplete in a Cmdlet param context #2928

mavaddat opened this issue Oct 22, 2021 · 4 comments · Fixed by #2949
Labels
Issue-Bug It either shouldn't be doing this or needs an investigation. Resolution-Fixed

Comments

@mavaddat
Copy link

mavaddat commented Oct 22, 2021

Prerequisites

  • Write a descriptive title.
  • Make sure you are able to repro it on the latest released version
  • Search the existing issues, especially the pinned issues.

Exception report

### Environment
PSReadLine: 2.2.0-beta3
PowerShell: 7.1.5
OS: Microsoft Windows 10.0.22483
BufferWidth: 133
BufferHeight: 30

Last 200 Keys

 Spacebar Enter
 $ i Tab n s Enter
 Spacebar - r e p l Tab Spacebar " " LeftArrow C : \ \ U s e r s \ \ m a v a d Ctrl+LeftArrow Ctrl+LeftArrow Ctrl+LeftArrow RightArrow RightArrow \ \ Ctrl+RightArrow RightArrow RightArrow RightArrow RightArrow RightArrow \ \ Ctrl+RightArrow Ctrl+RightArrow Ctrl+RightArrow , " " LeftArrow $ e n v : H O M E Tab RightArrow Enter
 Ctrl+LeftArrow Ctrl+LeftArrow ` Ctrl+RightArrow Ctrl+LeftArrow Ctrl+RightArrow Ctrl+RightArrow LeftArrow Enter
 UpArrow Spacebar | Spacebar s e t - c l i Tab Enter
 UpArrow UpArrow UpArrow ) Home Ctrl+RightArrow Ctrl+RightArrow Ctrl+LeftArrow ( End Spacebar - r e p Tab Spacebar Spacebar " C : \ \ \ \ U s e r s \ \ \ \ m a v a d " , " ` $ e n v : H O M E P A T H " Ctrl+LeftArrow Ctrl+LeftArrow Ctrl+LeftArrow Ctrl+LeftArrow Ctrl+LeftArrow Shift+Ctrl+RightArrow Shift+Ctrl+RightArrow Shift+Ctrl+RightArrow Shift+LeftArrow Shift+LeftArrow Shift+LeftArrow Delete $ e n v : H O M E Tab RightArrow Enter
 Ctrl+LeftArrow Ctrl+LeftArrow Enter
 UpArrow Ctrl+LeftArrow Ctrl+LeftArrow Ctrl+LeftArrow Ctrl+LeftArrow Backspace Ctrl+RightArrow Ctrl+RightArrow LeftArrow LeftArrow LeftArrow Delete Spacebar LeftArrow Ctrl+LeftArrow Ctrl+LeftArrow LeftArrow Spacebar [ ] LeftArrow r e g Tab

### Exception
System.ArgumentOutOfRangeException: The value must be greater than or equal to zero and less than the console's buffer size in that dimension. (Parameter 'top')
Actual value was 30.
   at System.ConsolePal.SetCursorPosition(Int32 left, Int32 top)
   at Microsoft.PowerShell.PSConsoleReadLine.Menu.DrawMenu(Menu previousMenu, Boolean menuSelect)
   at Microsoft.PowerShell.PSConsoleReadLine.MenuCompleteImpl(Menu menu, CommandCompletion completions)
   at Microsoft.PowerShell.PSConsoleReadLine.PossibleCompletionsImpl(CommandCompletion completions, Boolean menuSelect)
   at Microsoft.PowerShell.PSConsoleReadLine.CompleteImpl(Boolean menuSelect)
   at Microsoft.PowerShell.PSConsoleReadLine.MenuComplete(Nullable`1 key, Object arg)
   at Microsoft.PowerShell.PSConsoleReadLine.ProcessOneKey(PSKeyInfo key, Dictionary`2 dispatchTable, Boolean ignoreIfNoAction, Object arg)
   at Microsoft.PowerShell.PSConsoleReadLine.InputLoop()
   at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics, CancellationToken cancellationToken, Nullable`1 lastRunStatus)

Screenshot

I attempted to tab here.
Tab attempted at this position of the line

Environment data

PS Version: 7.1.5
PS HostName: ConsoleHost (Windows Terminal)
PSReadLine Version: 2.2.0-beta3
PSReadLine EditMode: Windows
OS: 10.0.22483.1000 (WinBuild.160101.0800)
BufferWidth: 133
BufferHeight: 30

Steps to reproduce

To get the error in the first place, I recalled the following into Pwsh without pressing enter (by pressing up arrow to recall last command):

$instMods = (Get-InstalledModule  | ConvertTo-Html | pandoc.exe --from=html-auto_identifiers-native_spans-native_divs --to=gfm-gfm_auto_identifiers) -replace  [reg] $env:HOMEPATH ,"`$env:HOMEPATH"

Nav back to the position at reg inside [reg] $env and press tab.

Expected behavior

Autocomplete regex assembly inside square brackets.

Actual behavior

System.ArgumentOutOfRangeException

@ghost ghost added the Needs-Triage 🔍 It's a new issue that core contributor team needs to triage. label Oct 22, 2021
@daxian-dbw
Copy link
Member

daxian-dbw commented Nov 4, 2021

Note that, the prompt length is very important to reproduce this issue locally. Also, the prompt should be at the last line of the screen buffer to reproduce this issue. Repro steps:

  1. Set the Windows Terminal buffer size to -- BufferWidth: 133, BufferHeight: 30.
  2. Run the following function to set the new prompt:
    function prompt { "PS:12                                                             > " }
    
  3. Press Enter a number of times until the prompt is at the last line of the terminal window.
  4. Copy and paste the following command to the terminal: $instMods = (Get-InstalledModule | ConvertTo-Html | pandoc12.exe --from=html-auto_identifiers-native_spans-native_divs --to=gfm-gfm_auto_identifiers) -replace [reg] $env:HOMEPATH ,"`$env:HOMEPATH"
  5. Move cursor to the closing bracket ] of the [reg].
  6. Press Ctrl+Space to trigger the menu completion

Here is the GIF for reproducing this:

Animation


The root cause of this issue lies at the following code:

var bufferEndPoint = Singleton.ConvertOffsetToPoint(Singleton._buffer.Length);
console.SetCursorPosition(bufferEndPoint.X, bufferEndPoint.Y);

In this case, the bufferEndPoint happens to point to the start of the next line off the buffer. We need to scroll the buffer in this case before setting the cursor position.

@daxian-dbw daxian-dbw added Issue-Bug It either shouldn't be doing this or needs an investigation. and removed Needs-Triage 🔍 It's a new issue that core contributor team needs to triage. labels Nov 4, 2021
@mavaddat
Copy link
Author

mavaddat commented Nov 4, 2021

Note that, the prompt length is very important to reproduce this issue locally. Also, the prompt should be at the last line of the screen buffer to reproduce this issue.

Nicely done. Impressive that you were able to find the nuanced conditions to reproduce this!

@ghost ghost added the In-PR A PR is opened targeting the issue label Nov 4, 2021
@daxian-dbw daxian-dbw added this to the 2.2.0-Consider milestone Nov 4, 2021
@daxian-dbw
Copy link
Member

@mavaddat Thank you for including so much details in your issue report! That's very helpful for us to fix those stability issues.

@ghost
Copy link

ghost commented Jan 7, 2022

🎉 This issue was addressed in 2949, which has now been successfully released in v2.2.0-beta5. 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug It either shouldn't be doing this or needs an investigation. Resolution-Fixed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants