Skip to content

Commit 1c86657

Browse files
committed
fix: insert auto-imports after header comments
Fixes rust-lang#8607. This commit changes the auto-import functionality and causes it to add imports after any leading comments, which are commonly license headers. This does not affect comments on items as they're considered part of the item itself and not separate.
1 parent 8be2be8 commit 1c86657

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

crates/ide_db/src/helpers/insert_use.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ fn insert_use_(
401401
.children_with_tokens()
402402
.filter(|child| match child {
403403
NodeOrToken::Node(node) => is_inner_attribute(node.clone()),
404-
NodeOrToken::Token(token) => is_inner_comment(token.clone()),
404+
NodeOrToken::Token(token) => is_comment(token.clone()),
405405
})
406406
.last()
407407
{
@@ -440,7 +440,6 @@ fn is_inner_attribute(node: SyntaxNode) -> bool {
440440
ast::Attr::cast(node).map(|attr| attr.kind()) == Some(ast::AttrKind::Inner)
441441
}
442442

443-
fn is_inner_comment(token: SyntaxToken) -> bool {
444-
ast::Comment::cast(token).and_then(|comment| comment.kind().doc)
445-
== Some(ast::CommentPlacement::Inner)
443+
fn is_comment(token: SyntaxToken) -> bool {
444+
ast::Comment::cast(token).is_some()
446445
}

crates/ide_db/src/helpers/insert_use/tests.rs

+24
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,30 @@ use foo::bar::Baz;"#,
390390
);
391391
}
392392

393+
#[test]
394+
fn inserts_after_single_line_comments() {
395+
check_none(
396+
"foo::bar::Baz",
397+
"// Represents a possible license header and/or general module comments",
398+
r#"// Represents a possible license header and/or general module comments
399+
400+
use foo::bar::Baz;"#,
401+
);
402+
}
403+
404+
#[test]
405+
fn inserts_before_single_line_item_comments() {
406+
check_none(
407+
"foo::bar::Baz",
408+
r#"// Represents a comment about a function
409+
fn foo() {}"#,
410+
r#"use foo::bar::Baz;
411+
412+
// Represents a comment about a function
413+
fn foo() {}"#,
414+
);
415+
}
416+
393417
#[test]
394418
fn inserts_after_multiline_inner_comments() {
395419
check_none(

0 commit comments

Comments
 (0)