aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/assists/add_missing_impl_members.rs6
-rw-r--r--crates/ra_assists/src/assists/fill_match_arms.rs25
-rw-r--r--crates/ra_assists/src/assists/move_bounds.rs9
-rw-r--r--crates/ra_assists/src/ast_builder.rs48
-rw-r--r--crates/ra_ide_api/src/diagnostics.rs8
5 files changed, 42 insertions, 54 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 11d69a04b..22d20909d 100644
--- a/crates/ra_assists/src/assists/add_missing_impl_members.rs
+++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs
@@ -4,7 +4,7 @@ use ra_syntax::{
4 SmolStr, 4 SmolStr,
5}; 5};
6 6
7use crate::{ast_builder::AstBuilder, ast_editor::AstEditor, Assist, AssistCtx, AssistId}; 7use crate::{ast_builder::Make, ast_editor::AstEditor, Assist, AssistCtx, AssistId};
8 8
9#[derive(PartialEq)] 9#[derive(PartialEq)]
10enum AddMissingImplMembersMode { 10enum AddMissingImplMembersMode {
@@ -102,9 +102,7 @@ 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(&AstBuilder::<ast::Block>::single_expr( 105 ast_editor.set_body(&Make::<ast::Block>::single_expr(Make::<ast::Expr>::unimplemented()));
106 AstBuilder::<ast::Expr>::unimplemented(),
107 ));
108 } 106 }
109 ast_editor.ast().to_owned() 107 ast_editor.ast().to_owned()
110} 108}
diff --git a/crates/ra_assists/src/assists/fill_match_arms.rs b/crates/ra_assists/src/assists/fill_match_arms.rs
index f1c36b805..817433526 100644
--- a/crates/ra_assists/src/assists/fill_match_arms.rs
+++ b/crates/ra_assists/src/assists/fill_match_arms.rs
@@ -3,7 +3,7 @@ use std::iter;
3use hir::{db::HirDatabase, Adt, HasSource}; 3use hir::{db::HirDatabase, Adt, HasSource};
4use ra_syntax::ast::{self, AstNode, NameOwner}; 4use ra_syntax::ast::{self, AstNode, NameOwner};
5 5
6use crate::{ast_builder::AstBuilder, Assist, AssistCtx, AssistId}; 6use crate::{ast_builder::Make, 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>()?;
@@ -29,13 +29,10 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
29 29
30 ctx.add_action(AssistId("fill_match_arms"), "fill match arms", |edit| { 30 ctx.add_action(AssistId("fill_match_arms"), "fill match arms", |edit| {
31 let variants = variant_list.variants(); 31 let variants = variant_list.variants();
32 let arms = variants.filter_map(build_pat).map(|pat| { 32 let arms = variants
33 AstBuilder::<ast::MatchArm>::from_pieces( 33 .filter_map(build_pat)
34 iter::once(pat), 34 .map(|pat| Make::<ast::MatchArm>::from(iter::once(pat), Make::<ast::Expr>::unit()));
35 AstBuilder::<ast::Expr>::unit(), 35 let new_arm_list = Make::<ast::MatchArmList>::from_arms(arms);
36 )
37 });
38 let new_arm_list = AstBuilder::<ast::MatchArmList>::from_arms(arms);
39 36
40 edit.target(match_expr.syntax().text_range()); 37 edit.target(match_expr.syntax().text_range());
41 edit.set_cursor(expr.syntax().text_range().start()); 38 edit.set_cursor(expr.syntax().text_range().start());
@@ -66,21 +63,21 @@ fn resolve_enum_def(
66} 63}
67 64
68fn build_pat(var: ast::EnumVariant) -> Option<ast::Pat> { 65fn build_pat(var: ast::EnumVariant) -> Option<ast::Pat> {
69 let path = AstBuilder::<ast::Path>::from_pieces(var.parent_enum().name()?, var.name()?); 66 let path = Make::<ast::Path>::from(var.parent_enum().name()?, var.name()?);
70 67
71 let pat: ast::Pat = match var.kind() { 68 let pat: ast::Pat = match var.kind() {
72 ast::StructKind::Tuple(field_list) => { 69 ast::StructKind::Tuple(field_list) => {
73 let pats = iter::repeat(AstBuilder::<ast::PlaceholderPat>::placeholder().into()) 70 let pats = iter::repeat(Make::<ast::PlaceholderPat>::placeholder().into())
74 .take(field_list.fields().count()); 71 .take(field_list.fields().count());
75 AstBuilder::<ast::TupleStructPat>::from_pieces(path, pats).into() 72 Make::<ast::TupleStructPat>::from(path, pats).into()
76 } 73 }
77 ast::StructKind::Named(field_list) => { 74 ast::StructKind::Named(field_list) => {
78 let pats = field_list 75 let pats = field_list
79 .fields() 76 .fields()
80 .map(|f| AstBuilder::<ast::BindPat>::from_name(f.name().unwrap()).into()); 77 .map(|f| Make::<ast::BindPat>::from_name(f.name().unwrap()).into());
81 AstBuilder::<ast::RecordPat>::from_pieces(path, pats).into() 78 Make::<ast::RecordPat>::from(path, pats).into()
82 } 79 }
83 ast::StructKind::Unit => AstBuilder::<ast::PathPat>::from_path(path).into(), 80 ast::StructKind::Unit => Make::<ast::PathPat>::from_path(path).into(),
84 }; 81 };
85 82
86 Some(pat) 83 Some(pat)
diff --git a/crates/ra_assists/src/assists/move_bounds.rs b/crates/ra_assists/src/assists/move_bounds.rs
index aa9036fed..6fd2fb72b 100644
--- a/crates/ra_assists/src/assists/move_bounds.rs
+++ b/crates/ra_assists/src/assists/move_bounds.rs
@@ -6,7 +6,7 @@ use ra_syntax::{
6 TextRange, 6 TextRange,
7}; 7};
8 8
9use crate::{ast_builder::AstBuilder, Assist, AssistCtx, AssistId}; 9use crate::{ast_builder::Make, Assist, AssistCtx, AssistId};
10 10
11pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 11pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
12 let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?; 12 let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?;
@@ -52,7 +52,7 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>)
52 } 52 }
53 53
54 let predicates = type_params.iter().filter_map(build_predicate); 54 let predicates = type_params.iter().filter_map(build_predicate);
55 let where_clause = AstBuilder::<ast::WhereClause>::from_predicates(predicates); 55 let where_clause = Make::<ast::WhereClause>::from_predicates(predicates);
56 56
57 let to_insert = match anchor.prev_sibling_or_token() { 57 let to_insert = match anchor.prev_sibling_or_token() {
58 Some(ref elem) if elem.kind() == WHITESPACE => { 58 Some(ref elem) if elem.kind() == WHITESPACE => {
@@ -69,9 +69,8 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>)
69} 69}
70 70
71fn build_predicate(param: &ast::TypeParam) -> Option<ast::WherePred> { 71fn build_predicate(param: &ast::TypeParam) -> Option<ast::WherePred> {
72 let path = AstBuilder::<ast::Path>::from_name(param.name()?); 72 let path = Make::<ast::Path>::from_name(param.name()?);
73 let predicate = 73 let predicate = Make::<ast::WherePred>::from(path, param.type_bound_list()?.bounds());
74 AstBuilder::<ast::WherePred>::from_pieces(path, param.type_bound_list()?.bounds());
75 Some(predicate) 74 Some(predicate)
76} 75}
77 76
diff --git a/crates/ra_assists/src/ast_builder.rs b/crates/ra_assists/src/ast_builder.rs
index 171b1c936..f6409085a 100644
--- a/crates/ra_assists/src/ast_builder.rs
+++ b/crates/ra_assists/src/ast_builder.rs
@@ -2,12 +2,12 @@ use itertools::Itertools;
2 2
3use ra_syntax::{ast, AstNode, SourceFile}; 3use ra_syntax::{ast, AstNode, SourceFile};
4 4
5pub struct AstBuilder<N: AstNode> { 5pub struct Make<N: AstNode> {
6 _phantom: std::marker::PhantomData<N>, 6 _phantom: std::marker::PhantomData<N>,
7} 7}
8 8
9impl AstBuilder<ast::RecordField> { 9impl Make<ast::RecordField> {
10 pub fn from_pieces(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField { 10 pub fn from(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField {
11 match expr { 11 match expr {
12 Some(expr) => Self::from_text(&format!("{}: {}", name.syntax(), expr.syntax())), 12 Some(expr) => Self::from_text(&format!("{}: {}", name.syntax(), expr.syntax())),
13 None => Self::from_text(&name.syntax().to_string()), 13 None => Self::from_text(&name.syntax().to_string()),
@@ -19,7 +19,7 @@ impl AstBuilder<ast::RecordField> {
19 } 19 }
20} 20}
21 21
22impl AstBuilder<ast::Block> { 22impl Make<ast::Block> {
23 pub fn single_expr(e: ast::Expr) -> ast::Block { 23 pub fn single_expr(e: ast::Expr) -> ast::Block {
24 Self::from_text(&format!("{{ {} }}", e.syntax())) 24 Self::from_text(&format!("{{ {} }}", e.syntax()))
25 } 25 }
@@ -29,7 +29,7 @@ impl AstBuilder<ast::Block> {
29 } 29 }
30} 30}
31 31
32impl AstBuilder<ast::Expr> { 32impl Make<ast::Expr> {
33 pub fn unit() -> ast::Expr { 33 pub fn unit() -> ast::Expr {
34 Self::from_text("()") 34 Self::from_text("()")
35 } 35 }
@@ -43,19 +43,19 @@ impl AstBuilder<ast::Expr> {
43 } 43 }
44} 44}
45 45
46impl AstBuilder<ast::NameRef> { 46impl Make<ast::NameRef> {
47 pub fn new(text: &str) -> ast::NameRef { 47 pub fn new(text: &str) -> ast::NameRef {
48 ast_node_from_file_text(&format!("fn f() {{ {}; }}", text)) 48 ast_node_from_file_text(&format!("fn f() {{ {}; }}", text))
49 } 49 }
50} 50}
51 51
52impl AstBuilder<ast::Path> { 52impl Make<ast::Path> {
53 pub fn from_name(name: ast::Name) -> ast::Path { 53 pub fn from_name(name: ast::Name) -> ast::Path {
54 let name = name.syntax().to_string(); 54 let name = name.syntax().to_string();
55 Self::from_text(name.as_str()) 55 Self::from_text(name.as_str())
56 } 56 }
57 57
58 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 {
59 Self::from_text(&format!("{}::{}", enum_name.syntax(), var_name.syntax())) 59 Self::from_text(&format!("{}::{}", enum_name.syntax(), var_name.syntax()))
60 } 60 }
61 61
@@ -64,7 +64,7 @@ impl AstBuilder<ast::Path> {
64 } 64 }
65} 65}
66 66
67impl AstBuilder<ast::BindPat> { 67impl Make<ast::BindPat> {
68 pub fn from_name(name: ast::Name) -> ast::BindPat { 68 pub fn from_name(name: ast::Name) -> ast::BindPat {
69 Self::from_text(name.text()) 69 Self::from_text(name.text())
70 } 70 }
@@ -74,7 +74,7 @@ impl AstBuilder<ast::BindPat> {
74 } 74 }
75} 75}
76 76
77impl AstBuilder<ast::PlaceholderPat> { 77impl Make<ast::PlaceholderPat> {
78 pub fn placeholder() -> ast::PlaceholderPat { 78 pub fn placeholder() -> ast::PlaceholderPat {
79 Self::from_text("_") 79 Self::from_text("_")
80 } 80 }
@@ -84,11 +84,8 @@ impl AstBuilder<ast::PlaceholderPat> {
84 } 84 }
85} 85}
86 86
87impl AstBuilder<ast::TupleStructPat> { 87impl Make<ast::TupleStructPat> {
88 pub fn from_pieces( 88 pub fn from(path: ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::TupleStructPat {
89 path: ast::Path,
90 pats: impl Iterator<Item = ast::Pat>,
91 ) -> ast::TupleStructPat {
92 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(", ");
93 Self::from_text(&format!("{}({})", path.syntax(), pats_str)) 90 Self::from_text(&format!("{}({})", path.syntax(), pats_str))
94 } 91 }
@@ -98,8 +95,8 @@ impl AstBuilder<ast::TupleStructPat> {
98 } 95 }
99} 96}
100 97
101impl AstBuilder<ast::RecordPat> { 98impl Make<ast::RecordPat> {
102 pub fn from_pieces(path: ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::RecordPat { 99 pub fn from(path: ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::RecordPat {
103 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(", ");
104 Self::from_text(&format!("{}{{ {} }}", path.syntax(), pats_str)) 101 Self::from_text(&format!("{}{{ {} }}", path.syntax(), pats_str))
105 } 102 }
@@ -109,7 +106,7 @@ impl AstBuilder<ast::RecordPat> {
109 } 106 }
110} 107}
111 108
112impl AstBuilder<ast::PathPat> { 109impl Make<ast::PathPat> {
113 pub fn from_path(path: ast::Path) -> ast::PathPat { 110 pub fn from_path(path: ast::Path) -> ast::PathPat {
114 let path_str = path.syntax().text().to_string(); 111 let path_str = path.syntax().text().to_string();
115 Self::from_text(path_str.as_str()) 112 Self::from_text(path_str.as_str())
@@ -120,8 +117,8 @@ impl AstBuilder<ast::PathPat> {
120 } 117 }
121} 118}
122 119
123impl AstBuilder<ast::MatchArm> { 120impl Make<ast::MatchArm> {
124 pub fn from_pieces(pats: impl Iterator<Item = ast::Pat>, expr: ast::Expr) -> ast::MatchArm { 121 pub fn from(pats: impl Iterator<Item = ast::Pat>, expr: ast::Expr) -> ast::MatchArm {
125 let pats_str = pats.map(|p| p.syntax().to_string()).join(" | "); 122 let pats_str = pats.map(|p| p.syntax().to_string()).join(" | ");
126 Self::from_text(&format!("{} => {}", pats_str, expr.syntax())) 123 Self::from_text(&format!("{} => {}", pats_str, expr.syntax()))
127 } 124 }
@@ -131,7 +128,7 @@ impl AstBuilder<ast::MatchArm> {
131 } 128 }
132} 129}
133 130
134impl AstBuilder<ast::MatchArmList> { 131impl Make<ast::MatchArmList> {
135 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 {
136 let arms_str = arms.map(|arm| format!("\n {}", arm.syntax())).join(","); 133 let arms_str = arms.map(|arm| format!("\n {}", arm.syntax())).join(",");
137 Self::from_text(&format!("{},\n", arms_str)) 134 Self::from_text(&format!("{},\n", arms_str))
@@ -142,11 +139,8 @@ impl AstBuilder<ast::MatchArmList> {
142 } 139 }
143} 140}
144 141
145impl AstBuilder<ast::WherePred> { 142impl Make<ast::WherePred> {
146 pub fn from_pieces( 143 pub fn from(path: ast::Path, bounds: impl Iterator<Item = ast::TypeBound>) -> ast::WherePred {
147 path: ast::Path,
148 bounds: impl Iterator<Item = ast::TypeBound>,
149 ) -> ast::WherePred {
150 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(" + ");
151 Self::from_text(&format!("{}: {}", path.syntax(), bounds)) 145 Self::from_text(&format!("{}: {}", path.syntax(), bounds))
152 } 146 }
@@ -156,7 +150,7 @@ impl AstBuilder<ast::WherePred> {
156 } 150 }
157} 151}
158 152
159impl AstBuilder<ast::WhereClause> { 153impl Make<ast::WhereClause> {
160 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 {
161 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(", ");
162 Self::from_text(preds.as_str()) 156 Self::from_text(preds.as_str())
diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs
index 6602fd27e..06e7ea6da 100644
--- a/crates/ra_ide_api/src/diagnostics.rs
+++ b/crates/ra_ide_api/src/diagnostics.rs
@@ -2,7 +2,7 @@ use std::cell::RefCell;
2 2
3use hir::diagnostics::{AstDiagnostic, Diagnostic as _, DiagnosticSink}; 3use hir::diagnostics::{AstDiagnostic, Diagnostic as _, DiagnosticSink};
4use itertools::Itertools; 4use itertools::Itertools;
5use ra_assists::{ast_builder::AstBuilder, ast_editor::AstEditor}; 5use ra_assists::{ast_builder::Make, ast_editor::AstEditor};
6use ra_db::SourceDatabase; 6use ra_db::SourceDatabase;
7use ra_prof::profile; 7use ra_prof::profile;
8use ra_syntax::{ 8use ra_syntax::{
@@ -59,9 +59,9 @@ 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 name_ref = AstBuilder::<ast::NameRef>::new(&f.to_string()); 62 let name_ref = Make::<ast::NameRef>::new(&f.to_string());
63 let unit = AstBuilder::<ast::Expr>::unit(); 63 let unit = Make::<ast::Expr>::unit();
64 ast_editor.append_field(&AstBuilder::<RecordField>::from_pieces(name_ref, Some(unit))); 64 ast_editor.append_field(&Make::<RecordField>::from(name_ref, Some(unit)));
65 } 65 }
66 66
67 let mut builder = TextEditBuilder::default(); 67 let mut builder = TextEditBuilder::default();