diff options
Diffstat (limited to 'crates/ra_syntax/src/ast')
-rw-r--r-- | crates/ra_syntax/src/ast/expr_extensions.rs | 26 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/generated.rs | 4 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/traits.rs | 2 |
3 files changed, 26 insertions, 6 deletions
diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index d7ea4354d..25dbd0bed 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs | |||
@@ -9,12 +9,12 @@ use crate::{ | |||
9 | 9 | ||
10 | #[derive(Debug, Clone, PartialEq, Eq)] | 10 | #[derive(Debug, Clone, PartialEq, Eq)] |
11 | pub enum ElseBranch { | 11 | pub enum ElseBranch { |
12 | Block(ast::Block), | 12 | Block(ast::BlockExpr), |
13 | IfExpr(ast::IfExpr), | 13 | IfExpr(ast::IfExpr), |
14 | } | 14 | } |
15 | 15 | ||
16 | impl ast::IfExpr { | 16 | impl ast::IfExpr { |
17 | pub fn then_branch(&self) -> Option<ast::Block> { | 17 | pub fn then_branch(&self) -> Option<ast::BlockExpr> { |
18 | self.blocks().nth(0) | 18 | self.blocks().nth(0) |
19 | } | 19 | } |
20 | pub fn else_branch(&self) -> Option<ElseBranch> { | 20 | pub fn else_branch(&self) -> Option<ElseBranch> { |
@@ -28,7 +28,7 @@ impl ast::IfExpr { | |||
28 | Some(res) | 28 | Some(res) |
29 | } | 29 | } |
30 | 30 | ||
31 | fn blocks(&self) -> AstChildren<ast::Block> { | 31 | fn blocks(&self) -> AstChildren<ast::BlockExpr> { |
32 | children(self) | 32 | children(self) |
33 | } | 33 | } |
34 | } | 34 | } |
@@ -289,6 +289,26 @@ impl ast::Literal { | |||
289 | } | 289 | } |
290 | } | 290 | } |
291 | 291 | ||
292 | impl ast::BlockExpr { | ||
293 | /// false if the block is an intrinsic part of the syntax and can't be | ||
294 | /// replaced with arbitrary expression. | ||
295 | /// | ||
296 | /// ```not_rust | ||
297 | /// fn foo() { not_stand_alone } | ||
298 | /// const FOO: () = { stand_alone }; | ||
299 | /// ``` | ||
300 | pub fn is_standalone(&self) -> bool { | ||
301 | let kind = match self.syntax().parent() { | ||
302 | None => return true, | ||
303 | Some(it) => it.kind(), | ||
304 | }; | ||
305 | match kind { | ||
306 | FN_DEF | MATCH_ARM | IF_EXPR | WHILE_EXPR | LOOP_EXPR | TRY_BLOCK_EXPR => false, | ||
307 | _ => true, | ||
308 | } | ||
309 | } | ||
310 | } | ||
311 | |||
292 | #[test] | 312 | #[test] |
293 | fn test_literal_with_attr() { | 313 | fn test_literal_with_attr() { |
294 | let parse = ast::SourceFile::parse(r#"const _: &str = { #[attr] "Hello" };"#); | 314 | let parse = ast::SourceFile::parse(r#"const _: &str = { #[attr] "Hello" };"#); |
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 7f91417c5..e2a92ae60 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs | |||
@@ -1003,7 +1003,7 @@ impl FnDef { | |||
1003 | pub fn param_list(&self) -> Option<ParamList> { | 1003 | pub fn param_list(&self) -> Option<ParamList> { |
1004 | AstChildren::new(&self.syntax).next() | 1004 | AstChildren::new(&self.syntax).next() |
1005 | } | 1005 | } |
1006 | pub fn body(&self) -> Option<Block> { | 1006 | pub fn body(&self) -> Option<BlockExpr> { |
1007 | AstChildren::new(&self.syntax).next() | 1007 | AstChildren::new(&self.syntax).next() |
1008 | } | 1008 | } |
1009 | pub fn ret_type(&self) -> Option<RetType> { | 1009 | pub fn ret_type(&self) -> Option<RetType> { |
@@ -3135,7 +3135,7 @@ impl AstNode for TryBlockExpr { | |||
3135 | } | 3135 | } |
3136 | } | 3136 | } |
3137 | impl TryBlockExpr { | 3137 | impl TryBlockExpr { |
3138 | pub fn block(&self) -> Option<Block> { | 3138 | pub fn body(&self) -> Option<BlockExpr> { |
3139 | AstChildren::new(&self.syntax).next() | 3139 | AstChildren::new(&self.syntax).next() |
3140 | } | 3140 | } |
3141 | } | 3141 | } |
diff --git a/crates/ra_syntax/src/ast/traits.rs b/crates/ra_syntax/src/ast/traits.rs index 20c251fba..c3e676d4c 100644 --- a/crates/ra_syntax/src/ast/traits.rs +++ b/crates/ra_syntax/src/ast/traits.rs | |||
@@ -28,7 +28,7 @@ pub trait VisibilityOwner: AstNode { | |||
28 | } | 28 | } |
29 | 29 | ||
30 | pub trait LoopBodyOwner: AstNode { | 30 | pub trait LoopBodyOwner: AstNode { |
31 | fn loop_body(&self) -> Option<ast::Block> { | 31 | fn loop_body(&self) -> Option<ast::BlockExpr> { |
32 | child_opt(self) | 32 | child_opt(self) |
33 | } | 33 | } |
34 | } | 34 | } |