@@ -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}