@@ -3776,26 +3776,30 @@ fn normal_mode(cx: &mut Context) {
3778fn try_restore_indent(doc: &mut Document, view_id: ViewId) {
3778fn try_restore_indent(doc: &mut Document, view_id: ViewId) {
3779− let doc_changes = doc.changes().changes();
3780− let text = doc.text().slice(..);
3781− let pos = doc.selection(view_id).primary().cursor(text);
3782− let mut can_restore_indent = false;
3784− // Removes trailing whitespace if insert mode is exited after starting a blank new line.
3785 use helix_core::chars::char_is_whitespace;
3779 use helix_core::chars::char_is_whitespace;
3786 use helix_core::Operation;
3780 use helix_core::Operation;
3782+ fn inserted_a_new_blank_line(changes: &[Operation], pos: usize, line_end_pos: usize) -> bool {
3787− if let [Operation::Retain(move_pos), Operation::Insert(ref inserted_str), Operation::Retain(_)] =
3783+ if let [Operation::Retain(move_pos), Operation::Insert(ref inserted_str), Operation::Retain(_)] =
3788− doc_changes
3784+ changes
3790− if move_pos + inserted_str.len32() as usize == pos
3791− && inserted_str.starts_with('\n')
3792− && inserted_str.chars().skip(1).all(char_is_whitespace)
3794− can_restore_indent = true;
3786+ move_pos + inserted_str.len32() as usize == pos
3787+ && inserted_str.starts_with('\n')
3788+ && inserted_str.chars().skip(1).all(char_is_whitespace)
3789+ && pos == line_end_pos // ensure no characters exists after current position
3798− if can_restore_indent {
3795+ let doc_changes = doc.changes().changes();
3796+ let text = doc.text().slice(..);
3797+ let range = doc.selection(view_id).primary();
3798+ let pos = range.cursor(text);
3799+ let line_end_pos = line_end_char_index(&text, range.cursor_line(text));
3801+ if inserted_a_new_blank_line(doc_changes, pos, line_end_pos) {
3802+ // Removes tailing whitespaces.
3799 let transaction =
3803 let transaction =
3800 Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| {
3804 Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| {
3801 let line_start_pos = text.line_to_char(range.cursor_line(text));
3805 let line_start_pos = text.line_to_char(range.cursor_line(text));