From 1596b31698acd1ca8fe25a1b699bef4a9a6feb1d Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Wed, 12 Feb 2020 16:21:55 +0200 Subject: Do not add imports before inner attributes --- .../handlers/replace_qualified_name_with_use.rs | 30 +++++++++++++++++++++- crates/ra_syntax/src/ast/extensions.rs | 9 +++++++ 2 files changed, 38 insertions(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs b/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs index b70c88ec2..4b5e37c11 100644 --- a/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs +++ b/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs @@ -431,7 +431,13 @@ fn best_action_for_target( .find(|n| n.text_range().start() < anchor.text_range().start()) .or_else(|| Some(anchor)); - ImportAction::add_new_use(anchor, false) + let add_after_anchor = anchor + .clone() + .and_then(ast::Attr::cast) + .as_ref() + .map(ast::Attr::is_inner_attribute) + .unwrap_or(false); + ImportAction::add_new_use(anchor, add_after_anchor) } } } @@ -958,6 +964,28 @@ mod foo { Debug<|> } +} + ", + ); + } + + #[test] + fn inserts_imports_after_inner_attributes() { + check_assist( + replace_qualified_name_with_use, + " +#![allow(dead_code)] + +fn main() { + std::fmt::Debug<|> +} + ", + " +#![allow(dead_code)] +use std::fmt::Debug; + +fn main() { + Debug<|> } ", ); diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index 7dcf084de..a297ae110 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs @@ -71,6 +71,15 @@ impl ast::Attr { _ => None, } } + + pub fn is_inner_attribute(&self) -> bool { + let first_token = self.syntax().first_token(); + let first_token_kind = first_token.as_ref().map(SyntaxToken::kind); + let second_token_kind = + first_token.and_then(|token| token.next_token()).as_ref().map(SyntaxToken::kind); + return first_token_kind == Some(SyntaxKind::POUND) + && second_token_kind == Some(SyntaxKind::EXCL); + } } #[derive(Debug, Clone, PartialEq, Eq)] -- cgit v1.2.3 From 848c5762667101c8a017e561da2fa7e39b212929 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Wed, 12 Feb 2020 16:44:52 +0200 Subject: Introduce AttrKind --- .../src/handlers/replace_qualified_name_with_use.rs | 3 +-- crates/ra_syntax/src/ast.rs | 6 +++--- crates/ra_syntax/src/ast/extensions.rs | 15 ++++++++++++--- 3 files changed, 16 insertions(+), 8 deletions(-) (limited to 'crates') diff --git a/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs b/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs index 4b5e37c11..eac452413 100644 --- a/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs +++ b/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs @@ -434,8 +434,7 @@ fn best_action_for_target( let add_after_anchor = anchor .clone() .and_then(ast::Attr::cast) - .as_ref() - .map(ast::Attr::is_inner_attribute) + .map(|attr| attr.kind() == ast::AttrKind::Inner) .unwrap_or(false); ImportAction::add_new_use(anchor, add_after_anchor) } diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index d3e8888bd..50441e75b 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -18,8 +18,8 @@ use crate::{ pub use self::{ expr_extensions::{ArrayExprKind, BinOp, ElseBranch, LiteralKind, PrefixOp, RangeOp}, extensions::{ - FieldKind, PathSegmentKind, SelfParamKind, SlicePatComponents, StructKind, TypeBoundKind, - VisibilityKind, + AttrKind, FieldKind, PathSegmentKind, SelfParamKind, SlicePatComponents, StructKind, + TypeBoundKind, VisibilityKind, }, generated::*, tokens::*, @@ -218,7 +218,7 @@ fn test_doc_comment_multi_line_block_strips_suffix() { fn test_comments_preserve_trailing_whitespace() { let file = SourceFile::parse( r#" -/// Representation of a Realm. +/// Representation of a Realm. /// In the specification these are called Realm Records. struct Realm {}"#, ) diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index a297ae110..44de4af89 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs @@ -37,6 +37,12 @@ fn text_of_first_token(node: &SyntaxNode) -> &SmolStr { node.green().children().next().and_then(|it| it.into_token()).unwrap().text() } +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum AttrKind { + Inner, + Outer, +} + impl ast::Attr { pub fn as_simple_atom(&self) -> Option { match self.input() { @@ -72,13 +78,16 @@ impl ast::Attr { } } - pub fn is_inner_attribute(&self) -> bool { + pub fn kind(&self) -> AttrKind { let first_token = self.syntax().first_token(); let first_token_kind = first_token.as_ref().map(SyntaxToken::kind); let second_token_kind = first_token.and_then(|token| token.next_token()).as_ref().map(SyntaxToken::kind); - return first_token_kind == Some(SyntaxKind::POUND) - && second_token_kind == Some(SyntaxKind::EXCL); + + match (first_token_kind, second_token_kind) { + (Some(SyntaxKind::POUND), Some(SyntaxKind::EXCL)) => AttrKind::Inner, + _ => AttrKind::Outer, + } } } -- cgit v1.2.3 From 2a7d97d82911ad03c549fa3c8c014e4b74c9696d Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Wed, 12 Feb 2020 17:04:16 +0200 Subject: Fix the trailing whitespace test --- crates/ra_syntax/src/ast.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'crates') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 50441e75b..9cc7930f7 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -217,10 +217,7 @@ fn test_doc_comment_multi_line_block_strips_suffix() { #[test] fn test_comments_preserve_trailing_whitespace() { let file = SourceFile::parse( - r#" -/// Representation of a Realm. -/// In the specification these are called Realm Records. -struct Realm {}"#, + "\n/// Representation of a Realm. \n/// In the specification these are called Realm Records.\nstruct Realm {}", ) .ok() .unwrap(); -- cgit v1.2.3