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

Allow accepting suggestion with the EndOfLine key (End) if cursor is at the end of line #3705

Open
1 task done
Jackenmen opened this issue Jun 3, 2023 · 3 comments
Open
1 task done
Labels
Area-Predictors Label for issues related to predictors Issue-Enhancement It's a feature request.
Milestone

Comments

@Jackenmen
Copy link

Jackenmen commented Jun 3, 2023

Prerequisites

  • Write a descriptive title.

Description of the new feature/enhancement

Currently, it is only possible to accept suggestions when at the end of line if the ForwardChar (right arrow) key is hit. I think it would also make sense to treat the EndOfLine (End) key the same way. I know that this is how zsh-autosuggestions work, I haven't tested if fish autosuggestions also work like that but I feel like it's an intuitive behavior since you're trying to go to the end of the suggested line. I'm sure it is possible with a script block (I haven't yet looked into how but probably will soon) but I think that this would be a sensible default to have.

Arguably, the End key makes more sense than the right arrow key, and the right arrow key could be used for accepting the next word but that's really just my opinion which doesn't really matter when there are already so many users that are used to this. + It is possible to change it with the provided ForwardCharAndAcceptNextSuggestionWord example.

Proposed technical implementation details (optional)

No response

[Edited by @daxian-dbw] Added one upvote to represent the duplicate issue #3741

@Jackenmen Jackenmen added the Issue-Enhancement It's a feature request. label Jun 3, 2023
@ghost ghost added the Needs-Triage 🔍 It's a new issue that core contributor team needs to triage. label Jun 3, 2023
@StevenBucher98
Copy link
Collaborator

Thanks for the issue @Jackenmen! You change change the AcceptSuggestion function to the End key to accept the suggestion. I do not know the exact key name of the End key but if you run [System.Console]::ReadKey(), press the end key and use that "Key" property to fill in this commend Set-PSReadLineKeyHandler -Chord '<END KEY PROPERTY>' -Function MenuComplete. I am restricted to a laptop currently so don't have that key to test myself 😄 Let me know if that works for you!

@StevenBucher98 StevenBucher98 added Needs-Author Feedback and removed Needs-Triage 🔍 It's a new issue that core contributor team needs to triage. labels Jun 4, 2023
@Jackenmen
Copy link
Author

I was asking for a change in the default behavior of the End key such that:

  • it moves the cursor to the end of the current editing line, if it isn't already there
  • it accepts the suggestion, otherwise (i.e. when the cursor is at the end of the current editing line)

This would make it act more like the RightArrow key works:

  • it moves the cursor one character to the right, if it isn't at the end of the current editing line
  • it accepts the suggestion, otherwise (i.e. when the cursor is at the end of the current editing line)

Right now, I use this custom key handler to do this:

Set-PSReadLineKeyHandler -Key "End" `
        -BriefDescription ForwardCharAndAcceptNextSuggestionWord `
        -LongDescription "Move the cursor to the end of the current editing line or accept the suggestion if it's already there" `
        -ScriptBlock {
    param($key, $arg)

    $line = $null
    $cursor = $null
    [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
    if ($cursor -lt $line.Length) {
        [Microsoft.PowerShell.PSConsoleReadLine]::EndOfLine($key, $arg)
    } else {
        [Microsoft.PowerShell.PSConsoleReadLine]::AcceptSuggestion($key, $arg)
    }
}

but as I described in the issue, I think that this would be a sensible default to have which is why I'm requesting it. Personally, I feel like it's a weird inconsistency considering that both the right arrow and the End key are used for cursor movement.

@Jackenmen
Copy link
Author

The relevant functions:

/// <summary>
/// If the input has multiple lines, move to the end of the current line,
/// or if already at the end of the line, move to the end of the input.
/// If the input has a single line, move to the end of the input.
/// </summary>
public static void EndOfLine(ConsoleKeyInfo? key = null, object arg = null)
{
int i = _singleton._current;
for (; i < _singleton._buffer.Length; i++)
{
if (_singleton._buffer[i] == '\n')
{
break;
}
}
_singleton.MoveCursor((i == _singleton._current) ? _singleton._buffer.Length : i);
}

/// <summary>
/// Move the cursor one character to the right. This may move the cursor to the next
/// line of multi-line input.
/// </summary>
public static void ForwardChar(ConsoleKeyInfo? key = null, object arg = null)
{
if (TryGetArgAsInt(arg, out var numericArg, 1))
{
if (_singleton._current == _singleton._buffer.Length && numericArg > 0)
{
AcceptSuggestion(key, arg);
}
else
{
SetCursorPosition(_singleton._current + numericArg);
}
}
}

@StevenBucher98 StevenBucher98 added the Area-Predictors Label for issues related to predictors label Aug 14, 2023
@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs-Triage 🔍 It's a new issue that core contributor team needs to triage. label Aug 14, 2023
@StevenBucher98 StevenBucher98 removed Needs-Triage 🔍 It's a new issue that core contributor team needs to triage. Needs-Attention 👋 labels Aug 21, 2023
@StevenBucher98 StevenBucher98 added this to the Future milestone Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Predictors Label for issues related to predictors Issue-Enhancement It's a feature request.
Projects
None yet
Development

No branches or pull requests

2 participants