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/add_missing_impl_members.rs | 6 +-- .../src/handlers/convert_comment_block.rs | 46 ++++++---------------- crates/syntax/src/ast/token_ext.rs | 5 ++- 3 files changed, 18 insertions(+), 39 deletions(-) diff --git a/crates/ide_assists/src/handlers/add_missing_impl_members.rs b/crates/ide_assists/src/handlers/add_missing_impl_members.rs index 63cea754d..0148635f9 100644 --- a/crates/ide_assists/src/handlers/add_missing_impl_members.rs +++ b/crates/ide_assists/src/handlers/add_missing_impl_members.rs @@ -3,9 +3,9 @@ use syntax::ast::{self, AstNode}; use crate::{ assist_context::{AssistContext, Assists}, - utils::add_trait_assoc_items_to_impl, - utils::DefaultMethods, - utils::{filter_assoc_items, render_snippet, Cursor}, + utils::{ + add_trait_assoc_items_to_impl, filter_assoc_items, render_snippet, Cursor, DefaultMethods, + }, AssistId, AssistKind, }; 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}; diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs index 090282d28..29d25a58a 100644 --- a/crates/syntax/src/ast/token_ext.rs +++ b/crates/syntax/src/ast/token_ext.rs @@ -102,8 +102,9 @@ impl CommentKind { kind } - fn prefix(&self) -> &'static str { - let &(prefix, _) = CommentKind::BY_PREFIX.iter().find(|(_, kind)| kind == self).unwrap(); + pub fn prefix(&self) -> &'static str { + let &(prefix, _) = + CommentKind::BY_PREFIX.iter().rev().find(|(_, kind)| kind == self).unwrap(); prefix } } -- cgit v1.2.3