Skip to content

Commit

Permalink
Refactor case where key event is solely a character
Browse files Browse the repository at this point in the history
  • Loading branch information
sudormrfbin authored and pickfire committed Jul 29, 2021
1 parent b90450b commit 8a2fa69
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 32 deletions.
36 changes: 6 additions & 30 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3632,11 +3632,7 @@ fn wclose(cx: &mut Context) {

fn select_register(cx: &mut Context) {
cx.on_next_key(move |cx, event| {
if let KeyEvent {
code: KeyCode::Char(ch),
..
} = event
{
if let Some(ch) = event.char() {
cx.editor.selected_register.select(ch);
}
})
Expand Down Expand Up @@ -3687,11 +3683,7 @@ fn select_textobject_inner(cx: &mut Context) {
fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
let count = cx.count();
cx.on_next_key(move |cx, event| {
if let KeyEvent {
code: KeyCode::Char(ch),
..
} = event
{
if let Some(ch) = event.char() {
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);

Expand All @@ -3713,11 +3705,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {

fn surround_add(cx: &mut Context) {
cx.on_next_key(move |cx, event| {
if let KeyEvent {
code: KeyCode::Char(ch),
..
} = event
{
if let Some(ch) = event.char() {
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
let selection = doc.selection(view.id);
Expand All @@ -3743,17 +3731,9 @@ fn surround_add(cx: &mut Context) {
fn surround_replace(cx: &mut Context) {
let count = cx.count();
cx.on_next_key(move |cx, event| {
if let KeyEvent {
code: KeyCode::Char(from),
..
} = event
{
if let Some(from) = event.char() {
cx.on_next_key(move |cx, event| {
if let KeyEvent {
code: KeyCode::Char(to),
..
} = event
{
if let Some(to) = event.char() {
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
let selection = doc.selection(view.id);
Expand Down Expand Up @@ -3783,11 +3763,7 @@ fn surround_replace(cx: &mut Context) {
fn surround_delete(cx: &mut Context) {
let count = cx.count();
cx.on_next_key(move |cx, event| {
if let KeyEvent {
code: KeyCode::Char(ch),
..
} = event
{
if let Some(ch) = event.char() {
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
let selection = doc.selection(view.id);
Expand Down
4 changes: 2 additions & 2 deletions helix-view/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ pub struct KeyEvent {
}

impl KeyEvent {
/// Get only the character involved in this event
/// If a character was pressed (without modifiers), return it.
pub fn char(&self) -> Option<char> {
match self.code {
KeyCode::Char(ch) => Some(ch),
KeyCode::Char(ch) if self.modifiers.is_empty() => Some(ch),
_ => None,
}
}
Expand Down

0 comments on commit 8a2fa69

Please sign in to comment.