@@ -13,10 +13,10 @@ use either::Either;
13
13
use hir:: {
14
14
Adt , AsAssocItem , AsExternAssocItem , AssocItem , AttributeTemplate , BuiltinAttr , BuiltinType ,
15
15
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 ,
20
20
} ;
21
21
use span:: Edition ;
22
22
use stdx:: { format_to, impl_from} ;
@@ -97,9 +97,39 @@ impl Definition {
97
97
}
98
98
99
99
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
+ }
100
108
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 ( ) ) ,
101
121
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 ,
103
133
}
104
134
}
105
135
@@ -932,3 +962,17 @@ impl TryFrom<DefWithBody> for Definition {
932
962
}
933
963
}
934
964
}
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