Auto merge of #15425 - alibektas:deunwrap/convert_comment_block, r=Veykril

minor : Deunwrap convert_comment_block and desugar_doc_comment

Closes subtask 13 of #15398 . I still don't know a more idiomatic way for the for loops I added, any suggestion would make me happy.
This commit is contained in:
bors 2023-09-22 15:47:55 +00:00
commit 8139e8e072
2 changed files with 17 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 {
@ -78,7 +76,7 @@ fn line_to_block(acc: &mut Assists, comment: ast::Comment) -> Option<()> {
// Establish the target of our edit based on the comments we found // Establish the target of our edit based on the comments we found
let target = TextRange::new( let target = TextRange::new(
comments[0].syntax().text_range().start(), comments[0].syntax().text_range().start(),
comments.last().unwrap().syntax().text_range().end(), comments.last()?.syntax().text_range().end(),
); );
acc.add( acc.add(
@ -91,8 +89,12 @@ 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 block_comment_body = let block_comment_body = comments
comments.into_iter().map(|c| line_comment_text(indentation, c)).join("\n"); .into_iter()
.map(|c| line_comment_text(indentation, c))
.collect::<Vec<String>>()
.into_iter()
.join("\n");
let block_prefix = let block_prefix =
CommentKind { shape: CommentShape::Block, ..comment.kind() }.prefix(); CommentKind { shape: CommentShape::Block, ..comment.kind() }.prefix();
@ -160,7 +162,8 @@ 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) -> String { pub(crate) fn line_comment_text(indentation: IndentLevel, comm: ast::Comment) -> String {
let contents_without_prefix = comm.text().strip_prefix(comm.prefix()).unwrap(); let text = comm.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

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();
@ -50,7 +48,7 @@ pub(crate) fn desugar_doc_comment(acc: &mut Assists, ctx: &AssistContext<'_>) ->
( (
TextRange::new( TextRange::new(
comments[0].syntax().text_range().start(), comments[0].syntax().text_range().start(),
comments.last().unwrap().syntax().text_range().end(), comments.last()?.syntax().text_range().end(),
), ),
Either::Right(comments), Either::Right(comments),
) )
@ -71,9 +69,11 @@ pub(crate) fn desugar_doc_comment(acc: &mut Assists, ctx: &AssistContext<'_>) ->
.map(|l| l.strip_prefix(&indentation).unwrap_or(l)) .map(|l| l.strip_prefix(&indentation).unwrap_or(l))
.join("\n") .join("\n")
} }
Either::Right(comments) => { Either::Right(comments) => comments
comments.into_iter().map(|c| line_comment_text(IndentLevel(0), c)).join("\n") .into_iter()
} .map(|cm| line_comment_text(IndentLevel(0), cm))
.collect::<Vec<_>>()
.join("\n"),
}; };
let hashes = "#".repeat(required_hashes(&text)); let hashes = "#".repeat(required_hashes(&text));