diff options
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r-- | crates/ra_syntax/src/ast/edit.rs | 11 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/make.rs | 5 |
2 files changed, 11 insertions, 5 deletions
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index f74c9f9c6..bdaecdc43 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs | |||
@@ -251,7 +251,7 @@ impl ast::UseItem { | |||
251 | #[must_use] | 251 | #[must_use] |
252 | pub fn with_use_tree(&self, use_tree: ast::UseTree) -> ast::UseItem { | 252 | pub fn with_use_tree(&self, use_tree: ast::UseTree) -> ast::UseItem { |
253 | if let Some(old) = self.use_tree() { | 253 | if let Some(old) = self.use_tree() { |
254 | return self.replace_descendants(iter::once((old, use_tree))); | 254 | return self.replace_descendant(old, use_tree); |
255 | } | 255 | } |
256 | self.clone() | 256 | self.clone() |
257 | } | 257 | } |
@@ -283,7 +283,7 @@ impl ast::UseTree { | |||
283 | #[must_use] | 283 | #[must_use] |
284 | pub fn with_path(&self, path: ast::Path) -> ast::UseTree { | 284 | pub fn with_path(&self, path: ast::Path) -> ast::UseTree { |
285 | if let Some(old) = self.path() { | 285 | if let Some(old) = self.path() { |
286 | return self.replace_descendants(iter::once((old, path))); | 286 | return self.replace_descendant(old, path); |
287 | } | 287 | } |
288 | self.clone() | 288 | self.clone() |
289 | } | 289 | } |
@@ -291,7 +291,7 @@ impl ast::UseTree { | |||
291 | #[must_use] | 291 | #[must_use] |
292 | pub fn with_use_tree_list(&self, use_tree_list: ast::UseTreeList) -> ast::UseTree { | 292 | pub fn with_use_tree_list(&self, use_tree_list: ast::UseTreeList) -> ast::UseTree { |
293 | if let Some(old) = self.use_tree_list() { | 293 | if let Some(old) = self.use_tree_list() { |
294 | return self.replace_descendants(iter::once((old, use_tree_list))); | 294 | return self.replace_descendant(old, use_tree_list); |
295 | } | 295 | } |
296 | self.clone() | 296 | self.clone() |
297 | } | 297 | } |
@@ -466,6 +466,11 @@ pub trait AstNodeEdit: AstNode + Sized { | |||
466 | } | 466 | } |
467 | 467 | ||
468 | #[must_use] | 468 | #[must_use] |
469 | fn replace_descendant<D: AstNode>(&self, old: D, new: D) -> Self { | ||
470 | self.replace_descendants(iter::once((old, new))) | ||
471 | } | ||
472 | |||
473 | #[must_use] | ||
469 | fn replace_descendants<D: AstNode>( | 474 | fn replace_descendants<D: AstNode>( |
470 | &self, | 475 | &self, |
471 | replacement_map: impl IntoIterator<Item = (D, D)>, | 476 | replacement_map: impl IntoIterator<Item = (D, D)>, |
diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 6aee39203..c818bba55 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs | |||
@@ -127,7 +127,7 @@ pub fn condition(expr: ast::Expr, pattern: Option<ast::Pat>) -> ast::Condition { | |||
127 | match pattern { | 127 | match pattern { |
128 | None => ast_from_text(&format!("const _: () = while {} {{}};", expr)), | 128 | None => ast_from_text(&format!("const _: () = while {} {{}};", expr)), |
129 | Some(pattern) => { | 129 | Some(pattern) => { |
130 | ast_from_text(&format!("const _: () = while {} = {} {{}};", pattern, expr)) | 130 | ast_from_text(&format!("const _: () = while let {} = {} {{}};", pattern, expr)) |
131 | } | 131 | } |
132 | } | 132 | } |
133 | } | 133 | } |
@@ -245,7 +245,8 @@ pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetSt | |||
245 | ast_from_text(&format!("fn f() {{ {} }}", text)) | 245 | ast_from_text(&format!("fn f() {{ {} }}", text)) |
246 | } | 246 | } |
247 | pub fn expr_stmt(expr: ast::Expr) -> ast::ExprStmt { | 247 | pub fn expr_stmt(expr: ast::Expr) -> ast::ExprStmt { |
248 | ast_from_text(&format!("fn f() {{ {}; }}", expr)) | 248 | let semi = if expr.is_block_like() { "" } else { ";" }; |
249 | ast_from_text(&format!("fn f() {{ {}{} (); }}", expr, semi)) | ||
249 | } | 250 | } |
250 | 251 | ||
251 | pub fn token(kind: SyntaxKind) -> SyntaxToken { | 252 | pub fn token(kind: SyntaxKind) -> SyntaxToken { |