Skip to content

Commit 477b654

Browse files
bors[bot]repnop
andauthored
Merge #11311
11311: fix: insert auto-imports after header comments r=repnop a=repnop Fixes #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. Co-authored-by: Wesley Norris <[email protected]>
2 parents 8be2be8 + ba82cc7 commit 477b654

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

crates/ide_db/src/helpers/insert_use.rs

Lines changed: 3 additions & 4 deletions
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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,62 @@ 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_after_multiple_single_line_comments() {
406+
check_none(
407+
"foo::bar::Baz",
408+
"// Represents a possible license header and/or general module comments
409+
// Second single-line comment
410+
// Third single-line comment",
411+
r#"// Represents a possible license header and/or general module comments
412+
// Second single-line comment
413+
// Third single-line comment
414+
415+
use foo::bar::Baz;"#,
416+
);
417+
}
418+
419+
#[test]
420+
fn inserts_before_single_line_item_comments() {
421+
check_none(
422+
"foo::bar::Baz",
423+
r#"// Represents a comment about a function
424+
fn foo() {}"#,
425+
r#"use foo::bar::Baz;
426+
427+
// Represents a comment about a function
428+
fn foo() {}"#,
429+
);
430+
}
431+
432+
#[test]
433+
fn inserts_after_single_line_header_comments_and_before_item() {
434+
check_none(
435+
"foo::bar::Baz",
436+
r#"// Represents a possible license header
437+
// Line two of possible license header
438+
439+
fn foo() {}"#,
440+
r#"// Represents a possible license header
441+
// Line two of possible license header
442+
443+
use foo::bar::Baz;
444+
445+
fn foo() {}"#,
446+
);
447+
}
448+
393449
#[test]
394450
fn inserts_after_multiline_inner_comments() {
395451
check_none(

0 commit comments

Comments
 (0)