Indent chain . even if there's more stuff afterwards

This commit is contained in:
Aleksey Kladov 2020-06-11 17:13:24 +02:00
parent 32157d48f4
commit 279a1ae564

View file

@ -21,7 +21,9 @@ use ra_ide_db::{source_change::SourceFileEdit, RootDatabase};
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 @@ mod tests {
}) })
} }
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 foo() {
) )
} }
#[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(