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); diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 2cb33fe3..c71d1850 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -889,6 +889,10 @@ impl Document { self.indent_style.as_str() } + pub fn changes(&self) -> &ChangeSet { + &self.changes + } + #[inline] /// File path on disk. pub fn path(&self) -> Option<&PathBuf> {