Skip to content

Commit 98e2f67

Browse files
committed
Fix inserting imports in front of inner attributes
1 parent c1925df commit 98e2f67

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

crates/assists/src/handlers/replace_qualified_name_with_use.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,9 @@ use std::fmt::{Debug, nested::{self, Display}};
348348
impl std::fmt::nested<|> for Foo {
349349
}
350350
",
351-
// FIXME(veykril): self is being pulled out for some reason now
351+
// FIXME(veykril): nested is duplicated now
352352
r"
353-
use std::fmt::{Debug, nested::{Display}, nested};
353+
use std::fmt::{Debug, nested::{self, Display}, nested};
354354
355355
impl nested for Foo {
356356
}
@@ -518,6 +518,7 @@ fn main() {
518518
",
519519
r"
520520
#![allow(dead_code)]
521+
521522
use std::fmt::Debug;
522523
523524
fn main() {

crates/assists/src/utils/insert_use.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,27 @@ fn find_insert_position(
275275
(InsertPosition::After(node.into()), AddBlankLine::BeforeTwice)
276276
}
277277
// 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+
}
279299
},
280300
}
281301
}
@@ -459,6 +479,36 @@ fn main() {}",
459479
)
460480
}
461481

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+
491+
use 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+
503+
fn main() {}",
504+
r"#![allow(unused_imports)]
505+
506+
use foo::bar;
507+
508+
fn main() {}",
509+
)
510+
}
511+
462512
#[test]
463513
fn merge_groups() {
464514
check_last("std::io", r"use std::fmt;", r"use std::{fmt, io};")

0 commit comments

Comments
 (0)