From 27cadcd531c017aa7c78c6f7a36f2b7f2ce8a196 Mon Sep 17 00:00:00 2001 From: Nick Spain Date: Fri, 1 Jan 2021 13:05:28 +1100 Subject: HasSource::source -> HasSource::source_old To start migrating HasSource::source to return an Option. --- crates/ide/src/display/navigation_target.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'crates/ide/src/display') diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs index bcde2b6f1..de4c0fa12 100644 --- a/crates/ide/src/display/navigation_target.rs +++ b/crates/ide/src/display/navigation_target.rs @@ -285,7 +285,7 @@ where D::Ast: ast::NameOwner + ShortLabel, { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { - let src = self.source(db); + let src = self.source_old(db); let mut res = NavigationTarget::from_named( db, src.as_ref().map(|it| it as &dyn ast::NameOwner), @@ -314,7 +314,7 @@ impl ToNav for hir::Module { impl ToNav for hir::Impl { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { - let src = self.source(db); + let src = self.source_old(db); let derive_attr = self.is_builtin_derive(db); let frange = if let Some(item) = &derive_attr { item.syntax().original_file_range(db) @@ -339,7 +339,7 @@ impl ToNav for hir::Impl { impl ToNav for hir::Field { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { - let src = self.source(db); + let src = self.source_old(db); match &src.value { FieldSource::Named(it) => { @@ -365,7 +365,7 @@ impl ToNav for hir::Field { impl ToNav for hir::MacroDef { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { - let src = self.source(db); + let src = self.source_old(db); log::debug!("nav target {:#?}", src.value.syntax()); let mut res = NavigationTarget::from_named( db, @@ -448,7 +448,7 @@ impl ToNav for hir::Label { impl ToNav for hir::TypeParam { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { - let src = self.source(db); + let src = self.source_old(db); let full_range = match &src.value { Either::Left(it) => it.syntax().text_range(), Either::Right(it) => it.syntax().text_range(), @@ -472,7 +472,7 @@ impl ToNav for hir::TypeParam { impl ToNav for hir::LifetimeParam { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { - let src = self.source(db); + let src = self.source_old(db); let full_range = src.value.syntax().text_range(); NavigationTarget { file_id: src.file_id.original_file(db), -- cgit v1.2.3 From ea4708c444509449b86c50b7b1b23f9ff5af4e97 Mon Sep 17 00:00:00 2001 From: Nick Spain Date: Fri, 1 Jan 2021 13:50:50 +1100 Subject: Mark HasSource::source_old as deprecated but allow at all call sites --- crates/ide/src/display/navigation_target.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'crates/ide/src/display') diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs index de4c0fa12..efa0418ad 100644 --- a/crates/ide/src/display/navigation_target.rs +++ b/crates/ide/src/display/navigation_target.rs @@ -285,6 +285,7 @@ where D::Ast: ast::NameOwner + ShortLabel, { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + #[allow(deprecated)] let src = self.source_old(db); let mut res = NavigationTarget::from_named( db, @@ -314,6 +315,7 @@ impl ToNav for hir::Module { impl ToNav for hir::Impl { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + #[allow(deprecated)] let src = self.source_old(db); let derive_attr = self.is_builtin_derive(db); let frange = if let Some(item) = &derive_attr { @@ -339,6 +341,7 @@ impl ToNav for hir::Impl { impl ToNav for hir::Field { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + #[allow(deprecated)] let src = self.source_old(db); match &src.value { @@ -365,6 +368,7 @@ impl ToNav for hir::Field { impl ToNav for hir::MacroDef { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + #[allow(deprecated)] let src = self.source_old(db); log::debug!("nav target {:#?}", src.value.syntax()); let mut res = NavigationTarget::from_named( @@ -448,6 +452,7 @@ impl ToNav for hir::Label { impl ToNav for hir::TypeParam { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + #[allow(deprecated)] let src = self.source_old(db); let full_range = match &src.value { Either::Left(it) => it.syntax().text_range(), @@ -472,6 +477,7 @@ impl ToNav for hir::TypeParam { impl ToNav for hir::LifetimeParam { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + #[allow(deprecated)] let src = self.source_old(db); let full_range = src.value.syntax().text_range(); NavigationTarget { -- cgit v1.2.3 From 14d0db0759c5b8e1d085ebab03a8b944a8921f2e Mon Sep 17 00:00:00 2001 From: Nick Spain Date: Fri, 1 Jan 2021 14:14:09 +1100 Subject: HasSource::source_old -> HasSource::source for places where proc-macros were special cased In #6901 some special case handling for proc-macros was introduced to prevent panicing as they have no AST. Now the new HasSource::source method is used that returns an option. Generally this was a pretty trivial change, the only thing of much interest is that `hir::MacroDef` now implements `TryToNav` not `ToNav` as this allows us to handle `HasSource::source` now returning an option. --- crates/ide/src/display/navigation_target.rs | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) (limited to 'crates/ide/src/display') diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs index efa0418ad..5dc3f4128 100644 --- a/crates/ide/src/display/navigation_target.rs +++ b/crates/ide/src/display/navigation_target.rs @@ -210,15 +210,7 @@ impl ToNav for FileSymbol { impl TryToNav for Definition { fn try_to_nav(&self, db: &RootDatabase) -> Option { match self { - Definition::Macro(it) => { - // FIXME: Currently proc-macro do not have ast-node, - // such that it does not have source - // more discussion: https://github.com/rust-analyzer/rust-analyzer/issues/6913 - if it.is_proc_macro() { - return None; - } - Some(it.to_nav(db)) - } + Definition::Macro(it) => it.try_to_nav(db), Definition::Field(it) => Some(it.to_nav(db)), Definition::ModuleDef(it) => it.try_to_nav(db), Definition::SelfType(it) => Some(it.to_nav(db)), @@ -366,10 +358,9 @@ impl ToNav for hir::Field { } } -impl ToNav for hir::MacroDef { - fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { - #[allow(deprecated)] - let src = self.source_old(db); +impl TryToNav for hir::MacroDef { + fn try_to_nav(&self, db: &RootDatabase) -> Option { + let src = self.source(db)?; log::debug!("nav target {:#?}", src.value.syntax()); let mut res = NavigationTarget::from_named( db, @@ -377,7 +368,7 @@ impl ToNav for hir::MacroDef { SymbolKind::Macro, ); res.docs = self.docs(db); - res + Some(res) } } -- cgit v1.2.3 From 3f1b3df65bee923e5de0652ea4b676530da29127 Mon Sep 17 00:00:00 2001 From: Nick Spain Date: Fri, 1 Jan 2021 17:13:15 +1100 Subject: Move impls of ToNav that use source() to TryToNav --- crates/ide/src/display/navigation_target.rs | 105 +++++++++++++--------------- 1 file changed, 50 insertions(+), 55 deletions(-) (limited to 'crates/ide/src/display') 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 { fn try_to_nav(&self, db: &RootDatabase) -> Option { match self { Definition::Macro(it) => it.try_to_nav(db), - Definition::Field(it) => Some(it.to_nav(db)), + Definition::Field(it) => it.try_to_nav(db), Definition::ModuleDef(it) => it.try_to_nav(db), - Definition::SelfType(it) => Some(it.to_nav(db)), + Definition::SelfType(it) => it.try_to_nav(db), Definition::Local(it) => Some(it.to_nav(db)), - Definition::TypeParam(it) => Some(it.to_nav(db)), - Definition::LifetimeParam(it) => Some(it.to_nav(db)), + Definition::TypeParam(it) => it.try_to_nav(db), + Definition::LifetimeParam(it) => it.try_to_nav(db), Definition::Label(it) => Some(it.to_nav(db)), Definition::ConstParam(it) => Some(it.to_nav(db)), } @@ -225,18 +225,17 @@ impl TryToNav for Definition { impl TryToNav for hir::ModuleDef { fn try_to_nav(&self, db: &RootDatabase) -> Option { - let res = match self { - hir::ModuleDef::Module(it) => it.to_nav(db), - hir::ModuleDef::Function(it) => it.to_nav(db), - hir::ModuleDef::Adt(it) => it.to_nav(db), - hir::ModuleDef::Variant(it) => it.to_nav(db), - hir::ModuleDef::Const(it) => it.to_nav(db), - hir::ModuleDef::Static(it) => it.to_nav(db), - hir::ModuleDef::Trait(it) => it.to_nav(db), - hir::ModuleDef::TypeAlias(it) => it.to_nav(db), - hir::ModuleDef::BuiltinType(_) => return None, - }; - Some(res) + match self { + hir::ModuleDef::Module(it) => Some(it.to_nav(db)), + hir::ModuleDef::Function(it) => it.try_to_nav(db), + hir::ModuleDef::Adt(it) => it.try_to_nav(db), + hir::ModuleDef::Variant(it) => it.try_to_nav(db), + hir::ModuleDef::Const(it) => it.try_to_nav(db), + hir::ModuleDef::Static(it) => it.try_to_nav(db), + hir::ModuleDef::Trait(it) => it.try_to_nav(db), + hir::ModuleDef::TypeAlias(it) => it.try_to_nav(db), + hir::ModuleDef::BuiltinType(_) => None, + } } } @@ -271,14 +270,13 @@ impl ToNavFromAst for hir::Trait { const KIND: SymbolKind = SymbolKind::Trait; } -impl ToNav for D +impl TryToNav for D where D: HasSource + ToNavFromAst + Copy + HasAttrs, D::Ast: ast::NameOwner + ShortLabel, { - fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { - #[allow(deprecated)] - let src = self.source_old(db); + fn try_to_nav(&self, db: &RootDatabase) -> Option { + let src = self.source(db)?; let mut res = NavigationTarget::from_named( db, src.as_ref().map(|it| it as &dyn ast::NameOwner), @@ -286,7 +284,7 @@ where ); res.docs = self.docs(db); res.description = src.value.short_label(); - res + Some(res) } } @@ -305,10 +303,9 @@ impl ToNav for hir::Module { } } -impl ToNav for hir::Impl { - fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { - #[allow(deprecated)] - let src = self.source_old(db); +impl TryToNav for hir::Impl { + fn try_to_nav(&self, db: &RootDatabase) -> Option { + let src = self.source(db)?; let derive_attr = self.is_builtin_derive(db); let frange = if let Some(item) = &derive_attr { item.syntax().original_file_range(db) @@ -321,22 +318,21 @@ impl ToNav for hir::Impl { src.value.self_ty().map(|ty| src.with_value(ty.syntax()).original_file_range(db).range) }; - NavigationTarget::from_syntax( + Some(NavigationTarget::from_syntax( frange.file_id, "impl".into(), focus_range, frange.range, SymbolKind::Impl, - ) + )) } } -impl ToNav for hir::Field { - fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { - #[allow(deprecated)] - let src = self.source_old(db); +impl TryToNav for hir::Field { + fn try_to_nav(&self, db: &RootDatabase) -> Option { + let src = self.source(db)?; - match &src.value { + let field_source = match &src.value { FieldSource::Named(it) => { let mut res = NavigationTarget::from_named(db, src.with_value(it), SymbolKind::Field); @@ -354,7 +350,8 @@ impl ToNav for hir::Field { SymbolKind::Field, ) } - } + }; + Some(field_source) } } @@ -372,22 +369,22 @@ impl TryToNav for hir::MacroDef { } } -impl ToNav for hir::Adt { - fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { +impl TryToNav for hir::Adt { + fn try_to_nav(&self, db: &RootDatabase) -> Option { match self { - hir::Adt::Struct(it) => it.to_nav(db), - hir::Adt::Union(it) => it.to_nav(db), - hir::Adt::Enum(it) => it.to_nav(db), + hir::Adt::Struct(it) => it.try_to_nav(db), + hir::Adt::Union(it) => it.try_to_nav(db), + hir::Adt::Enum(it) => it.try_to_nav(db), } } } -impl ToNav for hir::AssocItem { - fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { +impl TryToNav for hir::AssocItem { + fn try_to_nav(&self, db: &RootDatabase) -> Option { match self { - AssocItem::Function(it) => it.to_nav(db), - AssocItem::Const(it) => it.to_nav(db), - AssocItem::TypeAlias(it) => it.to_nav(db), + AssocItem::Function(it) => it.try_to_nav(db), + AssocItem::Const(it) => it.try_to_nav(db), + AssocItem::TypeAlias(it) => it.try_to_nav(db), } } } @@ -441,10 +438,9 @@ impl ToNav for hir::Label { } } -impl ToNav for hir::TypeParam { - fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { - #[allow(deprecated)] - let src = self.source_old(db); +impl TryToNav for hir::TypeParam { + fn try_to_nav(&self, db: &RootDatabase) -> Option { + let src = self.source(db)?; let full_range = match &src.value { Either::Left(it) => it.syntax().text_range(), Either::Right(it) => it.syntax().text_range(), @@ -453,7 +449,7 @@ impl ToNav for hir::TypeParam { Either::Left(_) => None, Either::Right(it) => it.name().map(|it| it.syntax().text_range()), }; - NavigationTarget { + Some(NavigationTarget { file_id: src.file_id.original_file(db), name: self.name(db).to_string().into(), kind: Some(SymbolKind::TypeParam), @@ -462,16 +458,15 @@ impl ToNav for hir::TypeParam { container_name: None, description: None, docs: None, - } + }) } } -impl ToNav for hir::LifetimeParam { - fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { - #[allow(deprecated)] - let src = self.source_old(db); +impl TryToNav for hir::LifetimeParam { + fn try_to_nav(&self, db: &RootDatabase) -> Option { + let src = self.source(db)?; let full_range = src.value.syntax().text_range(); - NavigationTarget { + Some(NavigationTarget { file_id: src.file_id.original_file(db), name: self.name(db).to_string().into(), kind: Some(SymbolKind::LifetimeParam), @@ -480,7 +475,7 @@ impl ToNav for hir::LifetimeParam { container_name: None, description: None, docs: None, - } + }) } } -- cgit v1.2.3 From 40cd6cdf67dcfad89a80ff3a662bec2dfd983d67 Mon Sep 17 00:00:00 2001 From: Nick Spain Date: Sat, 2 Jan 2021 22:11:25 +1100 Subject: Fix ConstParam HasSource impl and implement TryToNav not Nav --- crates/ide/src/display/navigation_target.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'crates/ide/src/display') diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs index 1fb26c226..e24c78301 100644 --- a/crates/ide/src/display/navigation_target.rs +++ b/crates/ide/src/display/navigation_target.rs @@ -218,7 +218,7 @@ impl TryToNav for Definition { Definition::TypeParam(it) => it.try_to_nav(db), Definition::LifetimeParam(it) => it.try_to_nav(db), Definition::Label(it) => Some(it.to_nav(db)), - Definition::ConstParam(it) => Some(it.to_nav(db)), + Definition::ConstParam(it) => it.try_to_nav(db), } } } @@ -479,11 +479,11 @@ impl TryToNav for hir::LifetimeParam { } } -impl ToNav for hir::ConstParam { - fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { - let src = self.source(db); +impl TryToNav for hir::ConstParam { + fn try_to_nav(&self, db: &RootDatabase) -> Option { + let src = self.source(db)?; let full_range = src.value.syntax().text_range(); - NavigationTarget { + Some(NavigationTarget { file_id: src.file_id.original_file(db), name: self.name(db).to_string().into(), kind: Some(SymbolKind::ConstParam), @@ -492,7 +492,7 @@ impl ToNav for hir::ConstParam { container_name: None, description: None, docs: None, - } + }) } } -- cgit v1.2.3