Skip to content

Commit 1596b31

Browse files
Do not add imports before inner attributes
1 parent 759100f commit 1596b31

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,13 @@ fn best_action_for_target(
431431
.find(|n| n.text_range().start() < anchor.text_range().start())
432432
.or_else(|| Some(anchor));
433433

434-
ImportAction::add_new_use(anchor, false)
434+
let add_after_anchor = anchor
435+
.clone()
436+
.and_then(ast::Attr::cast)
437+
.as_ref()
438+
.map(ast::Attr::is_inner_attribute)
439+
.unwrap_or(false);
440+
ImportAction::add_new_use(anchor, add_after_anchor)
435441
}
436442
}
437443
}
@@ -958,6 +964,28 @@ mod foo {
958964
959965
Debug<|>
960966
}
967+
}
968+
",
969+
);
970+
}
971+
972+
#[test]
973+
fn inserts_imports_after_inner_attributes() {
974+
check_assist(
975+
replace_qualified_name_with_use,
976+
"
977+
#![allow(dead_code)]
978+
979+
fn main() {
980+
std::fmt::Debug<|>
981+
}
982+
",
983+
"
984+
#![allow(dead_code)]
985+
use std::fmt::Debug;
986+
987+
fn main() {
988+
Debug<|>
961989
}
962990
",
963991
);

crates/ra_syntax/src/ast/extensions.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ impl ast::Attr {
7171
_ => None,
7272
}
7373
}
74+
75+
pub fn is_inner_attribute(&self) -> bool {
76+
let first_token = self.syntax().first_token();
77+
let first_token_kind = first_token.as_ref().map(SyntaxToken::kind);
78+
let second_token_kind =
79+
first_token.and_then(|token| token.next_token()).as_ref().map(SyntaxToken::kind);
80+
return first_token_kind == Some(SyntaxKind::POUND)
81+
&& second_token_kind == Some(SyntaxKind::EXCL);
82+
}
7483
}
7584

7685
#[derive(Debug, Clone, PartialEq, Eq)]

0 commit comments

Comments
 (0)