diff options
| author | Aleksey Kladov <aleksey.kladov@gmail.com> | 2019-01-25 11:23:15 +0300 |
|---|---|---|
| committer | Aleksey Kladov <aleksey.kladov@gmail.com> | 2019-01-25 23:12:30 +0300 |
| commit | dbd28e4203cc268d8c1a80e8fa5dbfe6c042c061 (patch) | |
| tree | 0870d8e76717dbafb5373a0f322fef125e6b5ffe /crates/ra_ide_api_light/src | |
| parent | e53eab3f25af2ebd381012f1b34f0bc52c6951eb (diff) | |
| download | rust-dbd28e4203cc268d8c1a80e8fa5dbfe6c042c061.tar.gz rust-dbd28e4203cc268d8c1a80e8fa5dbfe6c042c061.zip | |
fix re-indent
Diffstat (limited to 'crates/ra_ide_api_light/src')
| -rw-r--r-- | crates/ra_ide_api_light/src/formatting.rs | 19 | ||||
| -rw-r--r-- | crates/ra_ide_api_light/src/typing.rs | 40 |
2 files changed, 56 insertions, 3 deletions
diff --git a/crates/ra_ide_api_light/src/formatting.rs b/crates/ra_ide_api_light/src/formatting.rs index ca0fdb928e8..1f34b85d693 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<Item = &SyntaxNode> { + 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 5ff2b7c1f47..861027b9fb0 100644 --- a/crates/ra_ide_api_light/src/typing.rs +++ b/crates/ra_ide_api_light/src/typing.rs @@ -296,6 +296,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( r" |
