aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2020-09-03 13:38:34 +0100
committerLukas Wirth <[email protected]>2020-09-03 17:36:08 +0100
commit98e2f674e9e736720d1cd0a5b8c24e1fb10f64a1 (patch)
treeff50bd1b83004a247acd359498f75b775a441a66
parentc1925df7fc91a171925ecb59b9f1895ee59ce72f (diff)
Fix inserting imports in front of inner attributes
-rw-r--r--crates/assists/src/handlers/replace_qualified_name_with_use.rs5
-rw-r--r--crates/assists/src/utils/insert_use.rs52
2 files changed, 54 insertions, 3 deletions
diff --git a/crates/assists/src/handlers/replace_qualified_name_with_use.rs b/crates/assists/src/handlers/replace_qualified_name_with_use.rs
index 56e85125d..597bc268c 100644
--- a/crates/assists/src/handlers/replace_qualified_name_with_use.rs
+++ b/crates/assists/src/handlers/replace_qualified_name_with_use.rs
@@ -348,9 +348,9 @@ use std::fmt::{Debug, nested::{self, Display}};
348impl std::fmt::nested<|> for Foo { 348impl std::fmt::nested<|> for Foo {
349} 349}
350", 350",
351 // FIXME(veykril): self is being pulled out for some reason now 351 // FIXME(veykril): nested is duplicated now
352 r" 352 r"
353use std::fmt::{Debug, nested::{Display}, nested}; 353use std::fmt::{Debug, nested::{self, Display}, nested};
354 354
355impl nested for Foo { 355impl nested for Foo {
356} 356}
@@ -518,6 +518,7 @@ fn main() {
518 ", 518 ",
519 r" 519 r"
520#![allow(dead_code)] 520#![allow(dead_code)]
521
521use std::fmt::Debug; 522use std::fmt::Debug;
522 523
523fn main() { 524fn main() {
diff --git a/crates/assists/src/utils/insert_use.rs b/crates/assists/src/utils/insert_use.rs
index f2acda6f3..1ddd72197 100644
--- a/crates/assists/src/utils/insert_use.rs
+++ b/crates/assists/src/utils/insert_use.rs
@@ -275,7 +275,27 @@ fn find_insert_position(
275 (InsertPosition::After(node.into()), AddBlankLine::BeforeTwice) 275 (InsertPosition::After(node.into()), AddBlankLine::BeforeTwice)
276 } 276 }
277 // there are no imports in this file at all 277 // there are no imports in this file at all
278 None => (InsertPosition::First, AddBlankLine::AfterTwice), 278 None => {
279 // check if the scope has a inner attributes, we dont want to insert in front of it
280 match scope
281 .children()
282 // no flat_map here cause we want to short circuit the iterator
283 .map(ast::Attr::cast)
284 .take_while(|attr| {
285 attr.as_ref()
286 .map(|attr| attr.kind() == ast::AttrKind::Inner)
287 .unwrap_or(false)
288 })
289 .last()
290 .flatten()
291 {
292 Some(attr) => (
293 InsertPosition::After(attr.syntax().clone().into()),
294 AddBlankLine::BeforeTwice,
295 ),
296 None => (InsertPosition::First, AddBlankLine::AfterTwice),
297 }
298 }
279 }, 299 },
280 } 300 }
281 } 301 }
@@ -460,6 +480,36 @@ fn main() {}",
460 } 480 }
461 481
462 #[test] 482 #[test]
483 fn insert_after_inner_attr() {
484 // empty files will get two trailing newlines
485 // this is due to the test case insert_no_imports above
486 check_full(
487 "foo::bar",
488 r"#![allow(unused_imports)]",
489 r"#![allow(unused_imports)]
490
491use foo::bar;",
492 )
493 }
494
495 #[test]
496 fn insert_after_inner_attr2() {
497 // empty files will get two trailing newlines
498 // this is due to the test case insert_no_imports above
499 check_full(
500 "foo::bar",
501 r"#![allow(unused_imports)]
502
503fn main() {}",
504 r"#![allow(unused_imports)]
505
506use foo::bar;
507
508fn main() {}",
509 )
510 }
511
512 #[test]
463 fn merge_groups() { 513 fn merge_groups() {
464 check_last("std::io", r"use std::fmt;", r"use std::{fmt, io};") 514 check_last("std::io", r"use std::fmt;", r"use std::{fmt, io};")
465 } 515 }