Skip to content

Commit

Permalink
Migrate all existing indentation queries.
Browse files Browse the repository at this point in the history
Add more options to ComplexNode and use them to improve C/C++ indentation.
  • Loading branch information
Triton171 committed Jan 23, 2022
1 parent c417b9e commit eac73eb
Show file tree
Hide file tree
Showing 27 changed files with 107 additions and 58 deletions.
56 changes: 45 additions & 11 deletions helix-core/src/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,39 +267,54 @@ fn get_first_in_line(mut node: Node, byte_pos: usize, new_line: bool) -> Vec<boo
result
}

// This assumes that the name matches and checks for all the other conditions
// This assumes that the kind matches and checks for all the other conditions
fn matches<'a>(query_node: &IndentQueryNode, node: Node<'a>, cursor: &mut TreeCursor<'a>) -> bool {
match query_node {
IndentQueryNode::SimpleNode(_) => true,
IndentQueryNode::ComplexNode {
kind: _,
field_name,
kind_not_in,
parent_kind_in,
field_name_in,
} => {
if let Some(kind_not_in) = kind_not_in {
let kind = node.kind();
if kind_not_in.iter().any(|k| k == kind) {
return false;
}
}
if let Some(parent_kind_in) = parent_kind_in {
let parent_matches = node.parent().map_or(false, |p| {
parent_kind_in.iter().any(|kind| kind.as_str() == p.kind())
let parent_kind = p.kind();
parent_kind_in
.iter()
.any(|kind| kind.as_str() == parent_kind)
});
if !parent_matches {
return false;
}
}
if let Some(field_name) = field_name {
if let Some(field_name_in) = field_name_in {
let parent = match node.parent() {
None => {
return false;
}
Some(p) => p,
};
let mut found_child = false;
for child in parent.children_by_field_name(field_name, cursor) {
if child == node {
found_child = true;
cursor.reset(parent);
debug_assert!(cursor.goto_first_child());
loop {
if cursor.node() == node {
if let Some(cursor_name) = cursor.field_name() {
if !field_name_in.iter().any(|n| n == cursor_name) {
return false;
}
} else {
return false;
}
break;
}
}
if !found_child {
return false;
debug_assert!(cursor.goto_next_sibling());
}
}
true
Expand Down Expand Up @@ -525,6 +540,25 @@ mod test {
really_long_variable_name_using_up_the_line |=
really_long_fn_that_should_definitely_go_on_the_next_line();
let (
a_long_variable_name_in_this_tuple,
b_long_variable_name_in_this_tuple,
c_long_variable_name_in_this_tuple,
d_long_variable_name_in_this_tuple,
e_long_variable_name_in_this_tuple,
): (usize, usize, usize, usize, usize) =
if really_long_fn_that_should_definitely_go_on_the_next_line() {
(
03294239434,
1213412342314,
21231234134,
834534234549898789,
9879234234543853457,
)
} else {
(0, 1, 2, 3, 4)
};
let test_function = function_with_param(this_param,
that_param
);
Expand Down
3 changes: 2 additions & 1 deletion helix-core/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ pub enum IndentQueryNode {
// A node given by a list of characteristics which must all be fulfilled in order to match
ComplexNode {
kind: Option<String>,
field_name: Option<String>,
kind_not_in: Option<Vec<String>>,
parent_kind_in: Option<Vec<String>>,
field_name_in: Option<Vec<String>>,
},
}
impl IndentQueryNode {
Expand Down
11 changes: 9 additions & 2 deletions runtime/queries/c/indents.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
indent = [
[indent]
all = [
# Indent non-grouped bodies of if-, while- and do-while-statements.
# TODO Add indent for the for-statement (ideally the body of a for-statement should have a field name in the tree-sitter grammar)
{ kind_not_in = ["compound_statement"], parent_kind_in = ["if_statement", "while_statement", "do_statement"], field_name_in = ["consequence", "body"] }
]
tail = [
"compound_statement",
"field_declaration_list",
"enumerator_list",
Expand All @@ -9,7 +15,8 @@ indent = [
"expression_statement",
]

outdent = [
[outdent]
all = [
"case",
"}",
"]",
Expand Down
4 changes: 2 additions & 2 deletions runtime/queries/cmake/indents.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
indent = [
indent.tail = [
"if_condition",
"foreach_loop",
"while_loop",
Expand All @@ -7,6 +7,6 @@ indent = [
"normal_command",
]

outdent = [
outdent.all = [
")"
]
11 changes: 9 additions & 2 deletions runtime/queries/cpp/indents.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
indent = [
[indent]
all = [
# Indent non-grouped bodies of if-, while- and do-while-statements.
# TODO Add indent for the for-statement (ideally the body of a for-statement should have a field name in the tree-sitter grammar)
{ kind_not_in = ["compound_statement"], parent_kind_in = ["if_statement", "while_statement", "do_statement"], field_name_in = ["consequence", "body"] }
]
tail = [
"compound_statement",
"field_declaration_list",
"enumerator_list",
Expand All @@ -9,7 +15,8 @@ indent = [
"expression_statement",
]

outdent = [
[outdent]
all = [
"case",
"access_specifier",
"}",
Expand Down
4 changes: 2 additions & 2 deletions runtime/queries/dart/indents.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
indent = [
indent.tail = [
"class_body",
"function_body",
"function_expression_body",
Expand All @@ -13,7 +13,7 @@ indent = [
"arguments"
]

outdent = [
outdent.all = [
"}",
"]",
")"
Expand Down
4 changes: 2 additions & 2 deletions runtime/queries/fish/indents.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
indent = [
indent.tail = [
"function_definition",
"while_statement",
"for_statement",
Expand All @@ -7,6 +7,6 @@ indent = [
"switch_statement",
]

outdent = [
outdent.all = [
"end"
]
4 changes: 2 additions & 2 deletions runtime/queries/glsl/indents.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
indent = [
indent.tail = [
"init_declarator",
"compound_statement",
"preproc_arg",
Expand All @@ -10,7 +10,7 @@ indent = [
"compound_literal_expression"
]

outdent = [
outdent.all = [
"#define",
"#ifdef",
"#endif",
Expand Down
4 changes: 2 additions & 2 deletions runtime/queries/javascript/indents.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
indent = [
indent.tail = [
"array",
"object",
"arguments",
Expand All @@ -21,7 +21,7 @@ indent = [
"object_type",
]

outdent = [
outdent.all = [
"}",
"]",
")"
Expand Down
4 changes: 2 additions & 2 deletions runtime/queries/json/indents.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
indent = [
indent.tail = [
"object",
"array"
]

outdent = [
outdent.all = [
"]",
"}"
]
2 changes: 1 addition & 1 deletion runtime/queries/llvm-mir-yaml/indents.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
indent = [
indent.tail = [
"block_mapping_pair",
]
4 changes: 2 additions & 2 deletions runtime/queries/llvm-mir/indents.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
indent = [
indent.tail = [
"basic_block",
]

outdent = [
outdent.all = [
"label",
]
4 changes: 2 additions & 2 deletions runtime/queries/llvm/indents.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
indent = [
indent.tail = [
"function_body",
"instruction",
]

outdent = [
outdent.all = [
"}",
]
2 changes: 1 addition & 1 deletion runtime/queries/lua/indents.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
indent = [
indent.tail = [
"function_definition",
"variable_declaration",
"local_variable_declaration",
Expand Down
4 changes: 2 additions & 2 deletions runtime/queries/nix/indents.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
indent = [
indent.tail = [
# "function",
"bind",
"assert",
Expand All @@ -12,7 +12,7 @@ indent = [
"parenthesized",
]

outdent = [
outdent.all = [
"}",
"]",
]
4 changes: 2 additions & 2 deletions runtime/queries/ocaml/indents.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
indent = [
indent.tail = [
"let_binding",
"type_binding",
"structure",
Expand All @@ -8,6 +8,6 @@ indent = [
"match_case",
]

outdent = [
outdent.all = [
"}",
]
4 changes: 2 additions & 2 deletions runtime/queries/perl/indents.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
indent = [
indent.tail = [
"function",
"identifier",
"method_invocation",
Expand All @@ -12,6 +12,6 @@ indent = [
"word_list_qw"
]

outdent = [
outdent.all = [
"}"
]
2 changes: 1 addition & 1 deletion runtime/queries/php/indents.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
indent = [
indent.tail = [
"array_creation_expression",
"arguments",
"formal_parameters",
Expand Down
4 changes: 2 additions & 2 deletions runtime/queries/protobuf/indents.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
indent = [
indent.tail = [
"messageBody",
"enumBody",
"oneofBody",
Expand All @@ -7,6 +7,6 @@ indent = [
"msgLit",
]

outdent = [
outdent.all = [
"}",
]
4 changes: 2 additions & 2 deletions runtime/queries/python/indents.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
indent = [
indent.tail = [
"list",
"tuple",
"dictionary",
Expand Down Expand Up @@ -27,7 +27,7 @@ indent = [
"class_definition",
]

outdent = [
outdent.all = [
")",
"]",
"}",
Expand Down
4 changes: 2 additions & 2 deletions runtime/queries/ruby/indents.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
indent = [
indent.tail = [
"argument_list",
"array",
"begin",
Expand All @@ -16,7 +16,7 @@ indent = [
"singleton_method",
]

outdent = [
outdent.all = [
")",
"}",
"]",
Expand Down
4 changes: 2 additions & 2 deletions runtime/queries/rust/indents.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[indent]
all = [
{ parent_kind_in = ["assignment_expression", "compound_assignment_expr"], field_name = "right"},
{ parent_kind_in = ["let_declaration"], field_name = "value" }
{ parent_kind_in = ["assignment_expression", "compound_assignment_expr"], field_name_in = ["right"]},
{ parent_kind_in = ["let_declaration"], field_name_in = ["value"] }
]
tail = [
"use_list",
Expand Down
4 changes: 2 additions & 2 deletions runtime/queries/scala/indents.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

indent = [
indent.tail = [
"block",
"arguments",
"parameter",
Expand All @@ -16,7 +16,7 @@ indent = [
"match_expression"
]

outdent = [
outdent.all = [
"}",
"]",
")"
Expand Down
4 changes: 2 additions & 2 deletions runtime/queries/svelte/indents.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
indent = [
indent.tail = [
"element"
"if_statement"
"each_statement"
"await_statement"
]

outdent = [
outdent.all = [
"end_tag"
"else_statement"
"if_end_expr"
Expand Down
Loading

0 comments on commit eac73eb

Please sign in to comment.