aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-07-30 19:39:09 +0100
committerGitHub <[email protected]>2020-07-30 19:39:09 +0100
commit6f5b53013c54fa6d8429b717998cdb77a4d68b2c (patch)
treec09cf0434ecf282cf1763d34cf838952db105e0a /crates
parente28ea81b2b68a61b5c5eec3c815172b17256a25f (diff)
parentfbe60a2e284035d16c2a1ee743ee88db418689aa (diff)
Merge #5611
5611: Finalize attribute grammar r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/handlers/add_custom_impl.rs4
-rw-r--r--crates/ra_hir_def/src/attr.rs19
-rw-r--r--crates/ra_ide/src/completion/complete_attribute.rs10
-rw-r--r--crates/ra_syntax/src/ast/generated/nodes.rs77
-rw-r--r--crates/ra_syntax/src/ast/node_ext.rs28
5 files changed, 45 insertions, 93 deletions
diff --git a/crates/ra_assists/src/handlers/add_custom_impl.rs b/crates/ra_assists/src/handlers/add_custom_impl.rs
index acb07e36a..b67438b6b 100644
--- a/crates/ra_assists/src/handlers/add_custom_impl.rs
+++ b/crates/ra_assists/src/handlers/add_custom_impl.rs
@@ -29,8 +29,8 @@ use crate::{
29// } 29// }
30// ``` 30// ```
31pub(crate) fn add_custom_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 31pub(crate) fn add_custom_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
32 let input = ctx.find_node_at_offset::<ast::AttrInput>()?; 32 let attr = ctx.find_node_at_offset::<ast::Attr>()?;
33 let attr = input.syntax().parent().and_then(ast::Attr::cast)?; 33 let input = attr.token_tree()?;
34 34
35 let attr_name = attr 35 let attr_name = attr
36 .syntax() 36 .syntax()
diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs
index 70ccd4305..050832ce0 100644
--- a/crates/ra_hir_def/src/attr.rs
+++ b/crates/ra_hir_def/src/attr.rs
@@ -151,18 +151,15 @@ pub enum AttrInput {
151impl Attr { 151impl Attr {
152 fn from_src(ast: ast::Attr, hygiene: &Hygiene) -> Option<Attr> { 152 fn from_src(ast: ast::Attr, hygiene: &Hygiene) -> Option<Attr> {
153 let path = ModPath::from_src(ast.path()?, hygiene)?; 153 let path = ModPath::from_src(ast.path()?, hygiene)?;
154 let input = match ast.input() { 154 let input = if let Some(lit) = ast.literal() {
155 None => None, 155 // FIXME: escape? raw string?
156 Some(ast::AttrInput::Literal(lit)) => { 156 let value = lit.syntax().first_token()?.text().trim_matches('"').into();
157 // FIXME: escape? raw string? 157 Some(AttrInput::Literal(value))
158 let value = lit.syntax().first_token()?.text().trim_matches('"').into(); 158 } else if let Some(tt) = ast.token_tree() {
159 Some(AttrInput::Literal(value)) 159 Some(AttrInput::TokenTree(ast_to_token_tree(&tt)?.0))
160 } 160 } else {
161 Some(ast::AttrInput::TokenTree(tt)) => { 161 None
162 Some(AttrInput::TokenTree(ast_to_token_tree(&tt)?.0))
163 }
164 }; 162 };
165
166 Some(Attr { path, input }) 163 Some(Attr { path, input })
167 } 164 }
168} 165}
diff --git a/crates/ra_ide/src/completion/complete_attribute.rs b/crates/ra_ide/src/completion/complete_attribute.rs
index 109c5e9a8..2faaae974 100644
--- a/crates/ra_ide/src/completion/complete_attribute.rs
+++ b/crates/ra_ide/src/completion/complete_attribute.rs
@@ -13,20 +13,18 @@ use crate::completion::{
13 13
14pub(super) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { 14pub(super) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
15 let attribute = ctx.attribute_under_caret.as_ref()?; 15 let attribute = ctx.attribute_under_caret.as_ref()?;
16 match (attribute.path(), attribute.input()) { 16 match (attribute.path(), attribute.token_tree()) {
17 (Some(path), Some(ast::AttrInput::TokenTree(token_tree))) 17 (Some(path), Some(token_tree)) if path.to_string() == "derive" => {
18 if path.to_string() == "derive" =>
19 {
20 complete_derive(acc, ctx, token_tree) 18 complete_derive(acc, ctx, token_tree)
21 } 19 }
22 (Some(path), Some(ast::AttrInput::TokenTree(token_tree))) 20 (Some(path), Some(token_tree))
23 if ["allow", "warn", "deny", "forbid"] 21 if ["allow", "warn", "deny", "forbid"]
24 .iter() 22 .iter()
25 .any(|lint_level| lint_level == &path.to_string()) => 23 .any(|lint_level| lint_level == &path.to_string()) =>
26 { 24 {
27 complete_lint(acc, ctx, token_tree) 25 complete_lint(acc, ctx, token_tree)
28 } 26 }
29 (_, Some(ast::AttrInput::TokenTree(_token_tree))) => {} 27 (_, Some(_token_tree)) => {}
30 _ => complete_attribute_start(acc, ctx, attribute), 28 _ => complete_attribute_start(acc, ctx, attribute),
31 } 29 }
32 Some(()) 30 Some(())
diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs
index e898c9181..c20ff53bf 100644
--- a/crates/ra_syntax/src/ast/generated/nodes.rs
+++ b/crates/ra_syntax/src/ast/generated/nodes.rs
@@ -24,7 +24,8 @@ impl Attr {
24 pub fn l_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['[']) } 24 pub fn l_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['[']) }
25 pub fn path(&self) -> Option<Path> { support::child(&self.syntax) } 25 pub fn path(&self) -> Option<Path> { support::child(&self.syntax) }
26 pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) } 26 pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
27 pub fn input(&self) -> Option<AttrInput> { support::child(&self.syntax) } 27 pub fn literal(&self) -> Option<Literal> { support::child(&self.syntax) }
28 pub fn token_tree(&self) -> Option<TokenTree> { support::child(&self.syntax) }
28 pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) } 29 pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) }
29} 30}
30#[derive(Debug, Clone, PartialEq, Eq, Hash)] 31#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -321,9 +322,9 @@ pub struct ParamList {
321} 322}
322impl ParamList { 323impl ParamList {
323 pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) } 324 pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) }
324 pub fn params(&self) -> AstChildren<Param> { support::children(&self.syntax) }
325 pub fn self_param(&self) -> Option<SelfParam> { support::child(&self.syntax) } 325 pub fn self_param(&self) -> Option<SelfParam> { support::child(&self.syntax) }
326 pub fn comma_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![,]) } 326 pub fn comma_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![,]) }
327 pub fn params(&self) -> AstChildren<Param> { support::children(&self.syntax) }
327 pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) } 328 pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) }
328} 329}
329#[derive(Debug, Clone, PartialEq, Eq, Hash)] 330#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -356,17 +357,6 @@ impl BlockExpr {
356 pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) } 357 pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) }
357} 358}
358#[derive(Debug, Clone, PartialEq, Eq, Hash)] 359#[derive(Debug, Clone, PartialEq, Eq, Hash)]
359pub struct Param {
360 pub(crate) syntax: SyntaxNode,
361}
362impl ast::AttrsOwner for Param {}
363impl ast::TypeAscriptionOwner for Param {}
364impl Param {
365 pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) }
366 pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) }
367 pub fn dotdotdot_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![...]) }
368}
369#[derive(Debug, Clone, PartialEq, Eq, Hash)]
370pub struct SelfParam { 360pub struct SelfParam {
371 pub(crate) syntax: SyntaxNode, 361 pub(crate) syntax: SyntaxNode,
372} 362}
@@ -382,6 +372,17 @@ impl SelfParam {
382 pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) } 372 pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) }
383} 373}
384#[derive(Debug, Clone, PartialEq, Eq, Hash)] 374#[derive(Debug, Clone, PartialEq, Eq, Hash)]
375pub struct Param {
376 pub(crate) syntax: SyntaxNode,
377}
378impl ast::AttrsOwner for Param {}
379impl ast::TypeAscriptionOwner for Param {}
380impl Param {
381 pub fn pat(&self) -> Option<Pat> { support::child(&self.syntax) }
382 pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) }
383 pub fn dotdotdot_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![...]) }
384}
385#[derive(Debug, Clone, PartialEq, Eq, Hash)]
385pub struct TypeBoundList { 386pub struct TypeBoundList {
386 pub(crate) syntax: SyntaxNode, 387 pub(crate) syntax: SyntaxNode,
387} 388}
@@ -1378,11 +1379,6 @@ pub enum GenericParam {
1378} 1379}
1379impl ast::AttrsOwner for GenericParam {} 1380impl ast::AttrsOwner for GenericParam {}
1380#[derive(Debug, Clone, PartialEq, Eq, Hash)] 1381#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1381pub enum AttrInput {
1382 Literal(Literal),
1383 TokenTree(TokenTree),
1384}
1385#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1386pub enum Stmt { 1382pub enum Stmt {
1387 LetStmt(LetStmt), 1383 LetStmt(LetStmt),
1388 ExprStmt(ExprStmt), 1384 ExprStmt(ExprStmt),
@@ -1728,8 +1724,8 @@ impl AstNode for BlockExpr {
1728 } 1724 }
1729 fn syntax(&self) -> &SyntaxNode { &self.syntax } 1725 fn syntax(&self) -> &SyntaxNode { &self.syntax }
1730} 1726}
1731impl AstNode for Param { 1727impl AstNode for SelfParam {
1732 fn can_cast(kind: SyntaxKind) -> bool { kind == PARAM } 1728 fn can_cast(kind: SyntaxKind) -> bool { kind == SELF_PARAM }
1733 fn cast(syntax: SyntaxNode) -> Option<Self> { 1729 fn cast(syntax: SyntaxNode) -> Option<Self> {
1734 if Self::can_cast(syntax.kind()) { 1730 if Self::can_cast(syntax.kind()) {
1735 Some(Self { syntax }) 1731 Some(Self { syntax })
@@ -1739,8 +1735,8 @@ impl AstNode for Param {
1739 } 1735 }
1740 fn syntax(&self) -> &SyntaxNode { &self.syntax } 1736 fn syntax(&self) -> &SyntaxNode { &self.syntax }
1741} 1737}
1742impl AstNode for SelfParam { 1738impl AstNode for Param {
1743 fn can_cast(kind: SyntaxKind) -> bool { kind == SELF_PARAM } 1739 fn can_cast(kind: SyntaxKind) -> bool { kind == PARAM }
1744 fn cast(syntax: SyntaxNode) -> Option<Self> { 1740 fn cast(syntax: SyntaxNode) -> Option<Self> {
1745 if Self::can_cast(syntax.kind()) { 1741 if Self::can_cast(syntax.kind()) {
1746 Some(Self { syntax }) 1742 Some(Self { syntax })
@@ -3342,34 +3338,6 @@ impl AstNode for GenericParam {
3342 } 3338 }
3343 } 3339 }
3344} 3340}
3345impl From<Literal> for AttrInput {
3346 fn from(node: Literal) -> AttrInput { AttrInput::Literal(node) }
3347}
3348impl From<TokenTree> for AttrInput {
3349 fn from(node: TokenTree) -> AttrInput { AttrInput::TokenTree(node) }
3350}
3351impl AstNode for AttrInput {
3352 fn can_cast(kind: SyntaxKind) -> bool {
3353 match kind {
3354 LITERAL | TOKEN_TREE => true,
3355 _ => false,
3356 }
3357 }
3358 fn cast(syntax: SyntaxNode) -> Option<Self> {
3359 let res = match syntax.kind() {
3360 LITERAL => AttrInput::Literal(Literal { syntax }),
3361 TOKEN_TREE => AttrInput::TokenTree(TokenTree { syntax }),
3362 _ => return None,
3363 };
3364 Some(res)
3365 }
3366 fn syntax(&self) -> &SyntaxNode {
3367 match self {
3368 AttrInput::Literal(it) => &it.syntax,
3369 AttrInput::TokenTree(it) => &it.syntax,
3370 }
3371 }
3372}
3373impl From<LetStmt> for Stmt { 3341impl From<LetStmt> for Stmt {
3374 fn from(node: LetStmt) -> Stmt { Stmt::LetStmt(node) } 3342 fn from(node: LetStmt) -> Stmt { Stmt::LetStmt(node) }
3375} 3343}
@@ -3471,11 +3439,6 @@ impl std::fmt::Display for GenericParam {
3471 std::fmt::Display::fmt(self.syntax(), f) 3439 std::fmt::Display::fmt(self.syntax(), f)
3472 } 3440 }
3473} 3441}
3474impl std::fmt::Display for AttrInput {
3475 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3476 std::fmt::Display::fmt(self.syntax(), f)
3477 }
3478}
3479impl std::fmt::Display for Stmt { 3442impl std::fmt::Display for Stmt {
3480 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 3443 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3481 std::fmt::Display::fmt(self.syntax(), f) 3444 std::fmt::Display::fmt(self.syntax(), f)
@@ -3636,12 +3599,12 @@ impl std::fmt::Display for BlockExpr {
3636 std::fmt::Display::fmt(self.syntax(), f) 3599 std::fmt::Display::fmt(self.syntax(), f)
3637 } 3600 }
3638} 3601}
3639impl std::fmt::Display for Param { 3602impl std::fmt::Display for SelfParam {
3640 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 3603 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3641 std::fmt::Display::fmt(self.syntax(), f) 3604 std::fmt::Display::fmt(self.syntax(), f)
3642 } 3605 }
3643} 3606}
3644impl std::fmt::Display for SelfParam { 3607impl std::fmt::Display for Param {
3645 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 3608 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3646 std::fmt::Display::fmt(self.syntax(), f) 3609 std::fmt::Display::fmt(self.syntax(), f)
3647 } 3610 }
diff --git a/crates/ra_syntax/src/ast/node_ext.rs b/crates/ra_syntax/src/ast/node_ext.rs
index d2ee9586d..bba7310ad 100644
--- a/crates/ra_syntax/src/ast/node_ext.rs
+++ b/crates/ra_syntax/src/ast/node_ext.rs
@@ -7,7 +7,7 @@ use itertools::Itertools;
7use ra_parser::SyntaxKind; 7use ra_parser::SyntaxKind;
8 8
9use crate::{ 9use crate::{
10 ast::{self, support, AstNode, AttrInput, NameOwner, SyntaxNode}, 10 ast::{self, support, AstNode, NameOwner, SyntaxNode},
11 SmolStr, SyntaxElement, SyntaxToken, T, 11 SmolStr, SyntaxElement, SyntaxToken, T,
12}; 12};
13 13
@@ -39,29 +39,23 @@ pub enum AttrKind {
39 39
40impl ast::Attr { 40impl ast::Attr {
41 pub fn as_simple_atom(&self) -> Option<SmolStr> { 41 pub fn as_simple_atom(&self) -> Option<SmolStr> {
42 match self.input() { 42 if self.eq_token().is_some() || self.token_tree().is_some() {
43 None => self.simple_name(), 43 return None;
44 Some(_) => None,
45 } 44 }
45 self.simple_name()
46 } 46 }
47 47
48 pub fn as_simple_call(&self) -> Option<(SmolStr, ast::TokenTree)> { 48 pub fn as_simple_call(&self) -> Option<(SmolStr, ast::TokenTree)> {
49 match self.input() { 49 let tt = self.token_tree()?;
50 Some(AttrInput::TokenTree(tt)) => Some((self.simple_name()?, tt)), 50 Some((self.simple_name()?, tt))
51 _ => None,
52 }
53 } 51 }
54 52
55 pub fn as_simple_key_value(&self) -> Option<(SmolStr, SmolStr)> { 53 pub fn as_simple_key_value(&self) -> Option<(SmolStr, SmolStr)> {
56 match self.input() { 54 let lit = self.literal()?;
57 Some(AttrInput::Literal(lit)) => { 55 let key = self.simple_name()?;
58 let key = self.simple_name()?; 56 // FIXME: escape? raw string?
59 // FIXME: escape? raw string? 57 let value = lit.syntax().first_token()?.text().trim_matches('"').into();
60 let value = lit.syntax().first_token()?.text().trim_matches('"').into(); 58 Some((key, value))
61 Some((key, value))
62 }
63 _ => None,
64 }
65 } 59 }
66 60
67 pub fn simple_name(&self) -> Option<SmolStr> { 61 pub fn simple_name(&self) -> Option<SmolStr> {