Skip to content

Commit

Permalink
fix: clear selection when pressing Esc
Browse files Browse the repository at this point in the history
  • Loading branch information
deephbz committed Dec 31, 2024
1 parent 31603ee commit ff8a9da
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/core_editor/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,15 @@ impl Editor {
self.selection_anchor.map(|selection_anchor| {
let buffer_len = self.line_buffer.len();
if self.insertion_point() > selection_anchor {
(selection_anchor, (self.insertion_point() + 1).min(buffer_len))
(
selection_anchor,
(self.insertion_point() + 1).min(buffer_len),
)
} else {
(self.insertion_point(), (selection_anchor + 1).min(buffer_len))
(
self.insertion_point(),
(selection_anchor + 1).min(buffer_len),
)
}
})
}
Expand Down
20 changes: 18 additions & 2 deletions src/edit_mode/vi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,16 @@ impl EditMode for Vi {
(_, KeyModifiers::NONE, KeyCode::Esc) => {
self.cache.clear();
self.mode = ViMode::Normal;
ReedlineEvent::Multiple(vec![ReedlineEvent::Esc, ReedlineEvent::Repaint])
ReedlineEvent::Multiple(vec![
// Move left then right to clear the selection.
// Order matters here: this makes sure cursor does not move at end of line
ReedlineEvent::Edit(vec![
EditCommand::MoveLeft { select: false },
EditCommand::MoveRight { select: false },
]),
ReedlineEvent::Esc,
ReedlineEvent::Repaint,
])
}
(_, KeyModifiers::NONE, KeyCode::Enter) => {
self.mode = ViMode::Insert;
Expand Down Expand Up @@ -191,7 +200,14 @@ mod test {

assert_eq!(
result,
ReedlineEvent::Multiple(vec![ReedlineEvent::Esc, ReedlineEvent::Repaint])
ReedlineEvent::Multiple(vec![
ReedlineEvent::Edit(vec![
EditCommand::MoveLeft { select: false },
EditCommand::MoveRight { select: false }
]),
ReedlineEvent::Esc,
ReedlineEvent::Repaint
])
);
assert!(matches!(vi.mode, ViMode::Normal));
}
Expand Down

0 comments on commit ff8a9da

Please sign in to comment.