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 <repnop@outlook.com>
This commit is contained in:
bors[bot] 2022-01-17 23:57:30 +00:00 committed by GitHub
commit 477b654f4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 4 deletions

View File

@ -401,7 +401,7 @@ fn insert_use_(
.children_with_tokens()
.filter(|child| match child {
NodeOrToken::Node(node) => is_inner_attribute(node.clone()),
NodeOrToken::Token(token) => is_inner_comment(token.clone()),
NodeOrToken::Token(token) => is_comment(token.clone()),
})
.last()
{
@ -440,7 +440,6 @@ fn is_inner_attribute(node: SyntaxNode) -> bool {
ast::Attr::cast(node).map(|attr| attr.kind()) == Some(ast::AttrKind::Inner)
}
fn is_inner_comment(token: SyntaxToken) -> bool {
ast::Comment::cast(token).and_then(|comment| comment.kind().doc)
== Some(ast::CommentPlacement::Inner)
fn is_comment(token: SyntaxToken) -> bool {
ast::Comment::cast(token).is_some()
}

View File

@ -390,6 +390,62 @@ fn inserts_after_single_line_inner_comments() {
);
}
#[test]
fn inserts_after_single_line_comments() {
check_none(
"foo::bar::Baz",
"// Represents a possible license header and/or general module comments",
r#"// Represents a possible license header and/or general module comments
use foo::bar::Baz;"#,
);
}
#[test]
fn inserts_after_multiple_single_line_comments() {
check_none(
"foo::bar::Baz",
"// Represents a possible license header and/or general module comments
// Second single-line comment
// Third single-line comment",
r#"// Represents a possible license header and/or general module comments
// Second single-line comment
// Third single-line comment
use foo::bar::Baz;"#,
);
}
#[test]
fn inserts_before_single_line_item_comments() {
check_none(
"foo::bar::Baz",
r#"// Represents a comment about a function
fn foo() {}"#,
r#"use foo::bar::Baz;
// Represents a comment about a function
fn foo() {}"#,
);
}
#[test]
fn inserts_after_single_line_header_comments_and_before_item() {
check_none(
"foo::bar::Baz",
r#"// Represents a possible license header
// Line two of possible license header
fn foo() {}"#,
r#"// Represents a possible license header
// Line two of possible license header
use foo::bar::Baz;
fn foo() {}"#,
);
}
#[test]
fn inserts_after_multiline_inner_comments() {
check_none(