aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists')
-rw-r--r--crates/ra_assists/src/assists/add_missing_impl_members.rs7
-rw-r--r--crates/ra_assists/src/assists/fill_match_arms.rs27
-rw-r--r--crates/ra_assists/src/assists/move_bounds.rs10
-rw-r--r--crates/ra_assists/src/ast_builder.rs218
-rw-r--r--crates/ra_assists/src/ast_editor.rs52
-rw-r--r--crates/ra_assists/src/lib.rs4
6 files changed, 74 insertions, 244 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 @@
1use hir::{db::HirDatabase, HasSource}; 1use hir::{db::HirDatabase, HasSource};
2use ra_syntax::{ 2use ra_syntax::{
3 ast::{self, AstNode, NameOwner}, 3 ast::{self, make, AstNode, NameOwner},
4 SmolStr, 4 SmolStr,
5}; 5};
6 6
7use crate::{ast_builder::Make, ast_editor::AstEditor, Assist, AssistCtx, AssistId}; 7use crate::{ast_editor::AstEditor, Assist, AssistCtx, AssistId};
8 8
9#[derive(PartialEq)] 9#[derive(PartialEq)]
10enum AddMissingImplMembersMode { 10enum AddMissingImplMembersMode {
@@ -102,7 +102,8 @@ fn strip_docstring(item: ast::ImplItem) -> ast::ImplItem {
102fn add_body(fn_def: ast::FnDef) -> ast::FnDef { 102fn 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 @@
1use std::iter; 1use std::iter;
2 2
3use hir::{db::HirDatabase, Adt, HasSource}; 3use hir::{db::HirDatabase, Adt, HasSource};
4use ra_syntax::ast::{self, AstNode, NameOwner}; 4use ra_syntax::ast::{self, make, AstNode, NameOwner};
5 5
6use crate::{ast_builder::Make, Assist, AssistCtx, AssistId}; 6use crate::{Assist, AssistCtx, AssistId};
7 7
8pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 8pub(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
65fn build_pat(var: ast::EnumVariant) -> Option<ast::Pat> { 65fn 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 @@
1use hir::db::HirDatabase; 1use hir::db::HirDatabase;
2use ra_syntax::{ 2use 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
8use crate::{ast_builder::Make, ast_editor::AstEditor, Assist, AssistCtx, AssistId}; 8use crate::{ast_editor::AstEditor, Assist, AssistCtx, AssistId};
9 9
10pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 10pub(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
70fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> { 70fn 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(&param.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 @@
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}
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};
14use ra_text_edit::TextEditBuilder; 14use ra_text_edit::TextEditBuilder;
15 15
16use crate::ast_builder::tokens;
17
18pub struct AstEditor<N: AstNode> { 16pub 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
288mod 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 @@
8mod assist_ctx; 8mod assist_ctx;
9mod marks; 9mod marks;
10pub mod ast_editor; 10pub mod ast_editor;
11pub mod ast_builder;
12
13use itertools::Itertools;
14 11
15use hir::db::HirDatabase; 12use hir::db::HirDatabase;
13use itertools::Itertools;
16use ra_db::FileRange; 14use ra_db::FileRange;
17use ra_syntax::{TextRange, TextUnit}; 15use ra_syntax::{TextRange, TextUnit};
18use ra_text_edit::TextEdit; 16use ra_text_edit::TextEdit;