aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def/src/attr.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_def/src/attr.rs')
-rw-r--r--crates/hir_def/src/attr.rs16
1 files changed, 16 insertions, 0 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)]