Skip to content

fix: remove wrong comma after remove unnecessary braces #16185

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

Merged
merged 2 commits into from
Dec 23, 2023
Merged
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
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