@@ -13,10 +13,10 @@ use either::Either;
1313use hir:: {
1414 Adt , AsAssocItem , AsExternAssocItem , AssocItem , AttributeTemplate , BuiltinAttr , BuiltinType ,
1515 Const , Crate , DefWithBody , DeriveHelper , DocLinkDef , ExternAssocItem , ExternCrateDecl , Field ,
16- Function , GenericParam , GenericSubstitution , HasVisibility , HirDisplay , Impl , InlineAsmOperand ,
17- Label , Local , Macro , Module , ModuleDef , Name , PathResolution , Semantics , Static ,
18- StaticLifetime , Struct , ToolModule , Trait , TraitAlias , TupleField , TypeAlias , Variant ,
19- VariantDef , Visibility ,
16+ Function , GenericDef , GenericParam , GenericSubstitution , HasContainer , HasVisibility ,
17+ HirDisplay , Impl , InlineAsmOperand , ItemContainer , Label , Local , Macro , Module , ModuleDef ,
18+ Name , PathResolution , Semantics , Static , StaticLifetime , Struct , ToolModule , Trait , TraitAlias ,
19+ TupleField , TypeAlias , Variant , VariantDef , Visibility ,
2020} ;
2121use span:: Edition ;
2222use stdx:: { format_to, impl_from} ;
@@ -97,9 +97,39 @@ impl Definition {
9797 }
9898
9999 pub fn enclosing_definition ( & self , db : & RootDatabase ) -> Option < Definition > {
100+ fn container_to_definition ( container : ItemContainer ) -> Option < Definition > {
101+ match container {
102+ ItemContainer :: Trait ( it) => Some ( it. into ( ) ) ,
103+ ItemContainer :: Impl ( it) => Some ( it. into ( ) ) ,
104+ ItemContainer :: Module ( it) => Some ( it. into ( ) ) ,
105+ ItemContainer :: ExternBlock ( ) | ItemContainer :: Crate ( _) => None ,
106+ }
107+ }
100108 match self {
109+ Definition :: Macro ( it) => Some ( it. module ( db) . into ( ) ) ,
110+ Definition :: Module ( it) => it. parent ( db) . map ( Definition :: Module ) ,
111+ Definition :: Field ( it) => Some ( it. parent_def ( db) . into ( ) ) ,
112+ Definition :: Function ( it) => container_to_definition ( it. container ( db) ) ,
113+ Definition :: Adt ( it) => Some ( it. module ( db) . into ( ) ) ,
114+ Definition :: Const ( it) => container_to_definition ( it. container ( db) ) ,
115+ Definition :: Static ( it) => container_to_definition ( it. container ( db) ) ,
116+ Definition :: Trait ( it) => container_to_definition ( it. container ( db) ) ,
117+ Definition :: TraitAlias ( it) => container_to_definition ( it. container ( db) ) ,
118+ Definition :: TypeAlias ( it) => container_to_definition ( it. container ( db) ) ,
119+ Definition :: Variant ( it) => Some ( Adt :: Enum ( it. parent_enum ( db) ) . into ( ) ) ,
120+ Definition :: SelfType ( it) => Some ( it. module ( db) . into ( ) ) ,
101121 Definition :: Local ( it) => it. parent ( db) . try_into ( ) . ok ( ) ,
102- _ => None ,
122+ Definition :: GenericParam ( it) => Some ( it. parent ( ) . into ( ) ) ,
123+ Definition :: Label ( it) => it. parent ( db) . try_into ( ) . ok ( ) ,
124+ Definition :: ExternCrateDecl ( it) => container_to_definition ( it. container ( db) ) ,
125+ Definition :: DeriveHelper ( it) => Some ( it. derive ( ) . module ( db) . into ( ) ) ,
126+ Definition :: InlineAsmOperand ( it) => it. parent ( db) . try_into ( ) . ok ( ) ,
127+ Definition :: BuiltinAttr ( _)
128+ | Definition :: BuiltinType ( _)
129+ | Definition :: BuiltinLifetime ( _)
130+ | Definition :: TupleField ( _)
131+ | Definition :: ToolModule ( _)
132+ | Definition :: InlineAsmRegOrRegClass ( _) => None ,
103133 }
104134 }
105135
@@ -932,3 +962,17 @@ impl TryFrom<DefWithBody> for Definition {
932962 }
933963 }
934964}
965+
966+ impl From < GenericDef > for Definition {
967+ fn from ( def : GenericDef ) -> Self {
968+ match def {
969+ GenericDef :: Function ( it) => it. into ( ) ,
970+ GenericDef :: Adt ( it) => it. into ( ) ,
971+ GenericDef :: Trait ( it) => it. into ( ) ,
972+ GenericDef :: TraitAlias ( it) => it. into ( ) ,
973+ GenericDef :: TypeAlias ( it) => it. into ( ) ,
974+ GenericDef :: Impl ( it) => it. into ( ) ,
975+ GenericDef :: Const ( it) => it. into ( ) ,
976+ }
977+ }
978+ }
0 commit comments