From 6e658aae1c05fdd9f9ee36b39a948028bcaad446 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Wed, 14 Oct 2020 18:07:42 +0900 Subject: Auto-indent on enter based on tree-sitter scopes. --- helix-view/src/commands.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'helix-view/src') diff --git a/helix-view/src/commands.rs b/helix-view/src/commands.rs index 2cc797f8..542c28ee 100644 --- a/helix-view/src/commands.rs +++ b/helix-view/src/commands.rs @@ -435,7 +435,16 @@ pub mod insert { } pub fn insert_newline(view: &mut View, _count: usize) { - insert_char(view, '\n'); + let transaction = Transaction::change_by_selection(&view.state, |range| { + let indent_level = + helix_core::indent::suggested_indent_for_pos(&view.state, range.head); + let indent = " ".repeat(TAB_WIDTH).repeat(indent_level); + let mut text = String::with_capacity(1 + indent.len()); + text.push('\n'); + text.push_str(&indent); + (range.head, range.head, Some(text.into())) + }); + transaction.apply(&mut view.state); } // TODO: handle indent-aware delete @@ -605,3 +614,8 @@ pub fn unindent(view: &mut View, _count: usize) { transaction.apply(&mut view.state); append_changes_to_history(view); } + +pub fn indent_selection(view: &mut View, _count: usize) { + // loop over each line and recompute proper indentation + unimplemented!() +} -- cgit v1.2.3-70-g09d2