@@ -9,7 +9,7 @@ use crate::{
99
1010pub const TAB_WIDTH: usize = 4;
1111
12−fn indent_level_for_line(line: RopeSlice) -> usize {
12+fn indent_level_for_line(line: &RopeSlice) -> usize {
1313 let mut len = 0;
1414 for ch in line.chars() {
1515 match ch {
@@ -45,7 +45,7 @@ fn get_highest_syntax_node_at_bytepos(syntax: &Syntax, pos: usize) -> Option<Nod
4545}
4646
4747fn calculate_indentation(node: Option<Node>, newline: bool) -> usize {
48− let mut increment = 0;
48+ let mut increment: i32 = 0;
4949
5050 // Hardcoded for rust for now
5151 let indent_scopes = &[
@@ -55,8 +55,8 @@ fn calculate_indentation(node: Option<Node>, newline: bool) -> usize {
5555 "loop_expression",
5656 "if_expression",
5757 "if_let_expression",
58− "match_expression",
59− "match_arm",
58+ // "match_expression",
59+ // "match_arm",
6060 ];
6161
6262 // this is for multiline things, such as:
@@ -66,26 +66,28 @@ fn calculate_indentation(node: Option<Node>, newline: bool) -> usize {
6666 // where the first line isn't indented
6767 let indent_except_first_scopes = &[
6868 "block",
69+ "match_block",
6970 "arguments",
7071 "declaration_list",
7172 "field_declaration_list",
7273 "enum_variant_list",
7374 // "function_item",
74− // "call_expression",
7575 // "closure_expression",
7676 "binary_expression",
7777 "field_expression",
7878 //
79− "where_predicate", // where_clause instead?
79+ "where_clause",
8080 ];
8181
82− let outdent = &["}", "]", ")"];
82+ let outdent = &["where", "}", "]", ")"];
8383
8484 let mut node = match node {
8585 Some(node) => node,
8686 None => return 0,
8787 };
8888
89+ let mut prev_start = node.start_position().row;
90+
8991 // if we're calculating indentation for a brand new line then the current node will become the
9092 // parent node. We need to take it's indentation level into account too.
9193 let node_kind = node.kind();
@@ -101,45 +103,60 @@ fn calculate_indentation(node: Option<Node>, newline: bool) -> usize {
101103 let not_first_or_last_sibling = not_first_sibling && not_last_sibling;
102104
103105 let parent_kind = parent.kind();
106+ let start = parent.start_position().row;
104107
105108 // println!(
106− // "name: {}\tparent: {}\trange:\t{} {}\tfirst={:?}\tlast={:?}",
109+ // "name: {}\tparent: {}\trange:\t{} {}\tfirst={:?}\tlast={:?} start={} prev={}",
107110 // node.kind(),
108111 // parent.kind(),
109112 // node.range().start_point,
110113 // node.range().end_point,
111114 // node.prev_sibling().is_none(),
112115 // node.next_sibling().is_none(),
116+ // node.start_position(),
117+ // prev_start,
113118 // );
114119
120+ // detect deeply nested indents in the same line
121+ let starts_same_line = start == prev_start;
122+
115123 if outdent.contains(&node.kind()) {
116124 // we outdent by skipping the rules for the current level and jumping up
117− node = parent;
118− continue;
125+ // println!("skipping..");
126+ // node = parent;
127+ increment -= 1;
128+ // continue;
119129 }
120130
121− let is_scope = indent_scopes.contains(&parent_kind);
122−
123− // && not_first_or_last_sibling
124− if is_scope && not_first_or_last_sibling {
131+ // TODO: problem seems to be, ({ is two scopes that merge into one.
132+ // so when seeing } we're supposed to jump all the way out of both scopes, but we only do
133+ // so for one.
134+ // .map(|a| {
135+ // let len = 1;
136+ // })
137+
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
141+ {
125142 // println!("is_scope {}", parent_kind);
143+ prev_start = start;
126144 increment += 1
127145 }
128146
129− let is_scope = indent_except_first_scopes.contains(&parent_kind);
130−
131− // && not_first_sibling
132− if is_scope && not_first_sibling {
133− // println!("is_scope_except_first {}", parent_kind);
134− increment += 1
135− }
147+ // TODO: detect deeply nested indents in same line:
148+ // std::panic::set_hook(Box::new(move |info| {
149+ // hook(info); <-- indent here is 1
150+ // }));
136151
137152 // if last_scope && increment > 0 && ...{ ignore }
138153
139154 node = parent;
140155 }
141156
142− increment
157+ assert!(increment >= 0);
158+
159+ increment as usize
143160}
144161
145162fn 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
160177
161178fn suggested_indent_for_line(syntax: Option<&Syntax>, state: &State, line_num: usize) -> usize {
162179 let line = state.doc.line(line_num);
163− let current = indent_level_for_line(line);
180+ let current = indent_level_for_line(&line);
164181
165182 if let Some(start) = find_first_non_whitespace_char(state, line_num) {
166183 return suggested_indent_for_pos(syntax, state, start, false);
@@ -199,12 +216,12 @@ mod test {
199216 #[test]
200217 fn test_indent_level() {
201218 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);
203220 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);
205222 // mixed indentation
206223 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);
208225 }
209226
210227 #[test]
@@ -255,7 +272,26 @@ where
255272}
256273#[test]
257274//
275+match test {
276+ Some(a) => 1,
277+ None => {
278+ unimplemented!()
279+ }
280+}
281+std::panic::set_hook(Box::new(move |info| {
282+ hook(info);
283+}));
284+
285+{ { {
286+ 1
287+}}}
258288
289+pub fn change<I>(state: &State, changes: I) -> Self
290+where
291+ I: IntoIterator<Item = Change> + ExactSizeIterator,
292+{
293+ true
294+}
259295",
260296 );
261297
@@ -266,50 +302,18 @@ where
266302 .unwrap();
267303 let highlight_config = language_config.highlight_config(&[]).unwrap().unwrap();
268304 let syntax = Syntax::new(&state.doc, highlight_config.clone());
269−
270− // {
271− // {
272− // 1 + 1
273− // }
274− // }
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); // }
280−
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
305−
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− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 32), 2); // return;
312− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 33), 1); // }
313− assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 34), 0); // }
305+ let text = state.doc.slice(..);
306+
307+ for i in 0..state.doc.len_lines() {
308+ let line = text.line(i);
309+ let indent = indent_level_for_line(&line);
310+ assert_eq!(
311+ suggested_indent_for_line(Some(&syntax), &state, i),
312+ indent,
313+ "line {}: {}",
314+ i,
315+ line
316+ );
317+ }
314318 }
315319}