From 97f41d7343a4801f296cd6a7a45ece9249c15886 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 22 Apr 2019 00:52:21 +0300 Subject: test short structs --- crates/ra_assists/src/ast_editor.rs | 15 +++++++++++---- crates/ra_assists/src/fill_struct_fields.rs | 27 +++++++++++++++++++++++++++ crates/ra_syntax/src/syntax_node.rs | 1 + 3 files changed, 39 insertions(+), 4 deletions(-) (limited to 'crates') diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs index 5bf1fc0ed..7ef58aa8e 100644 --- a/crates/ra_assists/src/ast_editor.rs +++ b/crates/ra_assists/src/ast_editor.rs @@ -110,13 +110,20 @@ impl AstEditor { let position = match position { InsertPosition::First => after_l_curly!(), - InsertPosition::Last => match self.ast().fields().last() { - Some(it) => after_field!(it), - None => after_l_curly!(), - }, + InsertPosition::Last => { + if !is_multiline { + // don't insert comma before curly + to_insert.pop(); + } + match self.ast().fields().last() { + Some(it) => after_field!(it), + None => after_l_curly!(), + } + } InsertPosition::Before(anchor) => InsertPosition::Before(anchor.syntax().into()), InsertPosition::After(anchor) => after_field!(anchor), }; + self.ast = self.insert_children(position, to_insert.iter().cloned()); } diff --git a/crates/ra_assists/src/fill_struct_fields.rs b/crates/ra_assists/src/fill_struct_fields.rs index ca128168a..302d62ab1 100644 --- a/crates/ra_assists/src/fill_struct_fields.rs +++ b/crates/ra_assists/src/fill_struct_fields.rs @@ -194,4 +194,31 @@ mod tests { "#, ); } + + #[test] + fn fill_struct_short() { + check_assist( + fill_struct_fields, + r#" + struct S { + foo: u32, + bar: String, + } + + fn main() { + let s = S {<|> }; + } + "#, + r#" + struct S { + foo: u32, + bar: String, + } + + fn main() { + let s = <|>S { foo: (), bar: () }; + } + "#, + ); + } } diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs index d9591781d..628cabc29 100644 --- a/crates/ra_syntax/src/syntax_node.rs +++ b/crates/ra_syntax/src/syntax_node.rs @@ -24,6 +24,7 @@ use crate::{ pub use rowan::WalkEvent; pub(crate) use rowan::{GreenNode, GreenToken}; +#[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum InsertPosition { First, Last, -- cgit v1.2.3