diff options
Diffstat (limited to 'crates/ra_ide_api/src/name_kind.rs')
-rw-r--r-- | crates/ra_ide_api/src/name_kind.rs | 116 |
1 files changed, 86 insertions, 30 deletions
diff --git a/crates/ra_ide_api/src/name_kind.rs b/crates/ra_ide_api/src/name_kind.rs index 64adb1784..0effeb8a1 100644 --- a/crates/ra_ide_api/src/name_kind.rs +++ b/crates/ra_ide_api/src/name_kind.rs | |||
@@ -102,36 +102,92 @@ pub(crate) fn classify_name( | |||
102 | let parent = name.syntax().parent()?; | 102 | let parent = name.syntax().parent()?; |
103 | let file_id = file_id.into(); | 103 | let file_id = file_id.into(); |
104 | 104 | ||
105 | if let Some(pat) = ast::BindPat::cast(parent.clone()) { | 105 | macro_rules! match_ast { |
106 | return Some(Pat(AstPtr::new(&pat))); | 106 | (match $node:ident { |
107 | $( ast::$ast:ident($it:ident) => $res:block, )* | ||
108 | _ => $catch_all:expr, | ||
109 | }) => {{ | ||
110 | $( if let Some($it) = ast::$ast::cast($node.clone()) $res else )* | ||
111 | { $catch_all } | ||
112 | }}; | ||
107 | } | 113 | } |
108 | if let Some(var) = ast::EnumVariant::cast(parent.clone()) { | ||
109 | let src = hir::Source { file_id, ast: var }; | ||
110 | let var = hir::EnumVariant::from_source(db, src)?; | ||
111 | return Some(Def(var.into())); | ||
112 | } | ||
113 | if let Some(field) = ast::RecordFieldDef::cast(parent.clone()) { | ||
114 | let src = hir::Source { file_id, ast: hir::FieldSource::Named(field) }; | ||
115 | let field = hir::StructField::from_source(db, src)?; | ||
116 | return Some(FieldAccess(field)); | ||
117 | } | ||
118 | if let Some(field) = ast::TupleFieldDef::cast(parent.clone()) { | ||
119 | let src = hir::Source { file_id, ast: hir::FieldSource::Pos(field) }; | ||
120 | let field = hir::StructField::from_source(db, src)?; | ||
121 | return Some(FieldAccess(field)); | ||
122 | } | ||
123 | if let Some(_) = parent.parent().and_then(ast::ItemList::cast) { | ||
124 | let ast = ast::ImplItem::cast(parent.clone())?; | ||
125 | let src = hir::Source { file_id, ast }; | ||
126 | let item = hir::AssocItem::from_source(db, src)?; | ||
127 | return Some(AssocItem(item)); | ||
128 | } | ||
129 | if let Some(item) = ast::ModuleItem::cast(parent.clone()) { | ||
130 | let src = hir::Source { file_id, ast: item }; | ||
131 | let def = hir::ModuleDef::from_source(db, src)?; | ||
132 | return Some(Def(def)); | ||
133 | } | ||
134 | // FIXME: TYPE_PARAM, ALIAS, MACRO_CALL; Union | ||
135 | 114 | ||
136 | None | 115 | // FIXME: add ast::MacroCall(it) |
116 | match_ast! { | ||
117 | match parent { | ||
118 | ast::BindPat(it) => { | ||
119 | let pat = AstPtr::new(&it); | ||
120 | Some(Pat(pat)) | ||
121 | }, | ||
122 | ast::RecordFieldDef(it) => { | ||
123 | let src = hir::Source { file_id, ast: hir::FieldSource::Named(it) }; | ||
124 | let field = hir::StructField::from_source(db, src)?; | ||
125 | Some(FieldAccess(field)) | ||
126 | }, | ||
127 | ast::FnDef(it) => { | ||
128 | if parent.parent().and_then(ast::ItemList::cast).is_some() { | ||
129 | let src = hir::Source { file_id, ast: ast::ImplItem::from(it) }; | ||
130 | let item = hir::AssocItem::from_source(db, src)?; | ||
131 | Some(AssocItem(item)) | ||
132 | } else { | ||
133 | let src = hir::Source { file_id, ast: it }; | ||
134 | let def = hir::Function::from_source(db, src)?; | ||
135 | Some(Def(def.into())) | ||
136 | } | ||
137 | }, | ||
138 | ast::ConstDef(it) => { | ||
139 | if parent.parent().and_then(ast::ItemList::cast).is_some() { | ||
140 | let src = hir::Source { file_id, ast: ast::ImplItem::from(it) }; | ||
141 | let item = hir::AssocItem::from_source(db, src)?; | ||
142 | Some(AssocItem(item)) | ||
143 | } else { | ||
144 | let src = hir::Source { file_id, ast: it }; | ||
145 | let def = hir::Const::from_source(db, src)?; | ||
146 | Some(Def(def.into())) | ||
147 | } | ||
148 | }, | ||
149 | ast::TypeAliasDef(it) => { | ||
150 | if parent.parent().and_then(ast::ItemList::cast).is_some() { | ||
151 | let src = hir::Source { file_id, ast: ast::ImplItem::from(it) }; | ||
152 | let item = hir::AssocItem::from_source(db, src)?; | ||
153 | Some(AssocItem(item)) | ||
154 | } else { | ||
155 | let src = hir::Source { file_id, ast: it }; | ||
156 | let def = hir::TypeAlias::from_source(db, src)?; | ||
157 | Some(Def(def.into())) | ||
158 | } | ||
159 | }, | ||
160 | ast::Module(it) => { | ||
161 | let src = hir::Source { file_id, ast: hir::ModuleSource::Module(it) }; | ||
162 | let def = hir::Module::from_definition(db, src)?; | ||
163 | Some(Def(def.into())) | ||
164 | }, | ||
165 | ast::StructDef(it) => { | ||
166 | let src = hir::Source { file_id, ast: it }; | ||
167 | let def = hir::Struct::from_source(db, src)?; | ||
168 | Some(Def(def.into())) | ||
169 | }, | ||
170 | ast::EnumDef(it) => { | ||
171 | let src = hir::Source { file_id, ast: it }; | ||
172 | let def = hir::Enum::from_source(db, src)?; | ||
173 | Some(Def(def.into())) | ||
174 | }, | ||
175 | ast::TraitDef(it) => { | ||
176 | let src = hir::Source { file_id, ast: it }; | ||
177 | let def = hir::Trait::from_source(db, src)?; | ||
178 | Some(Def(def.into())) | ||
179 | }, | ||
180 | ast::StaticDef(it) => { | ||
181 | let src = hir::Source { file_id, ast: it }; | ||
182 | let def = hir::Static::from_source(db, src)?; | ||
183 | Some(Def(def.into())) | ||
184 | }, | ||
185 | ast::EnumVariant(it) => { | ||
186 | let src = hir::Source { file_id, ast: it }; | ||
187 | let def = hir::EnumVariant::from_source(db, src)?; | ||
188 | Some(Def(def.into())) | ||
189 | }, | ||
190 | _ => None, | ||
191 | } | ||
192 | } | ||
137 | } | 193 | } |