653: fix re-indent r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-01-25 20:13:07 +00:00
commit 03fc615ead
2 changed files with 56 additions and 3 deletions

View file

@ -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> {

View file

@ -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(