Bump Rustyline to 7.0.0 (#2776)

* Bump Rustyline to 7.0.0

* Append history instead of always save

* Add associated type to Hinter

* Convert to using Rustyline KeyEvent

* Use AcceptOrInsertLine as struct

* Cargo fmt

* Make convert_keyevent pub

* Better naming for RL conversion
This commit is contained in:
Chris Gillespie 2020-12-04 09:29:40 -08:00 committed by GitHub
parent 12bc92df35
commit e73278990c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 109 additions and 85 deletions

43
Cargo.lock generated
View File

@ -1251,16 +1251,6 @@ dependencies = [
"dirs-sys", "dirs-sys",
] ]
[[package]]
name = "dirs-next"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf36e65a80337bea855cd4ef9b8401ffce06a7baedf2e85ec467b1ac3f6e82b6"
dependencies = [
"cfg-if 1.0.0",
"dirs-sys-next",
]
[[package]] [[package]]
name = "dirs-sys" name = "dirs-sys"
version = "0.3.5" version = "0.3.5"
@ -1272,17 +1262,6 @@ dependencies = [
"winapi 0.3.9", "winapi 0.3.9",
] ]
[[package]]
name = "dirs-sys-next"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99de365f605554ae33f115102a02057d4fc18b01f3284d6870be0938743cfe7d"
dependencies = [
"libc",
"redox_users",
"winapi 0.3.9",
]
[[package]] [[package]]
name = "doc-comment" name = "doc-comment"
version = "0.3.3" version = "0.3.3"
@ -1562,6 +1541,16 @@ dependencies = [
"percent-encoding 2.1.0", "percent-encoding 2.1.0",
] ]
[[package]]
name = "fs2"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213"
dependencies = [
"libc",
"winapi 0.3.9",
]
[[package]] [[package]]
name = "fs_extra" name = "fs_extra"
version = "1.2.0" version = "1.2.0"
@ -4826,16 +4815,18 @@ dependencies = [
[[package]] [[package]]
name = "rustyline" name = "rustyline"
version = "6.3.0" version = "7.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f0d5e7b0219a3eadd5439498525d4765c59b7c993ef0c12244865cd2d988413" checksum = "1a5f54deba50e65ee4cf786dbc37e8b3c63bdccccbcf9d3a8a9fd0c1bb7e1984"
dependencies = [ dependencies = [
"cfg-if 0.1.10", "bitflags",
"dirs-next", "cfg-if 1.0.0",
"dirs 3.0.1",
"fs2",
"libc", "libc",
"log 0.4.11", "log 0.4.11",
"memchr", "memchr",
"nix 0.18.0", "nix 0.19.0",
"scopeguard", "scopeguard",
"unicode-segmentation", "unicode-segmentation",
"unicode-width", "unicode-width",

View File

@ -75,7 +75,7 @@ rayon = "1.4.0"
regex = "1.3.9" regex = "1.3.9"
roxmltree = "0.13.0" roxmltree = "0.13.0"
rust-embed = "5.6.0" rust-embed = "5.6.0"
rustyline = {version = "6.3.0", optional = true} rustyline = {version = "7.0.0", optional = true}
serde = {version = "1.0.115", features = ["derive"]} serde = {version = "1.0.115", features = ["derive"]}
serde_bytes = "0.11.5" serde_bytes = "0.11.5"
serde_ini = "0.2.0" serde_ini = "0.2.0"

View File

@ -1,24 +1,25 @@
use crate::commands::classified::block::run_block; use crate::commands::classified::block::run_block;
use crate::commands::classified::maybe_text_codec::{MaybeTextCodec, StringOrBinary}; use crate::commands::classified::maybe_text_codec::{MaybeTextCodec, StringOrBinary};
use crate::evaluation_context::EvaluationContext; use crate::evaluation_context::EvaluationContext;
#[cfg(feature = "rustyline-support")]
use crate::keybinding::{convert_keyevent, KeyEvent};
use crate::path::canonicalize; use crate::path::canonicalize;
use crate::prelude::*; use crate::prelude::*;
#[cfg(feature = "rustyline-support")] #[cfg(feature = "rustyline-support")]
use crate::shell::Helper; use crate::shell::Helper;
use crate::EnvironmentSyncer; use crate::EnvironmentSyncer;
use futures_codec::FramedRead; use futures_codec::FramedRead;
use log::{debug, trace};
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::hir::{ClassifiedCommand, Expression, InternalCommand, Literal, NamedArguments}; use nu_protocol::hir::{ClassifiedCommand, Expression, InternalCommand, Literal, NamedArguments};
use nu_protocol::{Primitive, ReturnSuccess, Scope, UntaggedValue, Value}; use nu_protocol::{Primitive, ReturnSuccess, Scope, UntaggedValue, Value};
use log::{debug, trace};
#[cfg(feature = "rustyline-support")] #[cfg(feature = "rustyline-support")]
use rustyline::{ use rustyline::{
self, self,
config::Configurer, config::Configurer,
config::{ColorMode, CompletionType, Config}, config::{ColorMode, CompletionType, Config},
error::ReadlineError, error::ReadlineError,
At, Cmd, Editor, KeyPress, Movement, Word, At, Cmd, Editor, Movement, Word,
}; };
use std::error::Error; use std::error::Error;
use std::iter::Iterator; use std::iter::Iterator;
@ -678,16 +679,19 @@ fn default_rustyline_editor_configuration() -> Editor<Helper> {
// add key bindings to move over a whole word with Ctrl+ArrowLeft and Ctrl+ArrowRight // add key bindings to move over a whole word with Ctrl+ArrowLeft and Ctrl+ArrowRight
rl.bind_sequence( rl.bind_sequence(
KeyPress::ControlLeft, convert_keyevent(KeyEvent::ControlLeft),
Cmd::Move(Movement::BackwardWord(1, Word::Vi)), Cmd::Move(Movement::BackwardWord(1, Word::Vi)),
); );
rl.bind_sequence( rl.bind_sequence(
KeyPress::ControlRight, convert_keyevent(KeyEvent::ControlRight),
Cmd::Move(Movement::ForwardWord(1, At::AfterEnd, Word::Vi)), Cmd::Move(Movement::ForwardWord(1, At::AfterEnd, Word::Vi)),
); );
// workaround for multiline-paste hang in rustyline (see https://github.com/kkawakam/rustyline/issues/202) // workaround for multiline-paste hang in rustyline (see https://github.com/kkawakam/rustyline/issues/202)
rl.bind_sequence(KeyPress::BracketedPasteStart, rustyline::Cmd::Noop); rl.bind_sequence(
convert_keyevent(KeyEvent::BracketedPasteStart),
rustyline::Cmd::Noop,
);
// Let's set the defaults up front and then override them later if the user indicates // Let's set the defaults up front and then override them later if the user indicates
// defaults taken from here https://github.com/kkawakam/rustyline/blob/2fe886c9576c1ea13ca0e5808053ad491a6fe049/src/config.rs#L150-L167 // defaults taken from here https://github.com/kkawakam/rustyline/blob/2fe886c9576c1ea13ca0e5808053ad491a6fe049/src/config.rs#L150-L167

View File

@ -1,38 +1,64 @@
use rustyline::{KeyCode, Modifiers};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
fn convert_keypress(keypress: KeyPress) -> rustyline::KeyPress { pub fn convert_keyevent(key_event: KeyEvent) -> rustyline::KeyEvent {
match keypress { match key_event {
KeyPress::UnknownEscSeq => rustyline::KeyPress::UnknownEscSeq, KeyEvent::UnknownEscSeq => convert_to_rl_keyevent(rustyline::KeyCode::UnknownEscSeq, None),
KeyPress::Backspace => rustyline::KeyPress::Backspace, KeyEvent::Backspace => convert_to_rl_keyevent(rustyline::KeyCode::Backspace, None),
KeyPress::BackTab => rustyline::KeyPress::BackTab, KeyEvent::BackTab => convert_to_rl_keyevent(rustyline::KeyCode::BackTab, None),
KeyPress::BracketedPasteStart => rustyline::KeyPress::BracketedPasteStart, KeyEvent::BracketedPasteStart => {
KeyPress::BracketedPasteEnd => rustyline::KeyPress::BracketedPasteEnd, convert_to_rl_keyevent(rustyline::KeyCode::BracketedPasteStart, None)
KeyPress::Char(c) => rustyline::KeyPress::Char(c), }
KeyPress::ControlDown => rustyline::KeyPress::ControlDown, KeyEvent::BracketedPasteEnd => {
KeyPress::ControlLeft => rustyline::KeyPress::ControlLeft, convert_to_rl_keyevent(rustyline::KeyCode::BracketedPasteEnd, None)
KeyPress::ControlRight => rustyline::KeyPress::ControlRight, }
KeyPress::ControlUp => rustyline::KeyPress::ControlUp, KeyEvent::Char(c) => convert_to_rl_keyevent(rustyline::KeyCode::Char(c), None),
KeyPress::Ctrl(c) => rustyline::KeyPress::Ctrl(c), KeyEvent::ControlDown => {
KeyPress::Delete => rustyline::KeyPress::Delete, convert_to_rl_keyevent(rustyline::KeyCode::Down, Some(Modifiers::CTRL))
KeyPress::Down => rustyline::KeyPress::Down, }
KeyPress::End => rustyline::KeyPress::End, KeyEvent::ControlLeft => {
KeyPress::Enter => rustyline::KeyPress::Enter, convert_to_rl_keyevent(rustyline::KeyCode::Left, Some(Modifiers::CTRL))
KeyPress::Esc => rustyline::KeyPress::Esc, }
KeyPress::F(u) => rustyline::KeyPress::F(u), KeyEvent::ControlRight => {
KeyPress::Home => rustyline::KeyPress::Home, convert_to_rl_keyevent(rustyline::KeyCode::Right, Some(Modifiers::CTRL))
KeyPress::Insert => rustyline::KeyPress::Insert, }
KeyPress::Left => rustyline::KeyPress::Left, KeyEvent::ControlUp => {
KeyPress::Meta(c) => rustyline::KeyPress::Meta(c), convert_to_rl_keyevent(rustyline::KeyCode::Up, Some(Modifiers::CTRL))
KeyPress::Null => rustyline::KeyPress::Null, }
KeyPress::PageDown => rustyline::KeyPress::PageDown, KeyEvent::Ctrl(c) => rustyline::KeyEvent::ctrl(c),
KeyPress::PageUp => rustyline::KeyPress::PageUp, KeyEvent::Delete => convert_to_rl_keyevent(rustyline::KeyCode::Delete, None),
KeyPress::Right => rustyline::KeyPress::Right, KeyEvent::Down => convert_to_rl_keyevent(rustyline::KeyCode::Down, None),
KeyPress::ShiftDown => rustyline::KeyPress::ShiftDown, KeyEvent::End => convert_to_rl_keyevent(rustyline::KeyCode::End, None),
KeyPress::ShiftLeft => rustyline::KeyPress::ShiftLeft, KeyEvent::Enter => convert_to_rl_keyevent(rustyline::KeyCode::Enter, None),
KeyPress::ShiftRight => rustyline::KeyPress::ShiftRight, KeyEvent::Esc => convert_to_rl_keyevent(rustyline::KeyCode::Esc, None),
KeyPress::ShiftUp => rustyline::KeyPress::ShiftUp, KeyEvent::F(u) => convert_to_rl_keyevent(rustyline::KeyCode::F(u), None),
KeyPress::Tab => rustyline::KeyPress::Tab, KeyEvent::Home => convert_to_rl_keyevent(rustyline::KeyCode::Home, None),
KeyPress::Up => rustyline::KeyPress::Up, KeyEvent::Insert => convert_to_rl_keyevent(rustyline::KeyCode::Insert, None),
KeyEvent::Left => convert_to_rl_keyevent(rustyline::KeyCode::Left, None),
KeyEvent::Meta(c) => rustyline::KeyEvent::new(c, Modifiers::NONE),
KeyEvent::Null => convert_to_rl_keyevent(rustyline::KeyCode::Null, None),
KeyEvent::PageDown => convert_to_rl_keyevent(rustyline::KeyCode::PageDown, None),
KeyEvent::PageUp => convert_to_rl_keyevent(rustyline::KeyCode::PageUp, None),
KeyEvent::Right => convert_to_rl_keyevent(rustyline::KeyCode::Right, None),
KeyEvent::ShiftDown => {
convert_to_rl_keyevent(rustyline::KeyCode::Down, Some(Modifiers::SHIFT))
}
KeyEvent::ShiftLeft => {
convert_to_rl_keyevent(rustyline::KeyCode::Left, Some(Modifiers::SHIFT))
}
KeyEvent::ShiftRight => {
convert_to_rl_keyevent(rustyline::KeyCode::Right, Some(Modifiers::SHIFT))
}
KeyEvent::ShiftUp => convert_to_rl_keyevent(rustyline::KeyCode::Up, Some(Modifiers::SHIFT)),
KeyEvent::Tab => convert_to_rl_keyevent(rustyline::KeyCode::Tab, None),
KeyEvent::Up => convert_to_rl_keyevent(rustyline::KeyCode::Up, None),
}
}
fn convert_to_rl_keyevent(key_event: KeyCode, modifier: Option<Modifiers>) -> rustyline::KeyEvent {
rustyline::KeyEvent {
0: key_event,
1: modifier.unwrap_or(Modifiers::NONE),
} }
} }
@ -97,7 +123,9 @@ fn convert_cmd(cmd: Cmd) -> rustyline::Cmd {
match cmd { match cmd {
Cmd::Abort => rustyline::Cmd::Abort, Cmd::Abort => rustyline::Cmd::Abort,
Cmd::AcceptLine => rustyline::Cmd::AcceptLine, Cmd::AcceptLine => rustyline::Cmd::AcceptLine,
Cmd::AcceptOrInsertLine => rustyline::Cmd::AcceptOrInsertLine, Cmd::AcceptOrInsertLine => rustyline::Cmd::AcceptOrInsertLine {
accept_in_the_middle: false,
},
Cmd::BeginningOfHistory => rustyline::Cmd::BeginningOfHistory, Cmd::BeginningOfHistory => rustyline::Cmd::BeginningOfHistory,
Cmd::CapitalizeWord => rustyline::Cmd::CapitalizeWord, Cmd::CapitalizeWord => rustyline::Cmd::CapitalizeWord,
Cmd::ClearScreen => rustyline::Cmd::ClearScreen, Cmd::ClearScreen => rustyline::Cmd::ClearScreen,
@ -140,18 +168,18 @@ fn convert_cmd(cmd: Cmd) -> rustyline::Cmd {
} }
} }
fn convert_keybinding(keybinding: Keybinding) -> (rustyline::KeyPress, rustyline::Cmd) { fn convert_keybinding(keybinding: Keybinding) -> (rustyline::KeyEvent, rustyline::Cmd) {
( (
convert_keypress(keybinding.key), convert_keyevent(keybinding.key),
convert_cmd(keybinding.binding), convert_cmd(keybinding.binding),
) )
} }
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub enum KeyPress { pub enum KeyEvent {
/// Unsupported escape sequence (on unix platform) /// Unsupported escape sequence (on unix platform)
UnknownEscSeq, UnknownEscSeq,
/// ⌫ or `KeyPress::Ctrl('H')` /// ⌫ or `KeyEvent::Ctrl('H')`
Backspace, Backspace,
/// ⇤ (usually Shift-Tab) /// ⇤ (usually Shift-Tab)
BackTab, BackTab,
@ -177,9 +205,9 @@ pub enum KeyPress {
Down, Down,
/// ⇲ /// ⇲
End, End,
/// ↵ or `KeyPress::Ctrl('M')` /// ↵ or `KeyEvent::Ctrl('M')`
Enter, Enter,
/// Escape or `KeyPress::Ctrl('[')` /// Escape or `KeyEvent::Ctrl('[')`
Esc, Esc,
/// Function key /// Function key
F(u8), F(u8),
@ -191,7 +219,7 @@ pub enum KeyPress {
Left, Left,
/// Escape-char or Alt-char /// Escape-char or Alt-char
Meta(char), Meta(char),
/// `KeyPress::Char('\0')` /// `KeyEvent::Char('\0')`
Null, Null,
/// ⇟ /// ⇟
PageDown, PageDown,
@ -207,7 +235,7 @@ pub enum KeyPress {
ShiftRight, ShiftRight,
/// Shift-↑ /// Shift-↑
ShiftUp, ShiftUp,
/// ⇥ or `KeyPress::Ctrl('I')` /// ⇥ or `KeyEvent::Ctrl('I')`
Tab, Tab,
/// ↑ arrow key /// ↑ arrow key
Up, Up,
@ -399,7 +427,7 @@ pub type RepeatCount = usize;
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Keybinding { pub struct Keybinding {
key: KeyPress, key: KeyEvent,
binding: Cmd, binding: Cmd,
} }

View File

@ -62,6 +62,7 @@ impl rustyline::completion::Completer for Helper {
} }
impl rustyline::hint::Hinter for Helper { impl rustyline::hint::Hinter for Helper {
type Hint = String;
fn hint(&self, line: &str, pos: usize, ctx: &rustyline::Context<'_>) -> Option<String> { fn hint(&self, line: &str, pos: usize, ctx: &rustyline::Context<'_>) -> Option<String> {
self.hinter.as_ref().and_then(|h| h.hint(line, pos, &ctx)) self.hinter.as_ref().and_then(|h| h.hint(line, pos, &ctx))
} }

View File

@ -150,7 +150,7 @@
binding: binding:
Undo: 1 Undo: 1
# KeyPress::UnknownEscSeq => Cmd::Noop, # KeyEvent::UnknownEscSeq => Cmd::Noop,
- key: - key:
UnknownEscSeq: UnknownEscSeq:
binding: binding:
@ -161,7 +161,7 @@
########################################################## ##########################################################
# /// Unsupported escape sequence (on unix platform) # /// Unsupported escape sequence (on unix platform)
# UnknownEscSeq, # UnknownEscSeq,
# /// ⌫ or `KeyPress::Ctrl('H')` # /// ⌫ or `KeyEvent::Ctrl('H')`
# Backspace, # Backspace,
# /// ⇤ (usually Shift-Tab) # /// ⇤ (usually Shift-Tab)
# BackTab, # BackTab,
@ -187,9 +187,9 @@
# Down, # Down,
# /// ⇲ # /// ⇲
# End, # End,
# /// ↵ or `KeyPress::Ctrl('M')` # /// ↵ or `KeyEvent::Ctrl('M')`
# Enter, # Enter,
# /// Escape or `KeyPress::Ctrl('[')` # /// Escape or `KeyEvent::Ctrl('[')`
# Esc, # Esc,
# /// Function key # /// Function key
# F(u8), # F(u8),
@ -201,7 +201,7 @@
# Left, # Left,
# /// Escape-char or Alt-char # /// Escape-char or Alt-char
# Meta(char), # Meta(char),
# /// `KeyPress::Char('\0')` # /// `KeyEvent::Char('\0')`
# Null, # Null,
# /// ⇟ # /// ⇟
# PageDown, # PageDown,
@ -217,7 +217,7 @@
# ShiftRight, # ShiftRight,
# /// Shift-↑ # /// Shift-↑
# ShiftUp, # ShiftUp,
# /// ⇥ or `KeyPress::Ctrl('I')` # /// ⇥ or `KeyEvent::Ctrl('I')`
# Tab, # Tab,
# /// ↑ arrow key # /// ↑ arrow key
# Up, # Up,