aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/hir/src/attrs.rs2
-rw-r--r--crates/hir/src/db.rs12
-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
-rw-r--r--crates/ide_db/src/apply_change.rs1
6 files changed, 26 insertions, 59 deletions
diff --git a/crates/hir/src/attrs.rs b/crates/hir/src/attrs.rs
index c3e820d89..fb2631b3e 100644
--- a/crates/hir/src/attrs.rs
+++ b/crates/hir/src/attrs.rs
@@ -38,7 +38,7 @@ macro_rules! impl_has_attrs {
38 } 38 }
39 fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> { 39 fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> {
40 let def = AttrDefId::$def_id(self.into()); 40 let def = AttrDefId::$def_id(self.into());
41 db.documentation(def) 41 db.attrs(def).docs()
42 } 42 }
43 fn resolve_doc_path(self, db: &dyn HirDatabase, link: &str, ns: Option<Namespace>) -> Option<ModuleDef> { 43 fn resolve_doc_path(self, db: &dyn HirDatabase, link: &str, ns: Option<Namespace>) -> Option<ModuleDef> {
44 let def = AttrDefId::$def_id(self.into()); 44 let def = AttrDefId::$def_id(self.into());
diff --git a/crates/hir/src/db.rs b/crates/hir/src/db.rs
index 8c767b249..8d949b264 100644
--- a/crates/hir/src/db.rs
+++ b/crates/hir/src/db.rs
@@ -2,12 +2,12 @@
2 2
3pub use hir_def::db::{ 3pub use hir_def::db::{
4 AttrsQuery, BodyQuery, BodyWithSourceMapQuery, ConstDataQuery, CrateDefMapQueryQuery, 4 AttrsQuery, BodyQuery, BodyWithSourceMapQuery, ConstDataQuery, CrateDefMapQueryQuery,
5 CrateLangItemsQuery, DefDatabase, DefDatabaseStorage, DocumentationQuery, EnumDataQuery, 5 CrateLangItemsQuery, DefDatabase, DefDatabaseStorage, EnumDataQuery, ExprScopesQuery,
6 ExprScopesQuery, FunctionDataQuery, GenericParamsQuery, ImplDataQuery, ImportMapQuery, 6 FunctionDataQuery, GenericParamsQuery, ImplDataQuery, ImportMapQuery, InternConstQuery,
7 InternConstQuery, InternDatabase, InternDatabaseStorage, InternEnumQuery, InternFunctionQuery, 7 InternDatabase, InternDatabaseStorage, InternEnumQuery, InternFunctionQuery, InternImplQuery,
8 InternImplQuery, InternStaticQuery, InternStructQuery, InternTraitQuery, InternTypeAliasQuery, 8 InternStaticQuery, InternStructQuery, InternTraitQuery, InternTypeAliasQuery, InternUnionQuery,
9 InternUnionQuery, ItemTreeQuery, LangItemQuery, ModuleLangItemsQuery, StaticDataQuery, 9 ItemTreeQuery, LangItemQuery, ModuleLangItemsQuery, StaticDataQuery, StructDataQuery,
10 StructDataQuery, TraitDataQuery, TypeAliasDataQuery, UnionDataQuery, 10 TraitDataQuery, TypeAliasDataQuery, UnionDataQuery,
11}; 11};
12pub use hir_expand::db::{ 12pub use hir_expand::db::{
13 AstDatabase, AstDatabaseStorage, AstIdMapQuery, InternEagerExpansionQuery, InternMacroQuery, 13 AstDatabase, AstDatabaseStorage, AstIdMapQuery, InternEagerExpansionQuery, InternMacroQuery,
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 }
diff --git a/crates/ide_db/src/apply_change.rs b/crates/ide_db/src/apply_change.rs
index 987191fe3..e2251f2b7 100644
--- a/crates/ide_db/src/apply_change.rs
+++ b/crates/ide_db/src/apply_change.rs
@@ -166,7 +166,6 @@ impl RootDatabase {
166 hir::db::ModuleLangItemsQuery 166 hir::db::ModuleLangItemsQuery
167 hir::db::CrateLangItemsQuery 167 hir::db::CrateLangItemsQuery
168 hir::db::LangItemQuery 168 hir::db::LangItemQuery
169 hir::db::DocumentationQuery
170 hir::db::ImportMapQuery 169 hir::db::ImportMapQuery
171 170
172 // HirDatabase 171 // HirDatabase