aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authoruHOOCCOOHu <[email protected]>2019-09-29 22:15:03 +0100
committeruHOOCCOOHu <[email protected]>2019-09-30 09:17:53 +0100
commit5a4b4f507e9b90bfe41b451763868cba0a70c392 (patch)
tree7e136a69aa5bf67a2dacf74fdee189558970ff20 /crates
parent71efdaa6364142b359c59659ec10f35a1e53b5d2 (diff)
Fix API of Attr
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/assists/add_derive.rs2
-rw-r--r--crates/ra_hir/src/lang_item.rs2
-rw-r--r--crates/ra_hir/src/nameres/raw.rs5
-rw-r--r--crates/ra_ide_api/src/display/structure.rs2
-rw-r--r--crates/ra_ide_api/src/snapshots/highlighting.html2
-rw-r--r--crates/ra_syntax/src/ast/extensions.rs66
-rw-r--r--crates/ra_syntax/src/ast/generated.rs3
-rw-r--r--crates/ra_syntax/src/ast/traits.rs2
-rw-r--r--crates/ra_syntax/src/grammar.ron2
9 files changed, 32 insertions, 54 deletions
diff --git a/crates/ra_assists/src/assists/add_derive.rs b/crates/ra_assists/src/assists/add_derive.rs
index 9c88644df..8f2c6266e 100644
--- a/crates/ra_assists/src/assists/add_derive.rs
+++ b/crates/ra_assists/src/assists/add_derive.rs
@@ -13,7 +13,7 @@ pub(crate) fn add_derive(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist>
13 ctx.add_action(AssistId("add_derive"), "add `#[derive]`", |edit| { 13 ctx.add_action(AssistId("add_derive"), "add `#[derive]`", |edit| {
14 let derive_attr = nominal 14 let derive_attr = nominal
15 .attrs() 15 .attrs()
16 .filter_map(|x| x.as_call()) 16 .filter_map(|x| x.as_simple_call())
17 .filter(|(name, _arg)| name == "derive") 17 .filter(|(name, _arg)| name == "derive")
18 .map(|(_name, arg)| arg) 18 .map(|(_name, arg)| arg)
19 .next(); 19 .next();
diff --git a/crates/ra_hir/src/lang_item.rs b/crates/ra_hir/src/lang_item.rs
index bcce314d8..dbba433fe 100644
--- a/crates/ra_hir/src/lang_item.rs
+++ b/crates/ra_hir/src/lang_item.rs
@@ -151,7 +151,7 @@ impl LangItems {
151 151
152fn lang_item_name<T: AttrsOwner>(node: &T) -> Option<SmolStr> { 152fn lang_item_name<T: AttrsOwner>(node: &T) -> Option<SmolStr> {
153 node.attrs() 153 node.attrs()
154 .filter_map(|a| a.as_key_value()) 154 .filter_map(|a| a.as_simple_key_value())
155 .filter(|(key, _)| key == "lang") 155 .filter(|(key, _)| key == "lang")
156 .map(|(_, val)| val) 156 .map(|(_, val)| val)
157 .nth(0) 157 .nth(0)
diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs
index c494b95b0..0e27dd2db 100644
--- a/crates/ra_hir/src/nameres/raw.rs
+++ b/crates/ra_hir/src/nameres/raw.rs
@@ -353,8 +353,7 @@ impl<DB: AstDatabase> RawItemsCollector<&DB> {
353 353
354 let name = m.name().map(|it| it.as_name()); 354 let name = m.name().map(|it| it.as_name());
355 let ast_id = self.source_ast_id_map.ast_id(&m); 355 let ast_id = self.source_ast_id_map.ast_id(&m);
356 let export = m.has_atom_attr("macro_export") 356 let export = m.attrs().filter_map(|x| x.simple_name()).any(|name| name == "macro_export");
357 || m.attrs().filter_map(|x| x.as_call()).any(|(name, _)| name == "macro_export");
358 357
359 let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export }); 358 let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export });
360 self.push_item(current_module, RawItem::Macro(m)); 359 self.push_item(current_module, RawItem::Macro(m));
@@ -385,7 +384,7 @@ impl<DB: AstDatabase> RawItemsCollector<&DB> {
385 384
386fn extract_mod_path_attribute(module: &ast::Module) -> Option<SmolStr> { 385fn extract_mod_path_attribute(module: &ast::Module) -> Option<SmolStr> {
387 module.attrs().into_iter().find_map(|attr| { 386 module.attrs().into_iter().find_map(|attr| {
388 attr.as_key_value().and_then(|(name, value)| { 387 attr.as_simple_key_value().and_then(|(name, value)| {
389 let is_path = name == "path"; 388 let is_path = name == "path";
390 if is_path { 389 if is_path {
391 Some(value) 390 Some(value)
diff --git a/crates/ra_ide_api/src/display/structure.rs b/crates/ra_ide_api/src/display/structure.rs
index be042ed17..38a56d752 100644
--- a/crates/ra_ide_api/src/display/structure.rs
+++ b/crates/ra_ide_api/src/display/structure.rs
@@ -77,7 +77,7 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
77 node_range: node.syntax().text_range(), 77 node_range: node.syntax().text_range(),
78 kind: node.syntax().kind(), 78 kind: node.syntax().kind(),
79 detail, 79 detail,
80 deprecated: node.attrs().filter_map(|x| x.as_named()).any(|x| x == "deprecated"), 80 deprecated: node.attrs().filter_map(|x| x.simple_name()).any(|x| x == "deprecated"),
81 }) 81 })
82 } 82 }
83 83
diff --git a/crates/ra_ide_api/src/snapshots/highlighting.html b/crates/ra_ide_api/src/snapshots/highlighting.html
index b39c4d371..ae30ebba3 100644
--- a/crates/ra_ide_api/src/snapshots/highlighting.html
+++ b/crates/ra_ide_api/src/snapshots/highlighting.html
@@ -19,7 +19,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
19.keyword\.unsafe { color: #DFAF8F; } 19.keyword\.unsafe { color: #DFAF8F; }
20.keyword\.control { color: #F0DFAF; font-weight: bold; } 20.keyword\.control { color: #F0DFAF; font-weight: bold; }
21</style> 21</style>
22<pre><code><span class="attribute">#</span><span class="attribute">[</span><span class="attribute">derive</span><span class="attribute">(</span><span class="attribute">Clone</span><span class="attribute">,</span><span class="attribute"> </span><span class="attribute">Debug</span><span class="attribute">)</span><span class="attribute">]</span> 22<pre><code><span class="attribute">#</span><span class="attribute">[</span><span class="attribute text">derive</span><span class="attribute">(</span><span class="attribute">Clone</span><span class="attribute">,</span><span class="attribute"> </span><span class="attribute">Debug</span><span class="attribute">)</span><span class="attribute">]</span>
23<span class="keyword">struct</span> <span class="type">Foo</span> { 23<span class="keyword">struct</span> <span class="type">Foo</span> {
24 <span class="keyword">pub</span> <span class="field">x</span>: <span class="type">i32</span>, 24 <span class="keyword">pub</span> <span class="field">x</span>: <span class="type">i32</span>,
25 <span class="keyword">pub</span> <span class="field">y</span>: <span class="type">i32</span>, 25 <span class="keyword">pub</span> <span class="field">y</span>: <span class="type">i32</span>,
diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs
index 8c5ece65d..a7b886457 100644
--- a/crates/ra_syntax/src/ast/extensions.rs
+++ b/crates/ra_syntax/src/ast/extensions.rs
@@ -1,10 +1,8 @@
1//! Various extension methods to ast Nodes, which are hard to code-generate. 1//! Various extension methods to ast Nodes, which are hard to code-generate.
2//! Extensions for various expressions live in a sibling `expr_extensions` module. 2//! Extensions for various expressions live in a sibling `expr_extensions` module.
3 3
4use itertools::Itertools;
5
6use crate::{ 4use crate::{
7 ast::{self, child_opt, children, AstChildren, AstNode, SyntaxNode}, 5 ast::{self, child_opt, children, AstChildren, AstNode, AttrInput, SyntaxNode},
8 SmolStr, SyntaxElement, 6 SmolStr, SyntaxElement,
9 SyntaxKind::*, 7 SyntaxKind::*,
10 SyntaxToken, T, 8 SyntaxToken, T,
@@ -39,12 +37,7 @@ fn text_of_first_token(node: &SyntaxNode) -> &SmolStr {
39 37
40impl ast::Attr { 38impl ast::Attr {
41 pub fn is_inner(&self) -> bool { 39 pub fn is_inner(&self) -> bool {
42 let tt = match self.value() { 40 let prev = match self.syntax().prev_sibling() {
43 None => return false,
44 Some(tt) => tt,
45 };
46
47 let prev = match tt.syntax().prev_sibling() {
48 None => return false, 41 None => return false,
49 Some(prev) => prev, 42 Some(prev) => prev,
50 }; 43 };
@@ -52,48 +45,37 @@ impl ast::Attr {
52 prev.kind() == T![!] 45 prev.kind() == T![!]
53 } 46 }
54 47
55 pub fn as_atom(&self) -> Option<SmolStr> { 48 pub fn as_simple_atom(&self) -> Option<SmolStr> {
56 let tt = self.value()?; 49 match self.input() {
57 let (_bra, attr, _ket) = tt.syntax().children_with_tokens().collect_tuple()?; 50 None => self.simple_name(),
58 if attr.kind() == IDENT { 51 Some(_) => None,
59 Some(attr.as_token()?.text().clone())
60 } else {
61 None
62 } 52 }
63 } 53 }
64 54
65 pub fn as_call(&self) -> Option<(SmolStr, ast::TokenTree)> { 55 pub fn as_simple_call(&self) -> Option<(SmolStr, ast::TokenTree)> {
66 let tt = self.value()?; 56 match self.input() {
67 let (_bra, attr, args, _ket) = tt.syntax().children_with_tokens().collect_tuple()?; 57 Some(AttrInput::TokenTree(tt)) => Some((self.simple_name()?, tt)),
68 let args = ast::TokenTree::cast(args.as_node()?.clone())?; 58 _ => None,
69 if attr.kind() == IDENT {
70 Some((attr.as_token()?.text().clone(), args))
71 } else {
72 None
73 } 59 }
74 } 60 }
75 61
76 pub fn as_named(&self) -> Option<SmolStr> { 62 pub fn as_simple_key_value(&self) -> Option<(SmolStr, SmolStr)> {
77 let tt = self.value()?; 63 match self.input() {
78 let attr = tt.syntax().children_with_tokens().nth(1)?; 64 Some(AttrInput::Literal(lit)) => {
79 if attr.kind() == IDENT { 65 let key = self.simple_name()?;
80 Some(attr.as_token()?.text().clone()) 66 // FIXME: escape? raw string?
81 } else { 67 let value = lit.syntax().first_token()?.text().trim_matches('"').into();
82 None 68 Some((key, value))
69 }
70 _ => None,
83 } 71 }
84 } 72 }
85 73
86 pub fn as_key_value(&self) -> Option<(SmolStr, SmolStr)> { 74 pub fn simple_name(&self) -> Option<SmolStr> {
87 let tt = self.value()?; 75 let path = self.path()?;
88 let tt_node = tt.syntax(); 76 match (path.segment(), path.qualifier()) {
89 let attr = tt_node.children_with_tokens().nth(1)?; 77 (Some(segment), None) => Some(segment.syntax().first_token()?.text().clone()),
90 if attr.kind() == IDENT { 78 _ => None,
91 let key = attr.as_token()?.text().clone();
92 let val_node = tt_node.children_with_tokens().find(|t| t.kind() == STRING)?;
93 let val = val_node.as_token()?.text().trim_start_matches('"').trim_end_matches('"');
94 Some((key, SmolStr::new(val)))
95 } else {
96 None
97 } 79 }
98 } 80 }
99} 81}
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs
index 408449fd6..aaf03ce3f 100644
--- a/crates/ra_syntax/src/ast/generated.rs
+++ b/crates/ra_syntax/src/ast/generated.rs
@@ -172,9 +172,6 @@ impl Attr {
172 pub fn input(&self) -> Option<AttrInput> { 172 pub fn input(&self) -> Option<AttrInput> {
173 AstChildren::new(&self.syntax).next() 173 AstChildren::new(&self.syntax).next()
174 } 174 }
175 pub fn value(&self) -> Option<TokenTree> {
176 AstChildren::new(&self.syntax).next()
177 }
178} 175}
179#[derive(Debug, Clone, PartialEq, Eq, Hash)] 176#[derive(Debug, Clone, PartialEq, Eq, Hash)]
180pub enum AttrInput { 177pub enum AttrInput {
diff --git a/crates/ra_syntax/src/ast/traits.rs b/crates/ra_syntax/src/ast/traits.rs
index c3e676d4c..f275a4955 100644
--- a/crates/ra_syntax/src/ast/traits.rs
+++ b/crates/ra_syntax/src/ast/traits.rs
@@ -99,7 +99,7 @@ pub trait AttrsOwner: AstNode {
99 children(self) 99 children(self)
100 } 100 }
101 fn has_atom_attr(&self, atom: &str) -> bool { 101 fn has_atom_attr(&self, atom: &str) -> bool {
102 self.attrs().filter_map(|x| x.as_atom()).any(|x| x == atom) 102 self.attrs().filter_map(|x| x.as_simple_atom()).any(|x| x == atom)
103 } 103 }
104} 104}
105 105
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron
index 8cb45f394..30328f59f 100644
--- a/crates/ra_syntax/src/grammar.ron
+++ b/crates/ra_syntax/src/grammar.ron
@@ -577,7 +577,7 @@ Grammar(
577 options: [ "TokenTree", "Path" ], 577 options: [ "TokenTree", "Path" ],
578 ), 578 ),
579 "AttrInput": ( enum: [ "Literal", "TokenTree" ] ), 579 "AttrInput": ( enum: [ "Literal", "TokenTree" ] ),
580 "Attr": ( options: [ "Path", [ "input", "AttrInput" ], [ "value", "TokenTree" ] ] ), 580 "Attr": ( options: [ "Path", [ "input", "AttrInput" ] ] ),
581 "TokenTree": (), 581 "TokenTree": (),
582 "TypeParamList": ( 582 "TypeParamList": (
583 collections: [ 583 collections: [