diff options
author | Nick Spain <[email protected]> | 2021-01-01 06:13:15 +0000 |
---|---|---|
committer | Nick Spain <[email protected]> | 2021-01-02 10:53:52 +0000 |
commit | 3f1b3df65bee923e5de0652ea4b676530da29127 (patch) | |
tree | 81b898785f7d934eabc48bb3575cb14f66f6abce /crates/ide/src | |
parent | 6800c606ec7be5f19c1728a246eb2e2ffa4110f6 (diff) |
Move impls of ToNav that use source() to TryToNav
Diffstat (limited to 'crates/ide/src')
-rw-r--r-- | crates/ide/src/call_hierarchy.rs | 8 | ||||
-rw-r--r-- | crates/ide/src/display/navigation_target.rs | 105 | ||||
-rw-r--r-- | crates/ide/src/goto_implementation.rs | 6 | ||||
-rw-r--r-- | crates/ide/src/goto_type_definition.rs | 4 | ||||
-rw-r--r-- | crates/ide/src/hover.rs | 10 |
5 files changed, 64 insertions, 69 deletions
diff --git a/crates/ide/src/call_hierarchy.rs b/crates/ide/src/call_hierarchy.rs index 60e0cd4ad..3c2d39f5d 100644 --- a/crates/ide/src/call_hierarchy.rs +++ b/crates/ide/src/call_hierarchy.rs | |||
@@ -8,7 +8,7 @@ use ide_db::RootDatabase; | |||
8 | use syntax::{ast, match_ast, AstNode, TextRange}; | 8 | use syntax::{ast, match_ast, AstNode, TextRange}; |
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{ |
11 | display::ToNav, goto_definition, references, FilePosition, NavigationTarget, RangeInfo, | 11 | display::TryToNav, goto_definition, references, FilePosition, NavigationTarget, RangeInfo, |
12 | }; | 12 | }; |
13 | 13 | ||
14 | #[derive(Debug, Clone)] | 14 | #[derive(Debug, Clone)] |
@@ -61,7 +61,7 @@ pub(crate) fn incoming_calls(db: &RootDatabase, position: FilePosition) -> Optio | |||
61 | match node { | 61 | match node { |
62 | ast::Fn(it) => { | 62 | ast::Fn(it) => { |
63 | let def = sema.to_def(&it)?; | 63 | let def = sema.to_def(&it)?; |
64 | Some(def.to_nav(sema.db)) | 64 | def.try_to_nav(sema.db) |
65 | }, | 65 | }, |
66 | _ => None, | 66 | _ => None, |
67 | } | 67 | } |
@@ -99,7 +99,7 @@ pub(crate) fn outgoing_calls(db: &RootDatabase, position: FilePosition) -> Optio | |||
99 | match callable.kind() { | 99 | match callable.kind() { |
100 | hir::CallableKind::Function(it) => { | 100 | hir::CallableKind::Function(it) => { |
101 | let fn_def: hir::Function = it.into(); | 101 | let fn_def: hir::Function = it.into(); |
102 | let nav = fn_def.to_nav(db); | 102 | let nav = fn_def.try_to_nav(db)?; |
103 | Some(nav) | 103 | Some(nav) |
104 | } | 104 | } |
105 | _ => None, | 105 | _ => None, |
@@ -107,7 +107,7 @@ pub(crate) fn outgoing_calls(db: &RootDatabase, position: FilePosition) -> Optio | |||
107 | } | 107 | } |
108 | FnCallNode::MethodCallExpr(expr) => { | 108 | FnCallNode::MethodCallExpr(expr) => { |
109 | let function = sema.resolve_method_call(&expr)?; | 109 | let function = sema.resolve_method_call(&expr)?; |
110 | Some(function.to_nav(db)) | 110 | function.try_to_nav(db) |
111 | } | 111 | } |
112 | } { | 112 | } { |
113 | Some((func_target, name_ref.syntax().text_range())) | 113 | Some((func_target, name_ref.syntax().text_range())) |
diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs index 5dc3f4128..1fb26c226 100644 --- a/crates/ide/src/display/navigation_target.rs +++ b/crates/ide/src/display/navigation_target.rs | |||
@@ -211,12 +211,12 @@ impl TryToNav for Definition { | |||
211 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { | 211 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
212 | match self { | 212 | match self { |
213 | Definition::Macro(it) => it.try_to_nav(db), | 213 | Definition::Macro(it) => it.try_to_nav(db), |
214 | Definition::Field(it) => Some(it.to_nav(db)), | 214 | Definition::Field(it) => it.try_to_nav(db), |
215 | Definition::ModuleDef(it) => it.try_to_nav(db), | 215 | Definition::ModuleDef(it) => it.try_to_nav(db), |
216 | Definition::SelfType(it) => Some(it.to_nav(db)), | 216 | Definition::SelfType(it) => it.try_to_nav(db), |
217 | Definition::Local(it) => Some(it.to_nav(db)), | 217 | Definition::Local(it) => Some(it.to_nav(db)), |
218 | Definition::TypeParam(it) => Some(it.to_nav(db)), | 218 | Definition::TypeParam(it) => it.try_to_nav(db), |
219 | Definition::LifetimeParam(it) => Some(it.to_nav(db)), | 219 | Definition::LifetimeParam(it) => it.try_to_nav(db), |
220 | Definition::Label(it) => Some(it.to_nav(db)), | 220 | Definition::Label(it) => Some(it.to_nav(db)), |
221 | Definition::ConstParam(it) => Some(it.to_nav(db)), | 221 | Definition::ConstParam(it) => Some(it.to_nav(db)), |
222 | } | 222 | } |
@@ -225,18 +225,17 @@ impl TryToNav for Definition { | |||
225 | 225 | ||
226 | impl TryToNav for hir::ModuleDef { | 226 | impl TryToNav for hir::ModuleDef { |
227 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { | 227 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
228 | let res = match self { | 228 | match self { |
229 | hir::ModuleDef::Module(it) => it.to_nav(db), | 229 | hir::ModuleDef::Module(it) => Some(it.to_nav(db)), |
230 | hir::ModuleDef::Function(it) => it.to_nav(db), | 230 | hir::ModuleDef::Function(it) => it.try_to_nav(db), |
231 | hir::ModuleDef::Adt(it) => it.to_nav(db), | 231 | hir::ModuleDef::Adt(it) => it.try_to_nav(db), |
232 | hir::ModuleDef::Variant(it) => it.to_nav(db), | 232 | hir::ModuleDef::Variant(it) => it.try_to_nav(db), |
233 | hir::ModuleDef::Const(it) => it.to_nav(db), | 233 | hir::ModuleDef::Const(it) => it.try_to_nav(db), |
234 | hir::ModuleDef::Static(it) => it.to_nav(db), | 234 | hir::ModuleDef::Static(it) => it.try_to_nav(db), |
235 | hir::ModuleDef::Trait(it) => it.to_nav(db), | 235 | hir::ModuleDef::Trait(it) => it.try_to_nav(db), |
236 | hir::ModuleDef::TypeAlias(it) => it.to_nav(db), | 236 | hir::ModuleDef::TypeAlias(it) => it.try_to_nav(db), |
237 | hir::ModuleDef::BuiltinType(_) => return None, | 237 | hir::ModuleDef::BuiltinType(_) => None, |
238 | }; | 238 | } |
239 | Some(res) | ||
240 | } | 239 | } |
241 | } | 240 | } |
242 | 241 | ||
@@ -271,14 +270,13 @@ impl ToNavFromAst for hir::Trait { | |||
271 | const KIND: SymbolKind = SymbolKind::Trait; | 270 | const KIND: SymbolKind = SymbolKind::Trait; |
272 | } | 271 | } |
273 | 272 | ||
274 | impl<D> ToNav for D | 273 | impl<D> TryToNav for D |
275 | where | 274 | where |
276 | D: HasSource + ToNavFromAst + Copy + HasAttrs, | 275 | D: HasSource + ToNavFromAst + Copy + HasAttrs, |
277 | D::Ast: ast::NameOwner + ShortLabel, | 276 | D::Ast: ast::NameOwner + ShortLabel, |
278 | { | 277 | { |
279 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 278 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
280 | #[allow(deprecated)] | 279 | let src = self.source(db)?; |
281 | let src = self.source_old(db); | ||
282 | let mut res = NavigationTarget::from_named( | 280 | let mut res = NavigationTarget::from_named( |
283 | db, | 281 | db, |
284 | src.as_ref().map(|it| it as &dyn ast::NameOwner), | 282 | src.as_ref().map(|it| it as &dyn ast::NameOwner), |
@@ -286,7 +284,7 @@ where | |||
286 | ); | 284 | ); |
287 | res.docs = self.docs(db); | 285 | res.docs = self.docs(db); |
288 | res.description = src.value.short_label(); | 286 | res.description = src.value.short_label(); |
289 | res | 287 | Some(res) |
290 | } | 288 | } |
291 | } | 289 | } |
292 | 290 | ||
@@ -305,10 +303,9 @@ impl ToNav for hir::Module { | |||
305 | } | 303 | } |
306 | } | 304 | } |
307 | 305 | ||
308 | impl ToNav for hir::Impl { | 306 | impl TryToNav for hir::Impl { |
309 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 307 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
310 | #[allow(deprecated)] | 308 | let src = self.source(db)?; |
311 | let src = self.source_old(db); | ||
312 | let derive_attr = self.is_builtin_derive(db); | 309 | let derive_attr = self.is_builtin_derive(db); |
313 | let frange = if let Some(item) = &derive_attr { | 310 | let frange = if let Some(item) = &derive_attr { |
314 | item.syntax().original_file_range(db) | 311 | item.syntax().original_file_range(db) |
@@ -321,22 +318,21 @@ impl ToNav for hir::Impl { | |||
321 | src.value.self_ty().map(|ty| src.with_value(ty.syntax()).original_file_range(db).range) | 318 | src.value.self_ty().map(|ty| src.with_value(ty.syntax()).original_file_range(db).range) |
322 | }; | 319 | }; |
323 | 320 | ||
324 | NavigationTarget::from_syntax( | 321 | Some(NavigationTarget::from_syntax( |
325 | frange.file_id, | 322 | frange.file_id, |
326 | "impl".into(), | 323 | "impl".into(), |
327 | focus_range, | 324 | focus_range, |
328 | frange.range, | 325 | frange.range, |
329 | SymbolKind::Impl, | 326 | SymbolKind::Impl, |
330 | ) | 327 | )) |
331 | } | 328 | } |
332 | } | 329 | } |
333 | 330 | ||
334 | impl ToNav for hir::Field { | 331 | impl TryToNav for hir::Field { |
335 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 332 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
336 | #[allow(deprecated)] | 333 | let src = self.source(db)?; |
337 | let src = self.source_old(db); | ||
338 | 334 | ||
339 | match &src.value { | 335 | let field_source = match &src.value { |
340 | FieldSource::Named(it) => { | 336 | FieldSource::Named(it) => { |
341 | let mut res = | 337 | let mut res = |
342 | NavigationTarget::from_named(db, src.with_value(it), SymbolKind::Field); | 338 | NavigationTarget::from_named(db, src.with_value(it), SymbolKind::Field); |
@@ -354,7 +350,8 @@ impl ToNav for hir::Field { | |||
354 | SymbolKind::Field, | 350 | SymbolKind::Field, |
355 | ) | 351 | ) |
356 | } | 352 | } |
357 | } | 353 | }; |
354 | Some(field_source) | ||
358 | } | 355 | } |
359 | } | 356 | } |
360 | 357 | ||
@@ -372,22 +369,22 @@ impl TryToNav for hir::MacroDef { | |||
372 | } | 369 | } |
373 | } | 370 | } |
374 | 371 | ||
375 | impl ToNav for hir::Adt { | 372 | impl TryToNav for hir::Adt { |
376 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 373 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
377 | match self { | 374 | match self { |
378 | hir::Adt::Struct(it) => it.to_nav(db), | 375 | hir::Adt::Struct(it) => it.try_to_nav(db), |
379 | hir::Adt::Union(it) => it.to_nav(db), | 376 | hir::Adt::Union(it) => it.try_to_nav(db), |
380 | hir::Adt::Enum(it) => it.to_nav(db), | 377 | hir::Adt::Enum(it) => it.try_to_nav(db), |
381 | } | 378 | } |
382 | } | 379 | } |
383 | } | 380 | } |
384 | 381 | ||
385 | impl ToNav for hir::AssocItem { | 382 | impl TryToNav for hir::AssocItem { |
386 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 383 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
387 | match self { | 384 | match self { |
388 | AssocItem::Function(it) => it.to_nav(db), | 385 | AssocItem::Function(it) => it.try_to_nav(db), |
389 | AssocItem::Const(it) => it.to_nav(db), | 386 | AssocItem::Const(it) => it.try_to_nav(db), |
390 | AssocItem::TypeAlias(it) => it.to_nav(db), | 387 | AssocItem::TypeAlias(it) => it.try_to_nav(db), |
391 | } | 388 | } |
392 | } | 389 | } |
393 | } | 390 | } |
@@ -441,10 +438,9 @@ impl ToNav for hir::Label { | |||
441 | } | 438 | } |
442 | } | 439 | } |
443 | 440 | ||
444 | impl ToNav for hir::TypeParam { | 441 | impl TryToNav for hir::TypeParam { |
445 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 442 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
446 | #[allow(deprecated)] | 443 | let src = self.source(db)?; |
447 | let src = self.source_old(db); | ||
448 | let full_range = match &src.value { | 444 | let full_range = match &src.value { |
449 | Either::Left(it) => it.syntax().text_range(), | 445 | Either::Left(it) => it.syntax().text_range(), |
450 | Either::Right(it) => it.syntax().text_range(), | 446 | Either::Right(it) => it.syntax().text_range(), |
@@ -453,7 +449,7 @@ impl ToNav for hir::TypeParam { | |||
453 | Either::Left(_) => None, | 449 | Either::Left(_) => None, |
454 | Either::Right(it) => it.name().map(|it| it.syntax().text_range()), | 450 | Either::Right(it) => it.name().map(|it| it.syntax().text_range()), |
455 | }; | 451 | }; |
456 | NavigationTarget { | 452 | Some(NavigationTarget { |
457 | file_id: src.file_id.original_file(db), | 453 | file_id: src.file_id.original_file(db), |
458 | name: self.name(db).to_string().into(), | 454 | name: self.name(db).to_string().into(), |
459 | kind: Some(SymbolKind::TypeParam), | 455 | kind: Some(SymbolKind::TypeParam), |
@@ -462,16 +458,15 @@ impl ToNav for hir::TypeParam { | |||
462 | container_name: None, | 458 | container_name: None, |
463 | description: None, | 459 | description: None, |
464 | docs: None, | 460 | docs: None, |
465 | } | 461 | }) |
466 | } | 462 | } |
467 | } | 463 | } |
468 | 464 | ||
469 | impl ToNav for hir::LifetimeParam { | 465 | impl TryToNav for hir::LifetimeParam { |
470 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | 466 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
471 | #[allow(deprecated)] | 467 | let src = self.source(db)?; |
472 | let src = self.source_old(db); | ||
473 | let full_range = src.value.syntax().text_range(); | 468 | let full_range = src.value.syntax().text_range(); |
474 | NavigationTarget { | 469 | Some(NavigationTarget { |
475 | file_id: src.file_id.original_file(db), | 470 | file_id: src.file_id.original_file(db), |
476 | name: self.name(db).to_string().into(), | 471 | name: self.name(db).to_string().into(), |
477 | kind: Some(SymbolKind::LifetimeParam), | 472 | kind: Some(SymbolKind::LifetimeParam), |
@@ -480,7 +475,7 @@ impl ToNav for hir::LifetimeParam { | |||
480 | container_name: None, | 475 | container_name: None, |
481 | description: None, | 476 | description: None, |
482 | docs: None, | 477 | docs: None, |
483 | } | 478 | }) |
484 | } | 479 | } |
485 | } | 480 | } |
486 | 481 | ||
diff --git a/crates/ide/src/goto_implementation.rs b/crates/ide/src/goto_implementation.rs index 6eac39639..da9378a97 100644 --- a/crates/ide/src/goto_implementation.rs +++ b/crates/ide/src/goto_implementation.rs | |||
@@ -2,7 +2,7 @@ use hir::{Crate, Impl, Semantics}; | |||
2 | use ide_db::RootDatabase; | 2 | use ide_db::RootDatabase; |
3 | use syntax::{algo::find_node_at_offset, ast, AstNode}; | 3 | use syntax::{algo::find_node_at_offset, ast, AstNode}; |
4 | 4 | ||
5 | use crate::{display::ToNav, FilePosition, NavigationTarget, RangeInfo}; | 5 | use crate::{display::TryToNav, FilePosition, NavigationTarget, RangeInfo}; |
6 | 6 | ||
7 | // Feature: Go to Implementation | 7 | // Feature: Go to Implementation |
8 | // | 8 | // |
@@ -55,7 +55,7 @@ fn impls_for_def( | |||
55 | impls | 55 | impls |
56 | .into_iter() | 56 | .into_iter() |
57 | .filter(|impl_def| ty.is_equal_for_find_impls(&impl_def.target_ty(sema.db))) | 57 | .filter(|impl_def| ty.is_equal_for_find_impls(&impl_def.target_ty(sema.db))) |
58 | .map(|imp| imp.to_nav(sema.db)) | 58 | .filter_map(|imp| imp.try_to_nav(sema.db)) |
59 | .collect(), | 59 | .collect(), |
60 | ) | 60 | ) |
61 | } | 61 | } |
@@ -69,7 +69,7 @@ fn impls_for_trait( | |||
69 | 69 | ||
70 | let impls = Impl::for_trait(sema.db, krate, tr); | 70 | let impls = Impl::for_trait(sema.db, krate, tr); |
71 | 71 | ||
72 | Some(impls.into_iter().map(|imp| imp.to_nav(sema.db)).collect()) | 72 | Some(impls.into_iter().filter_map(|imp| imp.try_to_nav(sema.db)).collect()) |
73 | } | 73 | } |
74 | 74 | ||
75 | #[cfg(test)] | 75 | #[cfg(test)] |
diff --git a/crates/ide/src/goto_type_definition.rs b/crates/ide/src/goto_type_definition.rs index aba6bf5dc..7e84e06be 100644 --- a/crates/ide/src/goto_type_definition.rs +++ b/crates/ide/src/goto_type_definition.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | use ide_db::RootDatabase; | 1 | use ide_db::RootDatabase; |
2 | use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T}; | 2 | use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T}; |
3 | 3 | ||
4 | use crate::{display::ToNav, FilePosition, NavigationTarget, RangeInfo}; | 4 | use crate::{display::TryToNav, FilePosition, NavigationTarget, RangeInfo}; |
5 | 5 | ||
6 | // Feature: Go to Type Definition | 6 | // Feature: Go to Type Definition |
7 | // | 7 | // |
@@ -37,7 +37,7 @@ pub(crate) fn goto_type_definition( | |||
37 | 37 | ||
38 | let adt_def = ty.autoderef(db).filter_map(|ty| ty.as_adt()).last()?; | 38 | let adt_def = ty.autoderef(db).filter_map(|ty| ty.as_adt()).last()?; |
39 | 39 | ||
40 | let nav = adt_def.to_nav(db); | 40 | let nav = adt_def.try_to_nav(db)?; |
41 | Some(RangeInfo::new(node.text_range(), vec![nav])) | 41 | Some(RangeInfo::new(node.text_range(), vec![nav])) |
42 | } | 42 | } |
43 | 43 | ||
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index d2a0cfcd4..2737c900f 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs | |||
@@ -13,7 +13,7 @@ use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, | |||
13 | use test_utils::mark; | 13 | use test_utils::mark; |
14 | 14 | ||
15 | use crate::{ | 15 | use crate::{ |
16 | display::{macro_label, ShortLabel, ToNav, TryToNav}, | 16 | display::{macro_label, ShortLabel, TryToNav}, |
17 | doc_links::{remove_links, rewrite_links}, | 17 | doc_links::{remove_links, rewrite_links}, |
18 | markdown_remove::remove_markdown, | 18 | markdown_remove::remove_markdown, |
19 | markup::Markup, | 19 | markup::Markup, |
@@ -183,10 +183,10 @@ fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<Hov | |||
183 | 183 | ||
184 | match def { | 184 | match def { |
185 | Definition::ModuleDef(it) => match it { | 185 | Definition::ModuleDef(it) => match it { |
186 | ModuleDef::Adt(Adt::Struct(it)) => Some(to_action(it.to_nav(db))), | 186 | ModuleDef::Adt(Adt::Struct(it)) => Some(to_action(it.try_to_nav(db)?)), |
187 | ModuleDef::Adt(Adt::Union(it)) => Some(to_action(it.to_nav(db))), | 187 | ModuleDef::Adt(Adt::Union(it)) => Some(to_action(it.try_to_nav(db)?)), |
188 | ModuleDef::Adt(Adt::Enum(it)) => Some(to_action(it.to_nav(db))), | 188 | ModuleDef::Adt(Adt::Enum(it)) => Some(to_action(it.try_to_nav(db)?)), |
189 | ModuleDef::Trait(it) => Some(to_action(it.to_nav(db))), | 189 | ModuleDef::Trait(it) => Some(to_action(it.try_to_nav(db)?)), |
190 | _ => None, | 190 | _ => None, |
191 | }, | 191 | }, |
192 | _ => None, | 192 | _ => None, |