mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
Auto merge of #16066 - Young-Flash:auto_remove_brace, r=lnicola
fix: auto remove unnecessary braces after remove unused imports before  after 
This commit is contained in:
commit
65ed198819
@ -423,7 +423,7 @@ mod z {
|
|||||||
struct X();
|
struct X();
|
||||||
struct Y();
|
struct Y();
|
||||||
mod z {
|
mod z {
|
||||||
use super::{X};
|
use super::X;
|
||||||
|
|
||||||
fn w() {
|
fn w() {
|
||||||
let x = X();
|
let x = X();
|
||||||
@ -495,7 +495,7 @@ struct X();
|
|||||||
mod y {
|
mod y {
|
||||||
struct Y();
|
struct Y();
|
||||||
mod z {
|
mod z {
|
||||||
use crate::{X};
|
use crate::X;
|
||||||
fn f() {
|
fn f() {
|
||||||
let x = X();
|
let x = X();
|
||||||
}
|
}
|
||||||
@ -526,7 +526,7 @@ struct X();
|
|||||||
mod y {
|
mod y {
|
||||||
struct Y();
|
struct Y();
|
||||||
mod z {
|
mod z {
|
||||||
use crate::{y::Y};
|
use crate::y::Y;
|
||||||
fn f() {
|
fn f() {
|
||||||
let y = Y();
|
let y = Y();
|
||||||
}
|
}
|
||||||
@ -536,6 +536,79 @@ mod y {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn remove_unused_auto_remove_brace_nested() {
|
||||||
|
check_assist(
|
||||||
|
remove_unused_imports,
|
||||||
|
r#"
|
||||||
|
mod a {
|
||||||
|
pub struct A();
|
||||||
|
}
|
||||||
|
mod b {
|
||||||
|
struct F();
|
||||||
|
mod c {
|
||||||
|
$0use {{super::{{
|
||||||
|
{d::{{{{{{{S, U}}}}}}}},
|
||||||
|
{{{{e::{H, L, {{{R}}}}}}}},
|
||||||
|
F, super::a::A
|
||||||
|
}}}};$0
|
||||||
|
fn f() {
|
||||||
|
let f = F();
|
||||||
|
let l = L();
|
||||||
|
let a = A();
|
||||||
|
let s = S();
|
||||||
|
let h = H();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod d {
|
||||||
|
pub struct S();
|
||||||
|
pub struct U();
|
||||||
|
}
|
||||||
|
|
||||||
|
mod e {
|
||||||
|
pub struct H();
|
||||||
|
pub struct L();
|
||||||
|
pub struct R();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
mod a {
|
||||||
|
pub struct A();
|
||||||
|
}
|
||||||
|
mod b {
|
||||||
|
struct F();
|
||||||
|
mod c {
|
||||||
|
use super::{
|
||||||
|
d::S,
|
||||||
|
e::{H, L},
|
||||||
|
F, super::a::A
|
||||||
|
};
|
||||||
|
fn f() {
|
||||||
|
let f = F();
|
||||||
|
let l = L();
|
||||||
|
let a = A();
|
||||||
|
let s = S();
|
||||||
|
let h = H();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod d {
|
||||||
|
pub struct S();
|
||||||
|
pub struct U();
|
||||||
|
}
|
||||||
|
|
||||||
|
mod e {
|
||||||
|
pub struct H();
|
||||||
|
pub struct L();
|
||||||
|
pub struct R();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn remove_nested_all_unused() {
|
fn remove_nested_all_unused() {
|
||||||
check_assist(
|
check_assist(
|
||||||
|
@ -414,6 +414,7 @@ impl ast::UseTree {
|
|||||||
u.remove_recursive();
|
u.remove_recursive();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
u.remove_unnecessary_braces();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ use rowan::{GreenNodeData, GreenTokenData};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{self, support, AstNode, AstToken, HasAttrs, HasGenericParams, HasName, SyntaxNode},
|
ast::{self, support, AstNode, AstToken, HasAttrs, HasGenericParams, HasName, SyntaxNode},
|
||||||
NodeOrToken, SmolStr, SyntaxElement, SyntaxToken, TokenText, T,
|
ted, NodeOrToken, SmolStr, SyntaxElement, SyntaxToken, TokenText, T,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl ast::Lifetime {
|
impl ast::Lifetime {
|
||||||
@ -323,6 +323,10 @@ impl ast::UseTree {
|
|||||||
pub fn is_simple_path(&self) -> bool {
|
pub fn is_simple_path(&self) -> bool {
|
||||||
self.use_tree_list().is_none() && self.star_token().is_none()
|
self.use_tree_list().is_none() && self.star_token().is_none()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn parent_use_tree_list(&self) -> Option<ast::UseTreeList> {
|
||||||
|
self.syntax().parent().and_then(ast::UseTreeList::cast)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ast::UseTreeList {
|
impl ast::UseTreeList {
|
||||||
@ -340,6 +344,27 @@ impl ast::UseTreeList {
|
|||||||
.find_map(ast::Comment::cast)
|
.find_map(ast::Comment::cast)
|
||||||
.is_some()
|
.is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// take `use crate::{{{{A}}}}` for example
|
||||||
|
// the below remove the innermost {}, got `use crate::{{{A}}}`
|
||||||
|
remove_brace_in_use_tree_list(&self);
|
||||||
|
|
||||||
|
// the below remove othe unnecessary {}, got `use crate::A`
|
||||||
|
while let Some(parent_use_tree_list) = self.parent_use_tree().parent_use_tree_list() {
|
||||||
|
remove_brace_in_use_tree_list(&parent_use_tree_list);
|
||||||
|
self = parent_use_tree_list;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ast::Impl {
|
impl ast::Impl {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user