fix: Fix import insertion inserting after last comment in a file
This commit is contained in:
parent
5d5bbec9b6
commit
c0feb389ed
@ -8,7 +8,7 @@
|
|||||||
use syntax::{
|
use syntax::{
|
||||||
algo,
|
algo,
|
||||||
ast::{self, make, AstNode, HasAttrs, HasModuleItem, HasVisibility, PathSegmentKind},
|
ast::{self, make, AstNode, HasAttrs, HasModuleItem, HasVisibility, PathSegmentKind},
|
||||||
ted, AstToken, Direction, NodeOrToken, SyntaxNode, SyntaxToken,
|
ted, Direction, NodeOrToken, SyntaxKind, SyntaxNode,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@ -397,12 +397,16 @@ fn insert_use_(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// there are no imports in this file at all
|
// there are no imports in this file at all
|
||||||
|
// so put the import after all inner module attributes and possible license header comments
|
||||||
if let Some(last_inner_element) = scope_syntax
|
if let Some(last_inner_element) = scope_syntax
|
||||||
.children_with_tokens()
|
.children_with_tokens()
|
||||||
.filter(|child| match child {
|
.take_while(|child| match child {
|
||||||
NodeOrToken::Node(node) => is_inner_attribute(node.clone()),
|
NodeOrToken::Node(node) => is_inner_attribute(node.clone()),
|
||||||
NodeOrToken::Token(token) => is_comment(token.clone()),
|
NodeOrToken::Token(token) => {
|
||||||
|
[SyntaxKind::WHITESPACE, SyntaxKind::COMMENT].contains(&token.kind())
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
.filter(|child| child.as_token().map_or(true, |t| t.kind() != SyntaxKind::WHITESPACE))
|
||||||
.last()
|
.last()
|
||||||
{
|
{
|
||||||
cov_mark::hit!(insert_empty_inner_attr);
|
cov_mark::hit!(insert_empty_inner_attr);
|
||||||
@ -439,7 +443,3 @@ fn insert_use_(
|
|||||||
fn is_inner_attribute(node: SyntaxNode) -> bool {
|
fn is_inner_attribute(node: SyntaxNode) -> bool {
|
||||||
ast::Attr::cast(node).map(|attr| attr.kind()) == Some(ast::AttrKind::Inner)
|
ast::Attr::cast(node).map(|attr| attr.kind()) == Some(ast::AttrKind::Inner)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_comment(token: SyntaxToken) -> bool {
|
|
||||||
ast::Comment::cast(token).is_some()
|
|
||||||
}
|
|
||||||
|
@ -5,6 +5,24 @@
|
|||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn trailing_comment_in_empty_file() {
|
||||||
|
check(
|
||||||
|
"foo::bar",
|
||||||
|
r#"
|
||||||
|
struct Struct;
|
||||||
|
// 0 = 1
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
use foo::bar;
|
||||||
|
|
||||||
|
struct Struct;
|
||||||
|
// 0 = 1
|
||||||
|
"#,
|
||||||
|
ImportGranularity::Crate,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn respects_cfg_attr_fn() {
|
fn respects_cfg_attr_fn() {
|
||||||
check(
|
check(
|
||||||
|
Loading…
Reference in New Issue
Block a user