on master help sign in
Auto-indent on enter based on tree-sitter scopes.
commit6e658aae1c05fdd9f9ee36b39a948028bcaad446 authorBlaž Hrastnik <blaz@mxxn.io> dateWed 14 Oct 2020, 18:07 (6 years ago) parent6ae3c26
4 files changed+103−13
helix-core/src/indent.rs+70−12 helix-core/src/state.rs+11 helix-core/src/syntax.rs+7 helix-view/src/commands.rs+15−1
helix-core/src/indent.rs+70−12
@@ -4,6 +4,9 @@ use crate::{
44 Rope, RopeSlice, State,
55};
66
7+/// To determine indentation of a newly inserted line, figure out the indentation at the last col
8+/// of the previous line.
9+
710pub const TAB_WIDTH: usize = 4;
811
912fn indent_level_for_line(line: RopeSlice) -> usize {
@@ -53,22 +56,51 @@ fn walk(node: Option<Node>) -> usize {
5356
5457 let mut increment = 0;
5558
56− let not_first_or_last_sibling = node.next_sibling().is_some() && node.prev_sibling().is_some();
57− let is_scope = true;
58−
59− if not_first_or_last_sibling && is_scope {
60− increment += 1;
59+ // Hardcoded for rust for now
60+ let indent_scopes = &[
61+ "block",
62+ "function_item",
63+ "closure_expression",
64+ "while_expression",
65+ "for_expression",
66+ "loop_expression",
67+ "if_expression",
68+ "if_let_expression",
69+ "binary_expression",
70+ "match_expression",
71+ "match_arm",
72+ //
73+ "struct_item",
74+ "enum_item",
75+ "impl_item",
76+ //
77+ "mod_item",
78+ ];
79+
80+ // let indent_except_first_scopes = &[];
81+
82+ let not_first_sibling = node.next_sibling().is_some();
83+ let not_last_sibling = node.prev_sibling().is_some();
84+ let not_first_or_last_sibling = not_first_sibling && not_last_sibling;
85+
86+ let parent_kind = parent.kind();
87+ let is_scope = indent_scopes.iter().any(|scope| scope == &parent_kind);
88+
89+ // && not_first_or_last_sibling
90+ if is_scope {
91+ increment += 1
6192 }
6293
94+ // if last_scope && increment > 0 && ...{ ignore }
95+
6396 walk(Some(parent)) + increment
6497}
6598
66−// for_line_at_col
6799fn suggested_indent_for_line(state: &State, line_num: usize) -> usize {
68100 let line = state.doc.line(line_num);
69101 let current = indent_level_for_line(line);
70102
71− let mut byte_start = state.doc.line_to_byte(line_num);
103+ let mut start = state.doc.line_to_char(line_num);
72104
73105 // find first non-whitespace char
74106 for ch in line.chars() {
@@ -76,18 +108,23 @@ fn suggested_indent_for_line(state: &State, line_num: usize) -> usize {
76108 if ch != ' ' && ch != '\t' {
77109 break;
78110 }
79− byte_start += 1;
111+ start += 1;
80112 }
81113
114+ suggested_indent_for_pos(state, start)
115+}
116+
117+pub fn suggested_indent_for_pos(state: &State, pos: usize) -> usize {
82118 if let Some(syntax) = &state.syntax {
83− let node = get_highest_syntax_node_at_bytepos(state.syntax.as_ref().unwrap(), byte_start);
119+ let byte_start = state.doc.char_to_byte(pos);
120+ let node = get_highest_syntax_node_at_bytepos(syntax, byte_start);
84121
85− // let indentation = walk()
122+ let indentation = walk(node);
86123 // special case for comments
87124
88125 // if preserve_leading_whitespace
89126
90− unimplemented!()
127+ indentation
91128 } else {
92129 // TODO: case for non-tree sitter grammars
93130 0
@@ -99,7 +136,7 @@ mod test {
99136 use super::*;
100137
101138 #[test]
102− fn indent_level() {
139+ fn test_indent_level() {
103140 let line = Rope::from(" fn new"); // 8 spaces
104141 assert_eq!(indent_level_for_line(line.slice(..)), 2);
105142 let line = Rope::from("\t\t\tfn new"); // 3 tabs
@@ -108,4 +145,25 @@ mod test {
108145 let line = Rope::from("\t \tfn new"); // 1 tab, 4 spaces, tab
109146 assert_eq!(indent_level_for_line(line.slice(..)), 3);
110147 }
148+
149+ #[test]
150+ fn test_suggested_indent_for_line() {
151+ let doc = Rope::from(
152+ "mod test {
153+ fn hello_world() {
154+ 1 + 1
155+ }
156+}
157+",
158+ );
159+
160+ let mut state = State::new(doc);
161+ state.set_language("source.rust", &[]);
162+
163+ assert_eq!(suggested_indent_for_line(&state, 0), 0); // mod
164+ assert_eq!(suggested_indent_for_line(&state, 1), 1); // fn
165+ assert_eq!(suggested_indent_for_line(&state, 2), 2); // 1 + 1
166+ assert_eq!(suggested_indent_for_line(&state, 4), 1); // }
167+ assert_eq!(suggested_indent_for_line(&state, 5), 0); // }
168+ }
111169}
helix-core/src/state.rs+11
@@ -86,6 +86,17 @@ impl State {
8686 Ok(state)
8787 }
8888
89+ pub fn set_language(&mut self, scope: &str, scopes: &[String]) {
90+ if let Some(language_config) = LOADER.language_config_for_scope(scope) {
91+ let highlight_config = language_config.highlight_config(scopes).unwrap().unwrap();
92+ // TODO: config.configure(scopes) is now delayed, is that ok?
93+
94+ let syntax = Syntax::new(&self.doc, highlight_config.clone());
95+
96+ self.syntax = Some(syntax);
97+ };
98+ }
99+
89100 // TODO: doc/selection accessors
90101
91102 // TODO: be able to take either Rope or RopeSlice
helix-core/src/syntax.rs+7
@@ -135,6 +135,13 @@ impl Loader {
135135
136136 // TODO: content_regex handling conflict resolution
137137 }
138+
139+ pub fn language_config_for_scope(&self, scope: &str) -> Option<Arc<LanguageConfiguration>> {
140+ self.language_configs
141+ .iter()
142+ .find(|config| config.scope == scope)
143+ .cloned()
144+ }
138145}
139146
140147//
helix-view/src/commands.rs+15−1
@@ -435,7 +435,16 @@ pub mod insert {
435435 }
436436
437437 pub fn insert_newline(view: &mut View, _count: usize) {
438− insert_char(view, '\n');
438+ let transaction = Transaction::change_by_selection(&view.state, |range| {
439+ let indent_level =
440+ helix_core::indent::suggested_indent_for_pos(&view.state, range.head);
441+ let indent = " ".repeat(TAB_WIDTH).repeat(indent_level);
442+ let mut text = String::with_capacity(1 + indent.len());
443+ text.push('\n');
444+ text.push_str(&indent);
445+ (range.head, range.head, Some(text.into()))
446+ });
447+ transaction.apply(&mut view.state);
439448 }
440449
441450 // TODO: handle indent-aware delete
@@ -605,3 +614,8 @@ pub fn unindent(view: &mut View, _count: usize) {
605614 transaction.apply(&mut view.state);
606615 append_changes_to_history(view);
607616}
617+
618+pub fn indent_selection(view: &mut View, _count: usize) {
619+ // loop over each line and recompute proper indentation
620+ unimplemented!()
621+}