4850: Indent chain `.` even if there's more stuff afterwards r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-06-11 15:20:24 +00:00 committed by GitHub
commit 7c617000b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,7 +21,9 @@
use ra_syntax::{ use ra_syntax::{
algo::find_node_at_offset, algo::find_node_at_offset,
ast::{self, AstToken}, ast::{self, AstToken},
AstNode, SourceFile, TextRange, TextSize, AstNode, SourceFile,
SyntaxKind::{FIELD_EXPR, METHOD_CALL_EXPR},
TextRange, TextSize,
}; };
use ra_text_edit::TextEdit; use ra_text_edit::TextEdit;
@ -98,9 +100,12 @@ fn on_dot_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> {
}; };
let current_indent_len = TextSize::of(current_indent); let current_indent_len = TextSize::of(current_indent);
let parent = whitespace.syntax().parent();
// Make sure dot is a part of call chain // Make sure dot is a part of call chain
let field_expr = ast::FieldExpr::cast(whitespace.syntax().parent())?; if !matches!(parent.kind(), FIELD_EXPR | METHOD_CALL_EXPR) {
let prev_indent = leading_indent(field_expr.syntax())?; return None;
}
let prev_indent = leading_indent(&parent)?;
let target_indent = format!(" {}", prev_indent); let target_indent = format!(" {}", prev_indent);
let target_indent_len = TextSize::of(&target_indent); let target_indent_len = TextSize::of(&target_indent);
if current_indent_len == target_indent_len { if current_indent_len == target_indent_len {
@ -143,11 +148,11 @@ fn do_type_char(char_typed: char, before: &str) -> Option<String> {
}) })
} }
fn type_char(char_typed: char, before: &str, after: &str) { fn type_char(char_typed: char, ra_fixture_before: &str, ra_fixture_after: &str) {
let actual = do_type_char(char_typed, before) let actual = do_type_char(char_typed, ra_fixture_before)
.unwrap_or_else(|| panic!("typing `{}` did nothing", char_typed)); .unwrap_or_else(|| panic!("typing `{}` did nothing", char_typed));
assert_eq_text!(after, &actual); assert_eq_text!(ra_fixture_after, &actual);
} }
fn type_char_noop(char_typed: char, before: &str) { fn type_char_noop(char_typed: char, before: &str) {
@ -248,6 +253,27 @@ fn main() {
) )
} }
#[test]
fn indents_new_chain_call_with_let() {
type_char(
'.',
r#"
fn main() {
let _ = foo
<|>
bar()
}
"#,
r#"
fn main() {
let _ = foo
.
bar()
}
"#,
);
}
#[test] #[test]
fn indents_continued_chain_call() { fn indents_continued_chain_call() {
type_char( type_char(