on master help sign in
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

commit600ce70cf6d50ce37b96bfde90c6ade8db6cd8c3 authorWindSoilder <WindSoilder@outlook.com> dateTue 21 Dec 2021, 17:17 (5 years ago) parent5b4540f
2 files changed+35
helix-term/src/commands.rs+31 helix-view/src/document.rs+4
helix-term/src/commands.rs+31
@@ -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);
helix-view/src/document.rs+4
@@ -889,6 +889,10 @@ impl Document {
889889 self.indent_style.as_str()
890890 }
891891
892+ pub fn changes(&self) -> &ChangeSet {
893+ &self.changes
894+ }
895+
892896 #[inline]
893897 /// File path on disk.
894898 pub fn path(&self) -> Option<&PathBuf> {