Merge #8174
8174: Simplify code r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
c220b34095
@ -3,9 +3,9 @@
|
||||
|
||||
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,
|
||||
};
|
||||
|
||||
|
@ -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 @@
|
||||
/// */
|
||||
/// ```
|
||||
pub(crate) fn convert_comment_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
||||
if let Some(comment) = ctx.find_token_at_offset::<ast::Comment>() {
|
||||
// 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::<ast::Comment>()?;
|
||||
// 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};
|
||||
|
@ -102,8 +102,9 @@ pub(crate) fn from_text(text: &str) -> 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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user