Simplify code

changelog: skip
This commit is contained in:
Aleksey Kladov 2021-03-23 19:41:15 +03:00
parent 860e069d4d
commit e33959a888
3 changed files with 18 additions and 39 deletions

View file

@ -3,9 +3,9 @@ use syntax::ast::{self, AstNode};
use crate::{ use crate::{
assist_context::{AssistContext, Assists}, assist_context::{AssistContext, Assists},
utils::add_trait_assoc_items_to_impl, utils::{
utils::DefaultMethods, add_trait_assoc_items_to_impl, filter_assoc_items, render_snippet, Cursor, DefaultMethods,
utils::{filter_assoc_items, render_snippet, Cursor}, },
AssistId, AssistKind, AssistId, AssistKind,
}; };

View file

@ -1,13 +1,6 @@
use itertools::Itertools; use itertools::Itertools;
use syntax::{ use syntax::{
ast::{ ast::{self, edit::IndentLevel, Comment, CommentKind, CommentShape, Whitespace},
self,
edit::IndentLevel,
Comment, CommentKind,
CommentPlacement::{Inner, Outer},
CommentShape::{self, Block, Line},
Whitespace,
},
AstToken, Direction, SyntaxElement, TextRange, AstToken, Direction, SyntaxElement, TextRange,
}; };
@ -29,7 +22,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
/// */ /// */
/// ``` /// ```
pub(crate) fn convert_comment_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { pub(crate) fn convert_comment_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
if let Some(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() { if Whitespace::cast(prev).filter(|w| w.text().contains('\n')).is_none() {
@ -37,13 +30,10 @@ pub(crate) fn convert_comment_block(acc: &mut Assists, ctx: &AssistContext) -> O
} }
} }
return match comment.kind().shape { match comment.kind().shape {
ast::CommentShape::Block => block_to_line(acc, comment), ast::CommentShape::Block => block_to_line(acc, comment),
ast::CommentShape::Line => line_to_block(acc, comment), ast::CommentShape::Line => line_to_block(acc, comment),
};
} }
return None;
} }
fn block_to_line(acc: &mut Assists, comment: ast::Comment) -> Option<()> { 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, target,
|edit| { |edit| {
let indentation = IndentLevel::from_token(comment.syntax()); let indentation = IndentLevel::from_token(comment.syntax());
let line_prefix = let line_prefix = CommentKind { shape: CommentShape::Line, ..comment.kind() }.prefix();
comment_kind_prefix(CommentKind { shape: CommentShape::Line, ..comment.kind() });
let text = comment.text(); let text = comment.text();
let text = &text[comment.prefix().len()..(text.len() - "*/".len())].trim(); 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"); comments.into_iter().map(|c| line_comment_text(indentation, c)).join("\n");
let block_prefix = let block_prefix =
comment_kind_prefix(CommentKind { shape: CommentShape::Block, ..comment.kind() }); CommentKind { shape: CommentShape::Block, ..comment.kind() }.prefix();
let output = let output =
format!("{}\n{}\n{}*/", block_prefix, block_comment_body, indentation.to_string()); 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)] #[cfg(test)]
mod tests { mod tests {
use crate::tests::{check_assist, check_assist_not_applicable}; use crate::tests::{check_assist, check_assist_not_applicable};

View file

@ -102,8 +102,9 @@ impl CommentKind {
kind kind
} }
fn prefix(&self) -> &'static str { pub fn prefix(&self) -> &'static str {
let &(prefix, _) = CommentKind::BY_PREFIX.iter().find(|(_, kind)| kind == self).unwrap(); let &(prefix, _) =
CommentKind::BY_PREFIX.iter().rev().find(|(_, kind)| kind == self).unwrap();
prefix prefix
} }
} }