From 4ea0c12cf1520bc7d0f5def211a7cacadb45b7af Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 4 Feb 2020 13:22:32 +0100 Subject: Make sure that newly created nodes are the root of the tree --- crates/ra_syntax/src/ast/make.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'crates/ra_syntax/src/ast/make.rs') diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 36e648180..38c0e9a66 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -2,7 +2,7 @@ //! of smaller pieces. use itertools::Itertools; -use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxToken}; +use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxToken}; pub fn name(text: &str) -> ast::Name { ast_from_text(&format!("mod {};", text)) @@ -179,7 +179,16 @@ pub fn token(kind: SyntaxKind) -> SyntaxToken { fn ast_from_text(text: &str) -> N { let parse = SourceFile::parse(text); - parse.tree().syntax().descendants().find_map(N::cast).unwrap() + let node = parse.tree().syntax().descendants().find_map(N::cast).unwrap(); + let node = node.syntax().clone(); + let node = unroot(node); + let node = N::cast(node).unwrap(); + assert_eq!(node.syntax().text_range().start(), 0.into()); + node +} + +fn unroot(n: SyntaxNode) -> SyntaxNode { + SyntaxNode::new_root(n.green().clone()) } pub mod tokens { -- cgit v1.2.3 From a4c6e8c4e22ddea9668eb3380603ad53d8ce6a5e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 5 Feb 2020 10:50:07 +0100 Subject: Refactor if-let -> match assist to use ast::make --- crates/ra_syntax/src/ast/make.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'crates/ra_syntax/src/ast/make.rs') diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 38c0e9a66..629503dc5 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -122,11 +122,18 @@ pub fn match_arm(pats: impl IntoIterator, expr: ast::Expr) -> a } pub fn match_arm_list(arms: impl IntoIterator) -> ast::MatchArmList { - let arms_str = arms.into_iter().map(|arm| format!("\n {}", arm.syntax())).join(","); - return from_text(&format!("{},\n", arms_str)); + let arms_str = arms + .into_iter() + .map(|arm| { + let needs_comma = arm.expr().map_or(true, |it| !it.is_block_like()); + let comma = if needs_comma { "," } else { "" }; + format!(" {}{}\n", arm.syntax(), comma) + }) + .collect::(); + return from_text(&format!("{}", arms_str)); fn from_text(text: &str) -> ast::MatchArmList { - ast_from_text(&format!("fn f() {{ match () {{{}}} }}", text)) + ast_from_text(&format!("fn f() {{ match () {{\n{}}} }}", text)) } } -- cgit v1.2.3 From 56e3fbe588597847b508969b7e3243725f5a792c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 6 Feb 2020 23:59:27 +0100 Subject: A tiny bit more consistent API --- crates/ra_syntax/src/ast/make.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_syntax/src/ast/make.rs') diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 629503dc5..02966a3ff 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -158,7 +158,7 @@ pub fn where_clause(preds: impl IntoIterator) -> ast::Whe } } -pub fn if_expression(condition: &ast::Expr, statement: &str) -> ast::IfExpr { +pub fn if_expression(condition: ast::Expr, statement: &str) -> ast::IfExpr { ast_from_text(&format!( "fn f() {{ if !{} {{\n {}\n}}\n}}", condition.syntax().text(), -- cgit v1.2.3 From aa1234e02b1166c57dd2a3cd27fd0b0b3c6cba7e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 7 Feb 2020 12:07:38 +0100 Subject: Generalize invert_if to just always work --- crates/ra_syntax/src/ast/make.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'crates/ra_syntax/src/ast/make.rs') diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 02966a3ff..982a7bcdc 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -62,6 +62,10 @@ pub fn expr_return() -> ast::Expr { pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::Expr { expr_from_text(&format!("match {} {}", expr.syntax(), match_arm_list.syntax())) } +pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr { + let token = token(op); + expr_from_text(&format!("{}{}", token, expr.syntax())) +} fn expr_from_text(text: &str) -> ast::Expr { ast_from_text(&format!("const C: () = {};", text)) } @@ -203,7 +207,7 @@ pub mod tokens { use once_cell::sync::Lazy; pub(super) static SOURCE_FILE: Lazy> = - Lazy::new(|| SourceFile::parse("const C: <()>::Item = (1 != 1, 2 == 2)\n;")); + Lazy::new(|| SourceFile::parse("const C: <()>::Item = (1 != 1, 2 == 2, !true)\n;")); pub fn comma() -> SyntaxToken { SOURCE_FILE -- cgit v1.2.3 From 36ee9ecb678d775609bf3825f1c4fd8e0c56bf32 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 7 Feb 2020 11:51:16 +0100 Subject: Cleanup early return assist --- crates/ra_syntax/src/ast/make.rs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'crates/ra_syntax/src/ast/make.rs') diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 982a7bcdc..862eb1172 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -33,6 +33,21 @@ pub fn record_field(name: ast::NameRef, expr: Option) -> ast::RecordF } } +pub fn block_expr( + stmts: impl IntoIterator, + tail_expr: Option, +) -> ast::BlockExpr { + let mut text = "{\n".to_string(); + for stmt in stmts.into_iter() { + text += &format!(" {}\n", stmt.syntax()); + } + if let Some(tail_expr) = tail_expr { + text += &format!(" {}\n", tail_expr.syntax()) + } + text += "}"; + ast_from_text(&format!("fn f() {}", text)) +} + pub fn block_from_expr(e: ast::Expr) -> ast::Block { return from_text(&format!("{{ {} }}", e.syntax())); @@ -62,6 +77,9 @@ pub fn expr_return() -> ast::Expr { pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::Expr { expr_from_text(&format!("match {} {}", expr.syntax(), match_arm_list.syntax())) } +pub fn expr_if(condition: ast::Expr, then_branch: ast::BlockExpr) -> ast::Expr { + expr_from_text(&format!("if {} {}", condition.syntax(), then_branch.syntax())) +} pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr { let token = token(op); expr_from_text(&format!("{}{}", token, expr.syntax())) @@ -162,14 +180,6 @@ pub fn where_clause(preds: impl IntoIterator) -> ast::Whe } } -pub fn if_expression(condition: ast::Expr, statement: &str) -> ast::IfExpr { - ast_from_text(&format!( - "fn f() {{ if !{} {{\n {}\n}}\n}}", - condition.syntax().text(), - statement - )) -} - pub fn let_stmt(pattern: ast::Pat, initializer: Option) -> ast::LetStmt { let text = match initializer { Some(it) => format!("let {} = {};", pattern.syntax(), it.syntax()), @@ -177,6 +187,9 @@ pub fn let_stmt(pattern: ast::Pat, initializer: Option) -> ast::LetSt }; ast_from_text(&format!("fn f() {{ {} }}", text)) } +pub fn expr_stmt(expr: ast::Expr) -> ast::ExprStmt { + ast_from_text(&format!("fn f() {{ {}; }}", expr.syntax())) +} pub fn token(kind: SyntaxKind) -> SyntaxToken { tokens::SOURCE_FILE -- cgit v1.2.3