diff options
author | Aleksey Kladov <[email protected]> | 2019-09-26 10:18:26 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-09-26 13:29:28 +0100 |
commit | 183a38fb50f284de1ca02c05ed945e240f3c0274 (patch) | |
tree | 6f318218b3a97c9a2dba3e8311de4fddbce23b27 /crates | |
parent | d6bbdfefa7ed4b2b567558e76d5adadda9d9b83f (diff) |
keep ast creation API simple
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_assists/src/assists/add_missing_impl_members.rs | 7 | ||||
-rw-r--r-- | crates/ra_assists/src/assists/fill_match_arms.rs | 27 | ||||
-rw-r--r-- | crates/ra_assists/src/assists/move_bounds.rs | 10 | ||||
-rw-r--r-- | crates/ra_assists/src/ast_builder.rs | 218 | ||||
-rw-r--r-- | crates/ra_assists/src/ast_editor.rs | 52 | ||||
-rw-r--r-- | crates/ra_assists/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/ra_ide_api/src/diagnostics.rs | 9 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast.rs | 1 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/make.rs | 135 |
9 files changed, 213 insertions, 250 deletions
diff --git a/crates/ra_assists/src/assists/add_missing_impl_members.rs b/crates/ra_assists/src/assists/add_missing_impl_members.rs index 22d20909d..23da1e65f 100644 --- a/crates/ra_assists/src/assists/add_missing_impl_members.rs +++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs | |||
@@ -1,10 +1,10 @@ | |||
1 | use hir::{db::HirDatabase, HasSource}; | 1 | use hir::{db::HirDatabase, HasSource}; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | ast::{self, AstNode, NameOwner}, | 3 | ast::{self, make, AstNode, NameOwner}, |
4 | SmolStr, | 4 | SmolStr, |
5 | }; | 5 | }; |
6 | 6 | ||
7 | use crate::{ast_builder::Make, ast_editor::AstEditor, Assist, AssistCtx, AssistId}; | 7 | use crate::{ast_editor::AstEditor, Assist, AssistCtx, AssistId}; |
8 | 8 | ||
9 | #[derive(PartialEq)] | 9 | #[derive(PartialEq)] |
10 | enum AddMissingImplMembersMode { | 10 | enum AddMissingImplMembersMode { |
@@ -102,7 +102,8 @@ fn strip_docstring(item: ast::ImplItem) -> ast::ImplItem { | |||
102 | fn add_body(fn_def: ast::FnDef) -> ast::FnDef { | 102 | fn add_body(fn_def: ast::FnDef) -> ast::FnDef { |
103 | let mut ast_editor = AstEditor::new(fn_def.clone()); | 103 | let mut ast_editor = AstEditor::new(fn_def.clone()); |
104 | if fn_def.body().is_none() { | 104 | if fn_def.body().is_none() { |
105 | ast_editor.set_body(&Make::<ast::Block>::single_expr(Make::<ast::Expr>::unimplemented())); | 105 | let body = make::block_from_expr(make::expr_unimplemented()); |
106 | ast_editor.set_body(&body); | ||
106 | } | 107 | } |
107 | ast_editor.ast().to_owned() | 108 | ast_editor.ast().to_owned() |
108 | } | 109 | } |
diff --git a/crates/ra_assists/src/assists/fill_match_arms.rs b/crates/ra_assists/src/assists/fill_match_arms.rs index 817433526..db82db89a 100644 --- a/crates/ra_assists/src/assists/fill_match_arms.rs +++ b/crates/ra_assists/src/assists/fill_match_arms.rs | |||
@@ -1,9 +1,9 @@ | |||
1 | use std::iter; | 1 | use std::iter; |
2 | 2 | ||
3 | use hir::{db::HirDatabase, Adt, HasSource}; | 3 | use hir::{db::HirDatabase, Adt, HasSource}; |
4 | use ra_syntax::ast::{self, AstNode, NameOwner}; | 4 | use ra_syntax::ast::{self, make, AstNode, NameOwner}; |
5 | 5 | ||
6 | use crate::{ast_builder::Make, Assist, AssistCtx, AssistId}; | 6 | use crate::{Assist, AssistCtx, AssistId}; |
7 | 7 | ||
8 | pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 8 | pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
9 | let match_expr = ctx.node_at_offset::<ast::MatchExpr>()?; | 9 | let match_expr = ctx.node_at_offset::<ast::MatchExpr>()?; |
@@ -31,8 +31,8 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As | |||
31 | let variants = variant_list.variants(); | 31 | let variants = variant_list.variants(); |
32 | let arms = variants | 32 | let arms = variants |
33 | .filter_map(build_pat) | 33 | .filter_map(build_pat) |
34 | .map(|pat| Make::<ast::MatchArm>::from(iter::once(pat), Make::<ast::Expr>::unit())); | 34 | .map(|pat| make::match_arm(iter::once(pat), make::expr_unit())); |
35 | let new_arm_list = Make::<ast::MatchArmList>::from_arms(arms); | 35 | let new_arm_list = make::match_arm_list(arms); |
36 | 36 | ||
37 | edit.target(match_expr.syntax().text_range()); | 37 | edit.target(match_expr.syntax().text_range()); |
38 | edit.set_cursor(expr.syntax().text_range().start()); | 38 | edit.set_cursor(expr.syntax().text_range().start()); |
@@ -63,21 +63,22 @@ fn resolve_enum_def( | |||
63 | } | 63 | } |
64 | 64 | ||
65 | fn build_pat(var: ast::EnumVariant) -> Option<ast::Pat> { | 65 | fn build_pat(var: ast::EnumVariant) -> Option<ast::Pat> { |
66 | let path = Make::<ast::Path>::from(var.parent_enum().name()?, var.name()?); | 66 | let path = make::path_qualified( |
67 | make::path_from_name_ref(make::name_ref(&var.parent_enum().name()?.syntax().to_string())), | ||
68 | make::name_ref(&var.name()?.syntax().to_string()), | ||
69 | ); | ||
67 | 70 | ||
68 | let pat: ast::Pat = match var.kind() { | 71 | let pat: ast::Pat = match var.kind() { |
69 | ast::StructKind::Tuple(field_list) => { | 72 | ast::StructKind::Tuple(field_list) => { |
70 | let pats = iter::repeat(Make::<ast::PlaceholderPat>::placeholder().into()) | 73 | let pats = |
71 | .take(field_list.fields().count()); | 74 | iter::repeat(make::placeholder_pat().into()).take(field_list.fields().count()); |
72 | Make::<ast::TupleStructPat>::from(path, pats).into() | 75 | make::tuple_struct_pat(path, pats).into() |
73 | } | 76 | } |
74 | ast::StructKind::Named(field_list) => { | 77 | ast::StructKind::Named(field_list) => { |
75 | let pats = field_list | 78 | let pats = field_list.fields().map(|f| make::bind_pat(f.name().unwrap()).into()); |
76 | .fields() | 79 | make::record_pat(path, pats).into() |
77 | .map(|f| Make::<ast::BindPat>::from_name(f.name().unwrap()).into()); | ||
78 | Make::<ast::RecordPat>::from(path, pats).into() | ||
79 | } | 80 | } |
80 | ast::StructKind::Unit => Make::<ast::PathPat>::from_path(path).into(), | 81 | ast::StructKind::Unit => make::path_pat(path).into(), |
81 | }; | 82 | }; |
82 | 83 | ||
83 | Some(pat) | 84 | Some(pat) |
diff --git a/crates/ra_assists/src/assists/move_bounds.rs b/crates/ra_assists/src/assists/move_bounds.rs index 671826013..fd4bdc55c 100644 --- a/crates/ra_assists/src/assists/move_bounds.rs +++ b/crates/ra_assists/src/assists/move_bounds.rs | |||
@@ -1,11 +1,11 @@ | |||
1 | use hir::db::HirDatabase; | 1 | use hir::db::HirDatabase; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | ast::{self, AstNode, NameOwner, TypeBoundsOwner}, | 3 | ast::{self, make, AstNode, NameOwner, TypeBoundsOwner}, |
4 | SyntaxElement, | 4 | SyntaxElement, |
5 | SyntaxKind::*, | 5 | SyntaxKind::*, |
6 | }; | 6 | }; |
7 | 7 | ||
8 | use crate::{ast_builder::Make, ast_editor::AstEditor, Assist, AssistCtx, AssistId}; | 8 | use crate::{ast_editor::AstEditor, Assist, AssistCtx, AssistId}; |
9 | 9 | ||
10 | pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 10 | pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
11 | let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?; | 11 | let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?; |
@@ -50,7 +50,7 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) | |||
50 | 50 | ||
51 | let where_clause = { | 51 | let where_clause = { |
52 | let predicates = type_param_list.type_params().filter_map(build_predicate); | 52 | let predicates = type_param_list.type_params().filter_map(build_predicate); |
53 | Make::<ast::WhereClause>::from_predicates(predicates) | 53 | make::where_clause(predicates) |
54 | }; | 54 | }; |
55 | 55 | ||
56 | let to_insert = match anchor.prev_sibling_or_token() { | 56 | let to_insert = match anchor.prev_sibling_or_token() { |
@@ -68,8 +68,8 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) | |||
68 | } | 68 | } |
69 | 69 | ||
70 | fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> { | 70 | fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> { |
71 | let path = Make::<ast::Path>::from_name(param.name()?); | 71 | let path = make::path_from_name_ref(make::name_ref(¶m.name()?.syntax().to_string())); |
72 | let predicate = Make::<ast::WherePred>::from(path, param.type_bound_list()?.bounds()); | 72 | let predicate = make::where_pred(path, param.type_bound_list()?.bounds()); |
73 | Some(predicate) | 73 | Some(predicate) |
74 | } | 74 | } |
75 | 75 | ||
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 @@ | |||
1 | use itertools::Itertools; | ||
2 | |||
3 | use ra_syntax::{ast, AstNode, SourceFile}; | ||
4 | |||
5 | pub struct Make<N: AstNode> { | ||
6 | _phantom: std::marker::PhantomData<N>, | ||
7 | } | ||
8 | |||
9 | impl 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 | |||
22 | impl 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 | |||
32 | impl 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 | |||
46 | impl Make<ast::NameRef> { | ||
47 | pub fn from(text: &str) -> ast::NameRef { | ||
48 | ast_node_from_file_text(&format!("fn f() {{ {}; }}", text)) | ||
49 | } | ||
50 | } | ||
51 | |||
52 | impl 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 | |||
67 | impl 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 | |||
77 | impl 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 | |||
87 | impl 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 | |||
98 | impl 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 | |||
109 | impl 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 | |||
120 | impl 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 | |||
131 | impl 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 | |||
142 | impl 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 | |||
153 | impl 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 | |||
164 | fn 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 | |||
170 | pub(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 | } | ||
diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs index 4e253f0a4..2936c094b 100644 --- a/crates/ra_assists/src/ast_editor.rs +++ b/crates/ra_assists/src/ast_editor.rs | |||
@@ -13,8 +13,6 @@ use ra_syntax::{ | |||
13 | }; | 13 | }; |
14 | use ra_text_edit::TextEditBuilder; | 14 | use ra_text_edit::TextEditBuilder; |
15 | 15 | ||
16 | use crate::ast_builder::tokens; | ||
17 | |||
18 | pub struct AstEditor<N: AstNode> { | 16 | pub struct AstEditor<N: AstNode> { |
19 | original_ast: N, | 17 | original_ast: N, |
20 | ast: N, | 18 | ast: N, |
@@ -286,3 +284,53 @@ impl AstEditor<ast::TypeParam> { | |||
286 | self | 284 | self |
287 | } | 285 | } |
288 | } | 286 | } |
287 | |||
288 | mod tokens { | ||
289 | use once_cell::sync::Lazy; | ||
290 | use ra_syntax::{AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken, T}; | ||
291 | |||
292 | static SOURCE_FILE: Lazy<Parse<SourceFile>> = Lazy::new(|| SourceFile::parse(",\n; ;")); | ||
293 | |||
294 | pub(crate) fn comma() -> SyntaxToken { | ||
295 | SOURCE_FILE | ||
296 | .tree() | ||
297 | .syntax() | ||
298 | .descendants_with_tokens() | ||
299 | .filter_map(|it| it.into_token()) | ||
300 | .find(|it| it.kind() == T![,]) | ||
301 | .unwrap() | ||
302 | } | ||
303 | |||
304 | pub(crate) fn single_space() -> SyntaxToken { | ||
305 | SOURCE_FILE | ||
306 | .tree() | ||
307 | .syntax() | ||
308 | .descendants_with_tokens() | ||
309 | .filter_map(|it| it.into_token()) | ||
310 | .find(|it| it.kind() == WHITESPACE && it.text().as_str() == " ") | ||
311 | .unwrap() | ||
312 | } | ||
313 | |||
314 | #[allow(unused)] | ||
315 | pub(crate) fn single_newline() -> SyntaxToken { | ||
316 | SOURCE_FILE | ||
317 | .tree() | ||
318 | .syntax() | ||
319 | .descendants_with_tokens() | ||
320 | .filter_map(|it| it.into_token()) | ||
321 | .find(|it| it.kind() == WHITESPACE && it.text().as_str() == "\n") | ||
322 | .unwrap() | ||
323 | } | ||
324 | |||
325 | pub(crate) struct WsBuilder(SourceFile); | ||
326 | |||
327 | impl WsBuilder { | ||
328 | pub(crate) fn new(text: &str) -> WsBuilder { | ||
329 | WsBuilder(SourceFile::parse(text).ok().unwrap()) | ||
330 | } | ||
331 | pub(crate) fn ws(&self) -> SyntaxToken { | ||
332 | self.0.syntax().first_child_or_token().unwrap().into_token().unwrap() | ||
333 | } | ||
334 | } | ||
335 | |||
336 | } | ||
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 71b017076..897af2b02 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -8,11 +8,9 @@ | |||
8 | mod assist_ctx; | 8 | mod assist_ctx; |
9 | mod marks; | 9 | mod marks; |
10 | pub mod ast_editor; | 10 | pub mod ast_editor; |
11 | pub mod ast_builder; | ||
12 | |||
13 | use itertools::Itertools; | ||
14 | 11 | ||
15 | use hir::db::HirDatabase; | 12 | use hir::db::HirDatabase; |
13 | use itertools::Itertools; | ||
16 | use ra_db::FileRange; | 14 | use ra_db::FileRange; |
17 | use ra_syntax::{TextRange, TextUnit}; | 15 | use ra_syntax::{TextRange, TextUnit}; |
18 | use ra_text_edit::TextEdit; | 16 | use ra_text_edit::TextEdit; |
diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index f07061e99..144bc0a70 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs | |||
@@ -2,11 +2,11 @@ use std::cell::RefCell; | |||
2 | 2 | ||
3 | use hir::diagnostics::{AstDiagnostic, Diagnostic as _, DiagnosticSink}; | 3 | use hir::diagnostics::{AstDiagnostic, Diagnostic as _, DiagnosticSink}; |
4 | use itertools::Itertools; | 4 | use itertools::Itertools; |
5 | use ra_assists::{ast_builder::Make, ast_editor::AstEditor}; | 5 | use ra_assists::ast_editor::AstEditor; |
6 | use ra_db::SourceDatabase; | 6 | use ra_db::SourceDatabase; |
7 | use ra_prof::profile; | 7 | use ra_prof::profile; |
8 | use ra_syntax::{ | 8 | use ra_syntax::{ |
9 | ast::{self, AstNode}, | 9 | ast::{self, make, AstNode}, |
10 | Location, SyntaxNode, TextRange, T, | 10 | Location, SyntaxNode, TextRange, T, |
11 | }; | 11 | }; |
12 | use ra_text_edit::{TextEdit, TextEditBuilder}; | 12 | use ra_text_edit::{TextEdit, TextEditBuilder}; |
@@ -59,10 +59,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic> | |||
59 | let node = d.ast(db); | 59 | let node = d.ast(db); |
60 | let mut ast_editor = AstEditor::new(node); | 60 | let mut ast_editor = AstEditor::new(node); |
61 | for f in d.missed_fields.iter() { | 61 | for f in d.missed_fields.iter() { |
62 | let field = Make::<ast::RecordField>::from( | 62 | let field = make::record_field(make::name_ref(&f.to_string()), Some(make::expr_unit())); |
63 | Make::<ast::NameRef>::from(&f.to_string()), | ||
64 | Some(Make::<ast::Expr>::unit()), | ||
65 | ); | ||
66 | ast_editor.append_field(&field); | 63 | ast_editor.append_field(&field); |
67 | } | 64 | } |
68 | 65 | ||
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index a2f862869..f464d6534 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs | |||
@@ -5,6 +5,7 @@ mod traits; | |||
5 | mod tokens; | 5 | mod tokens; |
6 | mod extensions; | 6 | mod extensions; |
7 | mod expr_extensions; | 7 | mod expr_extensions; |
8 | pub mod make; | ||
8 | 9 | ||
9 | use std::marker::PhantomData; | 10 | use std::marker::PhantomData; |
10 | 11 | ||
diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs new file mode 100644 index 000000000..c06c62b3b --- /dev/null +++ b/crates/ra_syntax/src/ast/make.rs | |||
@@ -0,0 +1,135 @@ | |||
1 | //! This module contains free-standing functions for creating AST fragments out | ||
2 | //! of smaller pieces. | ||
3 | use itertools::Itertools; | ||
4 | |||
5 | use crate::{ast, AstNode, SourceFile}; | ||
6 | |||
7 | pub fn name_ref(text: &str) -> ast::NameRef { | ||
8 | ast_from_text(&format!("fn f() {{ {}; }}", text)) | ||
9 | } | ||
10 | |||
11 | pub fn path_from_name_ref(name_ref: ast::NameRef) -> ast::Path { | ||
12 | path_from_text(&name_ref.syntax().to_string()) | ||
13 | } | ||
14 | pub fn path_qualified(qual: ast::Path, name_ref: ast::NameRef) -> ast::Path { | ||
15 | path_from_text(&format!("{}::{}", qual.syntax(), name_ref.syntax())) | ||
16 | } | ||
17 | fn path_from_text(text: &str) -> ast::Path { | ||
18 | ast_from_text(text) | ||
19 | } | ||
20 | |||
21 | pub fn record_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField { | ||
22 | return match expr { | ||
23 | Some(expr) => from_text(&format!("{}: {}", name.syntax(), expr.syntax())), | ||
24 | None => from_text(&name.syntax().to_string()), | ||
25 | }; | ||
26 | |||
27 | fn from_text(text: &str) -> ast::RecordField { | ||
28 | ast_from_text(&format!("fn f() {{ S {{ {}, }} }}", text)) | ||
29 | } | ||
30 | } | ||
31 | |||
32 | pub fn block_from_expr(e: ast::Expr) -> ast::Block { | ||
33 | return from_text(&format!("{{ {} }}", e.syntax())); | ||
34 | |||
35 | fn from_text(text: &str) -> ast::Block { | ||
36 | ast_from_text(&format!("fn f() {}", text)) | ||
37 | } | ||
38 | } | ||
39 | |||
40 | pub fn expr_unit() -> ast::Expr { | ||
41 | expr_from_text("()") | ||
42 | } | ||
43 | pub fn expr_unimplemented() -> ast::Expr { | ||
44 | expr_from_text("unimplemented!()") | ||
45 | } | ||
46 | fn expr_from_text(text: &str) -> ast::Expr { | ||
47 | ast_from_text(&format!("const C: () = {};", text)) | ||
48 | } | ||
49 | |||
50 | pub fn bind_pat(name: ast::Name) -> ast::BindPat { | ||
51 | return from_text(name.text()); | ||
52 | |||
53 | fn from_text(text: &str) -> ast::BindPat { | ||
54 | ast_from_text(&format!("fn f({}: ())", text)) | ||
55 | } | ||
56 | } | ||
57 | |||
58 | pub fn placeholder_pat() -> ast::PlaceholderPat { | ||
59 | return from_text("_"); | ||
60 | |||
61 | fn from_text(text: &str) -> ast::PlaceholderPat { | ||
62 | ast_from_text(&format!("fn f({}: ())", text)) | ||
63 | } | ||
64 | } | ||
65 | |||
66 | pub fn tuple_struct_pat( | ||
67 | path: ast::Path, | ||
68 | pats: impl Iterator<Item = ast::Pat>, | ||
69 | ) -> ast::TupleStructPat { | ||
70 | let pats_str = pats.map(|p| p.syntax().to_string()).join(", "); | ||
71 | return from_text(&format!("{}({})", path.syntax(), pats_str)); | ||
72 | |||
73 | fn from_text(text: &str) -> ast::TupleStructPat { | ||
74 | ast_from_text(&format!("fn f({}: ())", text)) | ||
75 | } | ||
76 | } | ||
77 | |||
78 | pub fn record_pat(path: ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::RecordPat { | ||
79 | let pats_str = pats.map(|p| p.syntax().to_string()).join(", "); | ||
80 | return from_text(&format!("{}{{ {} }}", path.syntax(), pats_str)); | ||
81 | |||
82 | fn from_text(text: &str) -> ast::RecordPat { | ||
83 | ast_from_text(&format!("fn f({}: ())", text)) | ||
84 | } | ||
85 | } | ||
86 | |||
87 | pub fn path_pat(path: ast::Path) -> ast::PathPat { | ||
88 | let path_str = path.syntax().text().to_string(); | ||
89 | return from_text(path_str.as_str()); | ||
90 | fn from_text(text: &str) -> ast::PathPat { | ||
91 | ast_from_text(&format!("fn f({}: ())", text)) | ||
92 | } | ||
93 | } | ||
94 | |||
95 | pub fn match_arm(pats: impl Iterator<Item = ast::Pat>, expr: ast::Expr) -> ast::MatchArm { | ||
96 | let pats_str = pats.map(|p| p.syntax().to_string()).join(" | "); | ||
97 | return from_text(&format!("{} => {}", pats_str, expr.syntax())); | ||
98 | |||
99 | fn from_text(text: &str) -> ast::MatchArm { | ||
100 | ast_from_text(&format!("fn f() {{ match () {{{}}} }}", text)) | ||
101 | } | ||
102 | } | ||
103 | |||
104 | pub fn match_arm_list(arms: impl Iterator<Item = ast::MatchArm>) -> ast::MatchArmList { | ||
105 | let arms_str = arms.map(|arm| format!("\n {}", arm.syntax())).join(","); | ||
106 | return from_text(&format!("{},\n", arms_str)); | ||
107 | |||
108 | fn from_text(text: &str) -> ast::MatchArmList { | ||
109 | ast_from_text(&format!("fn f() {{ match () {{{}}} }}", text)) | ||
110 | } | ||
111 | } | ||
112 | |||
113 | pub fn where_pred(path: ast::Path, bounds: impl Iterator<Item = ast::TypeBound>) -> ast::WherePred { | ||
114 | let bounds = bounds.map(|b| b.syntax().to_string()).join(" + "); | ||
115 | return from_text(&format!("{}: {}", path.syntax(), bounds)); | ||
116 | |||
117 | fn from_text(text: &str) -> ast::WherePred { | ||
118 | ast_from_text(&format!("fn f() where {} {{ }}", text)) | ||
119 | } | ||
120 | } | ||
121 | |||
122 | pub fn where_clause(preds: impl Iterator<Item = ast::WherePred>) -> ast::WhereClause { | ||
123 | let preds = preds.map(|p| p.syntax().to_string()).join(", "); | ||
124 | return from_text(preds.as_str()); | ||
125 | |||
126 | fn from_text(text: &str) -> ast::WhereClause { | ||
127 | ast_from_text(&format!("fn f() where {} {{ }}", text)) | ||
128 | } | ||
129 | } | ||
130 | |||
131 | fn ast_from_text<N: AstNode>(text: &str) -> N { | ||
132 | let parse = SourceFile::parse(text); | ||
133 | let res = parse.tree().syntax().descendants().find_map(N::cast).unwrap(); | ||
134 | res | ||
135 | } | ||