on master help sign in
Auto-indent on enter based on tree-sitter scopes.
6e658aaBlaž Hrastnik
helix-core/src/indent.rs+70−12
@@ -4,6 +4,9 @@ use crate::{
4 Rope, RopeSlice, State,
4 Rope, RopeSlice, State,
5};
5};
6
6
7+/// To determine indentation of a newly inserted line, figure out the indentation at the last col
8+/// of the previous line.
9+
7pub const TAB_WIDTH: usize = 4;
10pub const TAB_WIDTH: usize = 4;
8
11
9fn indent_level_for_line(line: RopeSlice) -> usize {
12fn indent_level_for_line(line: RopeSlice) -> usize {
@@ -53,22 +56,51 @@ fn walk(node: Option<Node>) -> usize {
53
56
54 let mut increment = 0;
57 let mut increment = 0;
55
58
56− let not_first_or_last_sibling = node.next_sibling().is_some() && node.prev_sibling().is_some();
59+ // Hardcoded for rust for now
57− let is_scope = true;
60+ let indent_scopes = &[
58−
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
59− if not_first_or_last_sibling && is_scope {
90+ if is_scope {
60− increment += 1;
91+ increment += 1
61 }
92 }
62
93
94+ // if last_scope && increment > 0 && ...{ ignore }
95+
63 walk(Some(parent)) + increment
96 walk(Some(parent)) + increment
64}
97}
65
98
66−// for_line_at_col
67fn suggested_indent_for_line(state: &State, line_num: usize) -> usize {
99fn suggested_indent_for_line(state: &State, line_num: usize) -> usize {
68 let line = state.doc.line(line_num);
100 let line = state.doc.line(line_num);
69 let current = indent_level_for_line(line);
101 let current = indent_level_for_line(line);
70
102
71− let mut byte_start = state.doc.line_to_byte(line_num);
103+ let mut start = state.doc.line_to_char(line_num);
72
104
73 // find first non-whitespace char
105 // find first non-whitespace char
74 for ch in line.chars() {
106 for ch in line.chars() {
@@ -76,18 +108,23 @@ fn suggested_indent_for_line(state: &State, line_num: usize) -> usize {
76 if ch != ' ' && ch != '\t' {
108 if ch != ' ' && ch != '\t' {
77 break;
109 break;
78 }
110 }
79− byte_start += 1;
111+ start += 1;
80 }
112 }
81
113
114+ suggested_indent_for_pos(state, start)
115+}
116+
117+pub fn suggested_indent_for_pos(state: &State, pos: usize) -> usize {
82 if let Some(syntax) = &state.syntax {
118 if let Some(syntax) = &state.syntax {
119+ let byte_start = state.doc.char_to_byte(pos);
83− let node = get_highest_syntax_node_at_bytepos(state.syntax.as_ref().unwrap(), byte_start);
120+ let node = get_highest_syntax_node_at_bytepos(syntax, byte_start);
84
121
85− // let indentation = walk()
122+ let indentation = walk(node);
86 // special case for comments
123 // special case for comments
87
124
88 // if preserve_leading_whitespace
125 // if preserve_leading_whitespace
89
126
90− unimplemented!()
127+ indentation
91 } else {
128 } else {
92 // TODO: case for non-tree sitter grammars
129 // TODO: case for non-tree sitter grammars
93 0
130 0
@@ -99,7 +136,7 @@ mod test {
99 use super::*;
136 use super::*;
100
137
101 #[test]
138 #[test]
102− fn indent_level() {
139+ fn test_indent_level() {
103 let line = Rope::from(" fn new"); // 8 spaces
140 let line = Rope::from(" fn new"); // 8 spaces
104 assert_eq!(indent_level_for_line(line.slice(..)), 2);
141 assert_eq!(indent_level_for_line(line.slice(..)), 2);
105 let line = Rope::from("\t\t\tfn new"); // 3 tabs
142 let line = Rope::from("\t\t\tfn new"); // 3 tabs
@@ -108,4 +145,25 @@ mod test {
108 let line = Rope::from("\t \tfn new"); // 1 tab, 4 spaces, tab
145 let line = Rope::from("\t \tfn new"); // 1 tab, 4 spaces, tab
109 assert_eq!(indent_level_for_line(line.slice(..)), 3);
146 assert_eq!(indent_level_for_line(line.slice(..)), 3);
110 }
147 }
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+ }
111}
169}
helix-core/src/state.rs+11
@@ -86,6 +86,17 @@ impl State {
86 Ok(state)
86 Ok(state)
87 }
87 }
88
88
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+
89 // TODO: doc/selection accessors
100 // TODO: doc/selection accessors
90
101
91 // TODO: be able to take either Rope or RopeSlice
102 // TODO: be able to take either Rope or RopeSlice
helix-core/src/syntax.rs+7
@@ -135,6 +135,13 @@ impl Loader {
135
135
136 // TODO: content_regex handling conflict resolution
136 // TODO: content_regex handling conflict resolution
137 }
137 }
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+ }
138}
145}
139
146
140//
147//
helix-view/src/commands.rs+15−1
@@ -435,7 +435,16 @@ pub mod insert {
435 }
435 }
436
436
437 pub fn insert_newline(view: &mut View, _count: usize) {
437 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);
439 }
448 }
440
449
441 // TODO: handle indent-aware delete
450 // TODO: handle indent-aware delete
@@ -605,3 +614,8 @@ pub fn unindent(view: &mut View, _count: usize) {
605 transaction.apply(&mut view.state);
614 transaction.apply(&mut view.state);
606 append_changes_to_history(view);
615 append_changes_to_history(view);
607}
616}
617+
618+pub fn indent_selection(view: &mut View, _count: usize) {
619+ // loop over each line and recompute proper indentation
620+ unimplemented!()
621+}