From 0dcaded439ae4bd4670bc6a2fbf739cd4fce60af Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 11 Jun 2019 18:00:08 +0300 Subject: move source to a seaparate file --- crates/ra_hir/src/code_model/src.rs | 120 ++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 crates/ra_hir/src/code_model/src.rs (limited to 'crates/ra_hir/src/code_model') diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs new file mode 100644 index 000000000..7484faf04 --- /dev/null +++ b/crates/ra_hir/src/code_model/src.rs @@ -0,0 +1,120 @@ +use ra_syntax::{TreeArc, ast}; + +use crate::{ + HirFileId, DefDatabase, AstDatabase, Module, ModuleSource, + StructField, Struct, Enum, Union, EnumVariant, Function, Static, Trait, Const, TypeAlias, + FieldSource, MacroDef, ids::AstItemDef, +}; + +pub struct Source { + pub file_id: HirFileId, + pub ast: T, +} + +impl From<(HirFileId, T)> for Source { + fn from((file_id, ast): (HirFileId, T)) -> Self { + Source { file_id, ast } + } +} + +pub trait HasSource { + type Ast; + fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source; +} + +/// NB: Module is !HasSource, becase it has two source nodes at the same time: +/// definition and declaration. +impl Module { + /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. + pub fn definition_source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { + let def_map = db.crate_def_map(self.krate); + let decl_id = def_map[self.module_id].declaration; + let file_id = def_map[self.module_id].definition; + let module_source = ModuleSource::new(db, file_id, decl_id); + let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id()); + (file_id, module_source).into() + } + + /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. + /// `None` for the crate root. + pub fn declaration_source( + self, + db: &(impl DefDatabase + AstDatabase), + ) -> Option>> { + let def_map = db.crate_def_map(self.krate); + let decl = def_map[self.module_id].declaration?; + let ast = decl.to_node(db); + Some((decl.file_id(), ast).into()) + } +} + +impl HasSource for StructField { + type Ast = FieldSource; + fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { + self.source_impl(db).into() + } +} +impl HasSource for Struct { + type Ast = TreeArc; + fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { + self.id.source(db).into() + } +} +impl HasSource for Union { + type Ast = TreeArc; + fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { + self.id.source(db).into() + } +} +impl HasSource for Enum { + type Ast = TreeArc; + fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { + self.id.source(db).into() + } +} +impl HasSource for EnumVariant { + type Ast = TreeArc; + fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { + self.source_impl(db) + } +} +impl HasSource for Function { + type Ast = TreeArc; + + fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { + self.id.source(db).into() + } +} +impl HasSource for Const { + type Ast = TreeArc; + + fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { + self.id.source(db).into() + } +} +impl HasSource for Static { + type Ast = TreeArc; + + fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { + self.id.source(db).into() + } +} +impl HasSource for Trait { + type Ast = TreeArc; + fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { + self.id.source(db).into() + } +} +impl HasSource for TypeAlias { + type Ast = TreeArc; + fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { + self.id.source(db).into() + } +} +impl HasSource for MacroDef { + type Ast = TreeArc; + + fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { + (self.id.0.file_id(), self.id.0.to_node(db)).into() + } +} -- cgit v1.2.3 From 26753f0e4931e2980f008015cbd709a77d71c0f3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 11 Jun 2019 18:07:42 +0300 Subject: remove unneded From(..) impl --- crates/ra_hir/src/code_model/src.rs | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) (limited to 'crates/ra_hir/src/code_model') diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index 7484faf04..7d8abb39e 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -11,12 +11,6 @@ pub struct Source { pub ast: T, } -impl From<(HirFileId, T)> for Source { - fn from((file_id, ast): (HirFileId, T)) -> Self { - Source { file_id, ast } - } -} - pub trait HasSource { type Ast; fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source; @@ -30,9 +24,9 @@ impl Module { let def_map = db.crate_def_map(self.krate); let decl_id = def_map[self.module_id].declaration; let file_id = def_map[self.module_id].definition; - let module_source = ModuleSource::new(db, file_id, decl_id); + let ast = ModuleSource::new(db, file_id, decl_id); let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id()); - (file_id, module_source).into() + Source { file_id, ast } } /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. @@ -44,32 +38,32 @@ impl Module { let def_map = db.crate_def_map(self.krate); let decl = def_map[self.module_id].declaration?; let ast = decl.to_node(db); - Some((decl.file_id(), ast).into()) + Some(Source { file_id: decl.file_id(), ast }) } } impl HasSource for StructField { type Ast = FieldSource; fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { - self.source_impl(db).into() + self.source_impl(db) } } impl HasSource for Struct { type Ast = TreeArc; fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { - self.id.source(db).into() + self.id.source(db) } } impl HasSource for Union { type Ast = TreeArc; fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { - self.id.source(db).into() + self.id.source(db) } } impl HasSource for Enum { type Ast = TreeArc; fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { - self.id.source(db).into() + self.id.source(db) } } impl HasSource for EnumVariant { @@ -82,39 +76,39 @@ impl HasSource for Function { type Ast = TreeArc; fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { - self.id.source(db).into() + self.id.source(db) } } impl HasSource for Const { type Ast = TreeArc; fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { - self.id.source(db).into() + self.id.source(db) } } impl HasSource for Static { type Ast = TreeArc; fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { - self.id.source(db).into() + self.id.source(db) } } impl HasSource for Trait { type Ast = TreeArc; fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { - self.id.source(db).into() + self.id.source(db) } } impl HasSource for TypeAlias { type Ast = TreeArc; fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { - self.id.source(db).into() + self.id.source(db) } } impl HasSource for MacroDef { type Ast = TreeArc; fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { - (self.id.0.file_id(), self.id.0.to_node(db)).into() + Source { file_id: self.id.0.file_id(), ast: self.id.0.to_node(db) } } } -- cgit v1.2.3 From 14b1f8763486fe7bc4a468c70fce827ae34d3d74 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 11 Jun 2019 18:09:39 +0300 Subject: minor formatting --- crates/ra_hir/src/code_model/src.rs | 4 ---- 1 file changed, 4 deletions(-) (limited to 'crates/ra_hir/src/code_model') diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index 7d8abb39e..76adc7869 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -74,21 +74,18 @@ impl HasSource for EnumVariant { } impl HasSource for Function { type Ast = TreeArc; - fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { self.id.source(db) } } impl HasSource for Const { type Ast = TreeArc; - fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { self.id.source(db) } } impl HasSource for Static { type Ast = TreeArc; - fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { self.id.source(db) } @@ -107,7 +104,6 @@ impl HasSource for TypeAlias { } impl HasSource for MacroDef { type Ast = TreeArc; - fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { Source { file_id: self.id.0.file_id(), ast: self.id.0.to_node(db) } } -- cgit v1.2.3 From f6c227babdeb2d6b6888b36ddcbd8eb220240fa2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 11 Jun 2019 18:11:17 +0300 Subject: fix compilation --- crates/ra_hir/src/code_model/src.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir/src/code_model') diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index 76adc7869..5785d3b26 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -16,7 +16,7 @@ pub trait HasSource { fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source; } -/// NB: Module is !HasSource, becase it has two source nodes at the same time: +/// NB: Module is !HasSource, because it has two source nodes at the same time: /// definition and declaration. impl Module { /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. -- cgit v1.2.3 From ff6f6b3a5223ddab81c7357a3c59bdb09936a552 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 11 Jun 2019 18:14:27 +0300 Subject: move docs under code model --- crates/ra_hir/src/code_model/docs.rs | 96 ++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 crates/ra_hir/src/code_model/docs.rs (limited to 'crates/ra_hir/src/code_model') diff --git a/crates/ra_hir/src/code_model/docs.rs b/crates/ra_hir/src/code_model/docs.rs new file mode 100644 index 000000000..da2b9b854 --- /dev/null +++ b/crates/ra_hir/src/code_model/docs.rs @@ -0,0 +1,96 @@ +use std::sync::Arc; + +use ra_syntax::ast; + +use crate::{ + HirDatabase, DefDatabase, AstDatabase, HasSource, + Module, StructField, Struct, Enum, EnumVariant, Static, Const, Function, Union, Trait, TypeAlias, FieldSource, MacroDef, +}; + +#[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), + MacroDef(MacroDef), +} + +impl_froms!( + DocDef: Module, + StructField, + Struct, + Enum, + EnumVariant, + Static, + Const, + Function, + Union, + Trait, + TypeAlias, + MacroDef +); + +/// Holds documentation +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Documentation(Arc); + +impl Documentation { + fn new(s: &str) -> Documentation { + Documentation(s.into()) + } + + pub fn as_str(&self) -> &str { + &*self.0 + } +} + +impl Into for Documentation { + fn into(self) -> String { + self.as_str().to_owned() + } +} + +pub trait Docs { + fn docs(&self, db: &impl HirDatabase) -> Option; +} + +pub(crate) fn docs_from_ast(node: &impl ast::DocCommentsOwner) -> Option { + node.doc_comment_text().map(|it| Documentation::new(&it)) +} + +pub(crate) fn documentation_query( + db: &(impl DefDatabase + AstDatabase), + def: DocDef, +) -> Option { + match def { + DocDef::Module(it) => docs_from_ast(&*it.declaration_source(db)?.ast), + DocDef::StructField(it) => match it.source(db).ast { + FieldSource::Named(named) => docs_from_ast(&*named), + FieldSource::Pos(..) => return None, + }, + DocDef::Struct(it) => docs_from_ast(&*it.source(db).ast), + DocDef::Enum(it) => docs_from_ast(&*it.source(db).ast), + DocDef::EnumVariant(it) => docs_from_ast(&*it.source(db).ast), + DocDef::Static(it) => docs_from_ast(&*it.source(db).ast), + DocDef::Const(it) => docs_from_ast(&*it.source(db).ast), + DocDef::Function(it) => docs_from_ast(&*it.source(db).ast), + DocDef::Union(it) => docs_from_ast(&*it.source(db).ast), + DocDef::Trait(it) => docs_from_ast(&*it.source(db).ast), + DocDef::TypeAlias(it) => docs_from_ast(&*it.source(db).ast), + DocDef::MacroDef(it) => docs_from_ast(&*it.source(db).ast), + } +} + +impl + Copy> Docs for T { + fn docs(&self, db: &impl HirDatabase) -> Option { + db.documentation((*self).into()) + } +} -- cgit v1.2.3