2019-10-30 13:12:55 +00:00
|
|
|
//! A higher level attributes based on TokenTree, with also some shortcuts.
|
|
|
|
|
2023-01-09 18:29:28 +00:00
|
|
|
use std::{hash::Hash, ops, sync::Arc};
|
2019-10-30 13:12:55 +00:00
|
|
|
|
2020-12-17 23:23:46 +00:00
|
|
|
use base_db::CrateId;
|
2020-08-13 08:32:19 +00:00
|
|
|
use cfg::{CfgExpr, CfgOptions};
|
2019-12-03 16:07:56 +00:00
|
|
|
use either::Either;
|
2023-01-09 18:29:28 +00:00
|
|
|
use hir_expand::{
|
|
|
|
attrs::{collect_attrs, Attr, AttrId, RawAttrs},
|
|
|
|
HirFileId, InFile,
|
|
|
|
};
|
2020-12-07 17:06:46 +00:00
|
|
|
use itertools::Itertools;
|
2022-07-21 06:48:13 +00:00
|
|
|
use la_arena::{ArenaMap, Idx, RawIdx};
|
2023-01-09 18:29:28 +00:00
|
|
|
use mbe::DelimiterKind;
|
2020-08-12 16:26:51 +00:00
|
|
|
use syntax::{
|
2023-01-09 18:29:28 +00:00
|
|
|
ast::{self, HasAttrs, IsString},
|
|
|
|
AstPtr, AstToken, SmolStr, TextRange, TextSize,
|
2019-10-30 13:12:55 +00:00
|
|
|
};
|
|
|
|
|
2019-11-23 08:14:10 +00:00
|
|
|
use crate::{
|
2020-06-22 17:15:54 +00:00
|
|
|
db::DefDatabase,
|
2022-07-21 07:23:30 +00:00
|
|
|
item_tree::{AttrOwner, Fields, ItemTreeId, ItemTreeNode},
|
|
|
|
nameres::{ModuleOrigin, ModuleSource},
|
2021-03-19 20:23:57 +00:00
|
|
|
src::{HasChildSource, HasSource},
|
2022-07-21 06:48:13 +00:00
|
|
|
AdtId, AttrDefId, EnumId, GenericParamId, LocalEnumVariantId, LocalFieldId, Lookup, MacroId,
|
|
|
|
VariantId,
|
2019-11-23 08:14:10 +00:00
|
|
|
};
|
2019-10-30 13:12:55 +00:00
|
|
|
|
2020-12-07 17:49:03 +00:00
|
|
|
/// Holds documentation
|
2020-12-11 20:19:58 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2020-12-07 20:55:00 +00:00
|
|
|
pub struct Documentation(String);
|
2020-12-07 17:49:03 +00:00
|
|
|
|
|
|
|
impl Documentation {
|
2021-06-07 09:58:51 +00:00
|
|
|
pub fn new(s: String) -> Self {
|
|
|
|
Documentation(s)
|
2021-06-04 18:25:16 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 17:49:03 +00:00
|
|
|
pub fn as_str(&self) -> &str {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 19:11:03 +00:00
|
|
|
impl From<Documentation> for String {
|
|
|
|
fn from(Documentation(string): Documentation) -> Self {
|
|
|
|
string
|
2020-12-07 17:49:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 23:23:46 +00:00
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct Attrs(RawAttrs);
|
|
|
|
|
2021-03-19 20:23:57 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct AttrsWithOwner {
|
|
|
|
attrs: Attrs,
|
|
|
|
owner: AttrDefId,
|
|
|
|
}
|
|
|
|
|
2022-01-23 16:29:31 +00:00
|
|
|
impl Attrs {
|
2022-01-30 21:46:05 +00:00
|
|
|
pub fn get(&self, id: AttrId) -> Option<&Attr> {
|
|
|
|
(**self).iter().find(|attr| attr.id == id)
|
2022-01-23 16:29:31 +00:00
|
|
|
}
|
2023-01-09 18:29:28 +00:00
|
|
|
|
|
|
|
pub(crate) fn filter(db: &dyn DefDatabase, krate: CrateId, raw_attrs: RawAttrs) -> Attrs {
|
|
|
|
Attrs(raw_attrs.filter(db.upcast(), krate))
|
|
|
|
}
|
2022-01-23 16:29:31 +00:00
|
|
|
}
|
2019-11-22 08:27:47 +00:00
|
|
|
|
2020-12-17 23:23:46 +00:00
|
|
|
impl ops::Deref for Attrs {
|
|
|
|
type Target = [Attr];
|
|
|
|
|
|
|
|
fn deref(&self) -> &[Attr] {
|
2023-01-09 18:29:28 +00:00
|
|
|
&self.0
|
2020-12-17 23:23:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 20:23:57 +00:00
|
|
|
impl ops::Deref for AttrsWithOwner {
|
|
|
|
type Target = Attrs;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Attrs {
|
|
|
|
&self.attrs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 08:27:47 +00:00
|
|
|
impl Attrs {
|
2020-12-17 23:23:46 +00:00
|
|
|
pub const EMPTY: Self = Self(RawAttrs::EMPTY);
|
2020-06-23 17:42:19 +00:00
|
|
|
|
2021-01-04 20:56:21 +00:00
|
|
|
pub(crate) fn variants_attrs_query(
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
e: EnumId,
|
|
|
|
) -> Arc<ArenaMap<LocalEnumVariantId, Attrs>> {
|
2022-07-21 07:23:30 +00:00
|
|
|
// FIXME: There should be some proper form of mapping between item tree enum variant ids and hir enum variant ids
|
2021-01-04 20:56:21 +00:00
|
|
|
let mut res = ArenaMap::default();
|
|
|
|
|
2022-07-21 06:48:13 +00:00
|
|
|
let loc = e.lookup(db);
|
|
|
|
let krate = loc.container.krate;
|
|
|
|
let item_tree = loc.id.item_tree(db);
|
|
|
|
let enum_ = &item_tree[loc.id.value];
|
|
|
|
let crate_graph = db.crate_graph();
|
|
|
|
let cfg_options = &crate_graph[krate].cfg_options;
|
|
|
|
|
|
|
|
let mut idx = 0;
|
|
|
|
for variant in enum_.variants.clone() {
|
|
|
|
let attrs = item_tree.attrs(db, krate, variant.into());
|
|
|
|
if attrs.is_cfg_enabled(cfg_options) {
|
|
|
|
res.insert(Idx::from_raw(RawIdx::from(idx)), attrs);
|
|
|
|
idx += 1;
|
|
|
|
}
|
2021-01-04 20:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Arc::new(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn fields_attrs_query(
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
v: VariantId,
|
|
|
|
) -> Arc<ArenaMap<LocalFieldId, Attrs>> {
|
2022-07-21 07:23:30 +00:00
|
|
|
// FIXME: There should be some proper form of mapping between item tree field ids and hir field ids
|
2021-01-04 20:56:21 +00:00
|
|
|
let mut res = ArenaMap::default();
|
|
|
|
|
2022-07-21 06:48:13 +00:00
|
|
|
let crate_graph = db.crate_graph();
|
|
|
|
let (fields, item_tree, krate) = match v {
|
|
|
|
VariantId::EnumVariantId(it) => {
|
|
|
|
let e = it.parent;
|
|
|
|
let loc = e.lookup(db);
|
|
|
|
let krate = loc.container.krate;
|
|
|
|
let item_tree = loc.id.item_tree(db);
|
|
|
|
let enum_ = &item_tree[loc.id.value];
|
2021-01-04 20:56:21 +00:00
|
|
|
|
2022-07-21 06:48:13 +00:00
|
|
|
let cfg_options = &crate_graph[krate].cfg_options;
|
2023-01-11 16:07:35 +00:00
|
|
|
|
|
|
|
let Some(variant) = enum_.variants.clone().filter(|variant| {
|
2023-01-10 08:28:28 +00:00
|
|
|
let attrs = item_tree.attrs(db, krate, (*variant).into());
|
2023-01-11 16:07:35 +00:00
|
|
|
attrs.is_cfg_enabled(cfg_options)
|
|
|
|
})
|
|
|
|
.zip(0u32..)
|
|
|
|
.find(|(_variant, idx)| it.local_id == Idx::from_raw(RawIdx::from(*idx)))
|
|
|
|
.map(|(variant, _idx)| variant)
|
|
|
|
else {
|
2022-07-21 06:48:13 +00:00
|
|
|
return Arc::new(res);
|
|
|
|
};
|
2023-01-11 16:07:35 +00:00
|
|
|
|
2022-07-21 06:48:13 +00:00
|
|
|
(item_tree[variant].fields.clone(), item_tree, krate)
|
|
|
|
}
|
|
|
|
VariantId::StructId(it) => {
|
|
|
|
let loc = it.lookup(db);
|
|
|
|
let krate = loc.container.krate;
|
|
|
|
let item_tree = loc.id.item_tree(db);
|
|
|
|
let struct_ = &item_tree[loc.id.value];
|
|
|
|
(struct_.fields.clone(), item_tree, krate)
|
|
|
|
}
|
|
|
|
VariantId::UnionId(it) => {
|
|
|
|
let loc = it.lookup(db);
|
|
|
|
let krate = loc.container.krate;
|
|
|
|
let item_tree = loc.id.item_tree(db);
|
|
|
|
let union_ = &item_tree[loc.id.value];
|
|
|
|
(union_.fields.clone(), item_tree, krate)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let fields = match fields {
|
|
|
|
Fields::Record(fields) | Fields::Tuple(fields) => fields,
|
|
|
|
Fields::Unit => return Arc::new(res),
|
|
|
|
};
|
|
|
|
|
|
|
|
let cfg_options = &crate_graph[krate].cfg_options;
|
|
|
|
|
|
|
|
let mut idx = 0;
|
|
|
|
for field in fields {
|
|
|
|
let attrs = item_tree.attrs(db, krate, field.into());
|
|
|
|
if attrs.is_cfg_enabled(cfg_options) {
|
|
|
|
res.insert(Idx::from_raw(RawIdx::from(idx)), attrs);
|
|
|
|
idx += 1;
|
|
|
|
}
|
2021-01-04 20:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Arc::new(res)
|
|
|
|
}
|
|
|
|
|
2019-11-24 13:03:02 +00:00
|
|
|
pub fn by_key(&self, key: &'static str) -> AttrQuery<'_> {
|
|
|
|
AttrQuery { attrs: self, key }
|
2019-11-23 09:01:56 +00:00
|
|
|
}
|
2022-02-11 21:06:03 +00:00
|
|
|
}
|
2020-04-09 16:32:02 +00:00
|
|
|
|
2022-02-11 21:06:03 +00:00
|
|
|
impl Attrs {
|
2020-10-22 17:19:18 +00:00
|
|
|
pub fn cfg(&self) -> Option<CfgExpr> {
|
2022-07-21 07:23:30 +00:00
|
|
|
let mut cfgs = self.by_key("cfg").tt_values().map(CfgExpr::parse);
|
|
|
|
let first = cfgs.next()?;
|
|
|
|
match cfgs.next() {
|
|
|
|
Some(second) => {
|
|
|
|
let cfgs = [first, second].into_iter().chain(cfgs);
|
|
|
|
Some(CfgExpr::All(cfgs.collect()))
|
|
|
|
}
|
|
|
|
None => Some(first),
|
2020-10-22 17:19:18 +00:00
|
|
|
}
|
2020-07-23 14:22:17 +00:00
|
|
|
}
|
|
|
|
pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> bool {
|
2020-10-22 17:19:18 +00:00
|
|
|
match self.cfg() {
|
|
|
|
None => true,
|
|
|
|
Some(cfg) => cfg_options.check(&cfg) != Some(false),
|
|
|
|
}
|
2020-04-09 16:32:02 +00:00
|
|
|
}
|
2020-12-07 17:06:46 +00:00
|
|
|
|
2022-01-11 09:07:16 +00:00
|
|
|
pub fn lang(&self) -> Option<&SmolStr> {
|
|
|
|
self.by_key("lang").string_value()
|
|
|
|
}
|
|
|
|
|
2020-12-07 17:06:46 +00:00
|
|
|
pub fn docs(&self) -> Option<Documentation> {
|
2022-03-09 13:33:39 +00:00
|
|
|
let docs = self.by_key("doc").attrs().filter_map(|attr| attr.string_value());
|
|
|
|
let indent = doc_indent(self);
|
2021-03-17 13:38:11 +00:00
|
|
|
let mut buf = String::new();
|
|
|
|
for doc in docs {
|
|
|
|
// str::lines doesn't yield anything for the empty string
|
2021-03-17 15:10:58 +00:00
|
|
|
if !doc.is_empty() {
|
2021-03-17 13:38:11 +00: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 17:49:03 +00:00
|
|
|
None
|
|
|
|
} else {
|
2021-03-17 13:38:11 +00:00
|
|
|
Some(Documentation(buf))
|
2020-12-07 17:49:03 +00:00
|
|
|
}
|
2020-12-07 17:06:46 +00:00
|
|
|
}
|
2021-07-23 13:36:43 +00:00
|
|
|
|
|
|
|
pub fn has_doc_hidden(&self) -> bool {
|
2021-07-23 14:45:14 +00:00
|
|
|
self.by_key("doc").tt_values().any(|tt| {
|
2023-01-31 10:49:49 +00:00
|
|
|
tt.delimiter.kind == DelimiterKind::Parenthesis &&
|
2021-07-23 13:36:43 +00:00
|
|
|
matches!(&*tt.token_trees, [tt::TokenTree::Leaf(tt::Leaf::Ident(ident))] if ident.text == "hidden")
|
2021-07-23 14:45:14 +00:00
|
|
|
})
|
2021-07-23 13:36:43 +00:00
|
|
|
}
|
2022-02-11 21:06:03 +00:00
|
|
|
|
|
|
|
pub fn is_proc_macro(&self) -> bool {
|
|
|
|
self.by_key("proc_macro").exists()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_proc_macro_attribute(&self) -> bool {
|
|
|
|
self.by_key("proc_macro_attribute").exists()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_proc_macro_derive(&self) -> bool {
|
|
|
|
self.by_key("proc_macro_derive").exists()
|
|
|
|
}
|
2019-11-22 08:27:47 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 20:23:57 +00: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];
|
2022-07-21 07:23:30 +00:00
|
|
|
|
|
|
|
match mod_data.origin {
|
2022-07-21 14:05:52 +00:00
|
|
|
ModuleOrigin::File { definition, declaration_tree_id, .. } => {
|
|
|
|
let decl_attrs = declaration_tree_id
|
|
|
|
.item_tree(db)
|
|
|
|
.raw_attrs(AttrOwner::ModItem(declaration_tree_id.value.into()))
|
|
|
|
.clone();
|
2022-07-21 07:23:30 +00:00
|
|
|
let tree = db.file_item_tree(definition.into());
|
2022-07-21 14:05:52 +00:00
|
|
|
let def_attrs = tree.raw_attrs(AttrOwner::TopLevel).clone();
|
|
|
|
decl_attrs.merge(def_attrs)
|
2021-03-19 20:23:57 +00:00
|
|
|
}
|
2022-07-21 07:23:30 +00:00
|
|
|
ModuleOrigin::CrateRoot { definition } => {
|
|
|
|
let tree = db.file_item_tree(definition.into());
|
|
|
|
tree.raw_attrs(AttrOwner::TopLevel).clone()
|
|
|
|
}
|
2022-07-21 14:05:52 +00:00
|
|
|
ModuleOrigin::Inline { definition_tree_id, .. } => definition_tree_id
|
|
|
|
.item_tree(db)
|
|
|
|
.raw_attrs(AttrOwner::ModItem(definition_tree_id.value.into()))
|
|
|
|
.clone(),
|
2022-07-21 07:23:30 +00:00
|
|
|
ModuleOrigin::BlockExpr { block } => RawAttrs::from_attrs_owner(
|
2023-01-09 18:29:28 +00:00
|
|
|
db.upcast(),
|
2022-07-21 07:23:30 +00:00
|
|
|
InFile::new(block.file_id, block.to_node(db.upcast()))
|
|
|
|
.as_ref()
|
|
|
|
.map(|it| it as &dyn ast::HasAttrs),
|
2021-03-19 20:23:57 +00: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),
|
2023-03-03 15:24:07 +00:00
|
|
|
AttrDefId::TraitAliasId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
2022-03-08 22:51:19 +00:00
|
|
|
AttrDefId::MacroId(it) => match it {
|
|
|
|
MacroId::Macro2Id(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
|
|
|
MacroId::MacroRulesId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
|
|
|
MacroId::ProcMacroId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
|
|
|
},
|
2021-03-19 20:23:57 +00: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 {
|
2021-12-29 13:35:59 +00:00
|
|
|
GenericParamId::ConstParamId(it) => {
|
|
|
|
let src = it.parent().child_source(db);
|
|
|
|
RawAttrs::from_attrs_owner(
|
2023-01-09 18:29:28 +00:00
|
|
|
db.upcast(),
|
2023-03-03 15:24:03 +00:00
|
|
|
src.with_value(&src.value[it.local_id()]),
|
2021-12-29 13:35:59 +00:00
|
|
|
)
|
|
|
|
}
|
2021-03-19 20:23:57 +00:00
|
|
|
GenericParamId::TypeParamId(it) => {
|
2021-12-29 13:35:59 +00:00
|
|
|
let src = it.parent().child_source(db);
|
2021-03-19 20:23:57 +00:00
|
|
|
RawAttrs::from_attrs_owner(
|
2023-01-09 18:29:28 +00:00
|
|
|
db.upcast(),
|
2023-03-03 15:24:03 +00:00
|
|
|
src.with_value(&src.value[it.local_id()]),
|
2021-03-19 20:23:57 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
GenericParamId::LifetimeParamId(it) => {
|
|
|
|
let src = it.parent.child_source(db);
|
2023-01-09 18:29:28 +00:00
|
|
|
RawAttrs::from_attrs_owner(db.upcast(), src.with_value(&src.value[it.local_id]))
|
2021-03-19 20:23:57 +00:00
|
|
|
}
|
|
|
|
},
|
2021-12-07 16:31:26 +00:00
|
|
|
AttrDefId::ExternBlockId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
2021-03-19 20:23:57 +00:00
|
|
|
};
|
|
|
|
|
2023-01-09 18:29:28 +00:00
|
|
|
let attrs = raw_attrs.filter(db.upcast(), def.krate(db));
|
|
|
|
Self { attrs: Attrs(attrs), owner: def }
|
2021-03-19 20:23:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 19:50:42 +00:00
|
|
|
match mod_data.declaration_source(db) {
|
2021-03-19 20:23:57 +00:00
|
|
|
Some(it) => {
|
2021-05-10 19:50:42 +00:00
|
|
|
let mut map = AttrSourceMap::new(InFile::new(it.file_id, &it.value));
|
2021-03-19 20:23:57 +00:00
|
|
|
if let InFile { file_id, value: ModuleSource::SourceFile(file) } =
|
|
|
|
mod_data.definition_source(db)
|
|
|
|
{
|
2022-01-08 13:54:31 +00:00
|
|
|
map.append_module_inline_attrs(AttrSourceMap::new(InFile::new(
|
|
|
|
file_id, &file,
|
|
|
|
)));
|
2021-03-19 20:23:57 +00:00
|
|
|
}
|
2021-05-10 19:50:42 +00:00
|
|
|
return map;
|
2021-03-19 20:23:57 +00:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let InFile { file_id, value } = mod_data.definition_source(db);
|
2021-05-10 19:50:42 +00:00
|
|
|
let attrs_owner = match &value {
|
2021-09-27 10:54:24 +00: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 19:50:42 +00:00
|
|
|
};
|
|
|
|
return AttrSourceMap::new(InFile::new(file_id, attrs_owner));
|
2021-03-19 20:23:57 +00:00
|
|
|
}
|
2021-05-10 19:50:42 +00:00
|
|
|
}
|
2021-03-19 20:23:57 +00:00
|
|
|
}
|
|
|
|
AttrDefId::FieldId(id) => {
|
2021-04-06 20:25:44 +00: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 10:54:24 +00:00
|
|
|
Either::Left(it) => ast::AnyHasAttrs::new(it.to_node(&root)),
|
|
|
|
Either::Right(it) => ast::AnyHasAttrs::new(it.to_node(&root)),
|
2021-04-06 20:25:44 +00:00
|
|
|
};
|
|
|
|
InFile::new(file_id, owner)
|
2021-03-19 20:23:57 +00:00
|
|
|
}
|
|
|
|
AttrDefId::AdtId(adt) => match adt {
|
2021-09-27 10:54:24 +00: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 20:23:57 +00:00
|
|
|
},
|
2021-09-27 10:54:24 +00:00
|
|
|
AttrDefId::FunctionId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
|
2021-04-06 20:25:44 +00: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 10:54:24 +00:00
|
|
|
InFile::new(file_id, ast::AnyHasAttrs::new(map[id.local_id].to_node(&root)))
|
2021-04-06 20:25:44 +00:00
|
|
|
}
|
2021-09-27 10:54:24 +00: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),
|
2023-03-03 15:24:07 +00:00
|
|
|
AttrDefId::TraitAliasId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
|
2021-09-27 10:54:24 +00:00
|
|
|
AttrDefId::TypeAliasId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
|
2022-03-08 22:51:19 +00:00
|
|
|
AttrDefId::MacroId(id) => match id {
|
|
|
|
MacroId::Macro2Id(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
|
|
|
|
MacroId::MacroRulesId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
|
|
|
|
MacroId::ProcMacroId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
|
|
|
|
},
|
2021-09-27 10:54:24 +00:00
|
|
|
AttrDefId::ImplId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
|
2021-03-19 20:23:57 +00:00
|
|
|
AttrDefId::GenericParamId(id) => match id {
|
2023-03-03 15:24:03 +00:00
|
|
|
GenericParamId::ConstParamId(id) => id
|
|
|
|
.parent()
|
|
|
|
.child_source(db)
|
|
|
|
.map(|source| ast::AnyHasAttrs::new(source[id.local_id()].clone())),
|
|
|
|
GenericParamId::TypeParamId(id) => id
|
|
|
|
.parent()
|
|
|
|
.child_source(db)
|
|
|
|
.map(|source| ast::AnyHasAttrs::new(source[id.local_id()].clone())),
|
2021-03-19 20:23:57 +00:00
|
|
|
GenericParamId::LifetimeParamId(id) => id
|
|
|
|
.parent
|
|
|
|
.child_source(db)
|
2021-09-27 10:54:24 +00:00
|
|
|
.map(|source| ast::AnyHasAttrs::new(source[id.local_id].clone())),
|
2021-03-19 20:23:57 +00:00
|
|
|
},
|
2021-12-07 16:31:26 +00:00
|
|
|
AttrDefId::ExternBlockId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
|
2021-03-19 20:23:57 +00:00
|
|
|
};
|
|
|
|
|
2021-09-27 10:54:24 +00:00
|
|
|
AttrSourceMap::new(owner.as_ref().map(|node| node as &dyn HasAttrs))
|
2021-03-19 20:23:57 +00:00
|
|
|
}
|
2021-03-30 15:20:43 +00:00
|
|
|
|
|
|
|
pub fn docs_with_rangemap(
|
|
|
|
&self,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
) -> Option<(Documentation, DocsRangeMap)> {
|
2022-03-09 13:33:39 +00:00
|
|
|
let docs =
|
|
|
|
self.by_key("doc").attrs().filter_map(|attr| attr.string_value().map(|s| (s, attr.id)));
|
|
|
|
let indent = doc_indent(self);
|
2021-03-30 15:20:43 +00:00
|
|
|
let mut buf = String::new();
|
|
|
|
let mut mapping = Vec::new();
|
|
|
|
for (doc, idx) in docs {
|
|
|
|
if !doc.is_empty() {
|
2021-05-04 11:51:57 +00:00
|
|
|
let mut base_offset = 0;
|
|
|
|
for raw_line in doc.split('\n') {
|
|
|
|
let line = raw_line.trim_end();
|
2021-03-30 20:26:03 +00:00
|
|
|
let line_len = line.len();
|
2021-03-30 15:20:43 +00: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 20:26:03 +00:00
|
|
|
TextRange::new(buf_offset.try_into().ok()?, buf.len().try_into().ok()?),
|
2021-03-30 15:20:43 +00:00
|
|
|
idx,
|
2021-05-04 11:51:57 +00:00
|
|
|
TextRange::at(
|
|
|
|
(base_offset + offset).try_into().ok()?,
|
|
|
|
line_len.try_into().ok()?,
|
|
|
|
),
|
2021-03-30 15:20:43 +00:00
|
|
|
));
|
|
|
|
buf.push('\n');
|
2021-05-04 11:51:57 +00:00
|
|
|
base_offset += raw_line.len() + 1;
|
2021-03-30 15:20:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buf.push('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf.pop();
|
|
|
|
if buf.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
2021-05-10 19:50:42 +00:00
|
|
|
Some((Documentation(buf), DocsRangeMap { mapping, source_map: self.source_map(db) }))
|
2021-03-30 15:20:43 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-19 20:23:57 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 13:33:39 +00:00
|
|
|
fn doc_indent(attrs: &Attrs) -> usize {
|
|
|
|
attrs
|
|
|
|
.by_key("doc")
|
|
|
|
.attrs()
|
|
|
|
.filter_map(|attr| attr.string_value())
|
|
|
|
.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)
|
|
|
|
}
|
|
|
|
|
2021-09-23 13:37:52 +00:00
|
|
|
#[derive(Debug)]
|
2021-03-17 10:22:40 +00:00
|
|
|
pub struct AttrSourceMap {
|
2022-01-07 13:14:33 +00:00
|
|
|
source: Vec<Either<ast::Attr, ast::Comment>>,
|
|
|
|
file_id: HirFileId,
|
2022-01-08 13:54:31 +00: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 10:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AttrSourceMap {
|
2021-09-27 10:54:24 +00:00
|
|
|
fn new(owner: InFile<&dyn ast::HasAttrs>) -> Self {
|
2022-01-07 13:14:33 +00:00
|
|
|
Self {
|
|
|
|
source: collect_attrs(owner.value).map(|(_, it)| it).collect(),
|
|
|
|
file_id: owner.file_id,
|
2022-01-08 13:54:31 +00:00
|
|
|
mod_def_site_file_id: None,
|
2021-05-10 19:50:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 13:54:31 +00: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 13:14:33 +00:00
|
|
|
self.source.extend(other.source);
|
2022-01-08 13:54:31 +00:00
|
|
|
if other.file_id != self.file_id {
|
|
|
|
self.mod_def_site_file_id = Some((other.file_id, len));
|
|
|
|
}
|
2021-05-10 19:50:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 10:22:40 +00: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 13:14:33 +00:00
|
|
|
pub fn source_of(&self, attr: &Attr) -> InFile<&Either<ast::Attr, ast::Comment>> {
|
2021-05-10 19:50:42 +00:00
|
|
|
self.source_of_id(attr.id)
|
|
|
|
}
|
|
|
|
|
2022-01-07 13:14:33 +00:00
|
|
|
fn source_of_id(&self, id: AttrId) -> InFile<&Either<ast::Attr, ast::Comment>> {
|
2023-01-09 19:47:51 +00:00
|
|
|
let ast_idx = id.ast_index();
|
2022-01-08 13:54:31 +00:00
|
|
|
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 13:14:33 +00:00
|
|
|
self.source
|
2022-01-08 13:54:31 +00:00
|
|
|
.get(ast_idx)
|
|
|
|
.map(|it| InFile::new(file_id, it))
|
2022-12-23 18:42:58 +00:00
|
|
|
.unwrap_or_else(|| panic!("cannot find attr at index {id:?}"))
|
2021-03-17 10:22:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 15:20:43 +00:00
|
|
|
/// A struct to map text ranges from [`Documentation`] back to TextRanges in the syntax tree.
|
2021-09-23 13:37:52 +00:00
|
|
|
#[derive(Debug)]
|
2021-03-30 15:20:43 +00:00
|
|
|
pub struct DocsRangeMap {
|
2021-05-10 19:50:42 +00:00
|
|
|
source_map: AttrSourceMap,
|
2021-03-30 15:20:43 +00: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 17:44:21 +00:00
|
|
|
mapping: Vec<(TextRange, AttrId, TextRange)>,
|
2021-03-30 15:20:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DocsRangeMap {
|
2021-09-23 15:32:39 +00:00
|
|
|
/// Maps a [`TextRange`] relative to the documentation string back to its AST range
|
2021-03-30 20:26:03 +00:00
|
|
|
pub fn map(&self, range: TextRange) -> Option<InFile<TextRange>> {
|
|
|
|
let found = self.mapping.binary_search_by(|(probe, ..)| probe.ordering(range)).ok()?;
|
2021-06-13 03:57:19 +00:00
|
|
|
let (line_docs_range, idx, original_line_src_range) = self.mapping[found];
|
2021-03-30 20:26:03 +00:00
|
|
|
if !line_docs_range.contains_range(range) {
|
2021-03-30 15:20:43 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-03-30 20:26:03 +00:00
|
|
|
let relative_range = range - line_docs_range.start();
|
2021-03-30 15:20:43 +00:00
|
|
|
|
2021-08-02 12:59:28 +00:00
|
|
|
let InFile { file_id, value: source } = self.source_map.source_of_id(idx);
|
2021-03-30 15:20:43 +00:00
|
|
|
match source {
|
2021-09-23 15:32:39 +00:00
|
|
|
Either::Left(attr) => {
|
2022-03-12 12:04:13 +00:00
|
|
|
let string = get_doc_string_in_attr(attr)?;
|
2021-09-23 15:32:39 +00:00
|
|
|
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 15:20:43 +00:00
|
|
|
Either::Right(comment) => {
|
|
|
|
let text_range = comment.syntax().text_range();
|
|
|
|
let range = TextRange::at(
|
|
|
|
text_range.start()
|
2021-03-30 20:26:03 +00: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 15:20:43 +00:00
|
|
|
);
|
|
|
|
Some(InFile { file_id, value: range })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-17 10:22:40 +00:00
|
|
|
}
|
|
|
|
|
2021-09-23 15:32:39 +00: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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-01 12:58:24 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2022-01-11 09:07:16 +00:00
|
|
|
pub struct AttrQuery<'attr> {
|
|
|
|
attrs: &'attr Attrs,
|
2019-11-24 13:03:02 +00:00
|
|
|
key: &'static str,
|
|
|
|
}
|
2019-10-30 13:12:55 +00:00
|
|
|
|
2022-01-11 09:07:16 +00:00
|
|
|
impl<'attr> AttrQuery<'attr> {
|
2023-01-31 10:49:49 +00:00
|
|
|
pub fn tt_values(self) -> impl Iterator<Item = &'attr crate::tt::Subtree> {
|
2022-03-09 13:33:39 +00:00
|
|
|
self.attrs().filter_map(|attr| attr.token_tree_value())
|
2019-10-30 13:12:55 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 09:07:16 +00:00
|
|
|
pub fn string_value(self) -> Option<&'attr SmolStr> {
|
2022-03-09 13:33:39 +00:00
|
|
|
self.attrs().find_map(|attr| attr.string_value())
|
2019-11-24 13:03:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn exists(self) -> bool {
|
|
|
|
self.attrs().next().is_some()
|
2019-10-30 13:12:55 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 09:07:16 +00:00
|
|
|
pub fn attrs(self) -> impl Iterator<Item = &'attr Attr> + Clone {
|
2019-11-24 13:03:02 +00:00
|
|
|
let key = self.key;
|
|
|
|
self.attrs
|
|
|
|
.iter()
|
2021-11-04 17:12:05 +00:00
|
|
|
.filter(move |attr| attr.path.as_ident().map_or(false, |s| s.to_smol_str() == key))
|
2019-10-30 13:12:55 +00:00
|
|
|
}
|
2022-05-04 15:41:29 +00:00
|
|
|
|
2022-05-05 04:41:33 +00:00
|
|
|
/// Find string value for a specific key inside token tree
|
|
|
|
///
|
|
|
|
/// ```ignore
|
|
|
|
/// #[doc(html_root_url = "url")]
|
|
|
|
/// ^^^^^^^^^^^^^ key
|
|
|
|
/// ```
|
2022-05-04 15:41:29 +00:00
|
|
|
pub fn find_string_value_in_tt(self, key: &'attr str) -> Option<&SmolStr> {
|
|
|
|
self.tt_values().find_map(|tt| {
|
|
|
|
let name = tt.token_trees.iter()
|
|
|
|
.skip_while(|tt| !matches!(tt, tt::TokenTree::Leaf(tt::Leaf::Ident(tt::Ident { text, ..} )) if text == key))
|
|
|
|
.nth(2);
|
|
|
|
|
|
|
|
match name {
|
|
|
|
Some(tt::TokenTree::Leaf(tt::Leaf::Literal(tt::Literal{ref text, ..}))) => Some(text),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-10-30 13:12:55 +00:00
|
|
|
}
|
2019-11-23 08:14:10 +00:00
|
|
|
|
2020-12-17 23:23:46 +00:00
|
|
|
fn attrs_from_item_tree<N: ItemTreeNode>(id: ItemTreeId<N>, db: &dyn DefDatabase) -> RawAttrs {
|
2021-03-12 23:34:01 +00:00
|
|
|
let tree = id.item_tree(db);
|
2020-06-22 17:15:54 +00:00
|
|
|
let mod_item = N::id_to_mod_item(id.value);
|
2020-12-17 23:23:46 +00:00
|
|
|
tree.raw_attrs(mod_item.into()).clone()
|
2019-11-23 08:14:10 +00:00
|
|
|
}
|
2020-12-19 14:15:02 +00:00
|
|
|
|
2021-04-06 20:25:44 +00: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)
|
|
|
|
}
|