mirror of
https://github.com/nushell/nushell
synced 2024-12-27 05:23:11 +00:00
allow list to keybinding mode (#4821)
* allow list to keybinding mode * added comments to default.nu
This commit is contained in:
parent
90b2ec537f
commit
5fcc670860
3 changed files with 69 additions and 36 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -3402,7 +3402,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "reedline"
|
name = "reedline"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
source = "git+https://github.com/nushell/reedline?branch=main#87a32d51476222026ed514da11914de6cb3059ab"
|
source = "git+https://github.com/nushell/reedline?branch=main#4e5a20d127d84f4722e1d39d1852ccb42aa973f6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"crossterm",
|
"crossterm",
|
||||||
|
|
|
@ -198,7 +198,7 @@ let $config = {
|
||||||
name: completion_menu
|
name: completion_menu
|
||||||
modifier: none
|
modifier: none
|
||||||
keycode: tab
|
keycode: tab
|
||||||
mode: emacs # emacs vi_normal vi_insert
|
mode: emacs # Options: emacs vi_normal vi_insert
|
||||||
event: {
|
event: {
|
||||||
until: [
|
until: [
|
||||||
{ send: menu name: completion_menu }
|
{ send: menu name: completion_menu }
|
||||||
|
@ -210,14 +210,14 @@ let $config = {
|
||||||
name: completion_previous
|
name: completion_previous
|
||||||
modifier: shift
|
modifier: shift
|
||||||
keycode: backtab
|
keycode: backtab
|
||||||
mode: emacs # emacs vi_normal vi_insert
|
mode: [emacs, vi_normal, vi_insert] # Note: You can add the same keybinding to all modes by using a list
|
||||||
event: { send: menuprevious }
|
event: { send: menuprevious }
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
name: history_menu
|
name: history_menu
|
||||||
modifier: control
|
modifier: control
|
||||||
keycode: char_x
|
keycode: char_x
|
||||||
mode: emacs # emacs vi_normal vi_insert
|
mode: emacs
|
||||||
event: {
|
event: {
|
||||||
until: [
|
until: [
|
||||||
{ send: menu name: history_menu }
|
{ send: menu name: history_menu }
|
||||||
|
@ -229,7 +229,7 @@ let $config = {
|
||||||
name: history_previous
|
name: history_previous
|
||||||
modifier: control
|
modifier: control
|
||||||
keycode: char_z
|
keycode: char_z
|
||||||
mode: emacs # emacs vi_normal vi_insert
|
mode: emacs
|
||||||
event: {
|
event: {
|
||||||
until: [
|
until: [
|
||||||
{ send: menupageprevious }
|
{ send: menupageprevious }
|
||||||
|
|
|
@ -168,35 +168,32 @@ pub enum KeybindingsMode {
|
||||||
|
|
||||||
pub(crate) fn create_keybindings(config: &Config) -> Result<KeybindingsMode, ShellError> {
|
pub(crate) fn create_keybindings(config: &Config) -> Result<KeybindingsMode, ShellError> {
|
||||||
let parsed_keybindings = &config.keybindings;
|
let parsed_keybindings = &config.keybindings;
|
||||||
|
|
||||||
|
let mut emacs_keybindings = default_emacs_keybindings();
|
||||||
|
let mut insert_keybindings = default_vi_insert_keybindings();
|
||||||
|
let mut normal_keybindings = default_vi_normal_keybindings();
|
||||||
|
|
||||||
|
for keybinding in parsed_keybindings {
|
||||||
|
add_keybinding(
|
||||||
|
&keybinding.mode,
|
||||||
|
keybinding,
|
||||||
|
config,
|
||||||
|
&mut emacs_keybindings,
|
||||||
|
&mut insert_keybindings,
|
||||||
|
&mut normal_keybindings,
|
||||||
|
)?
|
||||||
|
}
|
||||||
|
|
||||||
match config.edit_mode.as_str() {
|
match config.edit_mode.as_str() {
|
||||||
"emacs" => {
|
"emacs" => {
|
||||||
let mut keybindings = default_emacs_keybindings();
|
add_menu_keybindings(&mut emacs_keybindings);
|
||||||
|
|
||||||
add_menu_keybindings(&mut keybindings);
|
Ok(KeybindingsMode::Emacs(emacs_keybindings))
|
||||||
|
|
||||||
for parsed_keybinding in parsed_keybindings {
|
|
||||||
if parsed_keybinding.mode.into_string("", config).as_str() == "emacs" {
|
|
||||||
add_keybinding(&mut keybindings, parsed_keybinding, config)?
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(KeybindingsMode::Emacs(keybindings))
|
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let mut insert_keybindings = default_vi_insert_keybindings();
|
|
||||||
let mut normal_keybindings = default_vi_normal_keybindings();
|
|
||||||
|
|
||||||
add_menu_keybindings(&mut insert_keybindings);
|
add_menu_keybindings(&mut insert_keybindings);
|
||||||
add_menu_keybindings(&mut normal_keybindings);
|
add_menu_keybindings(&mut normal_keybindings);
|
||||||
|
|
||||||
for parsed_keybinding in parsed_keybindings {
|
|
||||||
if parsed_keybinding.mode.into_string("", config).as_str() == "vi_insert" {
|
|
||||||
add_keybinding(&mut insert_keybindings, parsed_keybinding, config)?
|
|
||||||
} else if parsed_keybinding.mode.into_string("", config).as_str() == "vi_normal" {
|
|
||||||
add_keybinding(&mut normal_keybindings, parsed_keybinding, config)?
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(KeybindingsMode::Vi {
|
Ok(KeybindingsMode::Vi {
|
||||||
insert_keybindings,
|
insert_keybindings,
|
||||||
normal_keybindings,
|
normal_keybindings,
|
||||||
|
@ -206,6 +203,47 @@ pub(crate) fn create_keybindings(config: &Config) -> Result<KeybindingsMode, She
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_keybinding(
|
fn add_keybinding(
|
||||||
|
mode: &Value,
|
||||||
|
keybinding: &ParsedKeybinding,
|
||||||
|
config: &Config,
|
||||||
|
emacs_keybindings: &mut Keybindings,
|
||||||
|
insert_keybindings: &mut Keybindings,
|
||||||
|
normal_keybindings: &mut Keybindings,
|
||||||
|
) -> Result<(), ShellError> {
|
||||||
|
match &mode {
|
||||||
|
Value::String { val, span } => match val.as_str() {
|
||||||
|
"emacs" => add_parsed_keybinding(emacs_keybindings, keybinding, config),
|
||||||
|
"vi_insert" => add_parsed_keybinding(insert_keybindings, keybinding, config),
|
||||||
|
"vi_normal" => add_parsed_keybinding(normal_keybindings, keybinding, config),
|
||||||
|
m => Err(ShellError::UnsupportedConfigValue(
|
||||||
|
"emacs, vi_insert or vi_normal".to_string(),
|
||||||
|
m.to_string(),
|
||||||
|
*span,
|
||||||
|
)),
|
||||||
|
},
|
||||||
|
Value::List { vals, .. } => {
|
||||||
|
for inner_mode in vals {
|
||||||
|
add_keybinding(
|
||||||
|
inner_mode,
|
||||||
|
keybinding,
|
||||||
|
config,
|
||||||
|
emacs_keybindings,
|
||||||
|
insert_keybindings,
|
||||||
|
normal_keybindings,
|
||||||
|
)?
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
v => Err(ShellError::UnsupportedConfigValue(
|
||||||
|
"string or list of strings".to_string(),
|
||||||
|
v.into_abbreviated_string(config),
|
||||||
|
v.span()?,
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_parsed_keybinding(
|
||||||
keybindings: &mut Keybindings,
|
keybindings: &mut Keybindings,
|
||||||
keybinding: &ParsedKeybinding,
|
keybinding: &ParsedKeybinding,
|
||||||
config: &Config,
|
config: &Config,
|
||||||
|
@ -403,16 +441,11 @@ fn event_from_record(
|
||||||
let menu = extract_value("name", cols, vals, span)?;
|
let menu = extract_value("name", cols, vals, span)?;
|
||||||
Ok(ReedlineEvent::Menu(menu.into_string("", config)))
|
Ok(ReedlineEvent::Menu(menu.into_string("", config)))
|
||||||
}
|
}
|
||||||
"edit" => {
|
"executehostcommand" => {
|
||||||
let edit_command = parse_edit(
|
let cmd = extract_value("cmd", cols, vals, span)?;
|
||||||
&Value::Record {
|
Ok(ReedlineEvent::ExecuteHostCommand(
|
||||||
cols: cols.to_vec(),
|
cmd.into_string("", config),
|
||||||
vals: vals.to_vec(),
|
))
|
||||||
span: *span,
|
|
||||||
},
|
|
||||||
config,
|
|
||||||
)?;
|
|
||||||
Ok(ReedlineEvent::Edit(vec![edit_command]))
|
|
||||||
}
|
}
|
||||||
v => Err(ShellError::UnsupportedConfigValue(
|
v => Err(ShellError::UnsupportedConfigValue(
|
||||||
"Reedline event".to_string(),
|
"Reedline event".to_string(),
|
||||||
|
|
Loading…
Reference in a new issue