mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-27 10:17:15 +00:00
refactor: migrate let_else_to_match to editor
Signed-off-by: Prajwal S N <prajwalnadig21@gmail.com>
This commit is contained in:
parent
5adee2ad2c
commit
2eb7389b63
8 changed files with 335 additions and 142 deletions
|
|
@ -709,7 +709,7 @@ pub fn wildcard_pat() -> ast::WildcardPat {
|
|||
}
|
||||
|
||||
pub fn rest_pat() -> ast::RestPat {
|
||||
ast_from_text("fn f(..)")
|
||||
ast_from_text("fn f() { let ..; }")
|
||||
}
|
||||
|
||||
pub fn literal_pat(lit: &str) -> ast::LiteralPat {
|
||||
|
|
@ -788,8 +788,8 @@ pub fn record_pat_field(name_ref: ast::NameRef, pat: ast::Pat) -> ast::RecordPat
|
|||
ast_from_text(&format!("fn f(S {{ {name_ref}: {pat} }}: ()))"))
|
||||
}
|
||||
|
||||
pub fn record_pat_field_shorthand(name_ref: ast::NameRef) -> ast::RecordPatField {
|
||||
ast_from_text(&format!("fn f(S {{ {name_ref} }}: ()))"))
|
||||
pub fn record_pat_field_shorthand(pat: ast::Pat) -> ast::RecordPatField {
|
||||
ast_from_text(&format!("fn f(S {{ {pat} }}: ()))"))
|
||||
}
|
||||
|
||||
/// Returns a `IdentPat` if the path has just one segment, a `PathPat` otherwise.
|
||||
|
|
@ -801,16 +801,38 @@ pub fn path_pat(path: ast::Path) -> ast::Pat {
|
|||
}
|
||||
|
||||
/// Returns a `Pat` if the path has just one segment, an `OrPat` otherwise.
|
||||
pub fn or_pat(pats: impl IntoIterator<Item = ast::Pat>, leading_pipe: bool) -> ast::Pat {
|
||||
///
|
||||
/// Invariant: `pats` must be length > 1.
|
||||
pub fn or_pat(pats: impl IntoIterator<Item = ast::Pat>, leading_pipe: bool) -> ast::OrPat {
|
||||
let leading_pipe = if leading_pipe { "| " } else { "" };
|
||||
let pats = pats.into_iter().join(" | ");
|
||||
|
||||
return from_text(&format!("{leading_pipe}{pats}"));
|
||||
fn from_text(text: &str) -> ast::Pat {
|
||||
fn from_text(text: &str) -> ast::OrPat {
|
||||
ast_from_text(&format!("fn f({text}: ())"))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn box_pat(pat: ast::Pat) -> ast::BoxPat {
|
||||
ast_from_text(&format!("fn f(box {pat}: ())"))
|
||||
}
|
||||
|
||||
pub fn paren_pat(pat: ast::Pat) -> ast::ParenPat {
|
||||
ast_from_text(&format!("fn f(({pat}): ())"))
|
||||
}
|
||||
|
||||
pub fn range_pat(start: Option<ast::Pat>, end: Option<ast::Pat>) -> ast::RangePat {
|
||||
ast_from_text(&format!(
|
||||
"fn f({}..{}: ())",
|
||||
start.map(|e| e.to_string()).unwrap_or_default(),
|
||||
end.map(|e| e.to_string()).unwrap_or_default()
|
||||
))
|
||||
}
|
||||
|
||||
pub fn ref_pat(pat: ast::Pat) -> ast::RefPat {
|
||||
ast_from_text(&format!("fn f(&{pat}: ())"))
|
||||
}
|
||||
|
||||
pub fn match_arm(pat: ast::Pat, guard: Option<ast::MatchGuard>, expr: ast::Expr) -> ast::MatchArm {
|
||||
return match guard {
|
||||
Some(guard) => from_text(&format!("{pat} {guard} => {expr}")),
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use crate::{
|
|||
AstNode, NodeOrToken, SyntaxKind, SyntaxNode, SyntaxToken,
|
||||
ast::{
|
||||
self, HasArgList, HasGenericArgs, HasGenericParams, HasLoopBody, HasName, HasTypeBounds,
|
||||
HasVisibility, make,
|
||||
HasVisibility, RangeItem, make,
|
||||
},
|
||||
syntax_editor::SyntaxMappingBuilder,
|
||||
};
|
||||
|
|
@ -254,12 +254,12 @@ impl SyntaxFactory {
|
|||
ast
|
||||
}
|
||||
|
||||
pub fn record_pat_field_shorthand(&self, name_ref: ast::NameRef) -> ast::RecordPatField {
|
||||
let ast = make::record_pat_field_shorthand(name_ref.clone()).clone_for_update();
|
||||
pub fn record_pat_field_shorthand(&self, pat: ast::Pat) -> ast::RecordPatField {
|
||||
let ast = make::record_pat_field_shorthand(pat.clone()).clone_for_update();
|
||||
|
||||
if let Some(mut mapping) = self.mappings() {
|
||||
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
|
||||
builder.map_node(name_ref.syntax().clone(), ast.pat().unwrap().syntax().clone());
|
||||
builder.map_node(pat.syntax().clone(), ast.pat().unwrap().syntax().clone());
|
||||
builder.finish(&mut mapping);
|
||||
}
|
||||
|
||||
|
|
@ -294,6 +294,76 @@ impl SyntaxFactory {
|
|||
make::rest_pat().clone_for_update()
|
||||
}
|
||||
|
||||
pub fn or_pat(
|
||||
&self,
|
||||
pats: impl IntoIterator<Item = ast::Pat>,
|
||||
leading_pipe: bool,
|
||||
) -> ast::OrPat {
|
||||
let (pats, input) = iterator_input(pats);
|
||||
let ast = make::or_pat(pats, leading_pipe).clone_for_update();
|
||||
|
||||
if let Some(mut mapping) = self.mappings() {
|
||||
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
|
||||
builder.map_children(input, ast.pats().map(|it| it.syntax().clone()));
|
||||
builder.finish(&mut mapping);
|
||||
}
|
||||
|
||||
ast
|
||||
}
|
||||
|
||||
pub fn box_pat(&self, pat: ast::Pat) -> ast::BoxPat {
|
||||
let ast = make::box_pat(pat.clone()).clone_for_update();
|
||||
|
||||
if let Some(mut mapping) = self.mappings() {
|
||||
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
|
||||
builder.map_node(pat.syntax().clone(), ast.pat().unwrap().syntax().clone());
|
||||
builder.finish(&mut mapping);
|
||||
}
|
||||
|
||||
ast
|
||||
}
|
||||
|
||||
pub fn paren_pat(&self, pat: ast::Pat) -> ast::ParenPat {
|
||||
let ast = make::paren_pat(pat.clone()).clone_for_update();
|
||||
|
||||
if let Some(mut mapping) = self.mappings() {
|
||||
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
|
||||
builder.map_node(pat.syntax().clone(), ast.pat().unwrap().syntax().clone());
|
||||
builder.finish(&mut mapping);
|
||||
}
|
||||
|
||||
ast
|
||||
}
|
||||
|
||||
pub fn range_pat(&self, start: Option<ast::Pat>, end: Option<ast::Pat>) -> ast::RangePat {
|
||||
let ast = make::range_pat(start.clone(), end.clone()).clone_for_update();
|
||||
|
||||
if let Some(mut mapping) = self.mappings() {
|
||||
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
|
||||
if let Some(start) = start {
|
||||
builder.map_node(start.syntax().clone(), ast.start().unwrap().syntax().clone());
|
||||
}
|
||||
if let Some(end) = end {
|
||||
builder.map_node(end.syntax().clone(), ast.end().unwrap().syntax().clone());
|
||||
}
|
||||
builder.finish(&mut mapping);
|
||||
}
|
||||
|
||||
ast
|
||||
}
|
||||
|
||||
pub fn ref_pat(&self, pat: ast::Pat) -> ast::RefPat {
|
||||
let ast = make::ref_pat(pat.clone()).clone_for_update();
|
||||
|
||||
if let Some(mut mapping) = self.mappings() {
|
||||
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
|
||||
builder.map_node(pat.syntax().clone(), ast.pat().unwrap().syntax().clone());
|
||||
builder.finish(&mut mapping);
|
||||
}
|
||||
|
||||
ast
|
||||
}
|
||||
|
||||
pub fn block_expr(
|
||||
&self,
|
||||
statements: impl IntoIterator<Item = ast::Stmt>,
|
||||
|
|
@ -673,6 +743,38 @@ impl SyntaxFactory {
|
|||
ast
|
||||
}
|
||||
|
||||
pub fn let_else_stmt(
|
||||
&self,
|
||||
pattern: ast::Pat,
|
||||
ty: Option<ast::Type>,
|
||||
initializer: ast::Expr,
|
||||
diverging: ast::BlockExpr,
|
||||
) -> ast::LetStmt {
|
||||
let ast = make::let_else_stmt(
|
||||
pattern.clone(),
|
||||
ty.clone(),
|
||||
initializer.clone(),
|
||||
diverging.clone(),
|
||||
)
|
||||
.clone_for_update();
|
||||
|
||||
if let Some(mut mapping) = self.mappings() {
|
||||
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
|
||||
builder.map_node(pattern.syntax().clone(), ast.pat().unwrap().syntax().clone());
|
||||
if let Some(input) = ty {
|
||||
builder.map_node(input.syntax().clone(), ast.ty().unwrap().syntax().clone());
|
||||
}
|
||||
builder.map_node(
|
||||
initializer.syntax().clone(),
|
||||
ast.initializer().unwrap().syntax().clone(),
|
||||
);
|
||||
builder.map_node(diverging.syntax().clone(), ast.let_else().unwrap().syntax().clone());
|
||||
builder.finish(&mut mapping);
|
||||
}
|
||||
|
||||
ast
|
||||
}
|
||||
|
||||
pub fn type_arg(&self, ty: ast::Type) -> ast::TypeArg {
|
||||
let ast = make::type_arg(ty.clone()).clone_for_update();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue