aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/attr.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/attr.rs')
-rw-r--r--crates/ra_hir_def/src/attr.rs84
1 files changed, 84 insertions, 0 deletions
diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs
new file mode 100644
index 000000000..0e961ca12
--- /dev/null
+++ b/crates/ra_hir_def/src/attr.rs
@@ -0,0 +1,84 @@
1//! A higher level attributes based on TokenTree, with also some shortcuts.
2
3use std::sync::Arc;
4
5use hir_expand::hygiene::Hygiene;
6use mbe::ast_to_token_tree;
7use ra_cfg::CfgOptions;
8use ra_syntax::{
9 ast::{self, AstNode, AttrsOwner},
10 SmolStr,
11};
12use tt::Subtree;
13
14use crate::path::Path;
15
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct Attr {
18 pub(crate) path: Path,
19 pub(crate) input: Option<AttrInput>,
20}
21
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub enum AttrInput {
24 Literal(SmolStr),
25 TokenTree(Subtree),
26}
27
28impl Attr {
29 pub(crate) fn from_src(ast: ast::Attr, hygiene: &Hygiene) -> Option<Attr> {
30 let path = Path::from_src(ast.path()?, hygiene)?;
31 let input = match ast.input() {
32 None => None,
33 Some(ast::AttrInput::Literal(lit)) => {
34 // FIXME: escape? raw string?
35 let value = lit.syntax().first_token()?.text().trim_matches('"').into();
36 Some(AttrInput::Literal(value))
37 }
38 Some(ast::AttrInput::TokenTree(tt)) => {
39 Some(AttrInput::TokenTree(ast_to_token_tree(&tt)?.0))
40 }
41 };
42
43 Some(Attr { path, input })
44 }
45
46 pub fn from_attrs_owner(owner: &dyn AttrsOwner, hygiene: &Hygiene) -> Option<Arc<[Attr]>> {
47 let mut attrs = owner.attrs().peekable();
48 if attrs.peek().is_none() {
49 // Avoid heap allocation
50 return None;
51 }
52 Some(attrs.flat_map(|ast| Attr::from_src(ast, hygiene)).collect())
53 }
54
55 pub fn is_simple_atom(&self, name: &str) -> bool {
56 // FIXME: Avoid cloning
57 self.path.as_ident().map_or(false, |s| s.to_string() == name)
58 }
59
60 // FIXME: handle cfg_attr :-)
61 pub fn as_cfg(&self) -> Option<&Subtree> {
62 if !self.is_simple_atom("cfg") {
63 return None;
64 }
65 match &self.input {
66 Some(AttrInput::TokenTree(subtree)) => Some(subtree),
67 _ => None,
68 }
69 }
70
71 pub fn as_path(&self) -> Option<&SmolStr> {
72 if !self.is_simple_atom("path") {
73 return None;
74 }
75 match &self.input {
76 Some(AttrInput::Literal(it)) => Some(it),
77 _ => None,
78 }
79 }
80
81 pub fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> Option<bool> {
82 cfg_options.is_cfg_enabled(self.as_cfg()?)
83 }
84}