mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
make documenation a query
This commit is contained in:
parent
33026c654e
commit
2c28f5245d
3 changed files with 62 additions and 66 deletions
|
@ -196,7 +196,7 @@ impl Module {
|
||||||
/// `None` for the crate root.
|
/// `None` for the crate root.
|
||||||
pub fn declaration_source(
|
pub fn declaration_source(
|
||||||
self,
|
self,
|
||||||
db: &impl HirDatabase,
|
db: &(impl DefDatabase + AstDatabase),
|
||||||
) -> Option<(HirFileId, TreeArc<ast::Module>)> {
|
) -> Option<(HirFileId, TreeArc<ast::Module>)> {
|
||||||
let def_map = db.crate_def_map(self.krate);
|
let def_map = db.crate_def_map(self.krate);
|
||||||
let decl = def_map[self.module_id].declaration?;
|
let decl = def_map[self.module_id].declaration?;
|
||||||
|
|
|
@ -127,6 +127,9 @@ pub trait DefDatabase: SourceDatabase {
|
||||||
|
|
||||||
#[salsa::invoke(crate::lang_item::LangItems::lang_item_query)]
|
#[salsa::invoke(crate::lang_item::LangItems::lang_item_query)]
|
||||||
fn lang_item(&self, start_crate: Crate, item: SmolStr) -> Option<LangItemTarget>;
|
fn lang_item(&self, start_crate: Crate, item: SmolStr) -> Option<LangItemTarget>;
|
||||||
|
|
||||||
|
#[salsa::invoke(crate::docs::documentation_query)]
|
||||||
|
fn documentation(&self, def: crate::docs::DocDef) -> Option<crate::docs::Documentation>;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[salsa::query_group(HirDatabaseStorage)]
|
#[salsa::query_group(HirDatabaseStorage)]
|
||||||
|
|
|
@ -1,10 +1,44 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use ra_syntax::ast;
|
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
|
/// Holds documentation
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct Documentation(String);
|
pub struct Documentation(Arc<str>);
|
||||||
|
|
||||||
impl Documentation {
|
impl Documentation {
|
||||||
fn new(s: &str) -> Documentation {
|
fn new(s: &str) -> Documentation {
|
||||||
|
@ -12,13 +46,13 @@ impl Documentation {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_str(&self) -> &str {
|
pub fn as_str(&self) -> &str {
|
||||||
&self.0
|
&*self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Into<String> for Documentation {
|
impl Into<String> for Documentation {
|
||||||
fn into(self) -> String {
|
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<Documen
|
||||||
node.doc_comment_text().map(|it| Documentation::new(&it))
|
node.doc_comment_text().map(|it| Documentation::new(&it))
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Docs for Module {
|
pub(crate) fn documentation_query(
|
||||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
db: &(impl DefDatabase + AstDatabase),
|
||||||
self.declaration_source(db).and_then(|it| docs_from_ast(&*it.1))
|
def: DocDef,
|
||||||
}
|
) -> Option<Documentation> {
|
||||||
}
|
match def {
|
||||||
|
DocDef::Module(it) => docs_from_ast(&*it.declaration_source(db)?.1),
|
||||||
impl Docs for StructField {
|
DocDef::StructField(it) => match it.source(db).1 {
|
||||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
|
||||||
match self.source(db).1 {
|
|
||||||
FieldSource::Named(named) => docs_from_ast(&*named),
|
FieldSource::Named(named) => docs_from_ast(&*named),
|
||||||
FieldSource::Pos(..) => return None,
|
FieldSource::Pos(..) => return None,
|
||||||
}
|
},
|
||||||
|
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 Struct {
|
impl<T: Into<DocDef> + Copy> Docs for T {
|
||||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
||||||
docs_from_ast(&*self.source(db).1)
|
db.documentation((*self).into())
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Docs for Union {
|
|
||||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
|
||||||
docs_from_ast(&*self.source(db).1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Docs for Enum {
|
|
||||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
|
||||||
docs_from_ast(&*self.source(db).1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Docs for EnumVariant {
|
|
||||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
|
||||||
docs_from_ast(&*self.source(db).1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Docs for Function {
|
|
||||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
|
||||||
docs_from_ast(&*self.source(db).1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Docs for Const {
|
|
||||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
|
||||||
docs_from_ast(&*self.source(db).1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Docs for Static {
|
|
||||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
|
||||||
docs_from_ast(&*self.source(db).1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Docs for Trait {
|
|
||||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
|
||||||
docs_from_ast(&*self.source(db).1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Docs for TypeAlias {
|
|
||||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
|
||||||
docs_from_ast(&*self.source(db).1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue