mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-12-15 21:34:19 +00:00
feat: do not add new enum if it already exists
This commit is contained in:
parent
da27b89ca5
commit
2b52bffaef
1 changed files with 40 additions and 1 deletions
|
|
@ -461,7 +461,17 @@ fn add_enum_def(
|
|||
usages: &UsageSearchResult,
|
||||
target_node: SyntaxNode,
|
||||
target_module: &hir::Module,
|
||||
) {
|
||||
) -> Option<()> {
|
||||
if ctx
|
||||
.find_node_at_offset::<ast::SourceFile>()?
|
||||
.syntax()
|
||||
.children()
|
||||
.filter_map(|node| ast::Enum::cast(node).and_then(|e| ctx.sema.to_def(&e)))
|
||||
.any(|def| def.name(ctx.db()).as_str() == Some("Bool"))
|
||||
{
|
||||
return None;
|
||||
}
|
||||
|
||||
let make_enum_pub = usages
|
||||
.iter()
|
||||
.flat_map(|(_, refs)| refs)
|
||||
|
|
@ -480,6 +490,8 @@ fn add_enum_def(
|
|||
insert_before.text_range().start(),
|
||||
format!("{}\n\n{indent}", enum_def.syntax().text()),
|
||||
);
|
||||
|
||||
Some(())
|
||||
}
|
||||
|
||||
/// Finds where to put the new enum definition.
|
||||
|
|
@ -553,6 +565,33 @@ fn function(foo: Bool, bar: bool) {
|
|||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_duplicate_enums() {
|
||||
check_assist(
|
||||
bool_to_enum,
|
||||
r#"
|
||||
#[derive(PartialEq, Eq)]
|
||||
enum Bool { True, False }
|
||||
|
||||
fn function(foo: bool, $0bar: bool) {
|
||||
if bar {
|
||||
println!("bar");
|
||||
}
|
||||
}
|
||||
"#,
|
||||
r#"
|
||||
#[derive(PartialEq, Eq)]
|
||||
enum Bool { True, False }
|
||||
|
||||
fn function(foo: bool, bar: Bool) {
|
||||
if bar == Bool::True {
|
||||
println!("bar");
|
||||
}
|
||||
}
|
||||
"#,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parameter_with_last_param_usage() {
|
||||
check_assist(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue