This commit is contained in:
Ali Bektas 2023-09-10 23:15:37 +02:00
parent a66dbd11ed
commit 2fdf7e4b75
2 changed files with 8 additions and 14 deletions

View file

@ -25,9 +25,7 @@ pub(crate) fn convert_comment_block(acc: &mut Assists, ctx: &AssistContext<'_>)
let comment = ctx.find_token_at_offset::<ast::Comment>()?; let comment = ctx.find_token_at_offset::<ast::Comment>()?;
// Only allow comments which are alone on their line // Only allow comments which are alone on their line
if let Some(prev) = comment.syntax().prev_token() { if let Some(prev) = comment.syntax().prev_token() {
if Whitespace::cast(prev).filter(|w| w.text().contains('\n')).is_none() { Whitespace::cast(prev).filter(|w| w.text().contains('\n'))?;
return None;
}
} }
match comment.kind().shape { match comment.kind().shape {
@ -86,10 +84,8 @@ fn line_to_block(acc: &mut Assists, comment: ast::Comment) -> Option<()> {
// contents of each line comment when they're put into the block comment. // contents of each line comment when they're put into the block comment.
let indentation = IndentLevel::from_token(comment.syntax()); let indentation = IndentLevel::from_token(comment.syntax());
let cms = comments let cms =
.into_iter() comments.into_iter().map(|c| line_comment_text(indentation, c)).collect::<Vec<String>>();
.map(|c| line_comment_text(indentation, c))
.collect::<Option<Vec<String>>>()?;
acc.add( acc.add(
AssistId("line_to_block", AssistKind::RefactorRewrite), AssistId("line_to_block", AssistKind::RefactorRewrite),
@ -163,16 +159,16 @@ pub(crate) fn relevant_line_comments(comment: &ast::Comment) -> Vec<Comment> {
// */ // */
// //
// But since such comments aren't idiomatic we're okay with this. // But since such comments aren't idiomatic we're okay with this.
pub(crate) fn line_comment_text(indentation: IndentLevel, comm: ast::Comment) -> Option<String> { pub(crate) fn line_comment_text(indentation: IndentLevel, comm: ast::Comment) -> String {
let text = comm.text(); let text = comm.text();
let contents_without_prefix = text.strip_prefix(comm.prefix()).unwrap_or(text); let contents_without_prefix = text.strip_prefix(comm.prefix()).unwrap_or(text);
let contents = contents_without_prefix.strip_prefix(' ').unwrap_or(contents_without_prefix); let contents = contents_without_prefix.strip_prefix(' ').unwrap_or(contents_without_prefix);
// Don't add the indentation if the line is empty // Don't add the indentation if the line is empty
if contents.is_empty() { if contents.is_empty() {
Some(contents.to_owned()) contents.to_owned()
} else { } else {
Some(indentation.to_string() + contents) indentation.to_string() + contents
} }
} }

View file

@ -33,9 +33,7 @@ pub(crate) fn desugar_doc_comment(acc: &mut Assists, ctx: &AssistContext<'_>) ->
// Only allow comments which are alone on their line // Only allow comments which are alone on their line
if let Some(prev) = comment.syntax().prev_token() { if let Some(prev) = comment.syntax().prev_token() {
if Whitespace::cast(prev).filter(|w| w.text().contains('\n')).is_none() { Whitespace::cast(prev).filter(|w| w.text().contains('\n'))?;
return None;
}
} }
let indentation = IndentLevel::from_token(comment.syntax()).to_string(); let indentation = IndentLevel::from_token(comment.syntax()).to_string();
@ -69,7 +67,7 @@ pub(crate) fn desugar_doc_comment(acc: &mut Assists, ctx: &AssistContext<'_>) ->
Either::Right(comments) => comments Either::Right(comments) => comments
.into_iter() .into_iter()
.map(|cm| line_comment_text(IndentLevel(0), cm)) .map(|cm| line_comment_text(IndentLevel(0), cm))
.collect::<Option<Vec<_>>>()? .collect::<Vec<_>>()
.join("\n"), .join("\n"),
}; };