mirror of https://github.com/helix-editor/helix
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 functionpull/1330/head
parent
5b4540fc2d
commit
600ce70cf6
|
@ -3610,6 +3610,7 @@ fn normal_mode(cx: &mut Context) {
|
||||||
|
|
||||||
doc.mode = Mode::Normal;
|
doc.mode = Mode::Normal;
|
||||||
|
|
||||||
|
try_restore_indent(doc, view.id);
|
||||||
doc.append_changes_to_history(view.id);
|
doc.append_changes_to_history(view.id);
|
||||||
|
|
||||||
// if leaving append mode, move cursor back by 1
|
// 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.
|
// Store a jump on the jumplist.
|
||||||
fn push_jump(editor: &mut Editor) {
|
fn push_jump(editor: &mut Editor) {
|
||||||
let (view, doc) = current!(editor);
|
let (view, doc) = current!(editor);
|
||||||
|
|
|
@ -889,6 +889,10 @@ impl Document {
|
||||||
self.indent_style.as_str()
|
self.indent_style.as_str()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn changes(&self) -> &ChangeSet {
|
||||||
|
&self.changes
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
/// File path on disk.
|
/// File path on disk.
|
||||||
pub fn path(&self) -> Option<&PathBuf> {
|
pub fn path(&self) -> Option<&PathBuf> {
|
||||||
|
|
Loading…
Reference in New Issue