@@ -78,21 +78,26 @@ fn line_to_block(acc: &mut Assists, comment: ast::Comment) -> Option<()> {
78
78
// Establish the target of our edit based on the comments we found
79
79
let target = TextRange :: new (
80
80
comments[ 0 ] . syntax ( ) . text_range ( ) . start ( ) ,
81
- comments. last ( ) . unwrap ( ) . syntax ( ) . text_range ( ) . end ( ) ,
81
+ comments. last ( ) ? . syntax ( ) . text_range ( ) . end ( ) ,
82
82
) ;
83
83
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
+
84
95
acc. add (
85
96
AssistId ( "line_to_block" , AssistKind :: RefactorRewrite ) ,
86
97
"Replace line comments with a single block comment" ,
87
98
target,
88
99
|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 " ) ;
96
101
97
102
let block_prefix =
98
103
CommentKind { shape : CommentShape :: Block , ..comment. kind ( ) } . prefix ( ) ;
@@ -159,15 +164,15 @@ pub(crate) fn relevant_line_comments(comment: &ast::Comment) -> Vec<Comment> {
159
164
// */
160
165
//
161
166
// 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 ( ) ) ? ;
164
169
let contents = contents_without_prefix. strip_prefix ( ' ' ) . unwrap_or ( contents_without_prefix) ;
165
170
166
171
// Don't add the indentation if the line is empty
167
172
if contents. is_empty ( ) {
168
- contents. to_owned ( )
173
+ Some ( contents. to_owned ( ) )
169
174
} else {
170
- indentation. to_string ( ) + contents
175
+ Some ( indentation. to_string ( ) + contents)
171
176
}
172
177
}
173
178
0 commit comments