From 957cc592a6e4974efbefefd98aed904c206677ad Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 27 Sep 2020 10:34:03 -0700 Subject: [PATCH] wezterm: allow using `{}` (an empty table) to represent None in lua `wezterm.action{ExtendSelectionToMouseCursor=nil}` doesn't produce an `ExtendSelectionToMouseCursor(None)` value because a table value of `nil` is equivalent to that key not being present and lua sees just an empty table. Instead we need to accept `ExtendSelectionToMouseCursor={}` a valid way to indicate an `Option::None` which is what this commit does. Due to weirdness that I haven't had a chance to run down, passing that value through `wezterm.action` doesn't produce the intended value, so I'm adjusting the docs to show to specify an alternative syntax for this as part of this commit. refs: https://github.com/wez/wezterm/issues/282 --- docs/config/keys.markdown | 21 ++++++++++++++++++++- src/scripting/serde_lua/mod.rs | 32 +++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/docs/config/keys.markdown b/docs/config/keys.markdown index 7c313d67c..badd9c2cf 100644 --- a/docs/config/keys.markdown +++ b/docs/config/keys.markdown @@ -158,7 +158,7 @@ that order. | Triple Left Down | `NONE` | `SelectTextAtMouseCursor="Line"` | | Double Left Down | `NONE` | `SelectTextAtMouseCursor="Word"` | | Single Left Down | `NONE` | `SelectTextAtMouseCursor="Cell"` | -| Single Left Down | `SHIFT` | `ExtendSelectionToMouseCursor=nil` | +| Single Left Down | `SHIFT` | `ExtendSelectionToMouseCursor={}` | | Single Left Up | `NONE` | `CompleteSelectionOrOpenLinkAtMouseCursor` | | Double Left Up | `NONE` | `CompleteSelection` | | Triple Left Up | `NONE` | `CompleteSelection` | @@ -735,6 +735,25 @@ Extends the current text selection to the current mouse cursor position. The mode argument can be one of `Cell`, `Word` or `Line` to control the scope of the selection. +It is also possible to leave the mode unspecified like this: + +```lua +return { + mouse_bindings = { + { + event={Up={streak=1, button="Left"}}, + mods="SHIFT", + -- Note that there is no `wezterm.action` here + action=ExtendSelectionToMouseCursor={}, + }, + } +} +``` + +when unspecified, wezterm will use a default mode which at the time +of writing is `Cell`, but in a future release may be context sensitive +based on recent actions. + ## OpenLinkAtMouseCursor If the current mouse cursor position is over a cell that contains diff --git a/src/scripting/serde_lua/mod.rs b/src/scripting/serde_lua/mod.rs index e0a751b60..6b5875ee1 100644 --- a/src/scripting/serde_lua/mod.rs +++ b/src/scripting/serde_lua/mod.rs @@ -403,8 +403,16 @@ impl<'de, 'lua> Deserializer<'de> for ValueWrapper<'lua> { where V: Visitor<'de>, { - match self.0 { + match &self.0 { Value::Nil => v.visit_none(), + Value::Table(t) => { + let mut iter = t.clone().pairs::(); + if iter.next().is_none() { + v.visit_none() + } else { + v.visit_some(self) + } + } _ => v.visit_some(self), } } @@ -962,4 +970,26 @@ mod test { unknown variant `Invalid`, expected `Foo` or `Bar`", ); } + + #[test] + fn test_option_mode() { + #[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)] + enum MyEnum { + Foo, + Bar, + }; + #[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)] + enum AnotherEnum { + ThisOne(Option), + }; + + let lua = Lua::new(); + let res: AnotherEnum = + from_lua_value(lua.load("{ThisOne=\"Foo\"}").eval().unwrap()).unwrap(); + assert_eq!(res, AnotherEnum::ThisOne(Some(MyEnum::Foo))); + round_trip(res); + + let res: AnotherEnum = from_lua_value(lua.load("{ThisOne={}}").eval().unwrap()).unwrap(); + assert_eq!(res, AnotherEnum::ThisOne(None)); + } }