diff options
Diffstat (limited to 'crates/ra_assists/src/ast_builder.rs')
-rw-r--r-- | crates/ra_assists/src/ast_builder.rs | 48 |
1 files changed, 21 insertions, 27 deletions
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 | ||
3 | use ra_syntax::{ast, AstNode, SourceFile}; | 3 | use ra_syntax::{ast, AstNode, SourceFile}; |
4 | 4 | ||
5 | pub struct AstBuilder<N: AstNode> { | 5 | pub struct Make<N: AstNode> { |
6 | _phantom: std::marker::PhantomData<N>, | 6 | _phantom: std::marker::PhantomData<N>, |
7 | } | 7 | } |
8 | 8 | ||
9 | impl AstBuilder<ast::RecordField> { | 9 | impl 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 | ||
22 | impl AstBuilder<ast::Block> { | 22 | impl 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 | ||
32 | impl AstBuilder<ast::Expr> { | 32 | impl 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 | ||
46 | impl AstBuilder<ast::NameRef> { | 46 | impl 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 | ||
52 | impl AstBuilder<ast::Path> { | 52 | impl 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 | ||
67 | impl AstBuilder<ast::BindPat> { | 67 | impl 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 | ||
77 | impl AstBuilder<ast::PlaceholderPat> { | 77 | impl 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 | ||
87 | impl AstBuilder<ast::TupleStructPat> { | 87 | impl 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 | ||
101 | impl AstBuilder<ast::RecordPat> { | 98 | impl 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 | ||
112 | impl AstBuilder<ast::PathPat> { | 109 | impl 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 | ||
123 | impl AstBuilder<ast::MatchArm> { | 120 | impl 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 | ||
134 | impl AstBuilder<ast::MatchArmList> { | 131 | impl 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 | ||
145 | impl AstBuilder<ast::WherePred> { | 142 | impl 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 | ||
159 | impl AstBuilder<ast::WhereClause> { | 153 | impl 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()) |