Repository: rust-lang/rust-analyzer (2025-07-06T11:35:33Z)

Issue:
Enum variants in symbol search
Searching for an enum variant via symbol search currently yields no results. For example when searching for the workspace symbol `Bar` I would expect it to find the enum variant defined in `Foo` as defined below. I was looking in the documentation for any tips but tweaking any of these configs `rust-analyzer.workspace.symbol.search.*` didn't change the behavior for the given use-case. Did I miss something? Can this behavior be changed? If not is this intentional?

```rust
enum Foo {
    Bar { id: i32 },
    Baz { name: String },
}
```


Discussion:
Commenting here because I'm interested in the same feature. Would be a huge bonus to be able to search for enum variants via symbol search.



Patch:
diff --git a/crates/hir/src/symbols.rs b/crates/hir/src/symbols.rs
index 756650891d4b..dca10193e29b 100644
--- a/crates/hir/src/symbols.rs
+++ b/crates/hir/src/symbols.rs
@@ -125,6 +125,13 @@ impl<'a> SymbolCollector<'a> {
                 }
                 ModuleDefId::AdtId(AdtId::EnumId(id)) => {
                     this.push_decl(id, name, false, None);
+                    let enum_name = this.db.enum_signature(id).name.as_str().to_smolstr();
+                    this.with_container_name(Some(enum_name), |this| {
+                        let variants = id.enum_variants(this.db);
+                        for (variant_id, variant_name, _) in &variants.variants {
+                            this.push_decl(*variant_id, variant_name, true, None);
+                        }
+                    });
                 }
                 ModuleDefId::AdtId(AdtId::UnionId(id)) => {
                     this.push_decl(id, name, false, None);


Test patch:
diff --git a/crates/ide-db/src/test_data/test_symbol_index_collection.txt b/crates/ide-db/src/test_data/test_symbol_index_collection.txt
index de046e70c673..973256c470f3 100644
--- a/crates/ide-db/src/test_data/test_symbol_index_collection.txt
+++ b/crates/ide-db/src/test_data/test_symbol_index_collection.txt
@@ -10,6 +10,40 @@
             },
         },
         [
+            FileSymbol {
+                name: "A",
+                def: Variant(
+                    Variant {
+                        id: EnumVariantId(
+                            7800,
+                        ),
+                    },
+                ),
+                loc: DeclarationLocation {
+                    hir_file_id: FileId(
+                        EditionedFileId(
+                            Id(2000),
+                        ),
+                    ),
+                    ptr: SyntaxNodePtr {
+                        kind: VARIANT,
+                        range: 201..202,
+                    },
+                    name_ptr: AstPtr(
+                        SyntaxNodePtr {
+                            kind: NAME,
+                            range: 201..202,
+                        },
+                    ),
+                },
+                container_name: Some(
+                    "Enum",
+                ),
+                is_alias: false,
+                is_assoc: true,
+                is_import: false,
+                do_not_complete: Yes,
+            },
             FileSymbol {
                 name: "Alias",
                 def: TypeAlias(
@@ -42,6 +76,40 @@
                 is_import: false,
                 do_not_complete: Yes,
             },
+            FileSymbol {
+                name: "B",
+                def: Variant(
+                    Variant {
+                        id: EnumVariantId(
+                            7801,
+                        ),
+                    },
+                ),
+                loc: DeclarationLocation {
+                    hir_file_id: FileId(
+                        EditionedFileId(
+                            Id(2000),
+                        ),
+                    ),
+                    ptr: SyntaxNodePtr {
+                        kind: VARIANT,
+                        range: 204..205,
+                    },
+                    name_ptr: AstPtr(
+                        SyntaxNodePtr {
+                            kind: NAME,
+                            range: 204..205,
+                        },
+                    ),
+                },
+                container_name: Some(
+                    "Enum",
+                ),
+                is_alias: false,
+                is_assoc: true,
+                is_import: false,
+                do_not_complete: Yes,
+            },
             FileSymbol {
                 name: "CONST",
                 def: Const(