@@ -78,21 +78,26 @@ fn line_to_block(acc: &mut Assists, comment: ast::Comment) -> Option<()> {
7878 // Establish the target of our edit based on the comments we found
7979 let target = TextRange :: new (
8080 comments[ 0 ] . syntax ( ) . text_range ( ) . start ( ) ,
81- comments. last ( ) . unwrap ( ) . syntax ( ) . text_range ( ) . end ( ) ,
81+ comments. last ( ) ? . syntax ( ) . text_range ( ) . end ( ) ,
8282 ) ;
8383
84+ // We pick a single indentation level for the whole block comment based on the
85+ // comment where the assist was invoked. This will be prepended to the
86+ // contents of each line comment when they're put into the block comment.
87+ let indentation = IndentLevel :: from_token ( comment. syntax ( ) ) ;
88+
89+ let mut cms: Vec < String > = Vec :: new ( ) ;
90+ for cm in comments {
91+ let lcm = line_comment_text ( indentation, cm) ?;
92+ cms. push ( lcm) ;
93+ }
94+
8495 acc. add (
8596 AssistId ( "line_to_block" , AssistKind :: RefactorRewrite ) ,
8697 "Replace line comments with a single block comment" ,
8798 target,
8899 |edit| {
89- // We pick a single indentation level for the whole block comment based on the
90- // comment where the assist was invoked. This will be prepended to the
91- // contents of each line comment when they're put into the block comment.
92- let indentation = IndentLevel :: from_token ( comment. syntax ( ) ) ;
93-
94- let block_comment_body =
95- comments. into_iter ( ) . map ( |c| line_comment_text ( indentation, c) ) . join ( "\n " ) ;
100+ let block_comment_body = cms. into_iter ( ) . join ( "\n " ) ;
96101
97102 let block_prefix =
98103 CommentKind { shape : CommentShape :: Block , ..comment. kind ( ) } . prefix ( ) ;
@@ -159,15 +164,15 @@ pub(crate) fn relevant_line_comments(comment: &ast::Comment) -> Vec<Comment> {
159164// */
160165//
161166// But since such comments aren't idiomatic we're okay with this.
162- pub ( crate ) fn line_comment_text ( indentation : IndentLevel , comm : ast:: Comment ) -> String {
163- let contents_without_prefix = comm. text ( ) . strip_prefix ( comm. prefix ( ) ) . unwrap ( ) ;
167+ pub ( crate ) fn line_comment_text ( indentation : IndentLevel , comm : ast:: Comment ) -> Option < String > {
168+ let contents_without_prefix = comm. text ( ) . strip_prefix ( comm. prefix ( ) ) ? ;
164169 let contents = contents_without_prefix. strip_prefix ( ' ' ) . unwrap_or ( contents_without_prefix) ;
165170
166171 // Don't add the indentation if the line is empty
167172 if contents. is_empty ( ) {
168- contents. to_owned ( )
173+ Some ( contents. to_owned ( ) )
169174 } else {
170- indentation. to_string ( ) + contents
175+ Some ( indentation. to_string ( ) + contents)
171176 }
172177}
173178
0 commit comments