From 279a1ae5646aa474f6e6b6d2a0b9c248e7e0460c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 11 Jun 2020 17:13:24 +0200 Subject: Indent chain `.` even if there's more stuff afterwards --- crates/ra_ide/src/typing.rs | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide/src/typing.rs b/crates/ra_ide/src/typing.rs index 533306e2e..83776d2b6 100644 --- a/crates/ra_ide/src/typing.rs +++ b/crates/ra_ide/src/typing.rs @@ -21,7 +21,9 @@ use ra_ide_db::{source_change::SourceFileEdit, RootDatabase}; use ra_syntax::{ algo::find_node_at_offset, ast::{self, AstToken}, - AstNode, SourceFile, TextRange, TextSize, + AstNode, SourceFile, + SyntaxKind::{FIELD_EXPR, METHOD_CALL_EXPR}, + TextRange, TextSize, }; use ra_text_edit::TextEdit; @@ -98,9 +100,12 @@ fn on_dot_typed(file: &SourceFile, offset: TextSize) -> Option { }; let current_indent_len = TextSize::of(current_indent); + let parent = whitespace.syntax().parent(); // Make sure dot is a part of call chain - let field_expr = ast::FieldExpr::cast(whitespace.syntax().parent())?; - let prev_indent = leading_indent(field_expr.syntax())?; + if !matches!(parent.kind(), FIELD_EXPR | METHOD_CALL_EXPR) { + return None; + } + let prev_indent = leading_indent(&parent)?; let target_indent = format!(" {}", prev_indent); let target_indent_len = TextSize::of(&target_indent); if current_indent_len == target_indent_len { @@ -143,11 +148,11 @@ mod tests { }) } - fn type_char(char_typed: char, before: &str, after: &str) { - let actual = do_type_char(char_typed, before) + fn type_char(char_typed: char, ra_fixture_before: &str, ra_fixture_after: &str) { + let actual = do_type_char(char_typed, ra_fixture_before) .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) { @@ -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] fn indents_continued_chain_call() { type_char( -- cgit v1.2.3