aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/ast_editor.rs2
-rw-r--r--crates/ra_assists/src/introduce_variable.rs9
-rw-r--r--crates/ra_ide_api/src/syntax_tree.rs91
-rw-r--r--crates/ra_mbe/src/tests.rs31
-rw-r--r--crates/ra_syntax/src/ast/expr_extensions.rs20
5 files changed, 92 insertions, 61 deletions
diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs
index 6815638dc..048478662 100644
--- a/crates/ra_assists/src/ast_editor.rs
+++ b/crates/ra_assists/src/ast_editor.rs
@@ -274,7 +274,7 @@ impl AstBuilder<ast::Block> {
274 274
275impl AstBuilder<ast::Expr> { 275impl AstBuilder<ast::Expr> {
276 fn from_text(text: &str) -> ast::Expr { 276 fn from_text(text: &str) -> ast::Expr {
277 ast_node_from_file_text(&format!("fn f() {{ {}; }}", text)) 277 ast_node_from_file_text(&format!("const C: () = {};", text))
278 } 278 }
279 279
280 pub fn unit() -> ast::Expr { 280 pub fn unit() -> ast::Expr {
diff --git a/crates/ra_assists/src/introduce_variable.rs b/crates/ra_assists/src/introduce_variable.rs
index 95c18d0e3..470ffe120 100644
--- a/crates/ra_assists/src/introduce_variable.rs
+++ b/crates/ra_assists/src/introduce_variable.rs
@@ -3,7 +3,8 @@ use hir::db::HirDatabase;
3use ra_syntax::{ 3use ra_syntax::{
4 ast::{self, AstNode}, 4 ast::{self, AstNode},
5 SyntaxKind::{ 5 SyntaxKind::{
6 BREAK_EXPR, COMMENT, LAMBDA_EXPR, LOOP_EXPR, MATCH_ARM, PATH_EXPR, RETURN_EXPR, WHITESPACE, 6 BLOCK_EXPR, BREAK_EXPR, COMMENT, LAMBDA_EXPR, LOOP_EXPR, MATCH_ARM, PATH_EXPR, RETURN_EXPR,
7 WHITESPACE,
7 }, 8 },
8 SyntaxNode, TextUnit, 9 SyntaxNode, TextUnit,
9}; 10};
@@ -80,10 +81,12 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option
80/// In general that's true for any expression, but in some cases that would produce invalid code. 81/// In general that's true for any expression, but in some cases that would produce invalid code.
81fn valid_target_expr(node: SyntaxNode) -> Option<ast::Expr> { 82fn valid_target_expr(node: SyntaxNode) -> Option<ast::Expr> {
82 match node.kind() { 83 match node.kind() {
83 PATH_EXPR => None, 84 PATH_EXPR | LOOP_EXPR => None,
84 BREAK_EXPR => ast::BreakExpr::cast(node).and_then(|e| e.expr()), 85 BREAK_EXPR => ast::BreakExpr::cast(node).and_then(|e| e.expr()),
85 RETURN_EXPR => ast::ReturnExpr::cast(node).and_then(|e| e.expr()), 86 RETURN_EXPR => ast::ReturnExpr::cast(node).and_then(|e| e.expr()),
86 LOOP_EXPR => ast::ReturnExpr::cast(node).and_then(|e| e.expr()), 87 BLOCK_EXPR => {
88 ast::BlockExpr::cast(node).filter(|it| it.is_standalone()).map(ast::Expr::from)
89 }
87 _ => ast::Expr::cast(node), 90 _ => ast::Expr::cast(node),
88 } 91 }
89} 92}
diff --git a/crates/ra_ide_api/src/syntax_tree.rs b/crates/ra_ide_api/src/syntax_tree.rs
index dd31b9093..914759709 100644
--- a/crates/ra_ide_api/src/syntax_tree.rs
+++ b/crates/ra_ide_api/src/syntax_tree.rs
@@ -116,9 +116,10 @@ SOURCE_FILE@[0; 11)
116 L_PAREN@[6; 7) "(" 116 L_PAREN@[6; 7) "("
117 R_PAREN@[7; 8) ")" 117 R_PAREN@[7; 8) ")"
118 WHITESPACE@[8; 9) " " 118 WHITESPACE@[8; 9) " "
119 BLOCK@[9; 11) 119 BLOCK_EXPR@[9; 11)
120 L_CURLY@[9; 10) "{" 120 BLOCK@[9; 11)
121 R_CURLY@[10; 11) "}" 121 L_CURLY@[9; 10) "{"
122 R_CURLY@[10; 11) "}"
122"# 123"#
123 .trim() 124 .trim()
124 ); 125 );
@@ -148,26 +149,27 @@ SOURCE_FILE@[0; 60)
148 L_PAREN@[7; 8) "(" 149 L_PAREN@[7; 8) "("
149 R_PAREN@[8; 9) ")" 150 R_PAREN@[8; 9) ")"
150 WHITESPACE@[9; 10) " " 151 WHITESPACE@[9; 10) " "
151 BLOCK@[10; 60) 152 BLOCK_EXPR@[10; 60)
152 L_CURLY@[10; 11) "{" 153 BLOCK@[10; 60)
153 WHITESPACE@[11; 16) "\n " 154 L_CURLY@[10; 11) "{"
154 EXPR_STMT@[16; 58) 155 WHITESPACE@[11; 16) "\n "
155 MACRO_CALL@[16; 57) 156 EXPR_STMT@[16; 58)
156 PATH@[16; 22) 157 MACRO_CALL@[16; 57)
157 PATH_SEGMENT@[16; 22) 158 PATH@[16; 22)
158 NAME_REF@[16; 22) 159 PATH_SEGMENT@[16; 22)
159 IDENT@[16; 22) "assert" 160 NAME_REF@[16; 22)
160 EXCL@[22; 23) "!" 161 IDENT@[16; 22) "assert"
161 TOKEN_TREE@[23; 57) 162 EXCL@[22; 23) "!"
162 L_PAREN@[23; 24) "(" 163 TOKEN_TREE@[23; 57)
163 STRING@[24; 52) "\"\n fn foo() {\n ..." 164 L_PAREN@[23; 24) "("
164 COMMA@[52; 53) "," 165 STRING@[24; 52) "\"\n fn foo() {\n ..."
165 WHITESPACE@[53; 54) " " 166 COMMA@[52; 53) ","
166 STRING@[54; 56) "\"\"" 167 WHITESPACE@[53; 54) " "
167 R_PAREN@[56; 57) ")" 168 STRING@[54; 56) "\"\""
168 SEMI@[57; 58) ";" 169 R_PAREN@[56; 57) ")"
169 WHITESPACE@[58; 59) "\n" 170 SEMI@[57; 58) ";"
170 R_CURLY@[59; 60) "}" 171 WHITESPACE@[58; 59) "\n"
172 R_CURLY@[59; 60) "}"
171"# 173"#
172 .trim() 174 .trim()
173 ); 175 );
@@ -190,9 +192,10 @@ FN_DEF@[0; 11)
190 L_PAREN@[6; 7) "(" 192 L_PAREN@[6; 7) "("
191 R_PAREN@[7; 8) ")" 193 R_PAREN@[7; 8) ")"
192 WHITESPACE@[8; 9) " " 194 WHITESPACE@[8; 9) " "
193 BLOCK@[9; 11) 195 BLOCK_EXPR@[9; 11)
194 L_CURLY@[9; 10) "{" 196 BLOCK@[9; 11)
195 R_CURLY@[10; 11) "}" 197 L_CURLY@[9; 10) "{"
198 R_CURLY@[10; 11) "}"
196"# 199"#
197 .trim() 200 .trim()
198 ); 201 );
@@ -258,10 +261,11 @@ SOURCE_FILE@[0; 12)
258 L_PAREN@[6; 7) "(" 261 L_PAREN@[6; 7) "("
259 R_PAREN@[7; 8) ")" 262 R_PAREN@[7; 8) ")"
260 WHITESPACE@[8; 9) " " 263 WHITESPACE@[8; 9) " "
261 BLOCK@[9; 12) 264 BLOCK_EXPR@[9; 12)
262 L_CURLY@[9; 10) "{" 265 BLOCK@[9; 12)
263 WHITESPACE@[10; 11) "\n" 266 L_CURLY@[9; 10) "{"
264 R_CURLY@[11; 12) "}" 267 WHITESPACE@[10; 11) "\n"
268 R_CURLY@[11; 12) "}"
265"# 269"#
266 .trim() 270 .trim()
267 ); 271 );
@@ -292,10 +296,11 @@ SOURCE_FILE@[0; 12)
292 L_PAREN@[6; 7) "(" 296 L_PAREN@[6; 7) "("
293 R_PAREN@[7; 8) ")" 297 R_PAREN@[7; 8) ")"
294 WHITESPACE@[8; 9) " " 298 WHITESPACE@[8; 9) " "
295 BLOCK@[9; 12) 299 BLOCK_EXPR@[9; 12)
296 L_CURLY@[9; 10) "{" 300 BLOCK@[9; 12)
297 WHITESPACE@[10; 11) "\n" 301 L_CURLY@[9; 10) "{"
298 R_CURLY@[11; 12) "}" 302 WHITESPACE@[10; 11) "\n"
303 R_CURLY@[11; 12) "}"
299"# 304"#
300 .trim() 305 .trim()
301 ); 306 );
@@ -325,10 +330,11 @@ SOURCE_FILE@[0; 25)
325 L_PAREN@[6; 7) "(" 330 L_PAREN@[6; 7) "("
326 R_PAREN@[7; 8) ")" 331 R_PAREN@[7; 8) ")"
327 WHITESPACE@[8; 9) " " 332 WHITESPACE@[8; 9) " "
328 BLOCK@[9; 12) 333 BLOCK_EXPR@[9; 12)
329 L_CURLY@[9; 10) "{" 334 BLOCK@[9; 12)
330 WHITESPACE@[10; 11) "\n" 335 L_CURLY@[9; 10) "{"
331 R_CURLY@[11; 12) "}" 336 WHITESPACE@[10; 11) "\n"
337 R_CURLY@[11; 12) "}"
332 WHITESPACE@[12; 13) "\n" 338 WHITESPACE@[12; 13) "\n"
333 FN_DEF@[13; 25) 339 FN_DEF@[13; 25)
334 FN_KW@[13; 15) "fn" 340 FN_KW@[13; 15) "fn"
@@ -339,10 +345,11 @@ SOURCE_FILE@[0; 25)
339 L_PAREN@[19; 20) "(" 345 L_PAREN@[19; 20) "("
340 R_PAREN@[20; 21) ")" 346 R_PAREN@[20; 21) ")"
341 WHITESPACE@[21; 22) " " 347 WHITESPACE@[21; 22) " "
342 BLOCK@[22; 25) 348 BLOCK_EXPR@[22; 25)
343 L_CURLY@[22; 23) "{" 349 BLOCK@[22; 25)
344 WHITESPACE@[23; 24) "\n" 350 L_CURLY@[22; 23) "{"
345 R_CURLY@[24; 25) "}" 351 WHITESPACE@[23; 24) "\n"
352 R_CURLY@[24; 25) "}"
346"# 353"#
347 .trim() 354 .trim()
348 ); 355 );
diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs
index 1dbf22997..034ea639b 100644
--- a/crates/ra_mbe/src/tests.rs
+++ b/crates/ra_mbe/src/tests.rs
@@ -678,21 +678,22 @@ fn test_expr_order() {
678 PARAM_LIST@[5; 7) 678 PARAM_LIST@[5; 7)
679 L_PAREN@[5; 6) "(" 679 L_PAREN@[5; 6) "("
680 R_PAREN@[6; 7) ")" 680 R_PAREN@[6; 7) ")"
681 BLOCK@[7; 15) 681 BLOCK_EXPR@[7; 15)
682 L_CURLY@[7; 8) "{" 682 BLOCK@[7; 15)
683 EXPR_STMT@[8; 14) 683 L_CURLY@[7; 8) "{"
684 BIN_EXPR@[8; 13) 684 EXPR_STMT@[8; 14)
685 BIN_EXPR@[8; 11) 685 BIN_EXPR@[8; 13)
686 LITERAL@[8; 9) 686 BIN_EXPR@[8; 11)
687 INT_NUMBER@[8; 9) "1" 687 LITERAL@[8; 9)
688 PLUS@[9; 10) "+" 688 INT_NUMBER@[8; 9) "1"
689 LITERAL@[10; 11) 689 PLUS@[9; 10) "+"
690 INT_NUMBER@[10; 11) "1" 690 LITERAL@[10; 11)
691 STAR@[11; 12) "*" 691 INT_NUMBER@[10; 11) "1"
692 LITERAL@[12; 13) 692 STAR@[11; 12) "*"
693 INT_NUMBER@[12; 13) "2" 693 LITERAL@[12; 13)
694 SEMI@[13; 14) ";" 694 INT_NUMBER@[12; 13) "2"
695 R_CURLY@[14; 15) "}""#, 695 SEMI@[13; 14) ";"
696 R_CURLY@[14; 15) "}""#,
696 ); 697 );
697} 698}
698 699
diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs
index 1324965cf..25dbd0bed 100644
--- a/crates/ra_syntax/src/ast/expr_extensions.rs
+++ b/crates/ra_syntax/src/ast/expr_extensions.rs
@@ -289,6 +289,26 @@ impl ast::Literal {
289 } 289 }
290} 290}
291 291
292impl 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]
293fn test_literal_with_attr() { 313fn 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" };"#);