Skip to content

Commit 9c62571

Browse files
committed
Deunwrap convert_comment_block
1 parent 326f37e commit 9c62571

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,30 @@ pub(crate) fn desugar_doc_comment(acc: &mut Assists, ctx: &AssistContext<'_>) ->
5757
}
5858
};
5959

60+
let text = match comments {
61+
Either::Left(comment) => {
62+
let text = comment.text();
63+
text[comment.prefix().len()..(text.len() - "*/".len())]
64+
.trim()
65+
.lines()
66+
.map(|l| l.strip_prefix(&indentation).unwrap_or(l))
67+
.join("\n")
68+
}
69+
Either::Right(comments) => {
70+
let mut cms: Vec<String> = Vec::new();
71+
for cm in comments {
72+
let lcm = line_comment_text(IndentLevel(0), cm)?;
73+
cms.push(lcm);
74+
}
75+
cms.into_iter().join("\n")
76+
}
77+
};
78+
6079
acc.add(
6180
AssistId("desugar_doc_comment", AssistKind::RefactorRewrite),
6281
"Desugar doc-comment to attribute macro",
6382
target,
6483
|edit| {
65-
let text = match comments {
66-
Either::Left(comment) => {
67-
let text = comment.text();
68-
text[comment.prefix().len()..(text.len() - "*/".len())]
69-
.trim()
70-
.lines()
71-
.map(|l| l.strip_prefix(&indentation).unwrap_or(l))
72-
.join("\n")
73-
}
74-
Either::Right(comments) => {
75-
comments.into_iter().map(|c| line_comment_text(IndentLevel(0), c)).join("\n")
76-
}
77-
};
78-
7984
let hashes = "#".repeat(required_hashes(&text));
8085

8186
let prefix = match placement {

0 commit comments

Comments
 (0)