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,
};
/// */
/// ```
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<()> {
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();
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());
}
}
-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};