@@ -1,6 +1,6 @@
11use crate::{
22 find_first_non_whitespace_char,
3− syntax::Syntax,
3+ syntax::{IndentQuery, LanguageConfiguration, Syntax},
44 tree_sitter::{Node, Tree},
55 Rope, RopeSlice,
66};
@@ -43,40 +43,11 @@ fn get_highest_syntax_node_at_bytepos(syntax: &Syntax, pos: usize) -> Option<Nod
4343 Some(node)
4444}
4545
46−fn calculate_indentation(node: Option<Node>, newline: bool) -> usize {
47− let mut increment: i32 = 0;
48−
49− // Hardcoded for rust for now
50− let indent_scopes = &[
51− "while_expression",
52− "for_expression",
53− "loop_expression",
54− "if_expression",
55− "if_let_expression",
56− // "match_expression",
57− // "match_arm",
58− "tuple_expression",
59− "array_expression",
60− // indent_except_first_scopes
61− "use_list",
62− "block",
63− "match_block",
64− "arguments",
65− "parameters",
66− "declaration_list",
67− "field_declaration_list",
68− "field_initializer_list",
69− "struct_pattern",
70− "tuple_pattern",
71− "enum_variant_list",
72− // "function_item",
73− // "closure_expression",
74− "binary_expression",
75− "field_expression",
76− "where_clause",
77− ];
46+fn calculate_indentation(query: &IndentQuery, node: Option<Node>, newline: bool) -> usize {
47+ // NOTE: can't use contains() on query because of comparing Vec<String> and &str
48+ // https://doc.rust-lang.org/std/vec/struct.Vec.html#method.contains
7849
79− let outdent = &["where", "}", "]", ")"];
50+ let mut increment: i32 = 0;
8051
8152 let mut node = match node {
8253 Some(node) => node,
@@ -88,7 +59,7 @@ fn calculate_indentation(node: Option<Node>, newline: bool) -> usize {
8859 // if we're calculating indentation for a brand new line then the current node will become the
8960 // parent node. We need to take it's indentation level into account too.
9061 let node_kind = node.kind();
91− if newline && indent_scopes.contains(&node_kind) {
62+ if newline && query.indent.contains(node_kind) {
9263 increment += 1;
9364 }
9465
@@ -102,14 +73,14 @@ fn calculate_indentation(node: Option<Node>, newline: bool) -> usize {
10273 // }) <-- }) is two scopes
10374 let starts_same_line = start == prev_start;
10475
105− if outdent.contains(&node.kind()) && !starts_same_line {
76+ if query.outdent.contains(node.kind()) && !starts_same_line {
10677 // we outdent by skipping the rules for the current level and jumping up
10778 // node = parent;
10879 increment -= 1;
10980 // continue;
11081 }
11182
112− if indent_scopes.contains(&parent_kind) // && not_first_or_last_sibling
83+ if query.indent.contains(parent_kind) // && not_first_or_last_sibling
11384 && !starts_same_line
11485 {
11586 // println!("is_scope {}", parent_kind);
@@ -128,6 +99,7 @@ fn calculate_indentation(node: Option<Node>, newline: bool) -> usize {
12899}
129100
130101fn suggested_indent_for_line(
102+ language_config: &LanguageConfiguration,
131103 syntax: Option<&Syntax>,
132104 text: RopeSlice,
133105 line_num: usize,
@@ -137,7 +109,7 @@ fn suggested_indent_for_line(
137109 let current = indent_level_for_line(line, tab_width);
138110
139111 if let Some(start) = find_first_non_whitespace_char(text, line_num) {
140− return suggested_indent_for_pos(syntax, text, start, false);
112+ return suggested_indent_for_pos(Some(language_config), syntax, text, start, false);
141113 };
142114
143115 // if the line is blank, indent should be zero
@@ -148,18 +120,24 @@ fn suggested_indent_for_line(
148120// - it should return 0 when mass indenting stuff
149121// - it should look up the wrapper node and count it too when we press o/O
150122pub fn suggested_indent_for_pos(
123+ language_config: Option<&LanguageConfiguration>,
151124 syntax: Option<&Syntax>,
152125 text: RopeSlice,
153126 pos: usize,
154127 new_line: bool,
155128) -> usize {
156− if let Some(syntax) = syntax {
129+ if let (Some(query), Some(syntax)) = (
130+ language_config.and_then(|config| config.indent_query()),
131+ syntax,
132+ ) {
157133 let byte_start = text.char_to_byte(pos);
158134 let node = get_highest_syntax_node_at_bytepos(syntax, byte_start);
159135
136+ // let config = load indentation query config from Syntax(should contain language_config)
137+
160138 // TODO: special case for comments
161139 // TODO: if preserve_leading_whitespace
162− calculate_indentation(node, new_line)
140+ calculate_indentation(query, node, new_line)
163141 } else {
164142 // TODO: heuristics for non-tree sitter grammars
165143 0
@@ -286,6 +264,7 @@ where
286264 tab_width: 4,
287265 unit: String::from(" "),
288266 }),
267+ indent_query: OnceCell::new(),
289268 }],
290269 });
291270
@@ -304,7 +283,7 @@ where
304283 let line = text.line(i);
305284 let indent = indent_level_for_line(line, tab_width);
306285 assert_eq!(
307− suggested_indent_for_line(Some(&syntax), text, i, tab_width),
286+ suggested_indent_for_line(&language_config, Some(&syntax), text, i, tab_width),
308287 indent,
309288 "line {}: {}",
310289 i,