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.rs218
1 files changed, 0 insertions, 218 deletions
diff --git a/crates/ra_assists/src/ast_builder.rs b/crates/ra_assists/src/ast_builder.rs
deleted file mode 100644
index 9a62b96b3..000000000
--- a/crates/ra_assists/src/ast_builder.rs
+++ /dev/null
@@ -1,218 +0,0 @@
1use itertools::Itertools;
2
3use ra_syntax::{ast, AstNode, SourceFile};
4
5pub struct Make<N: AstNode> {
6 _phantom: std::marker::PhantomData<N>,
7}
8
9impl Make<ast::RecordField> {
10 pub fn from(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField {
11 match expr {
12 Some(expr) => Self::from_text(&format!("{}: {}", name.syntax(), expr.syntax())),
13 None => Self::from_text(&name.syntax().to_string()),
14 }
15 }
16
17 fn from_text(text: &str) -> ast::RecordField {
18 ast_node_from_file_text(&format!("fn f() {{ S {{ {}, }} }}", text))
19 }
20}
21
22impl Make<ast::Block> {
23 pub fn single_expr(e: ast::Expr) -> ast::Block {
24 Self::from_text(&format!("{{ {} }}", e.syntax()))
25 }
26
27 fn from_text(text: &str) -> ast::Block {
28 ast_node_from_file_text(&format!("fn f() {}", text))
29 }
30}
31
32impl Make<ast::Expr> {
33 pub fn unit() -> ast::Expr {
34 Self::from_text("()")
35 }
36
37 pub fn unimplemented() -> ast::Expr {
38 Self::from_text("unimplemented!()")
39 }
40
41 fn from_text(text: &str) -> ast::Expr {
42 ast_node_from_file_text(&format!("const C: () = {};", text))
43 }
44}
45
46impl Make<ast::NameRef> {
47 pub fn from(text: &str) -> ast::NameRef {
48 ast_node_from_file_text(&format!("fn f() {{ {}; }}", text))
49 }
50}
51
52impl Make<ast::Path> {
53 pub fn from_name(name: ast::Name) -> ast::Path {
54 let name = name.syntax().to_string();
55 Self::from_text(name.as_str())
56 }
57
58 pub fn from(enum_name: ast::Name, var_name: ast::Name) -> ast::Path {
59 Self::from_text(&format!("{}::{}", enum_name.syntax(), var_name.syntax()))
60 }
61
62 fn from_text(text: &str) -> ast::Path {
63 ast_node_from_file_text(text)
64 }
65}
66
67impl Make<ast::BindPat> {
68 pub fn from_name(name: ast::Name) -> ast::BindPat {
69 Self::from_text(name.text())
70 }
71
72 fn from_text(text: &str) -> ast::BindPat {
73 ast_node_from_file_text(&format!("fn f({}: ())", text))
74 }
75}
76
77impl Make<ast::PlaceholderPat> {
78 pub fn placeholder() -> ast::PlaceholderPat {
79 Self::from_text("_")
80 }
81
82 fn from_text(text: &str) -> ast::PlaceholderPat {
83 ast_node_from_file_text(&format!("fn f({}: ())", text))
84 }
85}
86
87impl Make<ast::TupleStructPat> {
88 pub fn from(path: ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::TupleStructPat {
89 let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", ");
90 Self::from_text(&format!("{}({})", path.syntax(), pats_str))
91 }
92
93 fn from_text(text: &str) -> ast::TupleStructPat {
94 ast_node_from_file_text(&format!("fn f({}: ())", text))
95 }
96}
97
98impl Make<ast::RecordPat> {
99 pub fn from(path: ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::RecordPat {
100 let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", ");
101 Self::from_text(&format!("{}{{ {} }}", path.syntax(), pats_str))
102 }
103
104 fn from_text(text: &str) -> ast::RecordPat {
105 ast_node_from_file_text(&format!("fn f({}: ())", text))
106 }
107}
108
109impl Make<ast::PathPat> {
110 pub fn from_path(path: ast::Path) -> ast::PathPat {
111 let path_str = path.syntax().text().to_string();
112 Self::from_text(path_str.as_str())
113 }
114
115 fn from_text(text: &str) -> ast::PathPat {
116 ast_node_from_file_text(&format!("fn f({}: ())", text))
117 }
118}
119
120impl Make<ast::MatchArm> {
121 pub fn from(pats: impl Iterator<Item = ast::Pat>, expr: ast::Expr) -> ast::MatchArm {
122 let pats_str = pats.map(|p| p.syntax().to_string()).join(" | ");
123 Self::from_text(&format!("{} => {}", pats_str, expr.syntax()))
124 }
125
126 fn from_text(text: &str) -> ast::MatchArm {
127 ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text))
128 }
129}
130
131impl Make<ast::MatchArmList> {
132 pub fn from_arms(arms: impl Iterator<Item = ast::MatchArm>) -> ast::MatchArmList {
133 let arms_str = arms.map(|arm| format!("\n {}", arm.syntax())).join(",");
134 Self::from_text(&format!("{},\n", arms_str))
135 }
136
137 fn from_text(text: &str) -> ast::MatchArmList {
138 ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text))
139 }
140}
141
142impl Make<ast::WherePred> {
143 pub fn from(path: ast::Path, bounds: impl Iterator<Item = ast::TypeBound>) -> ast::WherePred {
144 let bounds = bounds.map(|b| b.syntax().to_string()).collect::<Vec<_>>().join(" + ");
145 Self::from_text(&format!("{}: {}", path.syntax(), bounds))
146 }
147
148 fn from_text(text: &str) -> ast::WherePred {
149 ast_node_from_file_text(&format!("fn f() where {} {{ }}", text))
150 }
151}
152
153impl Make<ast::WhereClause> {
154 pub fn from_predicates(preds: impl Iterator<Item = ast::WherePred>) -> ast::WhereClause {
155 let preds = preds.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", ");
156 Self::from_text(preds.as_str())
157 }
158
159 fn from_text(text: &str) -> ast::WhereClause {
160 ast_node_from_file_text(&format!("fn f() where {} {{ }}", text))
161 }
162}
163
164fn ast_node_from_file_text<N: AstNode>(text: &str) -> N {
165 let parse = SourceFile::parse(text);
166 let res = parse.tree().syntax().descendants().find_map(N::cast).unwrap();
167 res
168}
169
170pub(crate) mod tokens {
171 use once_cell::sync::Lazy;
172 use ra_syntax::{AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken, T};
173
174 static SOURCE_FILE: Lazy<Parse<SourceFile>> = Lazy::new(|| SourceFile::parse(",\n; ;"));
175
176 pub(crate) fn comma() -> SyntaxToken {
177 SOURCE_FILE
178 .tree()
179 .syntax()
180 .descendants_with_tokens()
181 .filter_map(|it| it.into_token())
182 .find(|it| it.kind() == T![,])
183 .unwrap()
184 }
185
186 pub(crate) fn single_space() -> SyntaxToken {
187 SOURCE_FILE
188 .tree()
189 .syntax()
190 .descendants_with_tokens()
191 .filter_map(|it| it.into_token())
192 .find(|it| it.kind() == WHITESPACE && it.text().as_str() == " ")
193 .unwrap()
194 }
195
196 #[allow(unused)]
197 pub(crate) fn single_newline() -> SyntaxToken {
198 SOURCE_FILE
199 .tree()
200 .syntax()
201 .descendants_with_tokens()
202 .filter_map(|it| it.into_token())
203 .find(|it| it.kind() == WHITESPACE && it.text().as_str() == "\n")
204 .unwrap()
205 }
206
207 pub(crate) struct WsBuilder(SourceFile);
208
209 impl WsBuilder {
210 pub(crate) fn new(text: &str) -> WsBuilder {
211 WsBuilder(SourceFile::parse(text).ok().unwrap())
212 }
213 pub(crate) fn ws(&self) -> SyntaxToken {
214 self.0.syntax().first_child_or_token().unwrap().into_token().unwrap()
215 }
216 }
217
218}