on master help sign in
Refactor case where key event is solely a character
commit8a2fa692f26f5bff5861151f395304837f5d93ec authorGokul Soumya <gokulps15@gmail.com> dateWed 28 Jul 2021, 21:33 (5 years ago) parentb90450b
2 files changed+8−32
helix-term/src/commands.rs+6−30 helix-view/src/input.rs+2−2
helix-term/src/commands.rs+6−30
@@ -3632,11 +3632,7 @@ fn wclose(cx: &mut Context) {
36323632
36333633fn select_register(cx: &mut Context) {
36343634 cx.on_next_key(move |cx, event| {
3635− if let KeyEvent {
3636− code: KeyCode::Char(ch),
3637− ..
3638− } = event
3639− {
3635+ if let Some(ch) = event.char() {
36403636 cx.editor.selected_register.select(ch);
36413637 }
36423638 })
@@ -3687,11 +3683,7 @@ fn select_textobject_inner(cx: &mut Context) {
36873683fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
36883684 let count = cx.count();
36893685 cx.on_next_key(move |cx, event| {
3690− if let KeyEvent {
3691− code: KeyCode::Char(ch),
3692− ..
3693− } = event
3694− {
3686+ if let Some(ch) = event.char() {
36953687 let (view, doc) = current!(cx.editor);
36963688 let text = doc.text().slice(..);
36973689
@@ -3713,11 +3705,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
37133705
37143706fn surround_add(cx: &mut Context) {
37153707 cx.on_next_key(move |cx, event| {
3716− if let KeyEvent {
3717− code: KeyCode::Char(ch),
3718− ..
3719− } = event
3720− {
3708+ if let Some(ch) = event.char() {
37213709 let (view, doc) = current!(cx.editor);
37223710 let text = doc.text().slice(..);
37233711 let selection = doc.selection(view.id);
@@ -3743,17 +3731,9 @@ fn surround_add(cx: &mut Context) {
37433731fn surround_replace(cx: &mut Context) {
37443732 let count = cx.count();
37453733 cx.on_next_key(move |cx, event| {
3746− if let KeyEvent {
3747− code: KeyCode::Char(from),
3748− ..
3749− } = event
3750− {
3734+ if let Some(from) = event.char() {
37513735 cx.on_next_key(move |cx, event| {
3752− if let KeyEvent {
3753− code: KeyCode::Char(to),
3754− ..
3755− } = event
3756− {
3736+ if let Some(to) = event.char() {
37573737 let (view, doc) = current!(cx.editor);
37583738 let text = doc.text().slice(..);
37593739 let selection = doc.selection(view.id);
@@ -3783,11 +3763,7 @@ fn surround_replace(cx: &mut Context) {
37833763fn surround_delete(cx: &mut Context) {
37843764 let count = cx.count();
37853765 cx.on_next_key(move |cx, event| {
3786− if let KeyEvent {
3787− code: KeyCode::Char(ch),
3788− ..
3789− } = event
3790− {
3766+ if let Some(ch) = event.char() {
37913767 let (view, doc) = current!(cx.editor);
37923768 let text = doc.text().slice(..);
37933769 let selection = doc.selection(view.id);
helix-view/src/input.rs+2−2
@@ -15,10 +15,10 @@ pub struct KeyEvent {
1515}
1616
1717impl KeyEvent {
18− /// Get only the character involved in this event
18+ /// If a character was pressed (without modifiers), return it.
1919 pub fn char(&self) -> Option<char> {
2020 match self.code {
21− KeyCode::Char(ch) => Some(ch),
21+ KeyCode::Char(ch) if self.modifiers.is_empty() => Some(ch),
2222 _ => None,
2323 }
2424 }