From 2c28f5245dd8ede715d99b55fdad0ceb3de9dbe7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 8 Jun 2019 14:36:39 +0300 Subject: make documenation a query --- crates/ra_hir/src/code_model.rs | 2 +- crates/ra_hir/src/db.rs | 3 + crates/ra_hir/src/docs.rs | 123 +++++++++++++++++++--------------------- 3 files changed, 62 insertions(+), 66 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 861d6aefe..cf16ed94d 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -196,7 +196,7 @@ impl Module { /// `None` for the crate root. pub fn declaration_source( self, - db: &impl HirDatabase, + db: &(impl DefDatabase + AstDatabase), ) -> Option<(HirFileId, TreeArc)> { let def_map = db.crate_def_map(self.krate); let decl = def_map[self.module_id].declaration?; diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 3afd0994c..d2a372f3b 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -127,6 +127,9 @@ pub trait DefDatabase: SourceDatabase { #[salsa::invoke(crate::lang_item::LangItems::lang_item_query)] fn lang_item(&self, start_crate: Crate, item: SmolStr) -> Option; + + #[salsa::invoke(crate::docs::documentation_query)] + fn documentation(&self, def: crate::docs::DocDef) -> Option; } #[salsa::query_group(HirDatabaseStorage)] diff --git a/crates/ra_hir/src/docs.rs b/crates/ra_hir/src/docs.rs index 38c0922eb..900fd2aa8 100644 --- a/crates/ra_hir/src/docs.rs +++ b/crates/ra_hir/src/docs.rs @@ -1,10 +1,44 @@ +use std::sync::Arc; + use ra_syntax::ast; -use crate::{HirDatabase, Module, StructField, Struct, Enum, EnumVariant, Static, Const, Function, Union, Trait, TypeAlias, FieldSource}; +use crate::{ + HirDatabase, DefDatabase, AstDatabase, + Module, StructField, Struct, Enum, EnumVariant, Static, Const, Function, Union, Trait, TypeAlias, FieldSource +}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum DocDef { + Module(Module), + StructField(StructField), + Struct(Struct), + Enum(Enum), + EnumVariant(EnumVariant), + Static(Static), + Const(Const), + Function(Function), + Union(Union), + Trait(Trait), + TypeAlias(TypeAlias), +} + +impl_froms!( + DocDef: Module, + StructField, + Struct, + Enum, + EnumVariant, + Static, + Const, + Function, + Union, + Trait, + TypeAlias +); /// Holds documentation -#[derive(Debug, Clone)] -pub struct Documentation(String); +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Documentation(Arc); impl Documentation { fn new(s: &str) -> Documentation { @@ -12,13 +46,13 @@ impl Documentation { } pub fn as_str(&self) -> &str { - &self.0 + &*self.0 } } impl Into for Documentation { fn into(self) -> String { - self.0.clone() + self.as_str().to_owned() } } @@ -30,71 +64,30 @@ pub(crate) fn docs_from_ast(node: &impl ast::DocCommentsOwner) -> Option Option { - self.declaration_source(db).and_then(|it| docs_from_ast(&*it.1)) - } -} - -impl Docs for StructField { - fn docs(&self, db: &impl HirDatabase) -> Option { - match self.source(db).1 { +pub(crate) fn documentation_query( + db: &(impl DefDatabase + AstDatabase), + def: DocDef, +) -> Option { + match def { + DocDef::Module(it) => docs_from_ast(&*it.declaration_source(db)?.1), + DocDef::StructField(it) => match it.source(db).1 { FieldSource::Named(named) => docs_from_ast(&*named), FieldSource::Pos(..) => return None, - } - } -} - -impl Docs for Struct { - fn docs(&self, db: &impl HirDatabase) -> Option { - docs_from_ast(&*self.source(db).1) - } -} - -impl Docs for Union { - fn docs(&self, db: &impl HirDatabase) -> Option { - docs_from_ast(&*self.source(db).1) - } -} - -impl Docs for Enum { - fn docs(&self, db: &impl HirDatabase) -> Option { - docs_from_ast(&*self.source(db).1) - } -} - -impl Docs for EnumVariant { - fn docs(&self, db: &impl HirDatabase) -> Option { - docs_from_ast(&*self.source(db).1) - } -} - -impl Docs for Function { - fn docs(&self, db: &impl HirDatabase) -> Option { - docs_from_ast(&*self.source(db).1) - } -} - -impl Docs for Const { - fn docs(&self, db: &impl HirDatabase) -> Option { - docs_from_ast(&*self.source(db).1) - } -} - -impl Docs for Static { - fn docs(&self, db: &impl HirDatabase) -> Option { - docs_from_ast(&*self.source(db).1) - } -} - -impl Docs for Trait { - fn docs(&self, db: &impl HirDatabase) -> Option { - docs_from_ast(&*self.source(db).1) + }, + DocDef::Struct(it) => docs_from_ast(&*it.source(db).1), + DocDef::Enum(it) => docs_from_ast(&*it.source(db).1), + DocDef::EnumVariant(it) => docs_from_ast(&*it.source(db).1), + DocDef::Static(it) => docs_from_ast(&*it.source(db).1), + DocDef::Const(it) => docs_from_ast(&*it.source(db).1), + DocDef::Function(it) => docs_from_ast(&*it.source(db).1), + DocDef::Union(it) => docs_from_ast(&*it.source(db).1), + DocDef::Trait(it) => docs_from_ast(&*it.source(db).1), + DocDef::TypeAlias(it) => docs_from_ast(&*it.source(db).1), } } -impl Docs for TypeAlias { +impl + Copy> Docs for T { fn docs(&self, db: &impl HirDatabase) -> Option { - docs_from_ast(&*self.source(db).1) + db.documentation((*self).into()) } } -- cgit v1.2.3