Simplify calculate_indentation.

imgbot
Blaž Hrastnik 3 years ago
parent 180521fefe
commit 9445b24b88

@ -48,7 +48,6 @@ fn calculate_indentation(node: Option<Node>, newline: bool) -> usize {
// Hardcoded for rust for now // Hardcoded for rust for now
let indent_scopes = &[ let indent_scopes = &[
// indent except first or block?
"while_expression", "while_expression",
"for_expression", "for_expression",
"loop_expression", "loop_expression",
@ -56,14 +55,9 @@ fn calculate_indentation(node: Option<Node>, newline: bool) -> usize {
"if_let_expression", "if_let_expression",
// "match_expression", // "match_expression",
// "match_arm", // "match_arm",
];
// this is for multiline things, such as: // indent_except_first_scopes
// self.method() "use_list",
// .chain()
// .chain()
// where the first line isn't indented
let indent_except_first_scopes = &[
"block", "block",
"match_block", "match_block",
"arguments", "arguments",
@ -74,10 +68,7 @@ fn calculate_indentation(node: Option<Node>, newline: bool) -> usize {
// "closure_expression", // "closure_expression",
"binary_expression", "binary_expression",
"field_expression", "field_expression",
//
"where_clause", "where_clause",
//
"use_list",
]; ];
let outdent = &["where", "}", "]", ")"]; let outdent = &["where", "}", "]", ")"];
@ -92,36 +83,20 @@ fn calculate_indentation(node: Option<Node>, newline: bool) -> usize {
// if we're calculating indentation for a brand new line then the current node will become the // if we're calculating indentation for a brand new line then the current node will become the
// parent node. We need to take it's indentation level into account too. // parent node. We need to take it's indentation level into account too.
let node_kind = node.kind(); let node_kind = node.kind();
if newline if newline && indent_scopes.contains(&node_kind) {
&& (indent_scopes.contains(&node_kind) || indent_except_first_scopes.contains(&node_kind))
{
increment += 1; increment += 1;
} }
while let Some(parent) = node.parent() { while let Some(parent) = node.parent() {
let not_first_sibling = node.prev_sibling().is_some();
let not_last_sibling = node.next_sibling().is_some();
let not_first_or_last_sibling = not_first_sibling && not_last_sibling;
let parent_kind = parent.kind(); let parent_kind = parent.kind();
let start = parent.start_position().row; let start = parent.start_position().row;
// detect deeply nested indents in the same line // detect deeply nested indents in the same line
// .map(|a| { <-- ({ is two scopes
// let len = 1; <-- indents one level
// }) <-- }) is two scopes
let starts_same_line = start == prev_start; let starts_same_line = start == prev_start;
// log::error!(
// "name: {}\tparent: {}\trange:\t{} {}\tfirst={:?}\tlast={:?} start={} prev={} same_line={}",
// node.kind(),
// parent.kind(),
// node.range().start_point,
// node.range().end_point,
// node.prev_sibling().is_none(),
// node.next_sibling().is_none(),
// node.start_position(),
// prev_start,
// starts_same_line
// );
if outdent.contains(&node.kind()) && !starts_same_line { if outdent.contains(&node.kind()) && !starts_same_line {
// we outdent by skipping the rules for the current level and jumping up // we outdent by skipping the rules for the current level and jumping up
// node = parent; // node = parent;
@ -129,15 +104,7 @@ fn calculate_indentation(node: Option<Node>, newline: bool) -> usize {
// continue; // continue;
} }
// TODO: problem seems to be, ({ is two scopes that merge into one. if indent_scopes.contains(&parent_kind) // && not_first_or_last_sibling
// so when seeing } we're supposed to jump all the way out of both scopes, but we only do
// so for one.
// .map(|a| {
// let len = 1;
// })
if (indent_scopes.contains(&parent_kind) // && not_first_or_last_sibling
|| indent_except_first_scopes.contains(&parent_kind))
&& !starts_same_line && !starts_same_line
{ {
// println!("is_scope {}", parent_kind); // println!("is_scope {}", parent_kind);
@ -145,11 +112,6 @@ fn calculate_indentation(node: Option<Node>, newline: bool) -> usize {
increment += 1 increment += 1
} }
// TODO: detect deeply nested indents in same line:
// std::panic::set_hook(Box::new(move |info| {
// hook(info); <-- indent here is 1
// }));
// if last_scope && increment > 0 && ...{ ignore } // if last_scope && increment > 0 && ...{ ignore }
node = parent; node = parent;

Loading…
Cancel
Save