aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_def/src')
-rw-r--r--crates/hir_def/src/attr.rs16
-rw-r--r--crates/hir_def/src/db.rs6
-rw-r--r--crates/hir_def/src/docs.rs48
3 files changed, 19 insertions, 51 deletions
diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs
index b2ce7ca3c..7825290e6 100644
--- a/crates/hir_def/src/attr.rs
+++ b/crates/hir_def/src/attr.rs
@@ -5,6 +5,7 @@ use std::{ops, sync::Arc};
5use cfg::{CfgExpr, CfgOptions}; 5use cfg::{CfgExpr, CfgOptions};
6use either::Either; 6use either::Either;
7use hir_expand::{hygiene::Hygiene, AstId, InFile}; 7use hir_expand::{hygiene::Hygiene, AstId, InFile};
8use itertools::Itertools;
8use mbe::ast_to_token_tree; 9use mbe::ast_to_token_tree;
9use syntax::{ 10use syntax::{
10 ast::{self, AstNode, AttrsOwner}, 11 ast::{self, AstNode, AttrsOwner},
@@ -14,6 +15,7 @@ use tt::Subtree;
14 15
15use crate::{ 16use crate::{
16 db::DefDatabase, 17 db::DefDatabase,
18 docs::Documentation,
17 item_tree::{ItemTreeId, ItemTreeNode}, 19 item_tree::{ItemTreeId, ItemTreeNode},
18 nameres::ModuleSource, 20 nameres::ModuleSource,
19 path::ModPath, 21 path::ModPath,
@@ -140,6 +142,20 @@ impl Attrs {
140 Some(cfg) => cfg_options.check(&cfg) != Some(false), 142 Some(cfg) => cfg_options.check(&cfg) != Some(false),
141 } 143 }
142 } 144 }
145
146 pub fn docs(&self) -> Option<Documentation> {
147 let mut docs = String::new();
148 self.by_key("doc")
149 .attrs()
150 .flat_map(|attr| match attr.input.as_ref()? {
151 AttrInput::Literal(s) => Some(s),
152 AttrInput::TokenTree(_) => None,
153 })
154 .intersperse(&SmolStr::new_inline("\n"))
155 // No FromIterator<SmolStr> for String
156 .for_each(|s| docs.push_str(s.as_str()));
157 if docs.is_empty() { None } else { Some(docs) }.map(|it| Documentation::new(&it))
158 }
143} 159}
144 160
145#[derive(Debug, Clone, PartialEq, Eq)] 161#[derive(Debug, Clone, PartialEq, Eq)]
diff --git a/crates/hir_def/src/db.rs b/crates/hir_def/src/db.rs
index 6d694de11..7f250da33 100644
--- a/crates/hir_def/src/db.rs
+++ b/crates/hir_def/src/db.rs
@@ -10,7 +10,6 @@ use crate::{
10 attr::Attrs, 10 attr::Attrs,
11 body::{scope::ExprScopes, Body, BodySourceMap}, 11 body::{scope::ExprScopes, Body, BodySourceMap},
12 data::{ConstData, FunctionData, ImplData, StaticData, TraitData, TypeAliasData}, 12 data::{ConstData, FunctionData, ImplData, StaticData, TraitData, TypeAliasData},
13 docs::Documentation,
14 generics::GenericParams, 13 generics::GenericParams,
15 import_map::ImportMap, 14 import_map::ImportMap,
16 item_tree::ItemTree, 15 item_tree::ItemTree,
@@ -105,11 +104,6 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
105 #[salsa::invoke(LangItems::lang_item_query)] 104 #[salsa::invoke(LangItems::lang_item_query)]
106 fn lang_item(&self, start_crate: CrateId, item: SmolStr) -> Option<LangItemTarget>; 105 fn lang_item(&self, start_crate: CrateId, item: SmolStr) -> Option<LangItemTarget>;
107 106
108 // FIXME(https://github.com/rust-analyzer/rust-analyzer/issues/2148#issuecomment-550519102)
109 // Remove this query completely, in favor of `Attrs::docs` method
110 #[salsa::invoke(Documentation::documentation_query)]
111 fn documentation(&self, def: AttrDefId) -> Option<Documentation>;
112
113 #[salsa::invoke(ImportMap::import_map_query)] 107 #[salsa::invoke(ImportMap::import_map_query)]
114 fn import_map(&self, krate: CrateId) -> Arc<ImportMap>; 108 fn import_map(&self, krate: CrateId) -> Arc<ImportMap>;
115} 109}
diff --git a/crates/hir_def/src/docs.rs b/crates/hir_def/src/docs.rs
index 3e59a8f47..6a27effef 100644
--- a/crates/hir_def/src/docs.rs
+++ b/crates/hir_def/src/docs.rs
@@ -5,16 +5,9 @@
5 5
6use std::sync::Arc; 6use std::sync::Arc;
7 7
8use either::Either;
9use itertools::Itertools; 8use itertools::Itertools;
10use syntax::{ast, SmolStr}; 9use syntax::{ast, SmolStr};
11 10
12use crate::{
13 db::DefDatabase,
14 src::{HasChildSource, HasSource},
15 AdtId, AttrDefId, Lookup,
16};
17
18/// Holds documentation 11/// Holds documentation
19#[derive(Debug, Clone, PartialEq, Eq)] 12#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct Documentation(Arc<str>); 13pub struct Documentation(Arc<str>);
@@ -26,7 +19,7 @@ impl Into<String> for Documentation {
26} 19}
27 20
28impl Documentation { 21impl Documentation {
29 fn new(s: &str) -> Documentation { 22 pub fn new(s: &str) -> Documentation {
30 Documentation(s.into()) 23 Documentation(s.into())
31 } 24 }
32 25
@@ -40,42 +33,6 @@ impl Documentation {
40 pub fn as_str(&self) -> &str { 33 pub fn as_str(&self) -> &str {
41 &*self.0 34 &*self.0
42 } 35 }
43
44 pub(crate) fn documentation_query(
45 db: &dyn DefDatabase,
46 def: AttrDefId,
47 ) -> Option<Documentation> {
48 match def {
49 AttrDefId::ModuleId(module) => {
50 let def_map = db.crate_def_map(module.krate);
51 let src = def_map[module.local_id].declaration_source(db)?;
52 docs_from_ast(&src.value)
53 }
54 AttrDefId::FieldId(it) => {
55 let src = it.parent.child_source(db);
56 match &src.value[it.local_id] {
57 Either::Left(_tuple) => None,
58 Either::Right(record) => docs_from_ast(record),
59 }
60 }
61 AttrDefId::AdtId(it) => match it {
62 AdtId::StructId(it) => docs_from_ast(&it.lookup(db).source(db).value),
63 AdtId::EnumId(it) => docs_from_ast(&it.lookup(db).source(db).value),
64 AdtId::UnionId(it) => docs_from_ast(&it.lookup(db).source(db).value),
65 },
66 AttrDefId::EnumVariantId(it) => {
67 let src = it.parent.child_source(db);
68 docs_from_ast(&src.value[it.local_id])
69 }
70 AttrDefId::TraitId(it) => docs_from_ast(&it.lookup(db).source(db).value),
71 AttrDefId::MacroDefId(it) => docs_from_ast(&it.ast_id?.to_node(db.upcast())),
72 AttrDefId::ConstId(it) => docs_from_ast(&it.lookup(db).source(db).value),
73 AttrDefId::StaticId(it) => docs_from_ast(&it.lookup(db).source(db).value),
74 AttrDefId::FunctionId(it) => docs_from_ast(&it.lookup(db).source(db).value),
75 AttrDefId::TypeAliasId(it) => docs_from_ast(&it.lookup(db).source(db).value),
76 AttrDefId::ImplId(_) => None,
77 }
78 }
79} 36}
80 37
81pub(crate) fn docs_from_ast<N>(node: &N) -> Option<Documentation> 38pub(crate) fn docs_from_ast<N>(node: &N) -> Option<Documentation>
@@ -94,7 +51,8 @@ fn merge_doc_comments_and_attrs(
94) -> Option<String> { 51) -> Option<String> {
95 match (doc_comment_text, doc_attr_text) { 52 match (doc_comment_text, doc_attr_text) {
96 (Some(mut comment_text), Some(attr_text)) => { 53 (Some(mut comment_text), Some(attr_text)) => {
97 comment_text.push_str("\n"); 54 comment_text.reserve(attr_text.len() + 1);
55 comment_text.push('\n');
98 comment_text.push_str(&attr_text); 56 comment_text.push_str(&attr_text);
99 Some(comment_text) 57 Some(comment_text)
100 } 58 }