mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 09:27:27 +00:00
Merge #653
653: fix re-indent r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
03fc615ead
2 changed files with 56 additions and 3 deletions
|
@ -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> {
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in a new issue