diff options
author | WindSoilder | 2021-12-21 09:17:55 +0000 |
---|---|---|
committer | GitHub | 2021-12-21 09:17:55 +0000 |
commit | 600ce70cf6d50ce37b96bfde90c6ade8db6cd8c3 (patch) | |
tree | 416df8cc2cee8b2c6fadf466d792425533e43e50 | |
parent | 5b4540fc2d8201f4bfe336cc10c1096059217225 (diff) |
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
-rw-r--r-- | helix-term/src/commands.rs | 31 | ||||
-rw-r--r-- | helix-view/src/document.rs | 4 |
2 files changed, 35 insertions, 0 deletions
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> { |