@@ -9,7 +9,7 @@ use crate::{
10pub const TAB_WIDTH: usize = 4;
10pub const TAB_WIDTH: usize = 4;
12−fn indent_level_for_line(line: RopeSlice) -> usize {
12+fn indent_level_for_line(line: &RopeSlice) -> usize {
13 let mut len = 0;
13 let mut len = 0;
14 for ch in line.chars() {
14 for ch in line.chars() {
15 match ch {
15 match ch {
@@ -45,7 +45,7 @@ fn get_highest_syntax_node_at_bytepos(syntax: &Syntax, pos: usize) -> Option<Nod
47fn calculate_indentation(node: Option<Node>, newline: bool) -> usize {
47fn calculate_indentation(node: Option<Node>, newline: bool) -> usize {
48− let mut increment = 0;
48+ let mut increment: i32 = 0;
50 // Hardcoded for rust for now
50 // Hardcoded for rust for now
51 let indent_scopes = &[
51 let indent_scopes = &[
@@ -55,8 +55,8 @@ fn calculate_indentation(node: Option<Node>, newline: bool) -> usize {
55 "loop_expression",
55 "loop_expression",
56 "if_expression",
56 "if_expression",
57 "if_let_expression",
57 "if_let_expression",
58− "match_expression",
58+ // "match_expression",
59− "match_arm",
59+ // "match_arm",
62 // this is for multiline things, such as:
62 // this is for multiline things, such as:
@@ -66,26 +66,28 @@ fn calculate_indentation(node: Option<Node>, newline: bool) -> usize {
66 // where the first line isn't indented
66 // where the first line isn't indented
67 let indent_except_first_scopes = &[
67 let indent_except_first_scopes = &[
69 "arguments",
70 "arguments",
70 "declaration_list",
71 "declaration_list",
71 "field_declaration_list",
72 "field_declaration_list",
72 "enum_variant_list",
73 "enum_variant_list",
73 // "function_item",
74 // "function_item",
74− // "call_expression",
75 // "closure_expression",
75 // "closure_expression",
76 "binary_expression",
76 "binary_expression",
77 "field_expression",
77 "field_expression",
79− "where_predicate", // where_clause instead?
79+ "where_clause",
82− let outdent = &["}", "]", ")"];
82+ let outdent = &["where", "}", "]", ")"];
84 let mut node = match node {
84 let mut node = match node {
85 Some(node) => node,
85 Some(node) => node,
86 None => return 0,
86 None => return 0,
89+ let mut prev_start = node.start_position().row;
89 // if we're calculating indentation for a brand new line then the current node will become the
91 // if we're calculating indentation for a brand new line then the current node will become the
90 // parent node. We need to take it's indentation level into account too.
92 // parent node. We need to take it's indentation level into account too.
91 let node_kind = node.kind();
93 let node_kind = node.kind();
@@ -101,45 +103,60 @@ fn calculate_indentation(node: Option<Node>, newline: bool) -> usize {
101 let not_first_or_last_sibling = not_first_sibling && not_last_sibling;
103 let not_first_or_last_sibling = not_first_sibling && not_last_sibling;
103 let parent_kind = parent.kind();
105 let parent_kind = parent.kind();
106+ let start = parent.start_position().row;
105 // println!(
108 // println!(
106− // "name: {}\tparent: {}\trange:\t{} {}\tfirst={:?}\tlast={:?}",
109+ // "name: {}\tparent: {}\trange:\t{} {}\tfirst={:?}\tlast={:?} start={} prev={}",
107 // node.kind(),
110 // node.kind(),
108 // parent.kind(),
111 // parent.kind(),
109 // node.range().start_point,
112 // node.range().start_point,
110 // node.range().end_point,
113 // node.range().end_point,
111 // node.prev_sibling().is_none(),
114 // node.prev_sibling().is_none(),
112 // node.next_sibling().is_none(),
115 // node.next_sibling().is_none(),
116+ // node.start_position(),
120+ // detect deeply nested indents in the same line
121+ let starts_same_line = start == prev_start;
115 if outdent.contains(&node.kind()) {
123 if outdent.contains(&node.kind()) {
116 // we outdent by skipping the rules for the current level and jumping up
124 // we outdent by skipping the rules for the current level and jumping up
125+ // println!("skipping..");
117− node = parent;
126+ // node = parent;
118− continue;
128+ // continue;
121− let is_scope = indent_scopes.contains(&parent_kind);
131+ // TODO: problem seems to be, ({ is two scopes that merge into one.
122−
132+ // so when seeing } we're supposed to jump all the way out of both scopes, but we only do
123− // && not_first_or_last_sibling
133+ // so for one.
124− if is_scope && not_first_or_last_sibling {
138+ if ((indent_scopes.contains(&parent_kind) && true) // not_first_or_last_sibling
139+ || (indent_except_first_scopes.contains(&parent_kind) && true))
140+ && !starts_same_line
125 // println!("is_scope {}", parent_kind);
142 // println!("is_scope {}", parent_kind);
126 increment += 1
144 increment += 1
129− let is_scope = indent_except_first_scopes.contains(&parent_kind);
147+ // TODO: detect deeply nested indents in same line:
130−
148+ // std::panic::set_hook(Box::new(move |info| {
131− // && not_first_sibling
149+ // hook(info); <-- indent here is 1
132− if is_scope && not_first_sibling {
150+ // }));
133− // println!("is_scope_except_first {}", parent_kind);
137 // if last_scope && increment > 0 && ...{ ignore }
152 // if last_scope && increment > 0 && ...{ ignore }
139 node = parent;
154 node = parent;
142− increment
157+ assert!(increment >= 0);
145fn find_first_non_whitespace_char(state: &State, line_num: usize) -> Option<usize> {
162fn find_first_non_whitespace_char(state: &State, line_num: usize) -> Option<usize> {
@@ -160,7 +177,7 @@ fn find_first_non_whitespace_char(state: &State, line_num: usize) -> Option<usiz
161fn suggested_indent_for_line(syntax: Option<&Syntax>, state: &State, line_num: usize) -> usize {
178fn suggested_indent_for_line(syntax: Option<&Syntax>, state: &State, line_num: usize) -> usize {
162 let line = state.doc.line(line_num);
179 let line = state.doc.line(line_num);
163− let current = indent_level_for_line(line);
180+ let current = indent_level_for_line(&line);
165 if let Some(start) = find_first_non_whitespace_char(state, line_num) {
182 if let Some(start) = find_first_non_whitespace_char(state, line_num) {
166 return suggested_indent_for_pos(syntax, state, start, false);
183 return suggested_indent_for_pos(syntax, state, start, false);
@@ -199,12 +216,12 @@ mod test {
200 fn test_indent_level() {
217 fn test_indent_level() {
201 let line = Rope::from(" fn new"); // 8 spaces
218 let line = Rope::from(" fn new"); // 8 spaces
202− assert_eq!(indent_level_for_line(line.slice(..)), 2);
219+ assert_eq!(indent_level_for_line(&line.slice(..)), 2);
203 let line = Rope::from("\t\t\tfn new"); // 3 tabs
220 let line = Rope::from("\t\t\tfn new"); // 3 tabs
204− assert_eq!(indent_level_for_line(line.slice(..)), 3);
221+ assert_eq!(indent_level_for_line(&line.slice(..)), 3);
205 // mixed indentation
222 // mixed indentation
206 let line = Rope::from("\t \tfn new"); // 1 tab, 4 spaces, tab
223 let line = Rope::from("\t \tfn new"); // 1 tab, 4 spaces, tab
207− assert_eq!(indent_level_for_line(line.slice(..)), 3);
224+ assert_eq!(indent_level_for_line(&line.slice(..)), 3);
@@ -255,7 +272,26 @@ where
281+std::panic::set_hook(Box::new(move |info| {
289+pub fn change<I>(state: &State, changes: I) -> Self
291+ I: IntoIterator<Item = Change> + ExactSizeIterator,
@@ -266,50 +302,18 @@ where
266 .unwrap();
302 .unwrap();
267 let highlight_config = language_config.highlight_config(&[]).unwrap().unwrap();
303 let highlight_config = language_config.highlight_config(&[]).unwrap().unwrap();
268 let syntax = Syntax::new(&state.doc, highlight_config.clone());
304 let syntax = Syntax::new(&state.doc, highlight_config.clone());
269−
305+ let text = state.doc.slice(..);
271− // {
307+ for i in 0..state.doc.len_lines() {
272− // 1 + 1
308+ let line = text.line(i);
273− // }
309+ let indent = indent_level_for_line(&line);
274− // }
310+ assert_eq!(
275− // assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 1), 0); // {
276− // assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 2), 1); // {
277− // assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 3), 2); //
278− // assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 4), 1); // }
279− // assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 5), 0); // }
281− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 1), 0); // mod
282− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 2), 1); // fn
283− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 3), 2); // 1 + 1
284− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 4), 0); //
285− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 5), 2); // does_indentation_work
286− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 7), 2); // let test_function
287− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 8), 3); // that_param
288− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 9), 2); // );
289− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 10), 0); //
290− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 11), 2); // let test_function
291− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 12), 3); // this_param
292− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 13), 3); // that_param
293− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 14), 2); // );
294− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 15), 0); //
295− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 16), 2); // let test_function
296− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 17), 3); // param2
297− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 18), 2); // );
298− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 20), 2); // let selection
299− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 21), 3); // changes
300− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 22), 4); // clone()
301− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 23), 4); // map()
302− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 24), 5); // let len
303− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 25), 5); // let pos
304− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 26), 5); // Range
306− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 27), 4); // })
307− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 28), 4); // .collect(),
308− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 29), 3); // 0,
309− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 30), 2); // })
310− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 31), 0); //
311+ suggested_indent_for_line(Some(&syntax), &state, i),
311− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 32), 2); // return;
312+ indent,
312− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 33), 1); // }
313+ "line {}: {}",
313− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 34), 0); // }
314+ i,