Skip to content

Commit 8139e8e

Browse files
committed
Auto merge of rust-lang#15425 - alibektas:deunwrap/convert_comment_block, r=Veykril
minor : Deunwrap convert_comment_block and desugar_doc_comment Closes subtask 13 of rust-lang#15398 . I still don't know a more idiomatic way for the for loops I added, any suggestion would make me happy.
2 parents 59bcbaf + 0a91a54 commit 8139e8e

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

crates/ide-assists/src/handlers/convert_comment_block.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ pub(crate) fn convert_comment_block(acc: &mut Assists, ctx: &AssistContext<'_>)
2525
let comment = ctx.find_token_at_offset::<ast::Comment>()?;
2626
// Only allow comments which are alone on their line
2727
if let Some(prev) = comment.syntax().prev_token() {
28-
if Whitespace::cast(prev).filter(|w| w.text().contains('\n')).is_none() {
29-
return None;
30-
}
28+
Whitespace::cast(prev).filter(|w| w.text().contains('\n'))?;
3129
}
3230

3331
match comment.kind().shape {
@@ -78,7 +76,7 @@ fn line_to_block(acc: &mut Assists, comment: ast::Comment) -> Option<()> {
7876
// Establish the target of our edit based on the comments we found
7977
let target = TextRange::new(
8078
comments[0].syntax().text_range().start(),
81-
comments.last().unwrap().syntax().text_range().end(),
79+
comments.last()?.syntax().text_range().end(),
8280
);
8381

8482
acc.add(
@@ -91,8 +89,12 @@ fn line_to_block(acc: &mut Assists, comment: ast::Comment) -> Option<()> {
9189
// contents of each line comment when they're put into the block comment.
9290
let indentation = IndentLevel::from_token(comment.syntax());
9391

94-
let block_comment_body =
95-
comments.into_iter().map(|c| line_comment_text(indentation, c)).join("\n");
92+
let block_comment_body = comments
93+
.into_iter()
94+
.map(|c| line_comment_text(indentation, c))
95+
.collect::<Vec<String>>()
96+
.into_iter()
97+
.join("\n");
9698

9799
let block_prefix =
98100
CommentKind { shape: CommentShape::Block, ..comment.kind() }.prefix();
@@ -160,7 +162,8 @@ pub(crate) fn relevant_line_comments(comment: &ast::Comment) -> Vec<Comment> {
160162
//
161163
// But since such comments aren't idiomatic we're okay with this.
162164
pub(crate) fn line_comment_text(indentation: IndentLevel, comm: ast::Comment) -> String {
163-
let contents_without_prefix = comm.text().strip_prefix(comm.prefix()).unwrap();
165+
let text = comm.text();
166+
let contents_without_prefix = text.strip_prefix(comm.prefix()).unwrap_or(text);
164167
let contents = contents_without_prefix.strip_prefix(' ').unwrap_or(contents_without_prefix);
165168

166169
// Don't add the indentation if the line is empty

crates/ide-assists/src/handlers/desugar_doc_comment.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ pub(crate) fn desugar_doc_comment(acc: &mut Assists, ctx: &AssistContext<'_>) ->
3333

3434
// Only allow comments which are alone on their line
3535
if let Some(prev) = comment.syntax().prev_token() {
36-
if Whitespace::cast(prev).filter(|w| w.text().contains('\n')).is_none() {
37-
return None;
38-
}
36+
Whitespace::cast(prev).filter(|w| w.text().contains('\n'))?;
3937
}
4038

4139
let indentation = IndentLevel::from_token(comment.syntax()).to_string();
@@ -50,7 +48,7 @@ pub(crate) fn desugar_doc_comment(acc: &mut Assists, ctx: &AssistContext<'_>) ->
5048
(
5149
TextRange::new(
5250
comments[0].syntax().text_range().start(),
53-
comments.last().unwrap().syntax().text_range().end(),
51+
comments.last()?.syntax().text_range().end(),
5452
),
5553
Either::Right(comments),
5654
)
@@ -71,9 +69,11 @@ pub(crate) fn desugar_doc_comment(acc: &mut Assists, ctx: &AssistContext<'_>) ->
7169
.map(|l| l.strip_prefix(&indentation).unwrap_or(l))
7270
.join("\n")
7371
}
74-
Either::Right(comments) => {
75-
comments.into_iter().map(|c| line_comment_text(IndentLevel(0), c)).join("\n")
76-
}
72+
Either::Right(comments) => comments
73+
.into_iter()
74+
.map(|cm| line_comment_text(IndentLevel(0), cm))
75+
.collect::<Vec<_>>()
76+
.join("\n"),
7777
};
7878

7979
let hashes = "#".repeat(required_hashes(&text));

0 commit comments

Comments
 (0)