From dbd28e4203cc268d8c1a80e8fa5dbfe6c042c061 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 25 Jan 2019 11:23:15 +0300 Subject: fix re-indent --- crates/ra_ide_api_light/src/formatting.rs | 19 ++++++++++++--- crates/ra_ide_api_light/src/typing.rs | 40 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) (limited to 'crates/ra_ide_api_light/src') diff --git a/crates/ra_ide_api_light/src/formatting.rs b/crates/ra_ide_api_light/src/formatting.rs index ca0fdb928..1f34b85d6 100644 --- a/crates/ra_ide_api_light/src/formatting.rs +++ b/crates/ra_ide_api_light/src/formatting.rs @@ -7,9 +7,22 @@ use ra_syntax::{ /// If the node is on the beginning of the line, calculate indent. pub(crate) fn leading_indent(node: &SyntaxNode) -> Option<&str> { - let prev = prev_leaf(node)?; - let ws_text = ast::Whitespace::cast(prev)?.text(); - ws_text.rfind('\n').map(|pos| &ws_text[pos + 1..]) + for leaf in prev_leaves(node) { + if let Some(ws) = ast::Whitespace::cast(leaf) { + let ws_text = ws.text(); + if let Some(pos) = ws_text.rfind('\n') { + return Some(&ws_text[pos + 1..]); + } + } + if leaf.leaf_text().unwrap().contains('\n') { + break; + } + } + None +} + +fn prev_leaves(node: &SyntaxNode) -> impl Iterator { + generate(prev_leaf(node), |&node| prev_leaf(node)) } fn prev_leaf(node: &SyntaxNode) -> Option<&SyntaxNode> { diff --git a/crates/ra_ide_api_light/src/typing.rs b/crates/ra_ide_api_light/src/typing.rs index 5ff2b7c1f..861027b9f 100644 --- a/crates/ra_ide_api_light/src/typing.rs +++ b/crates/ra_ide_api_light/src/typing.rs @@ -295,6 +295,46 @@ fn foo() { ); } + #[test] + fn indents_middle_of_chain_call() { + type_dot( + r" + fn source_impl() { + let var = enum_defvariant_list().unwrap() + <|> + .nth(92) + .unwrap(); + } + ", + r" + fn source_impl() { + let var = enum_defvariant_list().unwrap() + . + .nth(92) + .unwrap(); + } + ", + ); + type_dot( + r" + fn source_impl() { + let var = enum_defvariant_list().unwrap() + <|> + .nth(92) + .unwrap(); + } + ", + r" + fn source_impl() { + let var = enum_defvariant_list().unwrap() + . + .nth(92) + .unwrap(); + } + ", + ); + } + #[test] fn dont_indent_freestanding_dot() { type_dot( -- cgit v1.2.3