on master help sign in
Improve dedent behavior, make kill_to_line_end behave like emacs (#1173)

* restore indent when press esc right after open a new line * add comment for restore_indent * fix, and make kill to line end behaves like emacs * update comment * fix comment * adjust cancel restore_indent situation * check esc logic in mode transaction * improve comment * add more check for dedent * update comment * use matches to check for last_cmd * no need to introduct CommandFun type

commitc08d2fae587a0a5dd2a1e2e44a1f385d142c9d59 authorWindSoilder <WindSoilder@outlook.com> dateTue 30 Nov 2021, 15:40 (5 years ago) parent9429622
2 files changed+28−4
helix-term/src/commands.rs+15−4 helix-term/src/ui/editor.rs+13
helix-term/src/commands.rs+15−4
@@ -173,6 +173,10 @@ impl Command {
173173 self.doc
174174 }
175175
176+ pub fn fun(&self) -> fn(&mut Context) {
177+ self.fun
178+ }
179+
176180 #[rustfmt::skip]
177181 commands!(
178182 no_op, "Do nothing",
@@ -603,8 +607,15 @@ fn kill_to_line_end(cx: &mut Context) {
603607
604608 let selection = doc.selection(view.id).clone().transform(|range| {
605609 let line = range.cursor_line(text);
606− let pos = line_end_char_index(&text, line);
607− range.put_cursor(text, pos, true)
610+ let line_end_pos = line_end_char_index(&text, line);
611+ let pos = range.cursor(text);
612+
613+ let mut new_range = range.put_cursor(text, line_end_pos, true);
614+ // don't want to remove the line separator itself if the cursor doesn't reach the end of line.
615+ if pos != line_end_pos {
616+ new_range.head = line_end_pos;
617+ }
618+ new_range
608619 });
609620 delete_selection_insert_mode(doc, view, &selection);
610621}
@@ -3503,12 +3514,12 @@ fn open(cx: &mut Context, open: Open) {
35033514}
35043515
35053516// o inserts a new line after each line with a selection
3506−fn open_below(cx: &mut Context) {
3517+pub(crate) fn open_below(cx: &mut Context) {
35073518 open(cx, Open::Below)
35083519}
35093520
35103521// O inserts a new line before each line with a selection
3511−fn open_above(cx: &mut Context) {
3522+pub(crate) fn open_above(cx: &mut Context) {
35123523 open(cx, Open::Above)
35133524}
35143525
helix-term/src/ui/editor.rs+13
@@ -1023,6 +1023,19 @@ impl Component for EditorView {
10231023 (Mode::Insert, Mode::Normal) => {
10241024 // if exiting insert mode, remove completion
10251025 self.completion = None;
1026+
1027+ let last_cmd = self.last_insert.0.fun();
1028+ const OPEN_BELOW_FUN: fn(&mut commands::Context) = commands::open_below;
1029+ const OPEN_ABOVE_FUN: fn(&mut commands::Context) = commands::open_above;
1030+ // For user friendly,
1031+ // Remove whitespaces if we go from insert mode(through open below/above) to normal mode without any keys in between.
1032+ // Example: `o<esc>`.
1033+ if matches!(last_cmd, OPEN_BELOW_FUN | OPEN_ABOVE_FUN)
1034+ && self.last_insert.1.len() == 1
1035+ {
1036+ commands::Command::goto_line_start.execute(&mut cxt);
1037+ commands::Command::kill_to_line_end.execute(&mut cxt);
1038+ }
10261039 }
10271040 _ => (),
10281041 }