]> git.lizzy.rs Git - rust.git/commitdiff
Fix inserting imports in front of inner attributes
authorLukas Wirth <lukastw97@gmail.com>
Thu, 3 Sep 2020 12:38:34 +0000 (14:38 +0200)
committerLukas Wirth <lukastw97@gmail.com>
Thu, 3 Sep 2020 16:36:08 +0000 (18:36 +0200)
crates/assists/src/handlers/replace_qualified_name_with_use.rs
crates/assists/src/utils/insert_use.rs

index 56e85125d7a389c279b40c63c67e475996245974..597bc268cff7475c90ba2a42e6eb51a572f53a49 100644 (file)
@@ -348,9 +348,9 @@ fn test_replace_add_to_nested_self_already_included() {
 impl std::fmt::nested<|> for Foo {
 }
 ",
-            // FIXME(veykril): self is being pulled out for some reason now
+            // FIXME(veykril): nested is duplicated now
             r"
-use std::fmt::{Debug, nested::{Display}, nested};
+use std::fmt::{Debug, nested::{self, Display}, nested};
 
 impl nested for Foo {
 }
@@ -518,6 +518,7 @@ fn main() {
     ",
             r"
 #![allow(dead_code)]
+
 use std::fmt::Debug;
 
 fn main() {
index f2acda6f32ee47073103bd23b4dc275cae2ce64a..1ddd7219720f7dd6cf86c5d6bae0e7cce62e6a1b 100644 (file)
@@ -275,7 +275,27 @@ fn find_insert_position(
                             (InsertPosition::After(node.into()), AddBlankLine::BeforeTwice)
                         }
                         // there are no imports in this file at all
-                        None => (InsertPosition::First, AddBlankLine::AfterTwice),
+                        None => {
+                            // check if the scope has a inner attributes, we dont want to insert in front of it
+                            match scope
+                                .children()
+                                // no flat_map here cause we want to short circuit the iterator
+                                .map(ast::Attr::cast)
+                                .take_while(|attr| {
+                                    attr.as_ref()
+                                        .map(|attr| attr.kind() == ast::AttrKind::Inner)
+                                        .unwrap_or(false)
+                                })
+                                .last()
+                                .flatten()
+                            {
+                                Some(attr) => (
+                                    InsertPosition::After(attr.syntax().clone().into()),
+                                    AddBlankLine::BeforeTwice,
+                                ),
+                                None => (InsertPosition::First, AddBlankLine::AfterTwice),
+                            }
+                        }
                     },
                 }
             }
@@ -459,6 +479,36 @@ fn insert_empty_file() {
         )
     }
 
+    #[test]
+    fn insert_after_inner_attr() {
+        // empty files will get two trailing newlines
+        // this is due to the test case insert_no_imports above
+        check_full(
+            "foo::bar",
+            r"#![allow(unused_imports)]",
+            r"#![allow(unused_imports)]
+
+use foo::bar;",
+        )
+    }
+
+    #[test]
+    fn insert_after_inner_attr2() {
+        // empty files will get two trailing newlines
+        // this is due to the test case insert_no_imports above
+        check_full(
+            "foo::bar",
+            r"#![allow(unused_imports)]
+
+fn main() {}",
+            r"#![allow(unused_imports)]
+
+use foo::bar;
+
+fn main() {}",
+        )
+    }
+
     #[test]
     fn merge_groups() {
         check_last("std::io", r"use std::fmt;", r"use std::{fmt, io};")