Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-view/src/input.rs')
-rw-r--r--helix-view/src/input.rs284
1 files changed, 14 insertions, 270 deletions
diff --git a/helix-view/src/input.rs b/helix-view/src/input.rs
index 539680a6..bda0520e 100644
--- a/helix-view/src/input.rs
+++ b/helix-view/src/input.rs
@@ -1,4 +1,4 @@
-//! Input event handling, currently backed by termina.
+//! Input event handling, currently backed by crossterm.
use anyhow::{anyhow, Error};
use helix_core::unicode::{segmentation::UnicodeSegmentation, width::UnicodeWidthStr};
use serde::de::{self, Deserialize, Deserializer};
@@ -43,10 +43,6 @@ pub enum MouseEventKind {
ScrollDown,
/// Scrolled mouse wheel upwards (away from the user).
ScrollUp,
- /// Scrolled mouse wheel leftwards.
- ScrollLeft,
- /// Scrolled mouse wheel rightwards.
- ScrollRight,
}
/// Represents a mouse button.
@@ -65,7 +61,7 @@ pub enum MouseButton {
pub struct KeyEvent {
pub code: KeyCode,
pub modifiers: KeyModifiers,
- // TODO: termina now supports kind & state if terminal supports kitty's extended protocol
+ // TODO: crossterm now supports kind & state if terminal supports kitty's extended protocol
}
impl KeyEvent {
@@ -162,12 +158,7 @@ pub(crate) mod keys {
impl fmt::Display for KeyEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
- "{}{}{}{}",
- if self.modifiers.contains(KeyModifiers::SUPER) {
- "Meta-"
- } else {
- ""
- },
+ "{}{}{}",
if self.modifiers.contains(KeyModifiers::SHIFT) {
"S-"
} else {
@@ -317,10 +308,6 @@ impl UnicodeWidthStr for KeyEvent {
if self.modifiers.contains(KeyModifiers::CONTROL) {
width += 2;
}
- if self.modifiers.contains(KeyModifiers::SUPER) {
- // "-Meta"
- width += 5;
- }
width
}
@@ -334,7 +321,7 @@ impl std::str::FromStr for KeyEvent {
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut tokens: Vec<_> = s.split('-').collect();
- let mut code = match tokens.pop().ok_or_else(|| anyhow!("Missing key code"))? {
+ let code = match tokens.pop().ok_or_else(|| anyhow!("Missing key code"))? {
keys::BACKSPACE => KeyCode::Backspace,
keys::ENTER => KeyCode::Enter,
keys::LEFT => KeyCode::Left,
@@ -392,27 +379,10 @@ impl std::str::FromStr for KeyEvent {
function if function.len() > 1 && function.starts_with('F') => {
let function: String = function.chars().skip(1).collect();
let function = str::parse::<u8>(&function)?;
- (function > 0 && function < 25)
- .then_some(KeyCode::F(function))
+ (function > 0 && function < 13)
+ .then(|| KeyCode::F(function))
.ok_or_else(|| anyhow!("Invalid function key '{}'", function))?
}
- // Checking that the last token is empty ensures that this branch is only taken if
- // `-` is used as a code. For example this branch will not be taken for `S-` (which is
- // missing a code).
- _ if s.ends_with('-') && tokens.last().is_some_and(|t| t.is_empty()) => {
- if s == "-" {
- return Ok(KeyEvent {
- code: KeyCode::Char('-'),
- modifiers: KeyModifiers::empty(),
- });
- } else {
- let suggestion = format!("{}-{}", s.trim_end_matches('-'), keys::MINUS);
- return Err(anyhow!(
- "Key '-' cannot be used with modifiers, use '{}' instead",
- suggestion
- ));
- }
- }
invalid => return Err(anyhow!("Invalid key code '{}'", invalid)),
};
@@ -422,7 +392,6 @@ impl std::str::FromStr for KeyEvent {
"S" => KeyModifiers::SHIFT,
"A" => KeyModifiers::ALT,
"C" => KeyModifiers::CONTROL,
- "Meta" | "Cmd" | "Win" => KeyModifiers::SUPER,
_ => return Err(anyhow!("Invalid key modifier '{}-'", token)),
};
@@ -432,18 +401,6 @@ impl std::str::FromStr for KeyEvent {
modifiers.insert(flag);
}
- // Normalize character keys so that characters like C-S-r and C-R
- // are represented by equal KeyEvents.
- match code {
- KeyCode::Char(ch)
- if ch.is_ascii_lowercase() && modifiers.contains(KeyModifiers::SHIFT) =>
- {
- code = KeyCode::Char(ch.to_ascii_uppercase());
- modifiers.remove(KeyModifiers::SHIFT);
- }
- _ => (),
- }
-
Ok(KeyEvent { code, modifiers })
}
}
@@ -459,117 +416,6 @@ impl<'de> Deserialize<'de> for KeyEvent {
}
#[cfg(feature = "term")]
-impl From<termina::event::Event> for Event {
- fn from(event: termina::event::Event) -> Self {
- match event {
- termina::event::Event::Key(key) => Self::Key(key.into()),
- termina::event::Event::Mouse(mouse) => Self::Mouse(mouse.into()),
- termina::event::Event::WindowResized(termina::WindowSize { rows, cols, .. }) => {
- Self::Resize(cols, rows)
- }
- termina::event::Event::FocusIn => Self::FocusGained,
- termina::event::Event::FocusOut => Self::FocusLost,
- termina::event::Event::Paste(s) => Self::Paste(s),
- _ => unreachable!(),
- }
- }
-}
-
-#[cfg(feature = "term")]
-impl From<termina::event::MouseEvent> for MouseEvent {
- fn from(
- termina::event::MouseEvent {
- kind,
- column,
- row,
- modifiers,
- }: termina::event::MouseEvent,
- ) -> Self {
- Self {
- kind: kind.into(),
- column,
- row,
- modifiers: modifiers.into(),
- }
- }
-}
-
-#[cfg(feature = "term")]
-impl From<termina::event::MouseEventKind> for MouseEventKind {
- fn from(kind: termina::event::MouseEventKind) -> Self {
- match kind {
- termina::event::MouseEventKind::Down(button) => Self::Down(button.into()),
- termina::event::MouseEventKind::Up(button) => Self::Up(button.into()),
- termina::event::MouseEventKind::Drag(button) => Self::Drag(button.into()),
- termina::event::MouseEventKind::Moved => Self::Moved,
- termina::event::MouseEventKind::ScrollDown => Self::ScrollDown,
- termina::event::MouseEventKind::ScrollUp => Self::ScrollUp,
- termina::event::MouseEventKind::ScrollLeft => Self::ScrollLeft,
- termina::event::MouseEventKind::ScrollRight => Self::ScrollRight,
- }
- }
-}
-
-#[cfg(feature = "term")]
-impl From<termina::event::MouseButton> for MouseButton {
- fn from(button: termina::event::MouseButton) -> Self {
- match button {
- termina::event::MouseButton::Left => MouseButton::Left,
- termina::event::MouseButton::Right => MouseButton::Right,
- termina::event::MouseButton::Middle => MouseButton::Middle,
- }
- }
-}
-
-#[cfg(feature = "term")]
-impl From<termina::event::KeyEvent> for KeyEvent {
- fn from(
- termina::event::KeyEvent {
- code, modifiers, ..
- }: termina::event::KeyEvent,
- ) -> Self {
- if code == termina::event::KeyCode::BackTab {
- // special case for BackTab -> Shift-Tab
- let mut modifiers: KeyModifiers = modifiers.into();
- modifiers.insert(KeyModifiers::SHIFT);
- Self {
- code: KeyCode::Tab,
- modifiers,
- }
- } else {
- Self {
- code: code.into(),
- modifiers: modifiers.into(),
- }
- }
- }
-}
-
-#[cfg(feature = "term")]
-impl From<KeyEvent> for termina::event::KeyEvent {
- fn from(KeyEvent { code, modifiers }: KeyEvent) -> Self {
- if code == KeyCode::Tab && modifiers.contains(KeyModifiers::SHIFT) {
- // special case for Shift-Tab -> BackTab
- let mut modifiers = modifiers;
- modifiers.remove(KeyModifiers::SHIFT);
- termina::event::KeyEvent {
- code: termina::event::KeyCode::BackTab,
- modifiers: modifiers.into(),
- kind: termina::event::KeyEventKind::Press,
- state: termina::event::KeyEventState::NONE,
- }
- } else {
- termina::event::KeyEvent {
- code: code.into(),
- modifiers: modifiers.into(),
- kind: termina::event::KeyEventKind::Press,
- state: termina::event::KeyEventState::NONE,
- }
- }
- }
-}
-
-#[cfg(all(feature = "term", windows))]
impl From<crossterm::event::Event> for Event {
fn from(event: crossterm::event::Event) -> Self {
match event {
@@ -583,7 +429,7 @@ impl From<crossterm::event::Event> for Event {
}
}
-#[cfg(all(feature = "term", windows))]
+#[cfg(feature = "term")]
impl From<crossterm::event::MouseEvent> for MouseEvent {
fn from(
crossterm::event::MouseEvent {
@@ -602,7 +448,7 @@ impl From<crossterm::event::MouseEvent> for MouseEvent {
}
}
-#[cfg(all(feature = "term", windows))]
+#[cfg(feature = "term")]
impl From<crossterm::event::MouseEventKind> for MouseEventKind {
fn from(kind: crossterm::event::MouseEventKind) -> Self {
match kind {
@@ -612,13 +458,11 @@ impl From<crossterm::event::MouseEventKind> for MouseEventKind {
crossterm::event::MouseEventKind::Moved => Self::Moved,
crossterm::event::MouseEventKind::ScrollDown => Self::ScrollDown,
crossterm::event::MouseEventKind::ScrollUp => Self::ScrollUp,
- crossterm::event::MouseEventKind::ScrollLeft => Self::ScrollLeft,
- crossterm::event::MouseEventKind::ScrollRight => Self::ScrollRight,
}
}
}
-#[cfg(all(feature = "term", windows))]
+#[cfg(feature = "term")]
impl From<crossterm::event::MouseButton> for MouseButton {
fn from(button: crossterm::event::MouseButton) -> Self {
match button {
@@ -629,7 +473,7 @@ impl From<crossterm::event::MouseButton> for MouseButton {
}
}
-#[cfg(all(feature = "term", windows))]
+#[cfg(feature = "term")]
impl From<crossterm::event::KeyEvent> for KeyEvent {
fn from(
crossterm::event::KeyEvent {
@@ -653,7 +497,7 @@ impl From<crossterm::event::KeyEvent> for KeyEvent {
}
}
-#[cfg(all(feature = "term", windows))]
+#[cfg(feature = "term")]
impl From<KeyEvent> for crossterm::event::KeyEvent {
fn from(KeyEvent { code, modifiers }: KeyEvent) -> Self {
if code == KeyCode::Tab && modifiers.contains(KeyModifiers::SHIFT) {
@@ -676,6 +520,7 @@ impl From<KeyEvent> for crossterm::event::KeyEvent {
}
}
}
+
pub fn parse_macro(keys_str: &str) -> anyhow::Result<Vec<KeyEvent>> {
use anyhow::Context;
let mut keys_res: anyhow::Result<_> = Ok(Vec::new());
@@ -698,7 +543,7 @@ pub fn parse_macro(keys_str: &str) -> anyhow::Result<Vec<KeyEvent>> {
if c == ">" {
keys_res = Err(anyhow!("Unmatched '>'"));
} else if c != "<" {
- keys.push(if c == "-" { keys::MINUS } else { c });
+ keys.push(c);
i += end_i;
} else {
match s.find('>').context("'>' expected") {
@@ -798,13 +643,6 @@ mod test {
modifiers: KeyModifiers::NONE
}
);
- assert_eq!(
- str::parse::<KeyEvent>("-").unwrap(),
- KeyEvent {
- code: KeyCode::Char('-'),
- modifiers: KeyModifiers::NONE,
- }
- );
}
#[test]
@@ -840,46 +678,11 @@ mod test {
modifiers: KeyModifiers::ALT | KeyModifiers::CONTROL
}
);
-
- assert_eq!(
- str::parse::<KeyEvent>("C-S-r").unwrap(),
- str::parse::<KeyEvent>("C-R").unwrap(),
- );
-
- assert_eq!(
- str::parse::<KeyEvent>("S-w").unwrap(),
- KeyEvent {
- code: KeyCode::Char('W'),
- modifiers: KeyModifiers::NONE
- }
- );
-
- assert_eq!(
- str::parse::<KeyEvent>("Meta-c").unwrap(),
- KeyEvent {
- code: KeyCode::Char('c'),
- modifiers: KeyModifiers::SUPER
- }
- );
- assert_eq!(
- str::parse::<KeyEvent>("Win-s").unwrap(),
- KeyEvent {
- code: KeyCode::Char('s'),
- modifiers: KeyModifiers::SUPER
- }
- );
- assert_eq!(
- str::parse::<KeyEvent>("Cmd-d").unwrap(),
- KeyEvent {
- code: KeyCode::Char('d'),
- modifiers: KeyModifiers::SUPER
- }
- );
}
#[test]
fn parsing_nonsensical_keys_fails() {
- assert!(str::parse::<KeyEvent>("F25").is_err());
+ assert!(str::parse::<KeyEvent>("F13").is_err());
assert!(str::parse::<KeyEvent>("F0").is_err());
assert!(str::parse::<KeyEvent>("aaa").is_err());
assert!(str::parse::<KeyEvent>("S-S-a").is_err());
@@ -887,7 +690,6 @@ mod test {
assert!(str::parse::<KeyEvent>("FU").is_err());
assert!(str::parse::<KeyEvent>("123").is_err());
assert!(str::parse::<KeyEvent>("S--").is_err());
- assert!(str::parse::<KeyEvent>("S-").is_err());
assert!(str::parse::<KeyEvent>("S-percent").is_err());
}
@@ -1005,64 +807,6 @@ mod test {
},
])
);
-
- assert_eq!(
- parse_macro(":w aa-bb.txt<ret>").ok(),
- Some(vec![
- KeyEvent {
- code: KeyCode::Char(':'),
- modifiers: KeyModifiers::NONE,
- },
- KeyEvent {
- code: KeyCode::Char('w'),
- modifiers: KeyModifiers::NONE,
- },
- KeyEvent {
- code: KeyCode::Char(' '),
- modifiers: KeyModifiers::NONE,
- },
- KeyEvent {
- code: KeyCode::Char('a'),
- modifiers: KeyModifiers::NONE,
- },
- KeyEvent {
- code: KeyCode::Char('a'),
- modifiers: KeyModifiers::NONE,
- },
- KeyEvent {
- code: KeyCode::Char('-'),
- modifiers: KeyModifiers::NONE,
- },
- KeyEvent {
- code: KeyCode::Char('b'),
- modifiers: KeyModifiers::NONE,
- },
- KeyEvent {
- code: KeyCode::Char('b'),
- modifiers: KeyModifiers::NONE,
- },
- KeyEvent {
- code: KeyCode::Char('.'),
- modifiers: KeyModifiers::NONE,
- },
- KeyEvent {
- code: KeyCode::Char('t'),
- modifiers: KeyModifiers::NONE,
- },
- KeyEvent {
- code: KeyCode::Char('x'),
- modifiers: KeyModifiers::NONE,
- },
- KeyEvent {
- code: KeyCode::Char('t'),
- modifiers: KeyModifiers::NONE,
- },
- KeyEvent {
- code: KeyCode::Enter,
- modifiers: KeyModifiers::NONE,
- },
- ])
- );
}
#[test]