From 36865adcb946d5567fb61d3547b78fc71df58b20 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 11 Jun 2019 16:40:49 +0300 Subject: Introduce HasSource trait --- crates/ra_hir/src/code_model.rs | 26 ++++++++++++ crates/ra_hir/src/lib.rs | 2 +- crates/ra_ide_api/src/display/navigation_target.rs | 46 +++++++++------------- 3 files changed, 45 insertions(+), 29 deletions(-) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 63fdca55e..765850488 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -31,6 +31,11 @@ impl From<(HirFileId, T)> for Source { } } +pub trait HasSource { + type Ast; + fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source; +} + /// hir::Crate describes a single crate. It's the main interface with which /// a crate's dependencies interact. Mostly, it should be just a proxy for the /// root module. @@ -364,6 +369,13 @@ pub struct Struct { pub(crate) id: StructId, } +impl HasSource for Struct { + type Ast = TreeArc; + fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { + self.id.source(db).into() + } +} + impl Struct { pub fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { self.id.source(db).into() @@ -422,6 +434,13 @@ pub struct Union { pub(crate) id: StructId, } +impl HasSource for Union { + type Ast = TreeArc; + fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { + self.id.source(db).into() + } +} + impl Union { pub fn source( self, @@ -455,6 +474,13 @@ pub struct Enum { pub(crate) id: EnumId, } +impl HasSource for Enum { + type Ast = TreeArc; + fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { + self.id.source(db).into() + } +} + impl Enum { pub fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { self.id.source(db).into() diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 90e3f1275..02f5f7f40 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -81,5 +81,5 @@ pub use self::code_model::{ StructField, FieldSource, Static, Const, ConstSignature, Trait, TypeAlias, MacroDef, Container, - BuiltinType, Source, + BuiltinType, Source, HasSource, }; diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index 7c68bb41c..bef5f0980 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -1,6 +1,6 @@ use ra_db::{FileId, SourceDatabase}; use ra_syntax::{ - SyntaxNode, AstNode, SmolStr, TextRange, AstPtr, + SyntaxNode, AstNode, SmolStr, TextRange, AstPtr, TreeArc, SyntaxKind::{self, NAME}, ast::{self, DocCommentsOwner}, algo::visit::{visitor, Visitor}, @@ -186,35 +186,25 @@ impl NavigationTarget { } } + pub(crate) fn from_def_source(db: &RootDatabase, def: D) -> NavigationTarget + where + D: hir::HasSource>, + A: ast::DocCommentsOwner + ast::NameOwner + ShortLabel, + { + let src = def.source(db); + NavigationTarget::from_named( + src.file_id.original_file(db), + &*src.ast, + src.ast.doc_comment_text(), + src.ast.short_label(), + ) + } + pub(crate) fn from_adt_def(db: &RootDatabase, adt_def: hir::AdtDef) -> NavigationTarget { match adt_def { - hir::AdtDef::Struct(s) => { - let src = s.source(db); - NavigationTarget::from_named( - src.file_id.original_file(db), - &*src.ast, - src.ast.doc_comment_text(), - src.ast.short_label(), - ) - } - hir::AdtDef::Union(s) => { - let (file_id, node) = s.source(db); - NavigationTarget::from_named( - file_id.original_file(db), - &*node, - node.doc_comment_text(), - node.short_label(), - ) - } - hir::AdtDef::Enum(s) => { - let src = s.source(db); - NavigationTarget::from_named( - src.file_id.original_file(db), - &*src.ast, - src.ast.doc_comment_text(), - src.ast.short_label(), - ) - } + hir::AdtDef::Struct(it) => NavigationTarget::from_def_source(db, it), + hir::AdtDef::Union(it) => NavigationTarget::from_def_source(db, it), + hir::AdtDef::Enum(it) => NavigationTarget::from_def_source(db, it), } } -- cgit v1.2.3