From 600ce70cf6d50ce37b96bfde90c6ade8db6cd8c3 Mon Sep 17 00:00:00 2001 From: WindSoilder Date: Tue, 21 Dec 2021 17:17:55 +0800 Subject: Improve dedent behavior (#1232) * tmp add code for dedent * finish normal_mode with dedent behavior * use function pointer * rebase from origin * check dedent condition inside normal_mode implementation * using if let... * fix check * using char_is_whitespace instead of ch.is_whitespace * fix clippy * abstract restore_indent function--- helix-term/src/commands.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'helix-term') diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 28f955eb..297b49a1 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -3610,6 +3610,7 @@ fn normal_mode(cx: &mut Context) { doc.mode = Mode::Normal; + try_restore_indent(doc, view.id); doc.append_changes_to_history(view.id); // if leaving append mode, move cursor back by 1 @@ -3627,6 +3628,36 @@ fn normal_mode(cx: &mut Context) { } } +fn try_restore_indent(doc: &mut Document, view_id: ViewId) { + let doc_changes = doc.changes().changes(); + let text = doc.text().slice(..); + let pos = doc.selection(view_id).primary().cursor(text); + let mut can_restore_indent = false; + + // Removes trailing whitespace if insert mode is exited after starting a blank new line. + use helix_core::chars::char_is_whitespace; + use helix_core::Operation; + if let [Operation::Retain(move_pos), Operation::Insert(ref inserted_str), Operation::Retain(_)] = + doc_changes + { + if move_pos + inserted_str.len32() as usize == pos + && inserted_str.starts_with('\n') + && inserted_str.chars().skip(1).all(char_is_whitespace) + { + can_restore_indent = true; + } + } + + if can_restore_indent { + let transaction = + Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| { + let line_start_pos = text.line_to_char(range.cursor_line(text)); + (line_start_pos, pos, None) + }); + doc.apply(&transaction, view_id); + } +} + // Store a jump on the jumplist. fn push_jump(editor: &mut Editor) { let (view, doc) = current!(editor); -- cgit v1.2.3-70-g09d2