From 81561ff168e7357dfc03208be51fb4cbf3e3088c Mon Sep 17 00:00:00 2001 From: Shoyu Vanilla Date: Mon, 12 Aug 2024 23:36:28 +0900 Subject: [PATCH] fix: Trailing excess comma in "Convert to named struct" assist --- .../convert_tuple_struct_to_named_struct.rs | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs index 44f31dcb849..c72bd411d64 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs @@ -2,7 +2,7 @@ use ide_db::defs::{Definition, NameRefClass}; use syntax::{ ast::{self, AstNode, HasGenericParams, HasVisibility}, - match_ast, SyntaxNode, + match_ast, SyntaxKind, SyntaxNode, }; use crate::{assist_context::SourceChangeBuilder, AssistContext, AssistId, AssistKind, Assists}; @@ -100,7 +100,9 @@ fn edit_struct_def( ast::make::tokens::single_newline().text(), ); edit.insert(tuple_fields_text_range.start(), w.syntax().text()); - edit.insert(tuple_fields_text_range.start(), ","); + if !w.syntax().last_token().is_some_and(|t| t.kind() == SyntaxKind::COMMA) { + edit.insert(tuple_fields_text_range.start(), ","); + } edit.insert( tuple_fields_text_range.start(), ast::make::tokens::single_newline().text(), @@ -879,6 +881,29 @@ enum A { fn f() { let a = Variant { field1: Inner }; } +"#, + ); + } + + #[test] + fn where_clause_with_trailing_comma() { + check_assist( + convert_tuple_struct_to_named_struct, + r#" +trait Foo {} + +struct Bar$0(pub T) +where + T: Foo,; +"#, + r#" +trait Foo {} + +struct Bar +where + T: Foo, +{ pub field1: T } + "#, ); }