2019-10-30 08:12:55 -05:00
|
|
|
//! A higher level attributes based on TokenTree, with also some shortcuts.
|
|
|
|
|
2021-10-21 10:49:28 -05:00
|
|
|
use std::{fmt, hash::Hash, ops, sync::Arc};
|
2019-10-30 08:12:55 -05:00
|
|
|
|
2020-12-17 17:23:46 -06:00
|
|
|
use base_db::CrateId;
|
2020-08-13 03:32:19 -05:00
|
|
|
use cfg::{CfgExpr, CfgOptions};
|
2019-12-03 10:07:56 -06:00
|
|
|
use either::Either;
|
2022-01-07 07:14:33 -06:00
|
|
|
use hir_expand::{hygiene::Hygiene, name::AsName, AstId, HirFileId, InFile};
|
2020-12-07 11:06:46 -06:00
|
|
|
use itertools::Itertools;
|
2021-01-14 18:11:07 -06:00
|
|
|
use la_arena::ArenaMap;
|
2021-11-17 12:46:32 -06:00
|
|
|
use mbe::{syntax_node_to_token_tree, DelimiterKind, Punct};
|
2021-03-13 11:18:42 -06:00
|
|
|
use smallvec::{smallvec, SmallVec};
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{
|
2021-09-27 05:54:24 -05:00
|
|
|
ast::{self, AstNode, HasAttrs, IsString},
|
2021-04-06 15:25:44 -05:00
|
|
|
match_ast, AstPtr, AstToken, SmolStr, SyntaxNode, TextRange, TextSize,
|
2019-10-30 08:12:55 -05:00
|
|
|
};
|
|
|
|
use tt::Subtree;
|
|
|
|
|
2019-11-23 02:14:10 -06:00
|
|
|
use crate::{
|
2020-06-22 12:15:54 -05:00
|
|
|
db::DefDatabase,
|
2021-04-01 13:35:21 -05:00
|
|
|
intern::Interned,
|
2020-06-22 12:15:54 -05:00
|
|
|
item_tree::{ItemTreeId, ItemTreeNode},
|
|
|
|
nameres::ModuleSource,
|
2020-12-18 18:09:48 -06:00
|
|
|
path::{ModPath, PathKind},
|
2021-03-19 15:23:57 -05:00
|
|
|
src::{HasChildSource, HasSource},
|
2021-01-04 14:56:21 -06:00
|
|
|
AdtId, AttrDefId, EnumId, GenericParamId, HasModule, LocalEnumVariantId, LocalFieldId, Lookup,
|
|
|
|
VariantId,
|
2019-11-23 02:14:10 -06:00
|
|
|
};
|
2019-10-30 08:12:55 -05:00
|
|
|
|
2020-12-07 11:49:03 -06:00
|
|
|
/// Holds documentation
|
2020-12-11 14:19:58 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2020-12-07 14:55:00 -06:00
|
|
|
pub struct Documentation(String);
|
2020-12-07 11:49:03 -06:00
|
|
|
|
|
|
|
impl Documentation {
|
2021-06-07 04:58:51 -05:00
|
|
|
pub fn new(s: String) -> Self {
|
|
|
|
Documentation(s)
|
2021-06-04 13:25:16 -05:00
|
|
|
}
|
|
|
|
|
2020-12-07 11:49:03 -06:00
|
|
|
pub fn as_str(&self) -> &str {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 13:11:03 -06:00
|
|
|
impl From<Documentation> for String {
|
|
|
|
fn from(Documentation(string): Documentation) -> Self {
|
|
|
|
string
|
2020-12-07 11:49:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 17:23:46 -06:00
|
|
|
/// Syntactical attributes, without filtering of `cfg_attr`s.
|
2019-11-22 02:27:47 -06:00
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Eq)]
|
2020-12-18 19:44:00 -06:00
|
|
|
pub(crate) struct RawAttrs {
|
2019-11-22 02:27:47 -06:00
|
|
|
entries: Option<Arc<[Attr]>>,
|
|
|
|
}
|
|
|
|
|
2020-12-17 17:23:46 -06:00
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct Attrs(RawAttrs);
|
|
|
|
|
2021-03-19 15:23:57 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct AttrsWithOwner {
|
|
|
|
attrs: Attrs,
|
|
|
|
owner: AttrDefId,
|
|
|
|
}
|
|
|
|
|
2020-12-17 17:23:46 -06:00
|
|
|
impl ops::Deref for RawAttrs {
|
2019-11-22 02:27:47 -06:00
|
|
|
type Target = [Attr];
|
|
|
|
|
|
|
|
fn deref(&self) -> &[Attr] {
|
|
|
|
match &self.entries {
|
|
|
|
Some(it) => &*it,
|
|
|
|
None => &[],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 17:23:46 -06:00
|
|
|
impl ops::Deref for Attrs {
|
|
|
|
type Target = [Attr];
|
|
|
|
|
|
|
|
fn deref(&self) -> &[Attr] {
|
|
|
|
match &self.0.entries {
|
|
|
|
Some(it) => &*it,
|
|
|
|
None => &[],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 07:14:33 -06:00
|
|
|
impl ops::Index<AttrId> for Attrs {
|
|
|
|
type Output = Attr;
|
|
|
|
|
|
|
|
fn index(&self, AttrId { ast_index, .. }: AttrId) -> &Self::Output {
|
|
|
|
&(**self)[ast_index as usize]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 15:23:57 -05:00
|
|
|
impl ops::Deref for AttrsWithOwner {
|
|
|
|
type Target = Attrs;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Attrs {
|
|
|
|
&self.attrs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 17:23:46 -06:00
|
|
|
impl RawAttrs {
|
2020-12-18 19:44:00 -06:00
|
|
|
pub(crate) const EMPTY: Self = Self { entries: None };
|
2020-12-17 17:23:46 -06:00
|
|
|
|
2021-09-27 05:54:24 -05:00
|
|
|
pub(crate) fn new(db: &dyn DefDatabase, owner: &dyn ast::HasAttrs, hygiene: &Hygiene) -> Self {
|
2021-03-17 08:38:11 -05:00
|
|
|
let entries = collect_attrs(owner)
|
2021-05-10 14:50:42 -05:00
|
|
|
.flat_map(|(id, attr)| match attr {
|
2021-06-11 11:20:15 -05:00
|
|
|
Either::Left(attr) => {
|
|
|
|
attr.meta().and_then(|meta| Attr::from_src(db, meta, hygiene, id))
|
|
|
|
}
|
2021-05-10 14:50:42 -05:00
|
|
|
Either::Right(comment) => comment.doc_comment().map(|doc| Attr {
|
|
|
|
id,
|
2021-05-24 17:50:19 -05:00
|
|
|
input: Some(Interned::new(AttrInput::Literal(SmolStr::new(doc)))),
|
2021-05-10 14:50:42 -05:00
|
|
|
path: Interned::new(ModPath::from(hir_expand::name!(doc))),
|
|
|
|
}),
|
2021-03-17 08:38:11 -05:00
|
|
|
})
|
|
|
|
.collect::<Arc<_>>();
|
|
|
|
|
|
|
|
Self { entries: if entries.is_empty() { None } else { Some(entries) } }
|
2020-12-17 17:23:46 -06:00
|
|
|
}
|
|
|
|
|
2021-09-27 05:54:24 -05:00
|
|
|
fn from_attrs_owner(db: &dyn DefDatabase, owner: InFile<&dyn ast::HasAttrs>) -> Self {
|
2020-12-17 17:23:46 -06:00
|
|
|
let hygiene = Hygiene::new(db.upcast(), owner.file_id);
|
2021-05-06 12:59:54 -05:00
|
|
|
Self::new(db, owner.value, &hygiene)
|
2020-12-17 17:23:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn merge(&self, other: Self) -> Self {
|
2021-05-10 14:50:42 -05:00
|
|
|
// FIXME: This needs to fixup `AttrId`s
|
2020-12-17 17:23:46 -06:00
|
|
|
match (&self.entries, &other.entries) {
|
|
|
|
(None, None) => Self::EMPTY,
|
|
|
|
(Some(entries), None) | (None, Some(entries)) => {
|
|
|
|
Self { entries: Some(entries.clone()) }
|
|
|
|
}
|
|
|
|
(Some(a), Some(b)) => {
|
|
|
|
Self { entries: Some(a.iter().chain(b.iter()).cloned().collect()) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Processes `cfg_attr`s, returning the resulting semantic `Attrs`.
|
2020-12-18 11:58:42 -06:00
|
|
|
pub(crate) fn filter(self, db: &dyn DefDatabase, krate: CrateId) -> Attrs {
|
|
|
|
let has_cfg_attrs = self.iter().any(|attr| {
|
|
|
|
attr.path.as_ident().map_or(false, |name| *name == hir_expand::name![cfg_attr])
|
|
|
|
});
|
|
|
|
if !has_cfg_attrs {
|
|
|
|
return Attrs(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
let crate_graph = db.crate_graph();
|
|
|
|
let new_attrs = self
|
|
|
|
.iter()
|
2021-03-13 11:18:42 -06:00
|
|
|
.flat_map(|attr| -> SmallVec<[_; 1]> {
|
2020-12-18 11:58:42 -06:00
|
|
|
let is_cfg_attr =
|
|
|
|
attr.path.as_ident().map_or(false, |name| *name == hir_expand::name![cfg_attr]);
|
|
|
|
if !is_cfg_attr {
|
2021-03-16 13:55:40 -05:00
|
|
|
return smallvec![attr.clone()];
|
2020-12-18 11:58:42 -06:00
|
|
|
}
|
|
|
|
|
2021-05-24 17:50:19 -05:00
|
|
|
let subtree = match attr.input.as_deref() {
|
2021-08-21 11:19:18 -05:00
|
|
|
Some(AttrInput::TokenTree(it, _)) => it,
|
2021-03-16 13:55:40 -05:00
|
|
|
_ => return smallvec![attr.clone()],
|
2020-12-18 11:58:42 -06:00
|
|
|
};
|
|
|
|
|
2021-03-13 11:18:42 -06:00
|
|
|
// Input subtree is: `(cfg, $(attr),+)`
|
|
|
|
// Split it up into a `cfg` subtree and the `attr` subtrees.
|
2020-12-18 11:58:42 -06:00
|
|
|
// FIXME: There should be a common API for this.
|
2021-08-21 11:19:18 -05:00
|
|
|
let mut parts = subtree.token_trees.split(
|
2021-03-13 11:18:42 -06:00
|
|
|
|tt| matches!(tt, tt::TokenTree::Leaf(tt::Leaf::Punct(p)) if p.char == ','),
|
|
|
|
);
|
|
|
|
let cfg = parts.next().unwrap();
|
2021-08-21 11:19:18 -05:00
|
|
|
let cfg = Subtree { delimiter: subtree.delimiter, token_trees: cfg.to_vec() };
|
2020-12-18 11:58:42 -06:00
|
|
|
let cfg = CfgExpr::parse(&cfg);
|
2021-04-09 06:36:22 -05:00
|
|
|
let index = attr.id;
|
2021-03-13 11:18:42 -06:00
|
|
|
let attrs = parts.filter(|a| !a.is_empty()).filter_map(|attr| {
|
|
|
|
let tree = Subtree { delimiter: None, token_trees: attr.to_vec() };
|
2021-03-16 13:55:40 -05:00
|
|
|
// FIXME hygiene
|
|
|
|
let hygiene = Hygiene::new_unhygienic();
|
2021-06-11 11:20:15 -05:00
|
|
|
Attr::from_tt(db, &tree, &hygiene, index)
|
2021-03-13 11:18:42 -06:00
|
|
|
});
|
2020-12-18 11:58:42 -06:00
|
|
|
|
|
|
|
let cfg_options = &crate_graph[krate].cfg_options;
|
|
|
|
if cfg_options.check(&cfg) == Some(false) {
|
2021-03-13 11:18:42 -06:00
|
|
|
smallvec![]
|
2020-12-18 11:58:42 -06:00
|
|
|
} else {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(cfg_attr_active);
|
2020-12-18 13:25:41 -06:00
|
|
|
|
2021-03-13 11:18:42 -06:00
|
|
|
attrs.collect()
|
2020-12-18 11:58:42 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Attrs(RawAttrs { entries: Some(new_attrs) })
|
2020-12-17 17:23:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 02:27:47 -06:00
|
|
|
impl Attrs {
|
2020-12-17 17:23:46 -06:00
|
|
|
pub const EMPTY: Self = Self(RawAttrs::EMPTY);
|
2020-06-23 12:42:19 -05:00
|
|
|
|
2021-01-04 14:56:21 -06:00
|
|
|
pub(crate) fn variants_attrs_query(
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
e: EnumId,
|
|
|
|
) -> Arc<ArenaMap<LocalEnumVariantId, Attrs>> {
|
2021-03-09 12:09:02 -06:00
|
|
|
let krate = e.lookup(db).container.krate;
|
2021-01-04 14:56:21 -06:00
|
|
|
let src = e.child_source(db);
|
|
|
|
let mut res = ArenaMap::default();
|
|
|
|
|
|
|
|
for (id, var) in src.value.iter() {
|
2021-09-27 05:54:24 -05:00
|
|
|
let attrs = RawAttrs::from_attrs_owner(db, src.with_value(var as &dyn ast::HasAttrs))
|
2021-01-04 14:56:21 -06:00
|
|
|
.filter(db, krate);
|
|
|
|
|
|
|
|
res.insert(id, attrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
Arc::new(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn fields_attrs_query(
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
v: VariantId,
|
|
|
|
) -> Arc<ArenaMap<LocalFieldId, Attrs>> {
|
|
|
|
let krate = v.module(db).krate;
|
|
|
|
let src = v.child_source(db);
|
|
|
|
let mut res = ArenaMap::default();
|
|
|
|
|
|
|
|
for (id, fld) in src.value.iter() {
|
2021-09-27 05:54:24 -05:00
|
|
|
let owner: &dyn HasAttrs = match fld {
|
2021-04-06 13:17:52 -05:00
|
|
|
Either::Left(tuple) => tuple,
|
|
|
|
Either::Right(record) => record,
|
2021-01-04 14:56:21 -06:00
|
|
|
};
|
2021-04-06 13:17:52 -05:00
|
|
|
let attrs = RawAttrs::from_attrs_owner(db, src.with_value(owner)).filter(db, krate);
|
2021-01-04 14:56:21 -06:00
|
|
|
|
|
|
|
res.insert(id, attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Arc::new(res)
|
|
|
|
}
|
|
|
|
|
2019-11-24 07:03:02 -06:00
|
|
|
pub fn by_key(&self, key: &'static str) -> AttrQuery<'_> {
|
|
|
|
AttrQuery { attrs: self, key }
|
2019-11-23 03:01:56 -06:00
|
|
|
}
|
2020-04-09 11:32:02 -05:00
|
|
|
|
2020-10-22 12:19:18 -05:00
|
|
|
pub fn cfg(&self) -> Option<CfgExpr> {
|
|
|
|
let mut cfgs = self.by_key("cfg").tt_values().map(CfgExpr::parse).collect::<Vec<_>>();
|
|
|
|
match cfgs.len() {
|
|
|
|
0 => None,
|
|
|
|
1 => Some(cfgs.pop().unwrap()),
|
|
|
|
_ => Some(CfgExpr::All(cfgs)),
|
|
|
|
}
|
2020-07-23 09:22:17 -05:00
|
|
|
}
|
|
|
|
pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> bool {
|
2020-10-22 12:19:18 -05:00
|
|
|
match self.cfg() {
|
|
|
|
None => true,
|
|
|
|
Some(cfg) => cfg_options.check(&cfg) != Some(false),
|
|
|
|
}
|
2020-04-09 11:32:02 -05:00
|
|
|
}
|
2020-12-07 11:06:46 -06:00
|
|
|
|
2022-01-11 03:07:16 -06:00
|
|
|
pub fn lang(&self) -> Option<&SmolStr> {
|
|
|
|
self.by_key("lang").string_value()
|
|
|
|
}
|
|
|
|
|
2020-12-07 11:06:46 -06:00
|
|
|
pub fn docs(&self) -> Option<Documentation> {
|
2021-05-24 17:50:19 -05:00
|
|
|
let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_deref()? {
|
2021-01-02 13:58:06 -06:00
|
|
|
AttrInput::Literal(s) => Some(s),
|
2021-08-21 11:19:18 -05:00
|
|
|
AttrInput::TokenTree(..) => None,
|
2021-01-02 13:58:06 -06:00
|
|
|
});
|
2021-03-17 08:38:11 -05:00
|
|
|
let indent = docs
|
|
|
|
.clone()
|
|
|
|
.flat_map(|s| s.lines())
|
|
|
|
.filter(|line| !line.chars().all(|c| c.is_whitespace()))
|
|
|
|
.map(|line| line.chars().take_while(|c| c.is_whitespace()).count())
|
|
|
|
.min()
|
|
|
|
.unwrap_or(0);
|
|
|
|
let mut buf = String::new();
|
|
|
|
for doc in docs {
|
|
|
|
// str::lines doesn't yield anything for the empty string
|
2021-03-17 10:10:58 -05:00
|
|
|
if !doc.is_empty() {
|
2021-03-17 08:38:11 -05:00
|
|
|
buf.extend(Itertools::intersperse(
|
|
|
|
doc.lines().map(|line| {
|
|
|
|
line.char_indices()
|
|
|
|
.nth(indent)
|
|
|
|
.map_or(line, |(offset, _)| &line[offset..])
|
|
|
|
.trim_end()
|
|
|
|
}),
|
|
|
|
"\n",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
buf.push('\n');
|
|
|
|
}
|
|
|
|
buf.pop();
|
|
|
|
if buf.is_empty() {
|
2020-12-07 11:49:03 -06:00
|
|
|
None
|
|
|
|
} else {
|
2021-03-17 08:38:11 -05:00
|
|
|
Some(Documentation(buf))
|
2020-12-07 11:49:03 -06:00
|
|
|
}
|
2020-12-07 11:06:46 -06:00
|
|
|
}
|
2021-07-23 08:36:43 -05:00
|
|
|
|
|
|
|
pub fn has_doc_hidden(&self) -> bool {
|
2021-07-23 09:45:14 -05:00
|
|
|
self.by_key("doc").tt_values().any(|tt| {
|
2021-07-23 08:36:43 -05:00
|
|
|
tt.delimiter_kind() == Some(DelimiterKind::Parenthesis) &&
|
|
|
|
matches!(&*tt.token_trees, [tt::TokenTree::Leaf(tt::Leaf::Ident(ident))] if ident.text == "hidden")
|
2021-07-23 09:45:14 -05:00
|
|
|
})
|
2021-07-23 08:36:43 -05:00
|
|
|
}
|
2019-11-22 02:27:47 -06:00
|
|
|
}
|
|
|
|
|
2021-03-19 15:23:57 -05:00
|
|
|
impl AttrsWithOwner {
|
|
|
|
pub(crate) fn attrs_query(db: &dyn DefDatabase, def: AttrDefId) -> Self {
|
|
|
|
// FIXME: this should use `Trace` to avoid duplication in `source_map` below
|
|
|
|
let raw_attrs = match def {
|
|
|
|
AttrDefId::ModuleId(module) => {
|
|
|
|
let def_map = module.def_map(db);
|
|
|
|
let mod_data = &def_map[module.local_id];
|
|
|
|
match mod_data.declaration_source(db) {
|
|
|
|
Some(it) => {
|
|
|
|
let raw_attrs = RawAttrs::from_attrs_owner(
|
|
|
|
db,
|
2021-09-27 05:54:24 -05:00
|
|
|
it.as_ref().map(|it| it as &dyn ast::HasAttrs),
|
2021-03-19 15:23:57 -05:00
|
|
|
);
|
|
|
|
match mod_data.definition_source(db) {
|
|
|
|
InFile { file_id, value: ModuleSource::SourceFile(file) } => raw_attrs
|
|
|
|
.merge(RawAttrs::from_attrs_owner(db, InFile::new(file_id, &file))),
|
|
|
|
_ => raw_attrs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => RawAttrs::from_attrs_owner(
|
|
|
|
db,
|
|
|
|
mod_data.definition_source(db).as_ref().map(|src| match src {
|
2021-09-27 05:54:24 -05:00
|
|
|
ModuleSource::SourceFile(file) => file as &dyn ast::HasAttrs,
|
|
|
|
ModuleSource::Module(module) => module as &dyn ast::HasAttrs,
|
|
|
|
ModuleSource::BlockExpr(block) => block as &dyn ast::HasAttrs,
|
2021-03-19 15:23:57 -05:00
|
|
|
}),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AttrDefId::FieldId(it) => {
|
|
|
|
return Self { attrs: db.fields_attrs(it.parent)[it.local_id].clone(), owner: def };
|
|
|
|
}
|
|
|
|
AttrDefId::EnumVariantId(it) => {
|
|
|
|
return Self {
|
|
|
|
attrs: db.variants_attrs(it.parent)[it.local_id].clone(),
|
|
|
|
owner: def,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
AttrDefId::AdtId(it) => match it {
|
|
|
|
AdtId::StructId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
|
|
|
AdtId::EnumId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
|
|
|
AdtId::UnionId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
|
|
|
},
|
|
|
|
AttrDefId::TraitId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
|
|
|
AttrDefId::MacroDefId(it) => it
|
|
|
|
.ast_id()
|
2021-06-28 12:16:38 -05:00
|
|
|
.either(|ast_id| attrs_from_ast(ast_id, db), |ast_id| attrs_from_ast(ast_id, db)),
|
2021-03-19 15:23:57 -05:00
|
|
|
AttrDefId::ImplId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
|
|
|
AttrDefId::ConstId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
|
|
|
AttrDefId::StaticId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
|
|
|
AttrDefId::FunctionId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
|
|
|
AttrDefId::TypeAliasId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
|
|
|
AttrDefId::GenericParamId(it) => match it {
|
|
|
|
GenericParamId::TypeParamId(it) => {
|
|
|
|
let src = it.parent.child_source(db);
|
|
|
|
RawAttrs::from_attrs_owner(
|
|
|
|
db,
|
|
|
|
src.with_value(
|
|
|
|
src.value[it.local_id].as_ref().either(|it| it as _, |it| it as _),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
GenericParamId::LifetimeParamId(it) => {
|
|
|
|
let src = it.parent.child_source(db);
|
|
|
|
RawAttrs::from_attrs_owner(db, src.with_value(&src.value[it.local_id]))
|
|
|
|
}
|
|
|
|
GenericParamId::ConstParamId(it) => {
|
|
|
|
let src = it.parent.child_source(db);
|
|
|
|
RawAttrs::from_attrs_owner(db, src.with_value(&src.value[it.local_id]))
|
|
|
|
}
|
|
|
|
},
|
2021-12-07 10:31:26 -06:00
|
|
|
AttrDefId::ExternBlockId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
2021-03-19 15:23:57 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let attrs = raw_attrs.filter(db, def.krate(db));
|
|
|
|
Self { attrs, owner: def }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn source_map(&self, db: &dyn DefDatabase) -> AttrSourceMap {
|
|
|
|
let owner = match self.owner {
|
|
|
|
AttrDefId::ModuleId(module) => {
|
|
|
|
// Modules can have 2 attribute owners (the `mod x;` item, and the module file itself).
|
|
|
|
|
|
|
|
let def_map = module.def_map(db);
|
|
|
|
let mod_data = &def_map[module.local_id];
|
2021-05-10 14:50:42 -05:00
|
|
|
match mod_data.declaration_source(db) {
|
2021-03-19 15:23:57 -05:00
|
|
|
Some(it) => {
|
2021-05-10 14:50:42 -05:00
|
|
|
let mut map = AttrSourceMap::new(InFile::new(it.file_id, &it.value));
|
2021-03-19 15:23:57 -05:00
|
|
|
if let InFile { file_id, value: ModuleSource::SourceFile(file) } =
|
|
|
|
mod_data.definition_source(db)
|
|
|
|
{
|
2022-01-08 07:54:31 -06:00
|
|
|
map.append_module_inline_attrs(AttrSourceMap::new(InFile::new(
|
|
|
|
file_id, &file,
|
|
|
|
)));
|
2021-03-19 15:23:57 -05:00
|
|
|
}
|
2021-05-10 14:50:42 -05:00
|
|
|
return map;
|
2021-03-19 15:23:57 -05:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let InFile { file_id, value } = mod_data.definition_source(db);
|
2021-05-10 14:50:42 -05:00
|
|
|
let attrs_owner = match &value {
|
2021-09-27 05:54:24 -05:00
|
|
|
ModuleSource::SourceFile(file) => file as &dyn ast::HasAttrs,
|
|
|
|
ModuleSource::Module(module) => module as &dyn ast::HasAttrs,
|
|
|
|
ModuleSource::BlockExpr(block) => block as &dyn ast::HasAttrs,
|
2021-05-10 14:50:42 -05:00
|
|
|
};
|
|
|
|
return AttrSourceMap::new(InFile::new(file_id, attrs_owner));
|
2021-03-19 15:23:57 -05:00
|
|
|
}
|
2021-05-10 14:50:42 -05:00
|
|
|
}
|
2021-03-19 15:23:57 -05:00
|
|
|
}
|
|
|
|
AttrDefId::FieldId(id) => {
|
2021-04-06 15:25:44 -05:00
|
|
|
let map = db.fields_attrs_source_map(id.parent);
|
|
|
|
let file_id = id.parent.file_id(db);
|
|
|
|
let root = db.parse_or_expand(file_id).unwrap();
|
|
|
|
let owner = match &map[id.local_id] {
|
2021-09-27 05:54:24 -05:00
|
|
|
Either::Left(it) => ast::AnyHasAttrs::new(it.to_node(&root)),
|
|
|
|
Either::Right(it) => ast::AnyHasAttrs::new(it.to_node(&root)),
|
2021-04-06 15:25:44 -05:00
|
|
|
};
|
|
|
|
InFile::new(file_id, owner)
|
2021-03-19 15:23:57 -05:00
|
|
|
}
|
|
|
|
AttrDefId::AdtId(adt) => match adt {
|
2021-09-27 05:54:24 -05:00
|
|
|
AdtId::StructId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
|
|
|
|
AdtId::UnionId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
|
|
|
|
AdtId::EnumId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
|
2021-03-19 15:23:57 -05:00
|
|
|
},
|
2021-09-27 05:54:24 -05:00
|
|
|
AttrDefId::FunctionId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
|
2021-04-06 15:25:44 -05:00
|
|
|
AttrDefId::EnumVariantId(id) => {
|
|
|
|
let map = db.variants_attrs_source_map(id.parent);
|
|
|
|
let file_id = id.parent.lookup(db).id.file_id();
|
|
|
|
let root = db.parse_or_expand(file_id).unwrap();
|
2021-09-27 05:54:24 -05:00
|
|
|
InFile::new(file_id, ast::AnyHasAttrs::new(map[id.local_id].to_node(&root)))
|
2021-04-06 15:25:44 -05:00
|
|
|
}
|
2021-09-27 05:54:24 -05:00
|
|
|
AttrDefId::StaticId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
|
|
|
|
AttrDefId::ConstId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
|
|
|
|
AttrDefId::TraitId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
|
|
|
|
AttrDefId::TypeAliasId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
|
2021-06-28 12:16:38 -05:00
|
|
|
AttrDefId::MacroDefId(id) => id.ast_id().either(
|
2021-09-27 05:54:24 -05:00
|
|
|
|it| it.with_value(ast::AnyHasAttrs::new(it.to_node(db.upcast()))),
|
|
|
|
|it| it.with_value(ast::AnyHasAttrs::new(it.to_node(db.upcast()))),
|
2021-06-28 12:16:38 -05:00
|
|
|
),
|
2021-09-27 05:54:24 -05:00
|
|
|
AttrDefId::ImplId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
|
2021-03-19 15:23:57 -05:00
|
|
|
AttrDefId::GenericParamId(id) => match id {
|
|
|
|
GenericParamId::TypeParamId(id) => {
|
|
|
|
id.parent.child_source(db).map(|source| match &source[id.local_id] {
|
2021-09-27 05:54:24 -05:00
|
|
|
Either::Left(id) => ast::AnyHasAttrs::new(id.clone()),
|
|
|
|
Either::Right(id) => ast::AnyHasAttrs::new(id.clone()),
|
2021-03-19 15:23:57 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
GenericParamId::LifetimeParamId(id) => id
|
|
|
|
.parent
|
|
|
|
.child_source(db)
|
2021-09-27 05:54:24 -05:00
|
|
|
.map(|source| ast::AnyHasAttrs::new(source[id.local_id].clone())),
|
2021-03-19 15:23:57 -05:00
|
|
|
GenericParamId::ConstParamId(id) => id
|
|
|
|
.parent
|
|
|
|
.child_source(db)
|
2021-09-27 05:54:24 -05:00
|
|
|
.map(|source| ast::AnyHasAttrs::new(source[id.local_id].clone())),
|
2021-03-19 15:23:57 -05:00
|
|
|
},
|
2021-12-07 10:31:26 -06:00
|
|
|
AttrDefId::ExternBlockId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
|
2021-03-19 15:23:57 -05:00
|
|
|
};
|
|
|
|
|
2021-09-27 05:54:24 -05:00
|
|
|
AttrSourceMap::new(owner.as_ref().map(|node| node as &dyn HasAttrs))
|
2021-03-19 15:23:57 -05:00
|
|
|
}
|
2021-03-30 10:20:43 -05:00
|
|
|
|
|
|
|
pub fn docs_with_rangemap(
|
|
|
|
&self,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
) -> Option<(Documentation, DocsRangeMap)> {
|
|
|
|
// FIXME: code duplication in `docs` above
|
2021-05-24 17:50:19 -05:00
|
|
|
let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_deref()? {
|
2021-04-09 06:36:22 -05:00
|
|
|
AttrInput::Literal(s) => Some((s, attr.id)),
|
2021-08-21 11:19:18 -05:00
|
|
|
AttrInput::TokenTree(..) => None,
|
2021-03-30 10:20:43 -05:00
|
|
|
});
|
|
|
|
let indent = docs
|
|
|
|
.clone()
|
|
|
|
.flat_map(|(s, _)| s.lines())
|
|
|
|
.filter(|line| !line.chars().all(|c| c.is_whitespace()))
|
|
|
|
.map(|line| line.chars().take_while(|c| c.is_whitespace()).count())
|
|
|
|
.min()
|
|
|
|
.unwrap_or(0);
|
|
|
|
let mut buf = String::new();
|
|
|
|
let mut mapping = Vec::new();
|
|
|
|
for (doc, idx) in docs {
|
|
|
|
if !doc.is_empty() {
|
2021-05-04 06:51:57 -05:00
|
|
|
let mut base_offset = 0;
|
|
|
|
for raw_line in doc.split('\n') {
|
|
|
|
let line = raw_line.trim_end();
|
2021-03-30 15:26:03 -05:00
|
|
|
let line_len = line.len();
|
2021-03-30 10:20:43 -05:00
|
|
|
let (offset, line) = match line.char_indices().nth(indent) {
|
|
|
|
Some((offset, _)) => (offset, &line[offset..]),
|
|
|
|
None => (0, line),
|
|
|
|
};
|
|
|
|
let buf_offset = buf.len();
|
|
|
|
buf.push_str(line);
|
|
|
|
mapping.push((
|
2021-03-30 15:26:03 -05:00
|
|
|
TextRange::new(buf_offset.try_into().ok()?, buf.len().try_into().ok()?),
|
2021-03-30 10:20:43 -05:00
|
|
|
idx,
|
2021-05-04 06:51:57 -05:00
|
|
|
TextRange::at(
|
|
|
|
(base_offset + offset).try_into().ok()?,
|
|
|
|
line_len.try_into().ok()?,
|
|
|
|
),
|
2021-03-30 10:20:43 -05:00
|
|
|
));
|
|
|
|
buf.push('\n');
|
2021-05-04 06:51:57 -05:00
|
|
|
base_offset += raw_line.len() + 1;
|
2021-03-30 10:20:43 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buf.push('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf.pop();
|
|
|
|
if buf.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
2021-05-10 14:50:42 -05:00
|
|
|
Some((Documentation(buf), DocsRangeMap { mapping, source_map: self.source_map(db) }))
|
2021-03-30 10:20:43 -05:00
|
|
|
}
|
|
|
|
}
|
2021-03-19 15:23:57 -05:00
|
|
|
}
|
|
|
|
|
2020-12-08 16:30:51 -06:00
|
|
|
fn inner_attributes(
|
|
|
|
syntax: &SyntaxNode,
|
|
|
|
) -> Option<(impl Iterator<Item = ast::Attr>, impl Iterator<Item = ast::Comment>)> {
|
|
|
|
let (attrs, docs) = match_ast! {
|
2020-12-08 16:21:20 -06:00
|
|
|
match syntax {
|
2022-01-07 07:14:33 -06:00
|
|
|
ast::SourceFile(it) => (it.attrs(), ast::DocCommentIter::from_syntax_node(it.syntax())),
|
2020-12-08 16:21:20 -06:00
|
|
|
ast::ExternBlock(it) => {
|
|
|
|
let extern_item_list = it.extern_item_list()?;
|
2022-01-07 07:14:33 -06:00
|
|
|
(extern_item_list.attrs(), ast::DocCommentIter::from_syntax_node(extern_item_list.syntax()))
|
2020-12-08 16:21:20 -06:00
|
|
|
},
|
|
|
|
ast::Fn(it) => {
|
|
|
|
let body = it.body()?;
|
2021-09-26 04:12:57 -05:00
|
|
|
let stmt_list = body.stmt_list()?;
|
2022-01-07 07:14:33 -06:00
|
|
|
(stmt_list.attrs(), ast::DocCommentIter::from_syntax_node(body.syntax()))
|
2020-12-08 16:21:20 -06:00
|
|
|
},
|
|
|
|
ast::Impl(it) => {
|
|
|
|
let assoc_item_list = it.assoc_item_list()?;
|
2022-01-07 07:14:33 -06:00
|
|
|
(assoc_item_list.attrs(), ast::DocCommentIter::from_syntax_node(assoc_item_list.syntax()))
|
2020-12-08 16:21:20 -06:00
|
|
|
},
|
|
|
|
ast::Module(it) => {
|
|
|
|
let item_list = it.item_list()?;
|
2022-01-07 07:14:33 -06:00
|
|
|
(item_list.attrs(), ast::DocCommentIter::from_syntax_node(item_list.syntax()))
|
2020-12-08 16:21:20 -06:00
|
|
|
},
|
|
|
|
// FIXME: BlockExpr's only accept inner attributes in specific cases
|
|
|
|
// Excerpt from the reference:
|
2020-12-08 16:30:51 -06:00
|
|
|
// Block expressions accept outer and inner attributes, but only when they are the outer
|
|
|
|
// expression of an expression statement or the final expression of another block expression.
|
2021-03-05 07:59:50 -06:00
|
|
|
ast::BlockExpr(_it) => return None,
|
2020-12-08 16:21:20 -06:00
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
};
|
2021-04-19 09:11:49 -05:00
|
|
|
let attrs = attrs.filter(|attr| attr.kind().is_inner());
|
2020-12-08 16:30:51 -06:00
|
|
|
let docs = docs.filter(|doc| doc.is_inner());
|
|
|
|
Some((attrs, docs))
|
2020-12-08 16:21:20 -06:00
|
|
|
}
|
|
|
|
|
2021-09-23 08:37:52 -05:00
|
|
|
#[derive(Debug)]
|
2021-03-17 05:22:40 -05:00
|
|
|
pub struct AttrSourceMap {
|
2022-01-07 07:14:33 -06:00
|
|
|
source: Vec<Either<ast::Attr, ast::Comment>>,
|
|
|
|
file_id: HirFileId,
|
2022-01-08 07:54:31 -06:00
|
|
|
/// If this map is for a module, this will be the [`HirFileId`] of the module's definition site,
|
|
|
|
/// while `file_id` will be the one of the module declaration site.
|
|
|
|
/// The usize is the index into `source` from which point on the entries reside in the def site
|
|
|
|
/// file.
|
|
|
|
mod_def_site_file_id: Option<(HirFileId, usize)>,
|
2021-03-17 05:22:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AttrSourceMap {
|
2021-09-27 05:54:24 -05:00
|
|
|
fn new(owner: InFile<&dyn ast::HasAttrs>) -> Self {
|
2022-01-07 07:14:33 -06:00
|
|
|
Self {
|
|
|
|
source: collect_attrs(owner.value).map(|(_, it)| it).collect(),
|
|
|
|
file_id: owner.file_id,
|
2022-01-08 07:54:31 -06:00
|
|
|
mod_def_site_file_id: None,
|
2021-05-10 14:50:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 07:54:31 -06:00
|
|
|
/// Append a second source map to this one, this is required for modules, whose outline and inline
|
|
|
|
/// attributes can reside in different files
|
|
|
|
fn append_module_inline_attrs(&mut self, other: Self) {
|
|
|
|
assert!(self.mod_def_site_file_id.is_none() && other.mod_def_site_file_id.is_none());
|
|
|
|
let len = self.source.len();
|
2022-01-07 07:14:33 -06:00
|
|
|
self.source.extend(other.source);
|
2022-01-08 07:54:31 -06:00
|
|
|
if other.file_id != self.file_id {
|
|
|
|
self.mod_def_site_file_id = Some((other.file_id, len));
|
|
|
|
}
|
2021-05-10 14:50:42 -05:00
|
|
|
}
|
|
|
|
|
2021-03-17 05:22:40 -05:00
|
|
|
/// Maps the lowered `Attr` back to its original syntax node.
|
|
|
|
///
|
|
|
|
/// `attr` must come from the `owner` used for AttrSourceMap
|
|
|
|
///
|
|
|
|
/// Note that the returned syntax node might be a `#[cfg_attr]`, or a doc comment, instead of
|
|
|
|
/// the attribute represented by `Attr`.
|
2022-01-07 07:14:33 -06:00
|
|
|
pub fn source_of(&self, attr: &Attr) -> InFile<&Either<ast::Attr, ast::Comment>> {
|
2021-05-10 14:50:42 -05:00
|
|
|
self.source_of_id(attr.id)
|
|
|
|
}
|
|
|
|
|
2022-01-07 07:14:33 -06:00
|
|
|
fn source_of_id(&self, id: AttrId) -> InFile<&Either<ast::Attr, ast::Comment>> {
|
2022-01-08 07:54:31 -06:00
|
|
|
let ast_idx = id.ast_index as usize;
|
|
|
|
let file_id = match self.mod_def_site_file_id {
|
|
|
|
Some((file_id, def_site_cut)) if def_site_cut <= ast_idx => file_id,
|
|
|
|
_ => self.file_id,
|
|
|
|
};
|
|
|
|
|
2022-01-07 07:14:33 -06:00
|
|
|
self.source
|
2022-01-08 07:54:31 -06:00
|
|
|
.get(ast_idx)
|
|
|
|
.map(|it| InFile::new(file_id, it))
|
2022-01-07 07:14:33 -06:00
|
|
|
.unwrap_or_else(|| panic!("cannot find attr at index {:?}", id))
|
2021-03-17 05:22:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 10:20:43 -05:00
|
|
|
/// A struct to map text ranges from [`Documentation`] back to TextRanges in the syntax tree.
|
2021-09-23 08:37:52 -05:00
|
|
|
#[derive(Debug)]
|
2021-03-30 10:20:43 -05:00
|
|
|
pub struct DocsRangeMap {
|
2021-05-10 14:50:42 -05:00
|
|
|
source_map: AttrSourceMap,
|
2021-03-30 10:20:43 -05:00
|
|
|
// (docstring-line-range, attr_index, attr-string-range)
|
|
|
|
// a mapping from the text range of a line of the [`Documentation`] to the attribute index and
|
|
|
|
// the original (untrimmed) syntax doc line
|
2021-04-08 12:44:21 -05:00
|
|
|
mapping: Vec<(TextRange, AttrId, TextRange)>,
|
2021-03-30 10:20:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DocsRangeMap {
|
2021-09-23 10:32:39 -05:00
|
|
|
/// Maps a [`TextRange`] relative to the documentation string back to its AST range
|
2021-03-30 15:26:03 -05:00
|
|
|
pub fn map(&self, range: TextRange) -> Option<InFile<TextRange>> {
|
|
|
|
let found = self.mapping.binary_search_by(|(probe, ..)| probe.ordering(range)).ok()?;
|
2021-06-12 22:57:19 -05:00
|
|
|
let (line_docs_range, idx, original_line_src_range) = self.mapping[found];
|
2021-03-30 15:26:03 -05:00
|
|
|
if !line_docs_range.contains_range(range) {
|
2021-03-30 10:20:43 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-03-30 15:26:03 -05:00
|
|
|
let relative_range = range - line_docs_range.start();
|
2021-03-30 10:20:43 -05:00
|
|
|
|
2021-08-02 07:59:28 -05:00
|
|
|
let InFile { file_id, value: source } = self.source_map.source_of_id(idx);
|
2021-03-30 10:20:43 -05:00
|
|
|
match source {
|
2021-09-23 10:32:39 -05:00
|
|
|
Either::Left(attr) => {
|
|
|
|
let string = get_doc_string_in_attr(&attr)?;
|
|
|
|
let text_range = string.open_quote_text_range()?;
|
|
|
|
let range = TextRange::at(
|
|
|
|
text_range.end() + original_line_src_range.start() + relative_range.start(),
|
|
|
|
string.syntax().text_range().len().min(range.len()),
|
|
|
|
);
|
|
|
|
Some(InFile { file_id, value: range })
|
|
|
|
}
|
2021-03-30 10:20:43 -05:00
|
|
|
Either::Right(comment) => {
|
|
|
|
let text_range = comment.syntax().text_range();
|
|
|
|
let range = TextRange::at(
|
|
|
|
text_range.start()
|
2021-03-30 15:26:03 -05:00
|
|
|
+ TextSize::try_from(comment.prefix().len()).ok()?
|
|
|
|
+ original_line_src_range.start()
|
|
|
|
+ relative_range.start(),
|
|
|
|
text_range.len().min(range.len()),
|
2021-03-30 10:20:43 -05:00
|
|
|
);
|
|
|
|
Some(InFile { file_id, value: range })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-17 05:22:40 -05:00
|
|
|
}
|
|
|
|
|
2021-09-23 10:32:39 -05:00
|
|
|
fn get_doc_string_in_attr(it: &ast::Attr) -> Option<ast::String> {
|
|
|
|
match it.expr() {
|
|
|
|
// #[doc = lit]
|
|
|
|
Some(ast::Expr::Literal(lit)) => match lit.kind() {
|
|
|
|
ast::LiteralKind::String(it) => Some(it),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
// #[cfg_attr(..., doc = "", ...)]
|
|
|
|
None => {
|
|
|
|
// FIXME: See highlight injection for what to do here
|
|
|
|
None
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 09:35:06 -05:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2022-01-07 07:14:33 -06:00
|
|
|
pub struct AttrId {
|
2021-05-10 14:50:42 -05:00
|
|
|
pub(crate) ast_index: u32,
|
|
|
|
}
|
2021-05-10 09:35:06 -05:00
|
|
|
|
2019-10-30 08:12:55 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct Attr {
|
2021-04-09 06:36:22 -05:00
|
|
|
pub(crate) id: AttrId,
|
2021-04-01 13:35:21 -05:00
|
|
|
pub(crate) path: Interned<ModPath>,
|
2021-05-24 17:50:19 -05:00
|
|
|
pub(crate) input: Option<Interned<AttrInput>>,
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
2021-05-24 17:50:19 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-10-30 08:12:55 -05:00
|
|
|
pub enum AttrInput {
|
2020-04-25 09:24:44 -05:00
|
|
|
/// `#[attr = "string"]`
|
2019-10-30 08:12:55 -05:00
|
|
|
Literal(SmolStr),
|
2020-04-25 09:24:44 -05:00
|
|
|
/// `#[attr(subtree)]`
|
2021-08-21 11:19:18 -05:00
|
|
|
TokenTree(tt::Subtree, mbe::TokenMap),
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
2021-05-21 16:45:09 -05:00
|
|
|
impl fmt::Display for AttrInput {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
AttrInput::Literal(lit) => write!(f, " = \"{}\"", lit.escape_debug()),
|
2021-08-21 11:19:18 -05:00
|
|
|
AttrInput::TokenTree(subtree, _) => subtree.fmt(f),
|
2021-05-21 16:45:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 08:12:55 -05:00
|
|
|
impl Attr {
|
2021-05-06 12:59:54 -05:00
|
|
|
fn from_src(
|
|
|
|
db: &dyn DefDatabase,
|
2021-06-11 11:20:15 -05:00
|
|
|
ast: ast::Meta,
|
2021-05-06 12:59:54 -05:00
|
|
|
hygiene: &Hygiene,
|
|
|
|
id: AttrId,
|
|
|
|
) -> Option<Attr> {
|
|
|
|
let path = Interned::new(ModPath::from_src(db, ast.path()?, hygiene)?);
|
2021-03-18 16:25:10 -05:00
|
|
|
let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() {
|
2020-12-07 13:38:28 -06:00
|
|
|
let value = match lit.kind() {
|
2020-12-08 06:47:58 -06:00
|
|
|
ast::LiteralKind::String(string) => string.value()?.into(),
|
2020-12-07 13:38:28 -06:00
|
|
|
_ => lit.syntax().first_token()?.text().trim_matches('"').into(),
|
2020-12-07 12:05:06 -06:00
|
|
|
};
|
2021-05-24 17:50:19 -05:00
|
|
|
Some(Interned::new(AttrInput::Literal(value)))
|
2020-07-30 13:16:04 -05:00
|
|
|
} else if let Some(tt) = ast.token_tree() {
|
2021-08-21 11:06:03 -05:00
|
|
|
let (tree, map) = syntax_node_to_token_tree(tt.syntax());
|
2021-08-21 11:19:18 -05:00
|
|
|
Some(Interned::new(AttrInput::TokenTree(tree, map)))
|
2020-07-30 13:16:04 -05:00
|
|
|
} else {
|
|
|
|
None
|
2019-10-30 08:12:55 -05:00
|
|
|
};
|
2021-04-09 06:36:22 -05:00
|
|
|
Some(Attr { id, path, input })
|
2020-12-19 08:15:02 -06:00
|
|
|
}
|
|
|
|
|
2021-06-11 11:20:15 -05:00
|
|
|
fn from_tt(
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
tt: &tt::Subtree,
|
|
|
|
hygiene: &Hygiene,
|
|
|
|
id: AttrId,
|
|
|
|
) -> Option<Attr> {
|
2021-12-27 08:54:51 -06:00
|
|
|
let (parse, _) = mbe::token_tree_to_syntax_node(tt, mbe::TopEntryPoint::MetaItem).ok()?;
|
2021-06-11 11:20:15 -05:00
|
|
|
let ast = ast::Meta::cast(parse.syntax_node())?;
|
|
|
|
|
|
|
|
Self::from_src(db, ast, hygiene, id)
|
|
|
|
}
|
|
|
|
|
2022-01-01 13:31:04 -06:00
|
|
|
/// Parses this attribute as a token tree consisting of comma separated paths.
|
|
|
|
pub fn parse_path_comma_token_tree(&self) -> Option<impl Iterator<Item = ModPath> + '_> {
|
2021-11-18 14:43:54 -06:00
|
|
|
let args = match self.input.as_deref() {
|
|
|
|
Some(AttrInput::TokenTree(args, _)) => args,
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
if args.delimiter_kind() != Some(DelimiterKind::Parenthesis) {
|
|
|
|
return None;
|
2020-12-18 18:09:48 -06:00
|
|
|
}
|
2021-11-18 14:43:54 -06:00
|
|
|
let paths = args
|
|
|
|
.token_trees
|
|
|
|
.iter()
|
|
|
|
.group_by(|tt| {
|
|
|
|
matches!(tt, tt::TokenTree::Leaf(tt::Leaf::Punct(Punct { char: ',', .. })))
|
|
|
|
})
|
|
|
|
.into_iter()
|
2022-01-06 06:39:20 -06:00
|
|
|
.filter(|(comma, _)| !*comma)
|
2021-11-18 14:43:54 -06:00
|
|
|
.map(|(_, tts)| {
|
|
|
|
let segments = tts.filter_map(|tt| match tt {
|
|
|
|
tt::TokenTree::Leaf(tt::Leaf::Ident(id)) => Some(id.as_name()),
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
ModPath::from_segments(PathKind::Plain, segments)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2022-01-02 10:34:28 -06:00
|
|
|
Some(paths.into_iter())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn path(&self) -> &ModPath {
|
|
|
|
&self.path
|
2020-12-18 18:09:48 -06:00
|
|
|
}
|
2021-03-16 15:05:07 -05:00
|
|
|
|
|
|
|
pub fn string_value(&self) -> Option<&SmolStr> {
|
2021-05-24 17:50:19 -05:00
|
|
|
match self.input.as_deref()? {
|
2021-03-16 15:05:07 -05:00
|
|
|
AttrInput::Literal(it) => Some(it),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 07:03:02 -06:00
|
|
|
}
|
|
|
|
|
2020-05-01 07:58:24 -05:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2022-01-11 03:07:16 -06:00
|
|
|
pub struct AttrQuery<'attr> {
|
|
|
|
attrs: &'attr Attrs,
|
2019-11-24 07:03:02 -06:00
|
|
|
key: &'static str,
|
|
|
|
}
|
2019-10-30 08:12:55 -05:00
|
|
|
|
2022-01-11 03:07:16 -06:00
|
|
|
impl<'attr> AttrQuery<'attr> {
|
|
|
|
pub fn tt_values(self) -> impl Iterator<Item = &'attr Subtree> {
|
2021-05-24 17:50:19 -05:00
|
|
|
self.attrs().filter_map(|attr| match attr.input.as_deref()? {
|
2021-08-21 11:19:18 -05:00
|
|
|
AttrInput::TokenTree(it, _) => Some(it),
|
2019-11-24 07:03:02 -06:00
|
|
|
_ => None,
|
|
|
|
})
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
2022-01-11 03:07:16 -06:00
|
|
|
pub fn string_value(self) -> Option<&'attr SmolStr> {
|
2021-05-24 17:50:19 -05:00
|
|
|
self.attrs().find_map(|attr| match attr.input.as_deref()? {
|
2019-11-24 07:03:02 -06:00
|
|
|
AttrInput::Literal(it) => Some(it),
|
2019-10-30 08:12:55 -05:00
|
|
|
_ => None,
|
2019-11-24 07:03:02 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn exists(self) -> bool {
|
|
|
|
self.attrs().next().is_some()
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
2022-01-11 03:07:16 -06:00
|
|
|
pub fn attrs(self) -> impl Iterator<Item = &'attr Attr> + Clone {
|
2019-11-24 07:03:02 -06:00
|
|
|
let key = self.key;
|
|
|
|
self.attrs
|
|
|
|
.iter()
|
2021-11-04 12:12:05 -05:00
|
|
|
.filter(move |attr| attr.path.as_ident().map_or(false, |s| s.to_smol_str() == key))
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
}
|
2019-11-23 02:14:10 -06:00
|
|
|
|
2020-12-17 17:23:46 -06:00
|
|
|
fn attrs_from_ast<N>(src: AstId<N>, db: &dyn DefDatabase) -> RawAttrs
|
2019-11-23 02:14:10 -06:00
|
|
|
where
|
2021-09-27 05:54:24 -05:00
|
|
|
N: ast::HasAttrs,
|
2019-11-23 02:14:10 -06:00
|
|
|
{
|
2020-03-13 10:05:46 -05:00
|
|
|
let src = InFile::new(src.file_id, src.to_node(db.upcast()));
|
2021-09-27 05:54:24 -05:00
|
|
|
RawAttrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn ast::HasAttrs))
|
2019-11-23 02:14:10 -06:00
|
|
|
}
|
|
|
|
|
2020-12-17 17:23:46 -06:00
|
|
|
fn attrs_from_item_tree<N: ItemTreeNode>(id: ItemTreeId<N>, db: &dyn DefDatabase) -> RawAttrs {
|
2021-03-12 17:34:01 -06:00
|
|
|
let tree = id.item_tree(db);
|
2020-06-22 12:15:54 -05:00
|
|
|
let mod_item = N::id_to_mod_item(id.value);
|
2020-12-17 17:23:46 -06:00
|
|
|
tree.raw_attrs(mod_item.into()).clone()
|
2019-11-23 02:14:10 -06:00
|
|
|
}
|
2020-12-19 08:15:02 -06:00
|
|
|
|
2021-03-17 10:10:58 -05:00
|
|
|
fn collect_attrs(
|
2021-09-27 05:54:24 -05:00
|
|
|
owner: &dyn ast::HasAttrs,
|
2021-05-10 14:50:42 -05:00
|
|
|
) -> impl Iterator<Item = (AttrId, Either<ast::Attr, ast::Comment>)> {
|
2020-12-19 08:15:02 -06:00
|
|
|
let (inner_attrs, inner_docs) = inner_attributes(owner.syntax())
|
2021-03-21 07:13:34 -05:00
|
|
|
.map_or((None, None), |(attrs, docs)| (Some(attrs), Some(docs)));
|
2020-12-19 08:15:02 -06:00
|
|
|
|
2021-04-19 09:11:49 -05:00
|
|
|
let outer_attrs = owner.attrs().filter(|attr| attr.kind().is_outer());
|
2022-01-07 07:14:33 -06:00
|
|
|
let attrs = outer_attrs
|
|
|
|
.chain(inner_attrs.into_iter().flatten())
|
|
|
|
.map(|attr| (attr.syntax().text_range().start(), Either::Left(attr)));
|
2020-12-19 08:15:02 -06:00
|
|
|
|
|
|
|
let outer_docs =
|
2022-01-07 07:14:33 -06:00
|
|
|
ast::DocCommentIter::from_syntax_node(owner.syntax()).filter(ast::Comment::is_outer);
|
|
|
|
let docs = outer_docs
|
|
|
|
.chain(inner_docs.into_iter().flatten())
|
|
|
|
.map(|docs_text| (docs_text.syntax().text_range().start(), Either::Right(docs_text)));
|
2020-12-19 08:15:02 -06:00
|
|
|
// sort here by syntax node offset because the source can have doc attributes and doc strings be interleaved
|
2022-01-07 07:14:33 -06:00
|
|
|
docs.chain(attrs)
|
|
|
|
.sorted_by_key(|&(offset, _)| offset)
|
|
|
|
.enumerate()
|
|
|
|
.map(|(id, (_, attr))| (AttrId { ast_index: id as u32 }, attr))
|
2020-12-19 08:15:02 -06:00
|
|
|
}
|
2021-04-06 15:25:44 -05:00
|
|
|
|
|
|
|
pub(crate) fn variants_attrs_source_map(
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
def: EnumId,
|
|
|
|
) -> Arc<ArenaMap<LocalEnumVariantId, AstPtr<ast::Variant>>> {
|
|
|
|
let mut res = ArenaMap::default();
|
|
|
|
let child_source = def.child_source(db);
|
|
|
|
|
|
|
|
for (idx, variant) in child_source.value.iter() {
|
|
|
|
res.insert(idx, AstPtr::new(variant));
|
|
|
|
}
|
|
|
|
|
|
|
|
Arc::new(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn fields_attrs_source_map(
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
def: VariantId,
|
|
|
|
) -> Arc<ArenaMap<LocalFieldId, Either<AstPtr<ast::TupleField>, AstPtr<ast::RecordField>>>> {
|
|
|
|
let mut res = ArenaMap::default();
|
|
|
|
let child_source = def.child_source(db);
|
|
|
|
|
|
|
|
for (idx, variant) in child_source.value.iter() {
|
|
|
|
res.insert(
|
|
|
|
idx,
|
|
|
|
variant
|
|
|
|
.as_ref()
|
|
|
|
.either(|l| Either::Left(AstPtr::new(l)), |r| Either::Right(AstPtr::new(r))),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Arc::new(res)
|
|
|
|
}
|