fix assists

This commit is contained in:
Aleksey Kladov 2019-09-02 21:41:50 +03:00
parent 5e3f291195
commit e94587e315
5 changed files with 92 additions and 61 deletions

View file

@ -274,7 +274,7 @@ impl AstBuilder<ast::Block> {
impl AstBuilder<ast::Expr> {
fn from_text(text: &str) -> ast::Expr {
ast_node_from_file_text(&format!("fn f() {{ {}; }}", text))
ast_node_from_file_text(&format!("const C: () = {};", text))
}
pub fn unit() -> ast::Expr {

View file

@ -3,7 +3,8 @@ use hir::db::HirDatabase;
use ra_syntax::{
ast::{self, AstNode},
SyntaxKind::{
BREAK_EXPR, COMMENT, LAMBDA_EXPR, LOOP_EXPR, MATCH_ARM, PATH_EXPR, RETURN_EXPR, WHITESPACE,
BLOCK_EXPR, BREAK_EXPR, COMMENT, LAMBDA_EXPR, LOOP_EXPR, MATCH_ARM, PATH_EXPR, RETURN_EXPR,
WHITESPACE,
},
SyntaxNode, TextUnit,
};
@ -80,10 +81,12 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option
/// In general that's true for any expression, but in some cases that would produce invalid code.
fn valid_target_expr(node: SyntaxNode) -> Option<ast::Expr> {
match node.kind() {
PATH_EXPR => None,
PATH_EXPR | LOOP_EXPR => None,
BREAK_EXPR => ast::BreakExpr::cast(node).and_then(|e| e.expr()),
RETURN_EXPR => ast::ReturnExpr::cast(node).and_then(|e| e.expr()),
LOOP_EXPR => ast::ReturnExpr::cast(node).and_then(|e| e.expr()),
BLOCK_EXPR => {
ast::BlockExpr::cast(node).filter(|it| it.is_standalone()).map(ast::Expr::from)
}
_ => ast::Expr::cast(node),
}
}

View file

@ -116,6 +116,7 @@ SOURCE_FILE@[0; 11)
L_PAREN@[6; 7) "("
R_PAREN@[7; 8) ")"
WHITESPACE@[8; 9) " "
BLOCK_EXPR@[9; 11)
BLOCK@[9; 11)
L_CURLY@[9; 10) "{"
R_CURLY@[10; 11) "}"
@ -148,6 +149,7 @@ SOURCE_FILE@[0; 60)
L_PAREN@[7; 8) "("
R_PAREN@[8; 9) ")"
WHITESPACE@[9; 10) " "
BLOCK_EXPR@[10; 60)
BLOCK@[10; 60)
L_CURLY@[10; 11) "{"
WHITESPACE@[11; 16) "\n "
@ -190,6 +192,7 @@ FN_DEF@[0; 11)
L_PAREN@[6; 7) "("
R_PAREN@[7; 8) ")"
WHITESPACE@[8; 9) " "
BLOCK_EXPR@[9; 11)
BLOCK@[9; 11)
L_CURLY@[9; 10) "{"
R_CURLY@[10; 11) "}"
@ -258,6 +261,7 @@ SOURCE_FILE@[0; 12)
L_PAREN@[6; 7) "("
R_PAREN@[7; 8) ")"
WHITESPACE@[8; 9) " "
BLOCK_EXPR@[9; 12)
BLOCK@[9; 12)
L_CURLY@[9; 10) "{"
WHITESPACE@[10; 11) "\n"
@ -292,6 +296,7 @@ SOURCE_FILE@[0; 12)
L_PAREN@[6; 7) "("
R_PAREN@[7; 8) ")"
WHITESPACE@[8; 9) " "
BLOCK_EXPR@[9; 12)
BLOCK@[9; 12)
L_CURLY@[9; 10) "{"
WHITESPACE@[10; 11) "\n"
@ -325,6 +330,7 @@ SOURCE_FILE@[0; 25)
L_PAREN@[6; 7) "("
R_PAREN@[7; 8) ")"
WHITESPACE@[8; 9) " "
BLOCK_EXPR@[9; 12)
BLOCK@[9; 12)
L_CURLY@[9; 10) "{"
WHITESPACE@[10; 11) "\n"
@ -339,6 +345,7 @@ SOURCE_FILE@[0; 25)
L_PAREN@[19; 20) "("
R_PAREN@[20; 21) ")"
WHITESPACE@[21; 22) " "
BLOCK_EXPR@[22; 25)
BLOCK@[22; 25)
L_CURLY@[22; 23) "{"
WHITESPACE@[23; 24) "\n"

View file

@ -678,6 +678,7 @@ fn test_expr_order() {
PARAM_LIST@[5; 7)
L_PAREN@[5; 6) "("
R_PAREN@[6; 7) ")"
BLOCK_EXPR@[7; 15)
BLOCK@[7; 15)
L_CURLY@[7; 8) "{"
EXPR_STMT@[8; 14)

View file

@ -289,6 +289,26 @@ impl ast::Literal {
}
}
impl ast::BlockExpr {
/// false if the block is an intrinsic part of the syntax and can't be
/// replaced with arbitrary expression.
///
/// ```not_rust
/// fn foo() { not_stand_alone }
/// const FOO: () = { stand_alone };
/// ```
pub fn is_standalone(&self) -> bool {
let kind = match self.syntax().parent() {
None => return true,
Some(it) => it.kind(),
};
match kind {
FN_DEF | MATCH_ARM | IF_EXPR | WHILE_EXPR | LOOP_EXPR | TRY_BLOCK_EXPR => false,
_ => true,
}
}
}
#[test]
fn test_literal_with_attr() {
let parse = ast::SourceFile::parse(r#"const _: &str = { #[attr] "Hello" };"#);