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

Support Partial Completion #513

Merged
merged 1 commit into from
May 2, 2021

Conversation

schrieveslaach
Copy link
Contributor

This commit adds the ability to complete the current line with the word of the hint. When the cursor is at the end of the current line and CTRL + right arrow has been pressed, the next word of the hint will be added to the editor's line.

Closes #413

@gwenn
Copy link
Collaborator

gwenn commented Apr 13, 2021

You should be able to almost do it by yourself with rustyline >= 8 without hardcoding a nth binding in rustyline.
Except maybe this limitation:

                Some(Cmd::CompleteHint) // TODO give access to hint

@schrieveslaach
Copy link
Contributor Author

@gwenn, do you suggest that I should extend ConditionalEventHandler or one of the parameters of handle in a way so that a custom handler access the hint to return Some(Cmd::Insert(…, …))?

This commit exposes the hint text to `ConditionalEventHandler` through
`EventContext` so that custom event handler can implement partial completion.
This feature is demonstrated in `custom_key_bindings`, fixing an open TODO.
@schrieveslaach schrieveslaach force-pushed the complete-hint-with-next-word branch from 67afb01 to a3f20c0 Compare April 14, 2021 05:35
@schrieveslaach
Copy link
Contributor Author

I found a solution that fixes the open TODO with a minimal extension of the public API. Please, have another look.

@gwenn
Copy link
Collaborator

gwenn commented Apr 14, 2021

Perfect !

@schrieveslaach
Copy link
Contributor Author

@gwenn, do you see any chance to merge this PR?

@gwenn gwenn merged commit 2f47843 into kkawakam:master May 2, 2021
@schrieveslaach schrieveslaach deleted the complete-hint-with-next-word branch May 3, 2021 08:02
@schrieveslaach
Copy link
Contributor Author

Thanks for merging. 👍 😎

@sophiajt
Copy link

@gwenn - do you know when the next rustyline will get released? We'd like to be able to use this functionality in Nushell, when it's available

@gwenn
Copy link
Collaborator

gwenn commented May 17, 2021

@jonathandturner 8.1.0 is ready (but not published yet). Could you please confirm that you don't have any building issue on windows related to this change ?

@sophiajt
Copy link

@gwenn - it's working for me in Windows

@gwenn
Copy link
Collaborator

gwenn commented May 18, 2021

@jonathandturner Version 8.1.0 released

@sophiajt
Copy link

@gwenn great, thanks!

@fdncred
Copy link

fdncred commented May 19, 2021

@gwenn Do you, or anyone else listening, have the ability to test this in MacOS? I have a mac and cannot get it to work. I originally thought that MacOS was eating the commands ctrl+right and ctrl+left because when I use those keystrokes it changes to the next virutal desktop. So, I disabled those keystrokes in the MacOS preferences. But when I tried again, it still didn't work. I'm just wondering if anyone else has this experience and what they did to fix it?

I also tried cmd+right/left and option+right/left to no avail.

@ryanpcmcquen
Copy link

Yeah, I am really excited for this but can't seem to get it working on Mac OS. Option + left/right would be ideal (that is what Fish uses).

@gwenn
Copy link
Collaborator

gwenn commented May 19, 2021

@fdncred You can trace key events (or unsupported ANSI escape sequences) easily by using RUST_LOG=rustyline=debug cargo run --example example 2> debug.log. Currently, on MacOS (BigSur), there is no event with iTerm or Terminal.
See https://superuser.com/questions/205470/why-is-ctrl-arrow-not-working-in-bash-on-os-x.
Maybe we should try and see if crossterm or termwiz or ... manages to catch these keys.

@gwenn
Copy link
Collaborator

gwenn commented May 19, 2021

crossterm % cargo run --example event-read => no event
termwiz % cargo run --example key_tester => no event
For iTerm, Cmd+Left / Right are used to switch between tabs.

@fdncred
Copy link

fdncred commented May 19, 2021

@fdncred You can trace key events (or unsupported ANSI escape sequences) easily by using RUST_LOG=rustyline=debug cargo run --example example 2> debug.log. Currently, on MacOS (BigSur), there is no event with iTerm or Terminal.
See https://superuser.com/questions/205470/why-is-ctrl-arrow-not-working-in-bash-on-os-x.
Maybe we should try and see if crossterm or termwiz or ... manages to catch these keys.

This is what I get using the command you specified. It launched and I did:
control+left
control+right
alt/option+left
alt/option+right
command+left
command+right
ctrl+c

It doesn't look like all my keystrokes were captured.

[DEBUG rustyline::history] PathInfo("history.txt", SystemTime { tv_sec: 1621454572, tv_nsec: 410962579 }, 1)
[DEBUG rustyline::edit] old layout: Layout { prompt_size: Position { col: 0, row: 0 }, default_prompt: false, cursor: Position { col: 0, row: 0 }, end: Position { col: 0, row: 0 } }
[DEBUG rustyline::edit] new layout: Layout { prompt_size: Position { col: 3, row: 0 }, default_prompt: true, cursor: Position { col: 3, row: 0 }, end: Position { col: 3, row: 0 } }
[DEBUG rustyline::tty::unix] key: KeyEvent(Left, ALT)
[DEBUG rustyline::keymap] Emacs command: Move(BackwardWord(1, Emacs))
[DEBUG rustyline::tty::unix] key: KeyEvent(Right, ALT)
[DEBUG rustyline::keymap] Emacs command: Move(ForwardWord(1, AfterEnd, Emacs))
[DEBUG rustyline::tty::unix] key: KeyEvent(Left, NONE)
[DEBUG rustyline::keymap] Emacs command: Move(BackwardChar(1))
[DEBUG rustyline::tty::unix] key: KeyEvent(Right, NONE)
[DEBUG rustyline::keymap] Emacs command: Move(ForwardChar(1))
[DEBUG rustyline::tty::unix] key: KeyEvent(Char('C'), CTRL)
[DEBUG rustyline::keymap] Emacs command: Interrupt

I also have this in my ~/.inputrc just trying out the different options.

"\e[5C": forward-word
"\e[5D": backward-word
"\e[1;5C": forward-word
"\e[1;5D": backward-word
"\e\e[C": forward-word
"\e\e[D": backward-word

@gwenn
Copy link
Collaborator

gwenn commented May 20, 2021

@fdncred If you see no KeyEvent (even UnknownEscSeq), it means that nothing is read from stdin.

@fdncred
Copy link

fdncred commented May 20, 2021

I guess that's what's happening because there are no debug logs for control+right and control+left. So, is the theory that MacOS is eating those keystrokes or is it iTerm2?

@ryanpcmcquen
Copy link

Any workarounds on MacOS yet?

@gwenn
Copy link
Collaborator

gwenn commented Jun 21, 2021

For MacOS and ctrl-left / ctrl-right, see https://superuser.com/a/357394:

System Preferences → Keyboard → Shortcuts → Mission Control

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support partial completions
5 participants