diff options
author | Seivan Heidari <[email protected]> | 2019-11-14 22:20:27 +0000 |
---|---|---|
committer | Seivan Heidari <[email protected]> | 2019-11-14 22:20:27 +0000 |
commit | c622413bc72ea56d5f62a16788d897cb61eca948 (patch) | |
tree | 9de3dbe8b5c935ed168efac4e70770e54fbe0714 /crates/ra_syntax/src | |
parent | 0525778a3ad590492b51cc11085d815f9bb8f92b (diff) | |
parent | bbb022d3999b3038549ec6c309efb065231c896a (diff) |
Merge branch 'master' of https://github.com/rust-analyzer/rust-analyzer into feature/themes
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r-- | crates/ra_syntax/src/ast/edit.rs | 2 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/make.rs | 71 |
2 files changed, 43 insertions, 30 deletions
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index 47bdbb81a..6f005a2d8 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs | |||
@@ -358,7 +358,7 @@ fn replace_children<N: AstNode>( | |||
358 | fn test_increase_indent() { | 358 | fn test_increase_indent() { |
359 | let arm_list = { | 359 | let arm_list = { |
360 | let arm = make::match_arm(iter::once(make::placeholder_pat().into()), make::expr_unit()); | 360 | let arm = make::match_arm(iter::once(make::placeholder_pat().into()), make::expr_unit()); |
361 | make::match_arm_list(vec![arm.clone(), arm].into_iter()) | 361 | make::match_arm_list(vec![arm.clone(), arm]) |
362 | }; | 362 | }; |
363 | assert_eq!( | 363 | assert_eq!( |
364 | arm_list.syntax().to_string(), | 364 | arm_list.syntax().to_string(), |
diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 95062ef6c..9749327fa 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs | |||
@@ -4,6 +4,10 @@ use itertools::Itertools; | |||
4 | 4 | ||
5 | use crate::{ast, AstNode, SourceFile}; | 5 | use crate::{ast, AstNode, SourceFile}; |
6 | 6 | ||
7 | pub fn name(text: &str) -> ast::Name { | ||
8 | ast_from_text(&format!("mod {};", text)) | ||
9 | } | ||
10 | |||
7 | pub fn name_ref(text: &str) -> ast::NameRef { | 11 | pub fn name_ref(text: &str) -> ast::NameRef { |
8 | ast_from_text(&format!("fn f() {{ {}; }}", text)) | 12 | ast_from_text(&format!("fn f() {{ {}; }}", text)) |
9 | } | 13 | } |
@@ -43,6 +47,21 @@ pub fn expr_unit() -> ast::Expr { | |||
43 | pub fn expr_unimplemented() -> ast::Expr { | 47 | pub fn expr_unimplemented() -> ast::Expr { |
44 | expr_from_text("unimplemented!()") | 48 | expr_from_text("unimplemented!()") |
45 | } | 49 | } |
50 | pub fn expr_path(path: ast::Path) -> ast::Expr { | ||
51 | expr_from_text(&path.syntax().to_string()) | ||
52 | } | ||
53 | pub fn expr_continue() -> ast::Expr { | ||
54 | expr_from_text("continue") | ||
55 | } | ||
56 | pub fn expr_break() -> ast::Expr { | ||
57 | expr_from_text("break") | ||
58 | } | ||
59 | pub fn expr_return() -> ast::Expr { | ||
60 | expr_from_text("return") | ||
61 | } | ||
62 | pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::Expr { | ||
63 | expr_from_text(&format!("match {} {}", expr.syntax(), match_arm_list.syntax())) | ||
64 | } | ||
46 | fn expr_from_text(text: &str) -> ast::Expr { | 65 | fn expr_from_text(text: &str) -> ast::Expr { |
47 | ast_from_text(&format!("const C: () = {};", text)) | 66 | ast_from_text(&format!("const C: () = {};", text)) |
48 | } | 67 | } |
@@ -65,9 +84,9 @@ pub fn placeholder_pat() -> ast::PlaceholderPat { | |||
65 | 84 | ||
66 | pub fn tuple_struct_pat( | 85 | pub fn tuple_struct_pat( |
67 | path: ast::Path, | 86 | path: ast::Path, |
68 | pats: impl Iterator<Item = ast::Pat>, | 87 | pats: impl IntoIterator<Item = ast::Pat>, |
69 | ) -> ast::TupleStructPat { | 88 | ) -> ast::TupleStructPat { |
70 | let pats_str = pats.map(|p| p.syntax().to_string()).join(", "); | 89 | let pats_str = pats.into_iter().map(|p| p.syntax().to_string()).join(", "); |
71 | return from_text(&format!("{}({})", path.syntax(), pats_str)); | 90 | return from_text(&format!("{}({})", path.syntax(), pats_str)); |
72 | 91 | ||
73 | fn from_text(text: &str) -> ast::TupleStructPat { | 92 | fn from_text(text: &str) -> ast::TupleStructPat { |
@@ -75,8 +94,8 @@ pub fn tuple_struct_pat( | |||
75 | } | 94 | } |
76 | } | 95 | } |
77 | 96 | ||
78 | pub fn record_pat(path: ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::RecordPat { | 97 | pub fn record_pat(path: ast::Path, pats: impl IntoIterator<Item = ast::Pat>) -> ast::RecordPat { |
79 | let pats_str = pats.map(|p| p.syntax().to_string()).join(", "); | 98 | let pats_str = pats.into_iter().map(|p| p.syntax().to_string()).join(", "); |
80 | return from_text(&format!("{} {{ {} }}", path.syntax(), pats_str)); | 99 | return from_text(&format!("{} {{ {} }}", path.syntax(), pats_str)); |
81 | 100 | ||
82 | fn from_text(text: &str) -> ast::RecordPat { | 101 | fn from_text(text: &str) -> ast::RecordPat { |
@@ -92,8 +111,8 @@ pub fn path_pat(path: ast::Path) -> ast::PathPat { | |||
92 | } | 111 | } |
93 | } | 112 | } |
94 | 113 | ||
95 | pub fn match_arm(pats: impl Iterator<Item = ast::Pat>, expr: ast::Expr) -> ast::MatchArm { | 114 | pub fn match_arm(pats: impl IntoIterator<Item = ast::Pat>, expr: ast::Expr) -> ast::MatchArm { |
96 | let pats_str = pats.map(|p| p.syntax().to_string()).join(" | "); | 115 | let pats_str = pats.into_iter().map(|p| p.syntax().to_string()).join(" | "); |
97 | return from_text(&format!("{} => {}", pats_str, expr.syntax())); | 116 | return from_text(&format!("{} => {}", pats_str, expr.syntax())); |
98 | 117 | ||
99 | fn from_text(text: &str) -> ast::MatchArm { | 118 | fn from_text(text: &str) -> ast::MatchArm { |
@@ -101,8 +120,8 @@ pub fn match_arm(pats: impl Iterator<Item = ast::Pat>, expr: ast::Expr) -> ast:: | |||
101 | } | 120 | } |
102 | } | 121 | } |
103 | 122 | ||
104 | pub fn match_arm_list(arms: impl Iterator<Item = ast::MatchArm>) -> ast::MatchArmList { | 123 | pub fn match_arm_list(arms: impl IntoIterator<Item = ast::MatchArm>) -> ast::MatchArmList { |
105 | let arms_str = arms.map(|arm| format!("\n {}", arm.syntax())).join(","); | 124 | let arms_str = arms.into_iter().map(|arm| format!("\n {}", arm.syntax())).join(","); |
106 | return from_text(&format!("{},\n", arms_str)); | 125 | return from_text(&format!("{},\n", arms_str)); |
107 | 126 | ||
108 | fn from_text(text: &str) -> ast::MatchArmList { | 127 | fn from_text(text: &str) -> ast::MatchArmList { |
@@ -110,25 +129,11 @@ pub fn match_arm_list(arms: impl Iterator<Item = ast::MatchArm>) -> ast::MatchAr | |||
110 | } | 129 | } |
111 | } | 130 | } |
112 | 131 | ||
113 | pub fn let_match_early(expr: ast::Expr, path: &str, early_expression: &str) -> ast::LetStmt { | 132 | pub fn where_pred( |
114 | return from_text(&format!( | 133 | path: ast::Path, |
115 | r#"let {} = match {} {{ | 134 | bounds: impl IntoIterator<Item = ast::TypeBound>, |
116 | {}(it) => it, | 135 | ) -> ast::WherePred { |
117 | None => {}, | 136 | let bounds = bounds.into_iter().map(|b| b.syntax().to_string()).join(" + "); |
118 | }};"#, | ||
119 | expr.syntax().text(), | ||
120 | expr.syntax().text(), | ||
121 | path, | ||
122 | early_expression | ||
123 | )); | ||
124 | |||
125 | fn from_text(text: &str) -> ast::LetStmt { | ||
126 | ast_from_text(&format!("fn f() {{ {} }}", text)) | ||
127 | } | ||
128 | } | ||
129 | |||
130 | pub fn where_pred(path: ast::Path, bounds: impl Iterator<Item = ast::TypeBound>) -> ast::WherePred { | ||
131 | let bounds = bounds.map(|b| b.syntax().to_string()).join(" + "); | ||
132 | return from_text(&format!("{}: {}", path.syntax(), bounds)); | 137 | return from_text(&format!("{}: {}", path.syntax(), bounds)); |
133 | 138 | ||
134 | fn from_text(text: &str) -> ast::WherePred { | 139 | fn from_text(text: &str) -> ast::WherePred { |
@@ -136,8 +141,8 @@ pub fn where_pred(path: ast::Path, bounds: impl Iterator<Item = ast::TypeBound>) | |||
136 | } | 141 | } |
137 | } | 142 | } |
138 | 143 | ||
139 | pub fn where_clause(preds: impl Iterator<Item = ast::WherePred>) -> ast::WhereClause { | 144 | pub fn where_clause(preds: impl IntoIterator<Item = ast::WherePred>) -> ast::WhereClause { |
140 | let preds = preds.map(|p| p.syntax().to_string()).join(", "); | 145 | let preds = preds.into_iter().map(|p| p.syntax().to_string()).join(", "); |
141 | return from_text(preds.as_str()); | 146 | return from_text(preds.as_str()); |
142 | 147 | ||
143 | fn from_text(text: &str) -> ast::WhereClause { | 148 | fn from_text(text: &str) -> ast::WhereClause { |
@@ -153,6 +158,14 @@ pub fn if_expression(condition: &ast::Expr, statement: &str) -> ast::IfExpr { | |||
153 | )) | 158 | )) |
154 | } | 159 | } |
155 | 160 | ||
161 | pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetStmt { | ||
162 | let text = match initializer { | ||
163 | Some(it) => format!("let {} = {};", pattern.syntax(), it.syntax()), | ||
164 | None => format!("let {};", pattern.syntax()), | ||
165 | }; | ||
166 | ast_from_text(&format!("fn f() {{ {} }}", text)) | ||
167 | } | ||
168 | |||
156 | fn ast_from_text<N: AstNode>(text: &str) -> N { | 169 | fn ast_from_text<N: AstNode>(text: &str) -> N { |
157 | let parse = SourceFile::parse(text); | 170 | let parse = SourceFile::parse(text); |
158 | let res = parse.tree().syntax().descendants().find_map(N::cast).unwrap(); | 171 | let res = parse.tree().syntax().descendants().find_map(N::cast).unwrap(); |