From b67b352ac7b84098cc6c5af7f2bbda74e9ef0863 Mon Sep 17 00:00:00 2001 From: Jimmy Miller Date: Sun, 17 Dec 2023 10:10:13 -0500 Subject: [PATCH] Make functions in impl have a container name fixes #16015 --- crates/hir/src/symbols.rs | 11 +- crates/ide-db/src/symbol_index.rs | 6 + .../test_symbol_index_collection.txt | 142 +++++++++++++----- 3 files changed, 115 insertions(+), 44 deletions(-) diff --git a/crates/hir/src/symbols.rs b/crates/hir/src/symbols.rs index a2a30edeb0..fa25b4011e 100644 --- a/crates/hir/src/symbols.rs +++ b/crates/hir/src/symbols.rs @@ -8,7 +8,7 @@ use hir_def::{ TraitId, }; use hir_expand::{HirFileId, InFile}; -use hir_ty::db::HirDatabase; +use hir_ty::{db::HirDatabase, display::HirDisplay}; use syntax::{ast::HasName, AstNode, AstPtr, SmolStr, SyntaxNode, SyntaxNodePtr}; use crate::{Module, ModuleDef, Semantics}; @@ -230,9 +230,12 @@ impl<'a> SymbolCollector<'a> { fn collect_from_impl(&mut self, impl_id: ImplId) { let impl_data = self.db.impl_data(impl_id); - for &assoc_item_id in &impl_data.items { - self.push_assoc_item(assoc_item_id) - } + let impl_name = Some(SmolStr::new(impl_data.self_ty.display(self.db).to_string())); + self.with_container_name(impl_name, |s| { + for &assoc_item_id in &impl_data.items { + s.push_assoc_item(assoc_item_id) + } + }) } fn collect_from_trait(&mut self, trait_id: TraitId) { diff --git a/crates/ide-db/src/symbol_index.rs b/crates/ide-db/src/symbol_index.rs index be8566b759..9d318b7c83 100644 --- a/crates/ide-db/src/symbol_index.rs +++ b/crates/ide-db/src/symbol_index.rs @@ -414,6 +414,12 @@ impl Struct { fn impl_fn() {} } +struct StructT; + +impl StructT { + fn generic_impl_fn() {} +} + trait Trait { fn trait_fn(&self); } 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 c9875c7f8f..f0b97779c7 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 @@ -23,12 +23,12 @@ ), ptr: SyntaxNodePtr { kind: TYPE_ALIAS, - range: 397..417, + range: 470..490, }, name_ptr: AstPtr( SyntaxNodePtr { kind: NAME, - range: 402..407, + range: 475..480, }, ), }, @@ -51,12 +51,12 @@ ), ptr: SyntaxNodePtr { kind: CONST, - range: 340..361, + range: 413..434, }, name_ptr: AstPtr( SyntaxNodePtr { kind: NAME, - range: 346..351, + range: 419..424, }, ), }, @@ -79,12 +79,12 @@ ), ptr: SyntaxNodePtr { kind: CONST, - range: 520..592, + range: 593..665, }, name_ptr: AstPtr( SyntaxNodePtr { kind: NAME, - range: 526..542, + range: 599..615, }, ), }, @@ -139,12 +139,12 @@ ), ptr: SyntaxNodePtr { kind: USE_TREE, - range: 654..676, + range: 727..749, }, name_ptr: AstPtr( SyntaxNodePtr { kind: NAME, - range: 663..676, + range: 736..749, }, ), }, @@ -197,12 +197,12 @@ ), ptr: SyntaxNodePtr { kind: STATIC, - range: 362..396, + range: 435..469, }, name_ptr: AstPtr( SyntaxNodePtr { kind: NAME, - range: 369..375, + range: 442..448, }, ), }, @@ -276,7 +276,7 @@ Struct( Struct { id: StructId( - 4, + 5, ), }, ), @@ -287,12 +287,12 @@ ), ptr: SyntaxNodePtr { kind: STRUCT, - range: 318..336, + range: 391..409, }, name_ptr: AstPtr( SyntaxNodePtr { kind: NAME, - range: 325..335, + range: 398..408, }, ), }, @@ -308,7 +308,7 @@ Struct( Struct { id: StructId( - 5, + 6, ), }, ), @@ -319,12 +319,12 @@ ), ptr: SyntaxNodePtr { kind: STRUCT, - range: 555..581, + range: 628..654, }, name_ptr: AstPtr( SyntaxNodePtr { kind: NAME, - range: 562..580, + range: 635..653, }, ), }, @@ -340,7 +340,7 @@ Struct( Struct { id: StructId( - 6, + 7, ), }, ), @@ -351,12 +351,42 @@ ), ptr: SyntaxNodePtr { kind: STRUCT, - range: 479..507, + range: 552..580, }, name_ptr: AstPtr( SyntaxNodePtr { kind: NAME, - range: 486..506, + range: 559..579, + }, + ), + }, + container_name: None, + is_alias: false, + is_assoc: false, + }, + FileSymbol { + name: "StructT", + def: Adt( + Struct( + Struct { + id: StructId( + 2, + ), + }, + ), + ), + loc: DeclarationLocation { + hir_file_id: FileId( + 0, + ), + ptr: SyntaxNodePtr { + kind: STRUCT, + range: 261..279, + }, + name_ptr: AstPtr( + SyntaxNodePtr { + kind: NAME, + range: 268..275, }, ), }, @@ -379,12 +409,12 @@ ), ptr: SyntaxNodePtr { kind: TRAIT, - range: 261..300, + range: 334..373, }, name_ptr: AstPtr( SyntaxNodePtr { kind: NAME, - range: 267..272, + range: 340..345, }, ), }, @@ -409,12 +439,12 @@ ), ptr: SyntaxNodePtr { kind: USE_TREE, - range: 682..696, + range: 755..769, }, name_ptr: AstPtr( SyntaxNodePtr { kind: NAME, - range: 691..696, + range: 764..769, }, ), }, @@ -469,12 +499,12 @@ ), ptr: SyntaxNodePtr { kind: MODULE, - range: 419..457, + range: 492..530, }, name_ptr: AstPtr( SyntaxNodePtr { kind: NAME, - range: 423..428, + range: 496..501, }, ), }, @@ -499,12 +529,12 @@ ), ptr: SyntaxNodePtr { kind: MODULE, - range: 594..604, + range: 667..677, }, name_ptr: AstPtr( SyntaxNodePtr { kind: NAME, - range: 598..603, + range: 671..676, }, ), }, @@ -542,6 +572,36 @@ is_alias: false, is_assoc: false, }, + FileSymbol { + name: "generic_impl_fn", + def: Function( + Function { + id: FunctionId( + 3, + ), + }, + ), + loc: DeclarationLocation { + hir_file_id: FileId( + 0, + ), + ptr: SyntaxNodePtr { + kind: FN, + range: 307..330, + }, + name_ptr: AstPtr( + SyntaxNodePtr { + kind: NAME, + range: 310..325, + }, + ), + }, + container_name: Some( + "StructT", + ), + is_alias: false, + is_assoc: true, + }, FileSymbol { name: "impl_fn", def: Function( @@ -566,7 +626,9 @@ }, ), }, - container_name: None, + container_name: Some( + "Struct", + ), is_alias: false, is_assoc: true, }, @@ -615,12 +677,12 @@ ), ptr: SyntaxNodePtr { kind: FN, - range: 302..338, + range: 375..411, }, name_ptr: AstPtr( SyntaxNodePtr { kind: NAME, - range: 305..309, + range: 378..382, }, ), }, @@ -645,12 +707,12 @@ ), ptr: SyntaxNodePtr { kind: USE_TREE, - range: 611..648, + range: 684..721, }, name_ptr: AstPtr( SyntaxNodePtr { kind: NAME, - range: 628..648, + range: 701..721, }, ), }, @@ -673,12 +735,12 @@ ), ptr: SyntaxNodePtr { kind: FN, - range: 279..298, + range: 352..371, }, name_ptr: AstPtr( SyntaxNodePtr { kind: NAME, - range: 282..290, + range: 355..363, }, ), }, @@ -705,7 +767,7 @@ Struct( Struct { id: StructId( - 2, + 3, ), }, ), @@ -716,12 +778,12 @@ ), ptr: SyntaxNodePtr { kind: STRUCT, - range: 435..455, + range: 508..528, }, name_ptr: AstPtr( SyntaxNodePtr { kind: NAME, - range: 442..454, + range: 515..527, }, ), }, @@ -776,7 +838,7 @@ Struct( Struct { id: StructId( - 3, + 4, ), }, ), @@ -836,7 +898,7 @@ Struct( Struct { id: StructId( - 3, + 4, ), }, ), @@ -866,7 +928,7 @@ Struct( Struct { id: StructId( - 3, + 4, ), }, ),