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 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'crates/ra_ide_api_light/src/formatting.rs') 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> { -- cgit v1.2.3