Skip to content

Commit 7d3d8c1

Browse files
committed
feat: add EditCommand::MoveLineUp and EditCommand::MoveLineDown
This allows us to select multiple lines using shift + up/down keys (or j/k keys in visual mode)
1 parent 57d9058 commit 7d3d8c1

File tree

5 files changed

+56
-12
lines changed

5 files changed

+56
-12
lines changed

src/core_editor/editor.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ impl Editor {
6464
EditCommand::MoveToPosition { position, select } => {
6565
self.move_to_position(*position, *select)
6666
}
67+
EditCommand::MoveLineUp { select } => self.move_up(*select),
68+
EditCommand::MoveLineDown { select } => self.move_down(*select),
6769
EditCommand::MoveLeft { select } => self.move_left(*select),
6870
EditCommand::MoveRight { select } => self.move_right(*select),
6971
EditCommand::MoveWordLeft { select } => self.move_word_left(*select),
@@ -619,6 +621,16 @@ impl Editor {
619621
self.line_buffer.insert_str(string);
620622
}
621623

624+
fn move_up(&mut self, select: bool) {
625+
self.update_selection_anchor(select);
626+
self.line_buffer.move_line_up();
627+
}
628+
629+
fn move_down(&mut self, select: bool) {
630+
self.update_selection_anchor(select);
631+
self.line_buffer.move_line_down();
632+
}
633+
622634
fn move_left(&mut self, select: bool) {
623635
self.update_selection_anchor(select);
624636
self.line_buffer.move_left();

src/edit_mode/keybindings.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,16 @@ pub fn add_common_selection_bindings(kb: &mut Keybindings) {
242242
use KeyCode as KC;
243243
use KeyModifiers as KM;
244244

245+
kb.add_binding(
246+
KM::SHIFT,
247+
KC::Up,
248+
edit_bind(EC::MoveLineUp { select: true }),
249+
);
250+
kb.add_binding(
251+
KM::SHIFT,
252+
KC::Down,
253+
edit_bind(EC::MoveLineDown { select: true }),
254+
);
245255
kb.add_binding(
246256
KM::SHIFT,
247257
KC::Left,

src/edit_mode/vi/motion.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,22 @@ impl Motion {
179179
select: select_mode,
180180
}]),
181181
]))],
182-
Motion::Up => vec![ReedlineOption::Event(ReedlineEvent::UntilFound(vec![
183-
ReedlineEvent::MenuUp,
184-
ReedlineEvent::Up,
185-
// todo: add EditCommand::MoveLineUp
186-
]))],
187-
Motion::Down => vec![ReedlineOption::Event(ReedlineEvent::UntilFound(vec![
188-
ReedlineEvent::MenuDown,
189-
ReedlineEvent::Down,
190-
// todo: add EditCommand::MoveLineDown
191-
]))],
182+
Motion::Up => vec![if select_mode {
183+
ReedlineOption::Edit(EditCommand::MoveLineUp { select: true })
184+
} else {
185+
ReedlineOption::Event(ReedlineEvent::UntilFound(vec![
186+
ReedlineEvent::MenuUp,
187+
ReedlineEvent::Up,
188+
]))
189+
}],
190+
Motion::Down => vec![if select_mode {
191+
ReedlineOption::Edit(EditCommand::MoveLineDown { select: true })
192+
} else {
193+
ReedlineOption::Event(ReedlineEvent::UntilFound(vec![
194+
ReedlineEvent::MenuDown,
195+
ReedlineEvent::Down,
196+
]))
197+
}],
192198
Motion::NextWord => vec![ReedlineOption::Edit(EditCommand::MoveWordRightStart {
193199
select: select_mode,
194200
})],

src/engine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,7 +1662,7 @@ impl Reedline {
16621662
// If we're at the top, move to previous history
16631663
self.previous_history();
16641664
} else {
1665-
self.editor.move_line_up();
1665+
self.run_edit_commands(&[EditCommand::MoveLineUp { select: false }]);
16661666
}
16671667
}
16681668

@@ -1672,7 +1672,7 @@ impl Reedline {
16721672
// If we're at the top, move to previous history
16731673
self.next_history();
16741674
} else {
1675-
self.editor.move_line_down();
1675+
self.run_edit_commands(&[EditCommand::MoveLineDown { select: false }]);
16761676
}
16771677
}
16781678

src/enums.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ pub enum EditCommand {
112112
select: bool,
113113
},
114114

115+
/// Move one line up
116+
MoveLineUp {
117+
/// Select the text between the current cursor position and destination
118+
select: bool,
119+
},
120+
121+
/// Move one line down
122+
MoveLineDown {
123+
/// Select the text between the current cursor position and destination
124+
select: bool,
125+
},
126+
115127
/// Move one character to the left
116128
MoveLeft {
117129
/// Select the text between the current cursor position and destination
@@ -482,6 +494,8 @@ impl Display for EditCommand {
482494
EditCommand::MoveToLineEnd { .. } => {
483495
write!(f, "MoveToLineEnd Optional[select: <bool>]")
484496
}
497+
EditCommand::MoveLineUp { .. } => write!(f, "MoveLineUp Optional[select: <bool>]"),
498+
EditCommand::MoveLineDown { .. } => write!(f, "MoveLineDown Optional[select: <bool>]"),
485499
EditCommand::MoveLeft { .. } => write!(f, "MoveLeft Optional[select: <bool>]"),
486500
EditCommand::MoveRight { .. } => write!(f, "MoveRight Optional[select: <bool>]"),
487501
EditCommand::MoveWordLeft { .. } => write!(f, "MoveWordLeft Optional[select: <bool>]"),
@@ -613,6 +627,8 @@ impl EditCommand {
613627
| EditCommand::MoveToLineEnd { select, .. }
614628
| EditCommand::MoveToLineNonBlankStart { select, .. }
615629
| EditCommand::MoveToPosition { select, .. }
630+
| EditCommand::MoveLineUp { select, .. }
631+
| EditCommand::MoveLineDown { select, .. }
616632
| EditCommand::MoveLeft { select, .. }
617633
| EditCommand::MoveRight { select, .. }
618634
| EditCommand::MoveWordLeft { select, .. }

0 commit comments

Comments
 (0)