Skip to content

refactor: append attrs by make args instead of ted #20413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions crates/ide-db/src/imports/insert_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl ImportScope {
) -> Option<Self> {
// The closest block expression ancestor
let mut block = None;
let mut required_cfgs = Vec::new();
let mut required_cfgs: Vec<ast::Attr> = Vec::new();
// Walk up the ancestor tree searching for a suitable node to do insertions on
// with special handling on cfg-gated items, in which case we want to insert imports locally
// or FIXME: annotate inserted imports with the same cfg
Expand Down Expand Up @@ -113,9 +113,14 @@ impl ImportScope {
required_cfgs,
});
}
required_cfgs.extend(has_attrs.attrs().filter(|attr| {
attr.as_simple_call().is_some_and(|(ident, _)| ident == "cfg")
}));
let mut attrs: Vec<ast::Attr> = has_attrs
.attrs()
.filter(|attr| {
attr.as_simple_call().is_some_and(|(ident, _)| ident == "cfg")
})
.collect();
attrs.append(&mut required_cfgs);
required_cfgs = attrs;
}
}
}
Expand Down Expand Up @@ -194,12 +199,8 @@ fn insert_use_with_alias_option(
use_tree = use_tree.clone_for_update();
use_tree.wrap_in_tree_list();
}
let use_item = make::use_(None, None, use_tree).clone_for_update();
for attr in
scope.required_cfgs.iter().map(|attr| attr.syntax().clone_subtree().clone_for_update())
{
ted::insert(ted::Position::first_child_of(use_item.syntax()), attr);
}

let use_item = make::use_(scope.required_cfgs.clone(), None, use_tree).clone_for_update();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think SyntaxFactor::use_ is more preferable here


// merge into existing imports if possible
if let Some(mb) = mb {
Expand Down
9 changes: 6 additions & 3 deletions crates/ide-db/src/imports/insert_use/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,23 @@ fn respects_cfg_attr_multiple_layers() {
check(
r"bar::Bar",
r#"
#[cfg(test)]
#[cfg(test1)]
impl () {
#[cfg(test2)]
#[cfg(test3)]
fn f($0) {}
}
"#,
r#"
#[cfg(test)]
#[cfg(test1)]
#[cfg(test2)]
#[cfg(test3)]
use bar::Bar;

#[cfg(test)]
#[cfg(test1)]
impl () {
#[cfg(test2)]
#[cfg(test3)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the purpose of this test change to verify robustness in a more layered scenario?

fn f() {}
}
"#,
Expand Down
Loading