aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/display/navigation_target.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api/src/display/navigation_target.rs')
-rw-r--r--crates/ra_ide_api/src/display/navigation_target.rs328
1 files changed, 177 insertions, 151 deletions
diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs
index 1bf81e7d5..41d467564 100644
--- a/crates/ra_ide_api/src/display/navigation_target.rs
+++ b/crates/ra_ide_api/src/display/navigation_target.rs
@@ -29,19 +29,8 @@ pub struct NavigationTarget {
29 docs: Option<String>, 29 docs: Option<String>,
30} 30}
31 31
32fn find_range_from_node( 32pub(crate) trait ToNav {
33 db: &RootDatabase, 33 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget;
34 src: hir::HirFileId,
35 node: &SyntaxNode,
36) -> (FileId, TextRange) {
37 let text_range = node.text_range();
38 let (file_id, text_range) = src
39 .expansion_info(db)
40 .and_then(|expansion_info| expansion_info.find_range(text_range))
41 .unwrap_or((src, text_range));
42
43 // FIXME: handle recursive macro generated macro
44 (file_id.original_file(db), text_range)
45} 34}
46 35
47impl NavigationTarget { 36impl NavigationTarget {
@@ -95,19 +84,6 @@ impl NavigationTarget {
95 NavigationTarget::from_named(db, file_id.into(), pat, None, None) 84 NavigationTarget::from_named(db, file_id.into(), pat, None, None)
96 } 85 }
97 86
98 pub(crate) fn from_symbol(db: &RootDatabase, symbol: FileSymbol) -> NavigationTarget {
99 NavigationTarget {
100 file_id: symbol.file_id,
101 name: symbol.name.clone(),
102 kind: symbol.ptr.kind(),
103 full_range: symbol.ptr.range(),
104 focus_range: symbol.name_range,
105 container_name: symbol.container_name.clone(),
106 description: description_from_symbol(db, &symbol),
107 docs: docs_from_symbol(db, &symbol),
108 }
109 }
110
111 pub(crate) fn from_pat( 87 pub(crate) fn from_pat(
112 db: &RootDatabase, 88 db: &RootDatabase,
113 file_id: FileId, 89 file_id: FileId,
@@ -136,39 +112,6 @@ impl NavigationTarget {
136 } 112 }
137 } 113 }
138 114
139 pub(crate) fn from_module(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
140 let src = module.definition_source(db);
141 let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
142 match src.ast {
143 ModuleSource::SourceFile(node) => {
144 let (file_id, text_range) = find_range_from_node(db, src.file_id, node.syntax());
145
146 NavigationTarget::from_syntax(
147 file_id,
148 name,
149 None,
150 text_range,
151 node.syntax(),
152 None,
153 None,
154 )
155 }
156 ModuleSource::Module(node) => {
157 let (file_id, text_range) = find_range_from_node(db, src.file_id, node.syntax());
158
159 NavigationTarget::from_syntax(
160 file_id,
161 name,
162 None,
163 text_range,
164 node.syntax(),
165 node.doc_comment_text(),
166 node.short_label(),
167 )
168 }
169 }
170 }
171
172 pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget { 115 pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
173 let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); 116 let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
174 if let Some(src) = module.declaration_source(db) { 117 if let Some(src) = module.declaration_source(db) {
@@ -183,55 +126,7 @@ impl NavigationTarget {
183 src.ast.short_label(), 126 src.ast.short_label(),
184 ); 127 );
185 } 128 }
186 NavigationTarget::from_module(db, module) 129 module.to_nav(db)
187 }
188
189 pub(crate) fn from_field(db: &RootDatabase, field: hir::StructField) -> NavigationTarget {
190 let src = field.source(db);
191 match src.ast {
192 FieldSource::Named(it) => NavigationTarget::from_named(
193 db,
194 src.file_id,
195 &it,
196 it.doc_comment_text(),
197 it.short_label(),
198 ),
199 FieldSource::Pos(it) => {
200 let (file_id, text_range) = find_range_from_node(db, src.file_id, it.syntax());
201 NavigationTarget::from_syntax(
202 file_id,
203 "".into(),
204 None,
205 text_range,
206 it.syntax(),
207 None,
208 None,
209 )
210 }
211 }
212 }
213
214 pub(crate) fn from_def_source<A, D>(db: &RootDatabase, def: D) -> NavigationTarget
215 where
216 D: HasSource<Ast = A>,
217 A: ast::DocCommentsOwner + ast::NameOwner + ShortLabel,
218 {
219 let src = def.source(db);
220 NavigationTarget::from_named(
221 db,
222 src.file_id,
223 &src.ast,
224 src.ast.doc_comment_text(),
225 src.ast.short_label(),
226 )
227 }
228
229 pub(crate) fn from_adt_def(db: &RootDatabase, adt_def: hir::Adt) -> NavigationTarget {
230 match adt_def {
231 hir::Adt::Struct(it) => NavigationTarget::from_def_source(db, it),
232 hir::Adt::Union(it) => NavigationTarget::from_def_source(db, it),
233 hir::Adt::Enum(it) => NavigationTarget::from_def_source(db, it),
234 }
235 } 130 }
236 131
237 pub(crate) fn from_def( 132 pub(crate) fn from_def(
@@ -239,14 +134,14 @@ impl NavigationTarget {
239 module_def: hir::ModuleDef, 134 module_def: hir::ModuleDef,
240 ) -> Option<NavigationTarget> { 135 ) -> Option<NavigationTarget> {
241 let nav = match module_def { 136 let nav = match module_def {
242 hir::ModuleDef::Module(module) => NavigationTarget::from_module(db, module), 137 hir::ModuleDef::Module(module) => module.to_nav(db),
243 hir::ModuleDef::Function(func) => NavigationTarget::from_def_source(db, func), 138 hir::ModuleDef::Function(it) => it.to_nav(db),
244 hir::ModuleDef::Adt(it) => NavigationTarget::from_adt_def(db, it), 139 hir::ModuleDef::Adt(it) => it.to_nav(db),
245 hir::ModuleDef::Const(it) => NavigationTarget::from_def_source(db, it), 140 hir::ModuleDef::Const(it) => it.to_nav(db),
246 hir::ModuleDef::Static(it) => NavigationTarget::from_def_source(db, it), 141 hir::ModuleDef::Static(it) => it.to_nav(db),
247 hir::ModuleDef::EnumVariant(it) => NavigationTarget::from_def_source(db, it), 142 hir::ModuleDef::EnumVariant(it) => it.to_nav(db),
248 hir::ModuleDef::Trait(it) => NavigationTarget::from_def_source(db, it), 143 hir::ModuleDef::Trait(it) => it.to_nav(db),
249 hir::ModuleDef::TypeAlias(it) => NavigationTarget::from_def_source(db, it), 144 hir::ModuleDef::TypeAlias(it) => it.to_nav(db),
250 hir::ModuleDef::BuiltinType(..) => { 145 hir::ModuleDef::BuiltinType(..) => {
251 return None; 146 return None;
252 } 147 }
@@ -254,41 +149,6 @@ impl NavigationTarget {
254 Some(nav) 149 Some(nav)
255 } 150 }
256 151
257 pub(crate) fn from_impl_block(
258 db: &RootDatabase,
259 impl_block: hir::ImplBlock,
260 ) -> NavigationTarget {
261 let src = impl_block.source(db);
262 let (file_id, text_range) = find_range_from_node(db, src.file_id, src.ast.syntax());
263
264 NavigationTarget::from_syntax(
265 file_id,
266 "impl".into(),
267 None,
268 text_range,
269 src.ast.syntax(),
270 None,
271 None,
272 )
273 }
274
275 pub(crate) fn from_assoc_item(
276 db: &RootDatabase,
277 assoc_item: hir::AssocItem,
278 ) -> NavigationTarget {
279 match assoc_item {
280 AssocItem::Function(it) => NavigationTarget::from_def_source(db, it),
281 AssocItem::Const(it) => NavigationTarget::from_def_source(db, it),
282 AssocItem::TypeAlias(it) => NavigationTarget::from_def_source(db, it),
283 }
284 }
285
286 pub(crate) fn from_macro_def(db: &RootDatabase, macro_call: hir::MacroDef) -> NavigationTarget {
287 let src = macro_call.source(db);
288 log::debug!("nav target {:#?}", src.ast.syntax());
289 NavigationTarget::from_named(db, src.file_id, &src.ast, src.ast.doc_comment_text(), None)
290 }
291
292 #[cfg(test)] 152 #[cfg(test)]
293 pub(crate) fn assert_match(&self, expected: &str) { 153 pub(crate) fn assert_match(&self, expected: &str) {
294 let actual = self.debug_render(); 154 let actual = self.debug_render();
@@ -359,6 +219,172 @@ impl NavigationTarget {
359 } 219 }
360} 220}
361 221
222impl ToNav for FileSymbol {
223 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
224 NavigationTarget {
225 file_id: self.file_id,
226 name: self.name.clone(),
227 kind: self.ptr.kind(),
228 full_range: self.ptr.range(),
229 focus_range: self.name_range,
230 container_name: self.container_name.clone(),
231 description: description_from_symbol(db, self),
232 docs: docs_from_symbol(db, self),
233 }
234 }
235}
236
237pub(crate) trait ToNavFromAst {}
238impl ToNavFromAst for hir::Function {}
239impl ToNavFromAst for hir::Const {}
240impl ToNavFromAst for hir::Static {}
241impl ToNavFromAst for hir::Struct {}
242impl ToNavFromAst for hir::Enum {}
243impl ToNavFromAst for hir::EnumVariant {}
244impl ToNavFromAst for hir::Union {}
245impl ToNavFromAst for hir::TypeAlias {}
246impl ToNavFromAst for hir::Trait {}
247
248impl<D> ToNav for D
249where
250 D: HasSource + ToNavFromAst + Copy,
251 D::Ast: ast::DocCommentsOwner + ast::NameOwner + ShortLabel,
252{
253 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
254 let src = self.source(db);
255 NavigationTarget::from_named(
256 db,
257 src.file_id,
258 &src.ast,
259 src.ast.doc_comment_text(),
260 src.ast.short_label(),
261 )
262 }
263}
264
265impl ToNav for hir::Module {
266 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
267 let src = self.definition_source(db);
268 let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default();
269 match src.ast {
270 ModuleSource::SourceFile(node) => {
271 let (file_id, text_range) = find_range_from_node(db, src.file_id, node.syntax());
272
273 NavigationTarget::from_syntax(
274 file_id,
275 name,
276 None,
277 text_range,
278 node.syntax(),
279 None,
280 None,
281 )
282 }
283 ModuleSource::Module(node) => {
284 let (file_id, text_range) = find_range_from_node(db, src.file_id, node.syntax());
285
286 NavigationTarget::from_syntax(
287 file_id,
288 name,
289 None,
290 text_range,
291 node.syntax(),
292 node.doc_comment_text(),
293 node.short_label(),
294 )
295 }
296 }
297 }
298}
299
300impl ToNav for hir::ImplBlock {
301 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
302 let src = self.source(db);
303 let (file_id, text_range) = find_range_from_node(db, src.file_id, src.ast.syntax());
304
305 NavigationTarget::from_syntax(
306 file_id,
307 "impl".into(),
308 None,
309 text_range,
310 src.ast.syntax(),
311 None,
312 None,
313 )
314 }
315}
316
317impl ToNav for hir::StructField {
318 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
319 let src = self.source(db);
320
321 match src.ast {
322 FieldSource::Named(it) => NavigationTarget::from_named(
323 db,
324 src.file_id,
325 &it,
326 it.doc_comment_text(),
327 it.short_label(),
328 ),
329 FieldSource::Pos(it) => {
330 let (file_id, text_range) = find_range_from_node(db, src.file_id, it.syntax());
331 NavigationTarget::from_syntax(
332 file_id,
333 "".into(),
334 None,
335 text_range,
336 it.syntax(),
337 None,
338 None,
339 )
340 }
341 }
342 }
343}
344
345impl ToNav for hir::MacroDef {
346 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
347 let src = self.source(db);
348 log::debug!("nav target {:#?}", src.ast.syntax());
349 NavigationTarget::from_named(db, src.file_id, &src.ast, src.ast.doc_comment_text(), None)
350 }
351}
352
353impl ToNav for hir::Adt {
354 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
355 match self {
356 hir::Adt::Struct(it) => it.to_nav(db),
357 hir::Adt::Union(it) => it.to_nav(db),
358 hir::Adt::Enum(it) => it.to_nav(db),
359 }
360 }
361}
362
363impl ToNav for hir::AssocItem {
364 fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
365 match self {
366 AssocItem::Function(it) => it.to_nav(db),
367 AssocItem::Const(it) => it.to_nav(db),
368 AssocItem::TypeAlias(it) => it.to_nav(db),
369 }
370 }
371}
372
373fn find_range_from_node(
374 db: &RootDatabase,
375 src: hir::HirFileId,
376 node: &SyntaxNode,
377) -> (FileId, TextRange) {
378 let text_range = node.text_range();
379 let (file_id, text_range) = src
380 .expansion_info(db)
381 .and_then(|expansion_info| expansion_info.find_range(text_range))
382 .unwrap_or((src, text_range));
383
384 // FIXME: handle recursive macro generated macro
385 (file_id.original_file(db), text_range)
386}
387
362pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> { 388pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> {
363 let parse = db.parse(symbol.file_id); 389 let parse = db.parse(symbol.file_id);
364 let node = symbol.ptr.to_node(parse.tree().syntax()); 390 let node = symbol.ptr.to_node(parse.tree().syntax());