@@ -3610,6 +3610,7 @@ fn normal_mode(cx: &mut Context) {
36103610
36113611 doc.mode = Mode::Normal;
36123612
3613+ try_restore_indent(doc, view.id);
36133614 doc.append_changes_to_history(view.id);
36143615
36153616 // if leaving append mode, move cursor back by 1
@@ -3627,6 +3628,36 @@ fn normal_mode(cx: &mut Context) {
36273628 }
36283629}
36293630
3631+fn try_restore_indent(doc: &mut Document, view_id: ViewId) {
3632+ let doc_changes = doc.changes().changes();
3633+ let text = doc.text().slice(..);
3634+ let pos = doc.selection(view_id).primary().cursor(text);
3635+ let mut can_restore_indent = false;
3636+
3637+ // Removes trailing whitespace if insert mode is exited after starting a blank new line.
3638+ use helix_core::chars::char_is_whitespace;
3639+ use helix_core::Operation;
3640+ if let [Operation::Retain(move_pos), Operation::Insert(ref inserted_str), Operation::Retain(_)] =
3641+ doc_changes
3642+ {
3643+ if move_pos + inserted_str.len32() as usize == pos
3644+ && inserted_str.starts_with('\n')
3645+ && inserted_str.chars().skip(1).all(char_is_whitespace)
3646+ {
3647+ can_restore_indent = true;
3648+ }
3649+ }
3650+
3651+ if can_restore_indent {
3652+ let transaction =
3653+ Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| {
3654+ let line_start_pos = text.line_to_char(range.cursor_line(text));
3655+ (line_start_pos, pos, None)
3656+ });
3657+ doc.apply(&transaction, view_id);
3658+ }
3659+}
3660+
36303661// Store a jump on the jumplist.
36313662fn push_jump(editor: &mut Editor) {
36323663 let (view, doc) = current!(editor);