diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs index 5bf1fc0ed53b..7ef58aa8eb52 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 ca128168a45a..302d62ab1129 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 d9591781dc8f..628cabc294cb 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,