diff options
Diffstat (limited to 'crates/ra_syntax/src/ast')
-rw-r--r-- | crates/ra_syntax/src/ast/edit.rs | 22 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/make.rs | 38 |
2 files changed, 47 insertions, 13 deletions
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index 1858e2b6c..40a04b9c5 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs | |||
@@ -139,7 +139,7 @@ impl ast::RecordFieldList { | |||
139 | let mut to_insert: ArrayVec<[SyntaxElement; 4]> = ArrayVec::new(); | 139 | let mut to_insert: ArrayVec<[SyntaxElement; 4]> = ArrayVec::new(); |
140 | to_insert.push(space.into()); | 140 | to_insert.push(space.into()); |
141 | to_insert.push(field.syntax().clone().into()); | 141 | to_insert.push(field.syntax().clone().into()); |
142 | to_insert.push(tokens::comma().into()); | 142 | to_insert.push(make::token(T![,]).into()); |
143 | 143 | ||
144 | macro_rules! after_l_curly { | 144 | macro_rules! after_l_curly { |
145 | () => {{ | 145 | () => {{ |
@@ -160,7 +160,7 @@ impl ast::RecordFieldList { | |||
160 | { | 160 | { |
161 | InsertPosition::After(comma) | 161 | InsertPosition::After(comma) |
162 | } else { | 162 | } else { |
163 | to_insert.insert(0, tokens::comma().into()); | 163 | to_insert.insert(0, make::token(T![,]).into()); |
164 | InsertPosition::After($anchor.syntax().clone().into()) | 164 | InsertPosition::After($anchor.syntax().clone().into()) |
165 | } | 165 | } |
166 | }; | 166 | }; |
@@ -259,6 +259,24 @@ impl ast::UseItem { | |||
259 | } | 259 | } |
260 | } | 260 | } |
261 | 261 | ||
262 | impl ast::UseTree { | ||
263 | #[must_use] | ||
264 | pub fn with_path(&self, path: ast::Path) -> ast::UseTree { | ||
265 | if let Some(old) = self.path() { | ||
266 | return replace_descendants(self, iter::once((old, path))); | ||
267 | } | ||
268 | self.clone() | ||
269 | } | ||
270 | |||
271 | #[must_use] | ||
272 | pub fn with_use_tree_list(&self, use_tree_list: ast::UseTreeList) -> ast::UseTree { | ||
273 | if let Some(old) = self.use_tree_list() { | ||
274 | return replace_descendants(self, iter::once((old, use_tree_list))); | ||
275 | } | ||
276 | self.clone() | ||
277 | } | ||
278 | } | ||
279 | |||
262 | #[must_use] | 280 | #[must_use] |
263 | pub fn strip_attrs_and_docs<N: ast::AttrsOwner>(node: &N) -> N { | 281 | pub fn strip_attrs_and_docs<N: ast::AttrsOwner>(node: &N) -> N { |
264 | N::cast(strip_attrs_and_docs_inner(node.syntax().clone())).unwrap() | 282 | N::cast(strip_attrs_and_docs_inner(node.syntax().clone())).unwrap() |
diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 0da24560e..53d6fa562 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs | |||
@@ -25,6 +25,31 @@ fn path_from_text(text: &str) -> ast::Path { | |||
25 | ast_from_text(text) | 25 | ast_from_text(text) |
26 | } | 26 | } |
27 | 27 | ||
28 | pub fn use_tree( | ||
29 | path: ast::Path, | ||
30 | use_tree_list: Option<ast::UseTreeList>, | ||
31 | alias: Option<ast::Alias>, | ||
32 | ) -> ast::UseTree { | ||
33 | let mut buf = "use ".to_string(); | ||
34 | buf += &path.syntax().to_string(); | ||
35 | if let Some(use_tree_list) = use_tree_list { | ||
36 | buf += &format!("::{}", use_tree_list.syntax()); | ||
37 | } | ||
38 | if let Some(alias) = alias { | ||
39 | buf += &format!(" {}", alias.syntax()); | ||
40 | } | ||
41 | ast_from_text(&buf) | ||
42 | } | ||
43 | |||
44 | pub fn use_tree_list(use_trees: impl IntoIterator<Item = ast::UseTree>) -> ast::UseTreeList { | ||
45 | let use_trees = use_trees.into_iter().map(|it| it.syntax().clone()).join(", "); | ||
46 | ast_from_text(&format!("use {{{}}};", use_trees)) | ||
47 | } | ||
48 | |||
49 | pub fn use_item(use_tree: ast::UseTree) -> ast::UseItem { | ||
50 | ast_from_text(&format!("use {};", use_tree.syntax())) | ||
51 | } | ||
52 | |||
28 | pub fn record_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField { | 53 | pub fn record_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField { |
29 | return match expr { | 54 | return match expr { |
30 | Some(expr) => from_text(&format!("{}: {}", name.syntax(), expr.syntax())), | 55 | Some(expr) => from_text(&format!("{}: {}", name.syntax(), expr.syntax())), |
@@ -219,22 +244,13 @@ fn unroot(n: SyntaxNode) -> SyntaxNode { | |||
219 | } | 244 | } |
220 | 245 | ||
221 | pub mod tokens { | 246 | pub mod tokens { |
222 | use crate::{ast, AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken, T}; | ||
223 | use once_cell::sync::Lazy; | 247 | use once_cell::sync::Lazy; |
224 | 248 | ||
249 | use crate::{ast, AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken}; | ||
250 | |||
225 | pub(super) static SOURCE_FILE: Lazy<Parse<SourceFile>> = | 251 | pub(super) static SOURCE_FILE: Lazy<Parse<SourceFile>> = |
226 | Lazy::new(|| SourceFile::parse("const C: <()>::Item = (1 != 1, 2 == 2, !true)\n;")); | 252 | Lazy::new(|| SourceFile::parse("const C: <()>::Item = (1 != 1, 2 == 2, !true)\n;")); |
227 | 253 | ||
228 | pub fn comma() -> SyntaxToken { | ||
229 | SOURCE_FILE | ||
230 | .tree() | ||
231 | .syntax() | ||
232 | .descendants_with_tokens() | ||
233 | .filter_map(|it| it.into_token()) | ||
234 | .find(|it| it.kind() == T![,]) | ||
235 | .unwrap() | ||
236 | } | ||
237 | |||
238 | pub fn single_space() -> SyntaxToken { | 254 | pub fn single_space() -> SyntaxToken { |
239 | SOURCE_FILE | 255 | SOURCE_FILE |
240 | .tree() | 256 | .tree() |