mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-21 07:41:58 +00:00
feat: for loop to while let assist
This commit is contained in:
parent
185f9deb45
commit
53f3e6fd5f
5 changed files with 467 additions and 2 deletions
|
@ -623,6 +623,10 @@ pub fn expr_for_loop(pat: ast::Pat, expr: ast::Expr, block: ast::BlockExpr) -> a
|
|||
expr_from_text(&format!("for {pat} in {expr} {block}"))
|
||||
}
|
||||
|
||||
pub fn expr_while_loop(condition: ast::Expr, block: ast::BlockExpr) -> ast::WhileExpr {
|
||||
expr_from_text(&format!("while {condition} {block}"))
|
||||
}
|
||||
|
||||
pub fn expr_loop(block: ast::BlockExpr) -> ast::Expr {
|
||||
expr_from_text(&format!("loop {block}"))
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
//! Wrappers over [`make`] constructors
|
||||
use crate::{
|
||||
ast::{
|
||||
self, make, HasArgList, HasGenericArgs, HasGenericParams, HasName, HasTypeBounds,
|
||||
HasVisibility,
|
||||
self, make, HasArgList, HasGenericArgs, HasGenericParams, HasLoopBody, HasName,
|
||||
HasTypeBounds, HasVisibility,
|
||||
},
|
||||
syntax_editor::SyntaxMappingBuilder,
|
||||
AstNode, NodeOrToken, SyntaxKind, SyntaxNode, SyntaxToken,
|
||||
|
@ -543,6 +543,19 @@ impl SyntaxFactory {
|
|||
ast
|
||||
}
|
||||
|
||||
pub fn expr_while_loop(&self, condition: ast::Expr, body: ast::BlockExpr) -> ast::WhileExpr {
|
||||
let ast = make::expr_while_loop(condition.clone(), body.clone()).clone_for_update();
|
||||
|
||||
if let Some(mut mapping) = self.mappings() {
|
||||
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
|
||||
builder.map_node(condition.syntax().clone(), ast.condition().unwrap().syntax().clone());
|
||||
builder.map_node(body.syntax().clone(), ast.loop_body().unwrap().syntax().clone());
|
||||
builder.finish(&mut mapping);
|
||||
}
|
||||
|
||||
ast
|
||||
}
|
||||
|
||||
pub fn expr_let(&self, pattern: ast::Pat, expr: ast::Expr) -> ast::LetExpr {
|
||||
let ast = make::expr_let(pattern.clone(), expr.clone()).clone_for_update();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue