From 1c866573cb4f8ec4801cf58da86f298cad2906e6 Mon Sep 17 00:00:00 2001 From: Wesley Norris Date: Mon, 17 Jan 2022 17:06:10 -0500 Subject: [PATCH 1/3] fix: insert auto-imports after header comments 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. --- crates/ide_db/src/helpers/insert_use.rs | 7 +++--- crates/ide_db/src/helpers/insert_use/tests.rs | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/crates/ide_db/src/helpers/insert_use.rs b/crates/ide_db/src/helpers/insert_use.rs index 1f3e3c55bf..d6f8cd0f4b 100644 --- a/crates/ide_db/src/helpers/insert_use.rs +++ b/crates/ide_db/src/helpers/insert_use.rs @@ -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() } diff --git a/crates/ide_db/src/helpers/insert_use/tests.rs b/crates/ide_db/src/helpers/insert_use/tests.rs index a5763eb41c..abd5e64c54 100644 --- a/crates/ide_db/src/helpers/insert_use/tests.rs +++ b/crates/ide_db/src/helpers/insert_use/tests.rs @@ -390,6 +390,30 @@ use foo::bar::Baz;"#, ); } +#[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_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_multiline_inner_comments() { check_none( From 7d10752299395448163999cd61be71760157f027 Mon Sep 17 00:00:00 2001 From: Wesley Norris Date: Mon, 17 Jan 2022 17:12:32 -0500 Subject: [PATCH 2/3] Add a test for multi-single-line comments as well --- crates/ide_db/src/helpers/insert_use/tests.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/crates/ide_db/src/helpers/insert_use/tests.rs b/crates/ide_db/src/helpers/insert_use/tests.rs index abd5e64c54..887319e924 100644 --- a/crates/ide_db/src/helpers/insert_use/tests.rs +++ b/crates/ide_db/src/helpers/insert_use/tests.rs @@ -401,6 +401,21 @@ 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( From ba82cc7722aeb1627da8cde9f342c73a937b276c Mon Sep 17 00:00:00 2001 From: Wesley Norris Date: Mon, 17 Jan 2022 18:44:43 -0500 Subject: [PATCH 3/3] Add test for comments not directly next to items --- crates/ide_db/src/helpers/insert_use/tests.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/crates/ide_db/src/helpers/insert_use/tests.rs b/crates/ide_db/src/helpers/insert_use/tests.rs index 887319e924..4219358a07 100644 --- a/crates/ide_db/src/helpers/insert_use/tests.rs +++ b/crates/ide_db/src/helpers/insert_use/tests.rs @@ -429,6 +429,23 @@ 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(