From e33959a8889104f6bda06755df9ade933aadcf4f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 23 Mar 2021 19:41:15 +0300 Subject: Simplify code changelog: skip --- .../src/handlers/convert_comment_block.rs | 46 ++++++---------------- 1 file changed, 12 insertions(+), 34 deletions(-) (limited to 'crates/ide_assists/src/handlers/convert_comment_block.rs') diff --git a/crates/ide_assists/src/handlers/convert_comment_block.rs b/crates/ide_assists/src/handlers/convert_comment_block.rs index 9dc3ee28f..d202a85f9 100644 --- a/crates/ide_assists/src/handlers/convert_comment_block.rs +++ b/crates/ide_assists/src/handlers/convert_comment_block.rs @@ -1,13 +1,6 @@ use itertools::Itertools; use syntax::{ - ast::{ - self, - edit::IndentLevel, - Comment, CommentKind, - CommentPlacement::{Inner, Outer}, - CommentShape::{self, Block, Line}, - Whitespace, - }, + ast::{self, edit::IndentLevel, Comment, CommentKind, CommentShape, Whitespace}, AstToken, Direction, SyntaxElement, TextRange, }; @@ -29,21 +22,18 @@ use crate::{AssistContext, AssistId, AssistKind, Assists}; /// */ /// ``` pub(crate) fn convert_comment_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { - if let Some(comment) = ctx.find_token_at_offset::() { - // Only allow comments which are alone on their line - if let Some(prev) = comment.syntax().prev_token() { - if Whitespace::cast(prev).filter(|w| w.text().contains('\n')).is_none() { - return None; - } + let comment = ctx.find_token_at_offset::()?; + // Only allow comments which are alone on their line + if let Some(prev) = comment.syntax().prev_token() { + if Whitespace::cast(prev).filter(|w| w.text().contains('\n')).is_none() { + return None; } - - return match comment.kind().shape { - ast::CommentShape::Block => block_to_line(acc, comment), - ast::CommentShape::Line => line_to_block(acc, comment), - }; } - return None; + match comment.kind().shape { + ast::CommentShape::Block => block_to_line(acc, comment), + ast::CommentShape::Line => line_to_block(acc, comment), + } } fn block_to_line(acc: &mut Assists, comment: ast::Comment) -> Option<()> { @@ -55,8 +45,7 @@ fn block_to_line(acc: &mut Assists, comment: ast::Comment) -> Option<()> { target, |edit| { let indentation = IndentLevel::from_token(comment.syntax()); - let line_prefix = - comment_kind_prefix(CommentKind { shape: CommentShape::Line, ..comment.kind() }); + let line_prefix = CommentKind { shape: CommentShape::Line, ..comment.kind() }.prefix(); let text = comment.text(); let text = &text[comment.prefix().len()..(text.len() - "*/".len())].trim(); @@ -105,7 +94,7 @@ fn line_to_block(acc: &mut Assists, comment: ast::Comment) -> Option<()> { comments.into_iter().map(|c| line_comment_text(indentation, c)).join("\n"); let block_prefix = - comment_kind_prefix(CommentKind { shape: CommentShape::Block, ..comment.kind() }); + CommentKind { shape: CommentShape::Block, ..comment.kind() }.prefix(); let output = format!("{}\n{}\n{}*/", block_prefix, block_comment_body, indentation.to_string()); @@ -182,17 +171,6 @@ fn line_comment_text(indentation: IndentLevel, comm: ast::Comment) -> String { } } -fn comment_kind_prefix(ck: ast::CommentKind) -> &'static str { - match (ck.shape, ck.doc) { - (Line, Some(Inner)) => "//!", - (Line, Some(Outer)) => "///", - (Line, None) => "//", - (Block, Some(Inner)) => "/*!", - (Block, Some(Outer)) => "/**", - (Block, None) => "/*", - } -} - #[cfg(test)] mod tests { use crate::tests::{check_assist, check_assist_not_applicable}; -- cgit v1.2.3