aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-01-13 15:21:23 +0000
committerAleksey Kladov <[email protected]>2019-01-13 15:21:23 +0000
commit884ce4a4207ca68a5299b3a2e4e33b8f1f158001 (patch)
treeed8a3c14e19a67426c9b08cebb8e34ae91c8f8ab /crates
parentc46e0300e6cebf78c78c28ca91e45b57e4e40954 (diff)
fix indent caclulation
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_ide_api_light/src/formatting.rs10
-rw-r--r--crates/ra_ide_api_light/src/typing.rs32
-rw-r--r--crates/test_utils/src/lib.rs20
3 files changed, 51 insertions, 11 deletions
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::{
2 AstNode, 2 AstNode,
3 SyntaxNode, SyntaxKind::*, 3 SyntaxNode, SyntaxKind::*,
4 ast::{self, AstToken}, 4 ast::{self, AstToken},
5 algo::generate,
5}; 6};
6 7
7/// If the node is on the begining of the line, calculate indent. 8/// If the node is on the begining of the line, calculate indent.
8pub(crate) fn leading_indent(node: &SyntaxNode) -> Option<&str> { 9pub(crate) fn leading_indent(node: &SyntaxNode) -> Option<&str> {
9 let prev = node.prev_sibling()?; 10 let prev = prev_leaf(node)?;
10 let ws_text = ast::Whitespace::cast(prev)?.text(); 11 let ws_text = ast::Whitespace::cast(prev)?.text();
11 ws_text.rfind('\n').map(|pos| &ws_text[pos + 1..]) 12 ws_text.rfind('\n').map(|pos| &ws_text[pos + 1..])
12} 13}
13 14
15fn prev_leaf(node: &SyntaxNode) -> Option<&SyntaxNode> {
16 generate(node.ancestors().find_map(SyntaxNode::prev_sibling), |it| {
17 it.last_child()
18 })
19 .last()
20}
21
14pub(crate) fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> { 22pub(crate) fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> {
15 let expr = block.expr()?; 23 let expr = block.expr()?;
16 if expr.syntax().text().contains('\n') { 24 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
@@ -228,6 +228,38 @@ fn foo() {
228 } 228 }
229 229
230 #[test] 230 #[test]
231 fn indents_new_chain_call_with_semi() {
232 type_dot(
233 r"
234 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
235 self.child_impl(db, name)
236 <|>;
237 }
238 ",
239 r"
240 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
241 self.child_impl(db, name)
242 .;
243 }
244 ",
245 );
246 type_dot(
247 r"
248 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
249 self.child_impl(db, name)
250 <|>;
251 }
252 ",
253 r"
254 pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
255 self.child_impl(db, name)
256 .;
257 }
258 ",
259 )
260 }
261
262 #[test]
231 fn indents_continued_chain_call() { 263 fn indents_continued_chain_call() {
232 type_dot( 264 type_dot(
233 r" 265 r"
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 = "<|>";
12 12
13#[macro_export] 13#[macro_export]
14macro_rules! assert_eq_text { 14macro_rules! assert_eq_text {
15 ($expected:expr, $actual:expr) => { 15 ($left:expr, $right:expr) => {
16 assert_eq_text!($expected, $actual,) 16 assert_eq_text!($left, $right,)
17 }; 17 };
18 ($expected:expr, $actual:expr, $($tt:tt)*) => {{ 18 ($left:expr, $right:expr, $($tt:tt)*) => {{
19 let expected = $expected; 19 let left = $left;
20 let actual = $actual; 20 let right = $right;
21 if expected != actual { 21 if left != right {
22 if expected.trim() == actual.trim() { 22 if left.trim() == right.trim() {
23 eprintln!("Expected:\n{:?}\n\nActual:\n{:?}\n\nWhitespace difference\n", expected, actual); 23 eprintln!("Left:\n{:?}\n\nRight:\n{:?}\n\nWhitespace difference\n", left, right);
24 } else { 24 } else {
25 let changeset = $crate::__Changeset::new(actual, expected, "\n"); 25 let changeset = $crate::__Changeset::new(right, left, "\n");
26 eprintln!("Expected:\n{}\n\nActual:\n{}\n\nDiff:\n{}\n", expected, actual, changeset); 26 eprintln!("Left:\n{}\n\nRight:\n{}\n\nDiff:\n{}\n", left, right, changeset);
27 } 27 }
28 eprintln!($($tt)*); 28 eprintln!($($tt)*);
29 panic!("text differs"); 29 panic!("text differs");