From 884ce4a4207ca68a5299b3a2e4e33b8f1f158001 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 13 Jan 2019 18:21:23 +0300 Subject: fix indent caclulation --- crates/ra_ide_api_light/src/formatting.rs | 10 +++++++++- crates/ra_ide_api_light/src/typing.rs | 32 +++++++++++++++++++++++++++++++ crates/test_utils/src/lib.rs | 20 +++++++++---------- 3 files changed, 51 insertions(+), 11 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide_api_light/src/formatting.rs b/crates/ra_ide_api_light/src/formatting.rs index 4635fbd60..599e3cdcb 100644 --- a/crates/ra_ide_api_light/src/formatting.rs +++ b/crates/ra_ide_api_light/src/formatting.rs @@ -2,15 +2,23 @@ use ra_syntax::{ AstNode, SyntaxNode, SyntaxKind::*, ast::{self, AstToken}, + algo::generate, }; /// If the node is on the begining of the line, calculate indent. pub(crate) fn leading_indent(node: &SyntaxNode) -> Option<&str> { - let prev = node.prev_sibling()?; + let prev = prev_leaf(node)?; let ws_text = ast::Whitespace::cast(prev)?.text(); ws_text.rfind('\n').map(|pos| &ws_text[pos + 1..]) } +fn prev_leaf(node: &SyntaxNode) -> Option<&SyntaxNode> { + generate(node.ancestors().find_map(SyntaxNode::prev_sibling), |it| { + it.last_child() + }) + .last() +} + pub(crate) fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> { let expr = block.expr()?; if expr.syntax().text().contains('\n') { diff --git a/crates/ra_ide_api_light/src/typing.rs b/crates/ra_ide_api_light/src/typing.rs index c8f3dfe44..5ff2b7c1f 100644 --- a/crates/ra_ide_api_light/src/typing.rs +++ b/crates/ra_ide_api_light/src/typing.rs @@ -227,6 +227,38 @@ fn foo() { ) } + #[test] + fn indents_new_chain_call_with_semi() { + type_dot( + r" + pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable> { + self.child_impl(db, name) + <|>; + } + ", + r" + pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable> { + self.child_impl(db, name) + .; + } + ", + ); + type_dot( + r" + pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable> { + self.child_impl(db, name) + <|>; + } + ", + r" + pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable> { + self.child_impl(db, name) + .; + } + ", + ) + } + #[test] fn indents_continued_chain_call() { type_dot( diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 012b1d0b4..9b1c9c9a0 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -12,18 +12,18 @@ pub const CURSOR_MARKER: &str = "<|>"; #[macro_export] macro_rules! assert_eq_text { - ($expected:expr, $actual:expr) => { - assert_eq_text!($expected, $actual,) + ($left:expr, $right:expr) => { + assert_eq_text!($left, $right,) }; - ($expected:expr, $actual:expr, $($tt:tt)*) => {{ - let expected = $expected; - let actual = $actual; - if expected != actual { - if expected.trim() == actual.trim() { - eprintln!("Expected:\n{:?}\n\nActual:\n{:?}\n\nWhitespace difference\n", expected, actual); + ($left:expr, $right:expr, $($tt:tt)*) => {{ + let left = $left; + let right = $right; + if left != right { + if left.trim() == right.trim() { + eprintln!("Left:\n{:?}\n\nRight:\n{:?}\n\nWhitespace difference\n", left, right); } else { - let changeset = $crate::__Changeset::new(actual, expected, "\n"); - eprintln!("Expected:\n{}\n\nActual:\n{}\n\nDiff:\n{}\n", expected, actual, changeset); + let changeset = $crate::__Changeset::new(right, left, "\n"); + eprintln!("Left:\n{}\n\nRight:\n{}\n\nDiff:\n{}\n", left, right, changeset); } eprintln!($($tt)*); panic!("text differs"); -- cgit v1.2.3