Skip to content

Commit

Permalink
Add tilde(~) switch case support to vim normal & visual modes (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
panarch authored Nov 28, 2024
1 parent 7fb33bd commit 845c4d2
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub enum KeyEvent {
Down,
Enter,
Tab,
Tilde,
Esc,
}

Expand Down
1 change: 1 addition & 0 deletions core/src/state/notebook/inner_state/editing_normal_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ async fn consume_idle(state: &mut NotebookState, event: Event) -> Result<Noteboo
Key(KeyEvent::B) => MoveCursorWordBack(1).into(),
Key(KeyEvent::Num(NumKey::Zero)) => MoveCursorLineStart.into(),
Key(KeyEvent::DollarSign) => MoveCursorLineEnd.into(),
Key(KeyEvent::Tilde) => SwitchCase.into(),
Key(KeyEvent::Caret) => MoveCursorLineNonEmptyStart.into(),
Key(KeyEvent::CapG) => MoveCursorBottom.into(),
Key(KeyEvent::I) => {
Expand Down
5 changes: 5 additions & 0 deletions core/src/state/notebook/inner_state/editing_visual_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ async fn consume_idle(
Key(KeyEvent::DollarSign) => MoveCursorLineEnd.into(),
Key(KeyEvent::Caret) => MoveCursorLineNonEmptyStart.into(),
Key(KeyEvent::CapG) => MoveCursorBottom.into(),
Key(KeyEvent::Tilde) => {
state.inner_state = InnerState::EditingNormalMode(VimNormalState::Idle);

SwitchCase.into()
}
Key(KeyEvent::D | KeyEvent::X) => {
state.inner_state = InnerState::EditingNormalMode(VimNormalState::Idle);

Expand Down
2 changes: 2 additions & 0 deletions core/src/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ pub enum NormalModeTransition {
Redo,
YankLines(usize),
DeleteInsideWord(usize),
SwitchCase,
}

#[derive(Display)]
Expand All @@ -163,6 +164,7 @@ pub enum VisualModeTransition {
YankSelection,
DeleteSelection,
DeleteSelectionAndInsertMode,
SwitchCase,
}

impl From<EntryTransition> for Transition {
Expand Down
1 change: 1 addition & 0 deletions tui/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ fn to_event(input: Input) -> Option<KeyEvent> {
KeyCode::Char('0') => NumKey::Zero.into(),
KeyCode::Char('$') => KeyEvent::DollarSign,
KeyCode::Char('^') => KeyEvent::Caret,
KeyCode::Char('~') => KeyEvent::Tilde,
KeyCode::Left => KeyEvent::Left,
KeyCode::Right => KeyEvent::Right,
KeyCode::Up => KeyEvent::Up,
Expand Down
34 changes: 34 additions & 0 deletions tui/src/transitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,13 @@ impl App {
self.context.notebook.mark_dirty();
self.context.notebook.update_yank();
}
SwitchCase => {
let editor = self.context.notebook.get_editor_mut();
editor.start_selection();
switch_case(editor);

self.context.notebook.mark_dirty();
}
};
}

Expand Down Expand Up @@ -593,6 +600,12 @@ impl App {
self.context.notebook.mark_dirty();
self.context.notebook.update_yank();
}
SwitchCase => {
let editor = self.context.notebook.get_editor_mut();
switch_case(editor);

self.context.notebook.mark_dirty();
}
}
}

Expand Down Expand Up @@ -687,3 +700,24 @@ fn reselect_for_yank(editor: &mut TextArea) {
editor.move_cursor(CursorMove::Jump(end.0 as u16, end.1 as u16));
editor.move_cursor(CursorMove::Forward);
}

fn switch_case(editor: &mut TextArea) {
let yank = editor.yank_text();
reselect_for_yank(editor);
editor.cut();

let changed = editor
.yank_text()
.chars()
.map(|c| {
if c.is_uppercase() {
c.to_lowercase().to_string()
} else {
c.to_uppercase().to_string()
}
})
.collect::<String>();

editor.insert_str(changed);
editor.set_yank_text(yank);
}
2 changes: 2 additions & 0 deletions tui/src/views/dialog/vim_keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub fn draw(frame: &mut Frame, keymap_kind: VimKeymapKind) {
Line::raw("[G] Move cursor to the end of the file"),
Line::raw(""),
Line::from("EDIT TEXT".white().on_dark_gray()),
Line::raw("[~] Toggle the case of the current character"),
Line::raw("[x] Delete character under the cursor"),
Line::raw("[u] Undo the last change"),
Line::raw("[Ctrl+r] Redo the last undone change"),
Expand Down Expand Up @@ -165,6 +166,7 @@ pub fn draw(frame: &mut Frame, keymap_kind: VimKeymapKind) {
"[x] Delete selected text".into(),
]),
Line::raw("[y] Yank (copy) selected text"),
Line::raw("[~] Toggle the case of the select text"),
]),
VimKeymapKind::VisualNumbering => ("VIM VISUAL MODE KEYMAP - NUMBERING", vec![
Line::from("EXTENDING NUMBERING MODE".white().on_dark_gray()),
Expand Down

0 comments on commit 845c4d2

Please sign in to comment.