From 2de2b1eca3c3a3a74c0374f4de0b0c3ff25e66a9 Mon Sep 17 00:00:00 2001 From: Nick Spain Date: Fri, 1 Jan 2021 13:27:38 +1100 Subject: Implement new HasSource::source for all implementors of HasSource --- crates/hir/src/has_source.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'crates/hir') diff --git a/crates/hir/src/has_source.rs b/crates/hir/src/has_source.rs index a8256c181..84fbeca75 100644 --- a/crates/hir/src/has_source.rs +++ b/crates/hir/src/has_source.rs @@ -17,6 +17,7 @@ use crate::{ pub trait HasSource { type Ast; fn source_old(self, db: &dyn HirDatabase) -> InFile; + fn source(self, db: &dyn HirDatabase) -> Option>; } /// NB: Module is !HasSource, because it has two source nodes at the same time: @@ -54,60 +55,106 @@ impl HasSource for Field { Either::Right(it) => FieldSource::Named(it), }) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + let var = VariantId::from(self.parent); + let src = var.child_source(db.upcast()); + let field_source = src.map(|it| match it[self.id].clone() { + Either::Left(it) => FieldSource::Pos(it), + Either::Right(it) => FieldSource::Named(it), + }); + Some(field_source) + } } impl HasSource for Struct { type Ast = ast::Struct; fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + Some(self.id.lookup(db.upcast()).source(db.upcast())) + } } impl HasSource for Union { type Ast = ast::Union; fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + Some(self.id.lookup(db.upcast()).source(db.upcast())) + } } impl HasSource for Enum { type Ast = ast::Enum; fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + Some(self.id.lookup(db.upcast()).source(db.upcast())) + } } impl HasSource for Variant { type Ast = ast::Variant; fn source_old(self, db: &dyn HirDatabase) -> InFile { self.parent.id.child_source(db.upcast()).map(|map| map[self.id].clone()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + Some(self.parent.id.child_source(db.upcast()).map(|map| map[self.id].clone())) + } } impl HasSource for Function { type Ast = ast::Fn; fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + Some(self.id.lookup(db.upcast()).source(db.upcast())) + } } impl HasSource for Const { type Ast = ast::Const; fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + Some(self.id.lookup(db.upcast()).source(db.upcast())) + } } impl HasSource for Static { type Ast = ast::Static; fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + Some(self.id.lookup(db.upcast()).source(db.upcast())) + } } impl HasSource for Trait { type Ast = ast::Trait; fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + Some(self.id.lookup(db.upcast()).source(db.upcast())) + } } impl HasSource for TypeAlias { type Ast = ast::TypeAlias; fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + Some(self.id.lookup(db.upcast()).source(db.upcast())) + } } impl HasSource for MacroDef { type Ast = ast::Macro; @@ -117,12 +164,21 @@ impl HasSource for MacroDef { value: self.id.ast_id.expect("MacroDef without ast_id").to_node(db.upcast()), } } + + fn source(self, db: &dyn HirDatabase) -> Option> { + let ast_id = self.id.ast_id?; + Some(InFile { file_id: ast_id.file_id, value: ast_id.to_node(db.upcast()) }) + } } impl HasSource for Impl { type Ast = ast::Impl; fn source_old(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + Some(self.id.lookup(db.upcast()).source(db.upcast())) + } } impl HasSource for TypeParam { @@ -131,6 +187,11 @@ impl HasSource for TypeParam { let child_source = self.id.parent.child_source(db.upcast()); child_source.map(|it| it[self.id.local_id].clone()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + let child_source = self.id.parent.child_source(db.upcast()); + Some(child_source.map(|it| it[self.id.local_id].clone())) + } } impl HasSource for LifetimeParam { @@ -139,6 +200,11 @@ impl HasSource for LifetimeParam { let child_source = self.id.parent.child_source(db.upcast()); child_source.map(|it| it[self.id.local_id].clone()) } + + fn source(self, db: &dyn HirDatabase) -> Option> { + let child_source = self.id.parent.child_source(db.upcast()); + Some(child_source.map(|it| it[self.id.local_id].clone())) + } } impl HasSource for ConstParam { -- cgit v1.2.3