aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/ast_builder.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/ast_builder.rs')
-rw-r--r--crates/ra_assists/src/ast_builder.rs131
1 files changed, 60 insertions, 71 deletions
diff --git a/crates/ra_assists/src/ast_builder.rs b/crates/ra_assists/src/ast_builder.rs
index e4ea1fca9..9a62b96b3 100644
--- a/crates/ra_assists/src/ast_builder.rs
+++ b/crates/ra_assists/src/ast_builder.rs
@@ -1,44 +1,35 @@
1use itertools::Itertools; 1use itertools::Itertools;
2 2
3use hir::Name;
4use ra_syntax::{ast, AstNode, SourceFile}; 3use ra_syntax::{ast, AstNode, SourceFile};
5 4
6pub struct AstBuilder<N: AstNode> { 5pub struct Make<N: AstNode> {
7 _phantom: std::marker::PhantomData<N>, 6 _phantom: std::marker::PhantomData<N>,
8} 7}
9 8
10impl AstBuilder<ast::RecordField> { 9impl Make<ast::RecordField> {
11 pub fn from_name(name: &Name) -> ast::RecordField { 10 pub fn from(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField {
12 ast_node_from_file_text(&format!("fn f() {{ S {{ {}: (), }} }}", name))
13 }
14
15 fn from_text(text: &str) -> ast::RecordField {
16 ast_node_from_file_text(&format!("fn f() {{ S {{ {}, }} }}", text))
17 }
18
19 pub fn from_pieces(name: &ast::NameRef, expr: Option<&ast::Expr>) -> ast::RecordField {
20 match expr { 11 match expr {
21 Some(expr) => Self::from_text(&format!("{}: {}", name.syntax(), expr.syntax())), 12 Some(expr) => Self::from_text(&format!("{}: {}", name.syntax(), expr.syntax())),
22 None => Self::from_text(&name.syntax().to_string()), 13 None => Self::from_text(&name.syntax().to_string()),
23 } 14 }
24 } 15 }
25}
26 16
27impl AstBuilder<ast::Block> { 17 fn from_text(text: &str) -> ast::RecordField {
28 fn from_text(text: &str) -> ast::Block { 18 ast_node_from_file_text(&format!("fn f() {{ S {{ {}, }} }}", text))
29 ast_node_from_file_text(&format!("fn f() {}", text))
30 } 19 }
20}
31 21
32 pub fn single_expr(e: &ast::Expr) -> ast::Block { 22impl Make<ast::Block> {
23 pub fn single_expr(e: ast::Expr) -> ast::Block {
33 Self::from_text(&format!("{{ {} }}", e.syntax())) 24 Self::from_text(&format!("{{ {} }}", e.syntax()))
34 } 25 }
35}
36 26
37impl AstBuilder<ast::Expr> { 27 fn from_text(text: &str) -> ast::Block {
38 fn from_text(text: &str) -> ast::Expr { 28 ast_node_from_file_text(&format!("fn f() {}", text))
39 ast_node_from_file_text(&format!("const C: () = {};", text))
40 } 29 }
30}
41 31
32impl Make<ast::Expr> {
42 pub fn unit() -> ast::Expr { 33 pub fn unit() -> ast::Expr {
43 Self::from_text("()") 34 Self::from_text("()")
44 } 35 }
@@ -46,130 +37,128 @@ impl AstBuilder<ast::Expr> {
46 pub fn unimplemented() -> ast::Expr { 37 pub fn unimplemented() -> ast::Expr {
47 Self::from_text("unimplemented!()") 38 Self::from_text("unimplemented!()")
48 } 39 }
49}
50 40
51impl AstBuilder<ast::NameRef> { 41 fn from_text(text: &str) -> ast::Expr {
52 pub fn new(text: &str) -> ast::NameRef { 42 ast_node_from_file_text(&format!("const C: () = {};", text))
53 ast_node_from_file_text(&format!("fn f() {{ {}; }}", text))
54 } 43 }
55} 44}
56 45
57impl AstBuilder<ast::Path> { 46impl Make<ast::NameRef> {
58 fn from_text(text: &str) -> ast::Path { 47 pub fn from(text: &str) -> ast::NameRef {
59 ast_node_from_file_text(text) 48 ast_node_from_file_text(&format!("fn f() {{ {}; }}", text))
60 } 49 }
50}
61 51
52impl Make<ast::Path> {
62 pub fn from_name(name: ast::Name) -> ast::Path { 53 pub fn from_name(name: ast::Name) -> ast::Path {
63 let name = name.syntax().to_string(); 54 let name = name.syntax().to_string();
64 Self::from_text(name.as_str()) 55 Self::from_text(name.as_str())
65 } 56 }
66 57
67 pub fn from_pieces(enum_name: ast::Name, var_name: ast::Name) -> ast::Path { 58 pub fn from(enum_name: ast::Name, var_name: ast::Name) -> ast::Path {
68 Self::from_text(&format!("{}::{}", enum_name.syntax(), var_name.syntax())) 59 Self::from_text(&format!("{}::{}", enum_name.syntax(), var_name.syntax()))
69 } 60 }
70}
71 61
72impl AstBuilder<ast::BindPat> { 62 fn from_text(text: &str) -> ast::Path {
73 fn from_text(text: &str) -> ast::BindPat { 63 ast_node_from_file_text(text)
74 ast_node_from_file_text(&format!("fn f({}: ())", text))
75 } 64 }
65}
76 66
77 pub fn from_name(name: &ast::Name) -> ast::BindPat { 67impl Make<ast::BindPat> {
68 pub fn from_name(name: ast::Name) -> ast::BindPat {
78 Self::from_text(name.text()) 69 Self::from_text(name.text())
79 } 70 }
80}
81 71
82impl AstBuilder<ast::PlaceholderPat> { 72 fn from_text(text: &str) -> ast::BindPat {
83 fn from_text(text: &str) -> ast::PlaceholderPat {
84 ast_node_from_file_text(&format!("fn f({}: ())", text)) 73 ast_node_from_file_text(&format!("fn f({}: ())", text))
85 } 74 }
75}
86 76
77impl Make<ast::PlaceholderPat> {
87 pub fn placeholder() -> ast::PlaceholderPat { 78 pub fn placeholder() -> ast::PlaceholderPat {
88 Self::from_text("_") 79 Self::from_text("_")
89 } 80 }
90}
91 81
92impl AstBuilder<ast::TupleStructPat> { 82 fn from_text(text: &str) -> ast::PlaceholderPat {
93 fn from_text(text: &str) -> ast::TupleStructPat {
94 ast_node_from_file_text(&format!("fn f({}: ())", text)) 83 ast_node_from_file_text(&format!("fn f({}: ())", text))
95 } 84 }
85}
96 86
97 pub fn from_pieces( 87impl Make<ast::TupleStructPat> {
98 path: &ast::Path, 88 pub fn from(path: ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::TupleStructPat {
99 pats: impl Iterator<Item = ast::Pat>,
100 ) -> ast::TupleStructPat {
101 let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", "); 89 let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", ");
102 Self::from_text(&format!("{}({})", path.syntax(), pats_str)) 90 Self::from_text(&format!("{}({})", path.syntax(), pats_str))
103 } 91 }
104}
105 92
106impl AstBuilder<ast::RecordPat> { 93 fn from_text(text: &str) -> ast::TupleStructPat {
107 fn from_text(text: &str) -> ast::RecordPat {
108 ast_node_from_file_text(&format!("fn f({}: ())", text)) 94 ast_node_from_file_text(&format!("fn f({}: ())", text))
109 } 95 }
96}
110 97
111 pub fn from_pieces(path: &ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::RecordPat { 98impl Make<ast::RecordPat> {
99 pub fn from(path: ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::RecordPat {
112 let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", "); 100 let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", ");
113 Self::from_text(&format!("{}{{ {} }}", path.syntax(), pats_str)) 101 Self::from_text(&format!("{}{{ {} }}", path.syntax(), pats_str))
114 } 102 }
115}
116 103
117impl AstBuilder<ast::PathPat> { 104 fn from_text(text: &str) -> ast::RecordPat {
118 fn from_text(text: &str) -> ast::PathPat {
119 ast_node_from_file_text(&format!("fn f({}: ())", text)) 105 ast_node_from_file_text(&format!("fn f({}: ())", text))
120 } 106 }
107}
121 108
122 pub fn from_path(path: &ast::Path) -> ast::PathPat { 109impl Make<ast::PathPat> {
110 pub fn from_path(path: ast::Path) -> ast::PathPat {
123 let path_str = path.syntax().text().to_string(); 111 let path_str = path.syntax().text().to_string();
124 Self::from_text(path_str.as_str()) 112 Self::from_text(path_str.as_str())
125 } 113 }
126}
127 114
128impl AstBuilder<ast::MatchArm> { 115 fn from_text(text: &str) -> ast::PathPat {
129 fn from_text(text: &str) -> ast::MatchArm { 116 ast_node_from_file_text(&format!("fn f({}: ())", text))
130 ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text))
131 } 117 }
118}
132 119
133 pub fn from_pieces(pats: impl Iterator<Item = ast::Pat>, expr: &ast::Expr) -> ast::MatchArm { 120impl Make<ast::MatchArm> {
121 pub fn from(pats: impl Iterator<Item = ast::Pat>, expr: ast::Expr) -> ast::MatchArm {
134 let pats_str = pats.map(|p| p.syntax().to_string()).join(" | "); 122 let pats_str = pats.map(|p| p.syntax().to_string()).join(" | ");
135 Self::from_text(&format!("{} => {}", pats_str, expr.syntax())) 123 Self::from_text(&format!("{} => {}", pats_str, expr.syntax()))
136 } 124 }
137}
138 125
139impl AstBuilder<ast::MatchArmList> { 126 fn from_text(text: &str) -> ast::MatchArm {
140 fn from_text(text: &str) -> ast::MatchArmList {
141 ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text)) 127 ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text))
142 } 128 }
129}
143 130
131impl Make<ast::MatchArmList> {
144 pub fn from_arms(arms: impl Iterator<Item = ast::MatchArm>) -> ast::MatchArmList { 132 pub fn from_arms(arms: impl Iterator<Item = ast::MatchArm>) -> ast::MatchArmList {
145 let arms_str = arms.map(|arm| format!("\n {}", arm.syntax())).join(","); 133 let arms_str = arms.map(|arm| format!("\n {}", arm.syntax())).join(",");
146 Self::from_text(&format!("{},\n", arms_str)) 134 Self::from_text(&format!("{},\n", arms_str))
147 } 135 }
148}
149 136
150impl AstBuilder<ast::WherePred> { 137 fn from_text(text: &str) -> ast::MatchArmList {
151 fn from_text(text: &str) -> ast::WherePred { 138 ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text))
152 ast_node_from_file_text(&format!("fn f() where {} {{ }}", text))
153 } 139 }
140}
154 141
155 pub fn from_pieces( 142impl Make<ast::WherePred> {
156 path: ast::Path, 143 pub fn from(path: ast::Path, bounds: impl Iterator<Item = ast::TypeBound>) -> ast::WherePred {
157 bounds: impl Iterator<Item = ast::TypeBound>,
158 ) -> ast::WherePred {
159 let bounds = bounds.map(|b| b.syntax().to_string()).collect::<Vec<_>>().join(" + "); 144 let bounds = bounds.map(|b| b.syntax().to_string()).collect::<Vec<_>>().join(" + ");
160 Self::from_text(&format!("{}: {}", path.syntax(), bounds)) 145 Self::from_text(&format!("{}: {}", path.syntax(), bounds))
161 } 146 }
162}
163 147
164impl AstBuilder<ast::WhereClause> { 148 fn from_text(text: &str) -> ast::WherePred {
165 fn from_text(text: &str) -> ast::WhereClause {
166 ast_node_from_file_text(&format!("fn f() where {} {{ }}", text)) 149 ast_node_from_file_text(&format!("fn f() where {} {{ }}", text))
167 } 150 }
151}
168 152
153impl Make<ast::WhereClause> {
169 pub fn from_predicates(preds: impl Iterator<Item = ast::WherePred>) -> ast::WhereClause { 154 pub fn from_predicates(preds: impl Iterator<Item = ast::WherePred>) -> ast::WhereClause {
170 let preds = preds.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", "); 155 let preds = preds.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", ");
171 Self::from_text(preds.as_str()) 156 Self::from_text(preds.as_str())
172 } 157 }
158
159 fn from_text(text: &str) -> ast::WhereClause {
160 ast_node_from_file_text(&format!("fn f() where {} {{ }}", text))
161 }
173} 162}
174 163
175fn ast_node_from_file_text<N: AstNode>(text: &str) -> N { 164fn ast_node_from_file_text<N: AstNode>(text: &str) -> N {