]> git.lizzy.rs Git - rust.git/commitdiff
Merge #8174
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>
Tue, 23 Mar 2021 16:44:28 +0000 (16:44 +0000)
committerGitHub <noreply@github.com>
Tue, 23 Mar 2021 16:44:28 +0000 (16:44 +0000)
8174: Simplify code r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
crates/ide_assists/src/handlers/add_missing_impl_members.rs
crates/ide_assists/src/handlers/convert_comment_block.rs
crates/syntax/src/ast/token_ext.rs

index 63cea754dae4fb44bae7b5eb7480024e4285c053..0148635f9eba29eb69a509966976a7132df8362d 100644 (file)
@@ -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,
 };
 
index 9dc3ee28fd168b93aba734d1d6db7c2db499426d..d202a85f958883845b0ee53f593fcf006dfaab77 100644 (file)
@@ -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,
 };
 
 ///   */
 /// ```
 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};
index 090282d2800ca80e258fda4e39cee2149d7c75ca..29d25a58a310fa5409215f176001ab0eaf912d25 100644 (file)
@@ -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
     }
 }