Skip to content

Commit

Permalink
Auto merge of #16185 - Young-Flash:fix_auto_remove_brace, r=lnicola
Browse files Browse the repository at this point in the history
fix: remove wrong comma after remove unnecessary braces

![remove_comma](https://github.com/rust-lang/rust-analyzer/assets/71162630/56ef8cfc-a024-4c4a-82de-a8cca513b32e)

follow up #16066, close #16181
  • Loading branch information
bors committed Dec 23, 2023
2 parents afbb8f3 + 6c9d2ad commit a24ede2
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
105 changes: 105 additions & 0 deletions crates/ide-assists/src/handlers/remove_unused_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,111 @@ mod b {
);
}

#[test]
fn remove_comma_after_auto_remove_brace() {
check_assist(
remove_unused_imports,
r#"
mod m {
pub mod x {
pub struct A;
pub struct B;
}
pub mod y {
pub struct C;
}
}
$0use m::{
x::{A, B},
y::C,
};$0
fn main() {
B;
}
"#,
r#"
mod m {
pub mod x {
pub struct A;
pub struct B;
}
pub mod y {
pub struct C;
}
}
use m::
x::B
;
fn main() {
B;
}
"#,
);
check_assist(
remove_unused_imports,
r#"
mod m {
pub mod x {
pub struct A;
pub struct B;
}
pub mod y {
pub struct C;
pub struct D;
}
pub mod z {
pub struct E;
pub struct F;
}
}
$0use m::{
x::{A, B},
y::{C, D,},
z::{E, F},
};$0
fn main() {
B;
C;
F;
}
"#,
r#"
mod m {
pub mod x {
pub struct A;
pub struct B;
}
pub mod y {
pub struct C;
pub struct D;
}
pub mod z {
pub struct E;
pub struct F;
}
}
use m::{
x::B,
y::C,
z::F,
};
fn main() {
B;
C;
F;
}
"#,
);
}

#[test]
fn remove_nested_all_unused() {
check_assist(
Expand Down
7 changes: 7 additions & 0 deletions crates/syntax/src/ast/node_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,20 @@ impl ast::UseTreeList {
.is_some()
}

pub fn comma(&self) -> impl Iterator<Item = SyntaxToken> {
self.syntax()
.children_with_tokens()
.filter_map(|it| it.into_token().filter(|it| it.kind() == T![,]))
}

/// Remove the unnecessary braces in current `UseTreeList`
pub fn remove_unnecessary_braces(mut self) {
let remove_brace_in_use_tree_list = |u: &ast::UseTreeList| {
let use_tree_count = u.use_trees().count();
if use_tree_count == 1 {
u.l_curly_token().map(ted::remove);
u.r_curly_token().map(ted::remove);
u.comma().for_each(ted::remove);
}
};

Expand Down

0 comments on commit a24ede2

Please sign in to comment.