-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #18196 - DropDemBits:sed-syntax-factory, r=Veykril
internal: Add `SyntaxFactory` to ease generating nodes with syntax mappings Part of [#15710](#15710) Instead of requiring passing a `&mut SyntaxEditor` to every make constructor to generate mappings, we instead wrap that logic in `SyntaxFactory`, and afterwards add all the mappings to the `SyntaxEditor`. Includes an example of using `SyntaxEditor` & `SyntaxFactory` in the `extract_variable` assist.
- Loading branch information
Showing
6 changed files
with
221 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//! Builds upon [`crate::ast::make`] constructors to create ast fragments with | ||
//! optional syntax mappings. | ||
//! | ||
//! Instead of forcing make constructors to perform syntax mapping, we instead | ||
//! let [`SyntaxFactory`] handle constructing the mappings. Care must be taken | ||
//! to remember to feed the syntax mappings into a [`SyntaxEditor`](crate::syntax_editor::SyntaxEditor), | ||
//! if applicable. | ||
mod constructors; | ||
|
||
use std::cell::{RefCell, RefMut}; | ||
|
||
use crate::syntax_editor::SyntaxMapping; | ||
|
||
pub struct SyntaxFactory { | ||
// Stored in a refcell so that the factory methods can be &self | ||
mappings: Option<RefCell<SyntaxMapping>>, | ||
} | ||
|
||
impl SyntaxFactory { | ||
/// Creates a new [`SyntaxFactory`], generating mappings between input nodes and generated nodes. | ||
pub fn new() -> Self { | ||
Self { mappings: Some(RefCell::new(SyntaxMapping::new())) } | ||
} | ||
|
||
/// Creates a [`SyntaxFactory`] without generating mappings. | ||
pub fn without_mappings() -> Self { | ||
Self { mappings: None } | ||
} | ||
|
||
/// Gets all of the tracked syntax mappings, if any. | ||
pub fn finish_with_mappings(self) -> SyntaxMapping { | ||
self.mappings.unwrap_or_default().into_inner() | ||
} | ||
|
||
fn mappings(&self) -> Option<RefMut<'_, SyntaxMapping>> { | ||
self.mappings.as_ref().map(|it| it.borrow_mut()) | ||
} | ||
} | ||
|
||
impl Default for SyntaxFactory { | ||
fn default() -> Self { | ||
Self::without_mappings() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
//! Wrappers over [`make`] constructors | ||
use itertools::Itertools; | ||
|
||
use crate::{ | ||
ast::{self, make, HasName}, | ||
syntax_editor::SyntaxMappingBuilder, | ||
AstNode, | ||
}; | ||
|
||
use super::SyntaxFactory; | ||
|
||
impl SyntaxFactory { | ||
pub fn name(&self, name: &str) -> ast::Name { | ||
make::name(name).clone_for_update() | ||
} | ||
|
||
pub fn ident_pat(&self, ref_: bool, mut_: bool, name: ast::Name) -> ast::IdentPat { | ||
let ast = make::ident_pat(ref_, mut_, name.clone()).clone_for_update(); | ||
|
||
if let Some(mut mapping) = self.mappings() { | ||
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone()); | ||
builder.map_node(name.syntax().clone(), ast.name().unwrap().syntax().clone()); | ||
builder.finish(&mut mapping); | ||
} | ||
|
||
ast | ||
} | ||
|
||
pub fn block_expr( | ||
&self, | ||
stmts: impl IntoIterator<Item = ast::Stmt>, | ||
tail_expr: Option<ast::Expr>, | ||
) -> ast::BlockExpr { | ||
let stmts = stmts.into_iter().collect_vec(); | ||
let input = stmts.iter().map(|it| it.syntax().clone()).collect_vec(); | ||
|
||
let ast = make::block_expr(stmts, tail_expr.clone()).clone_for_update(); | ||
|
||
if let Some((mut mapping, stmt_list)) = self.mappings().zip(ast.stmt_list()) { | ||
let mut builder = SyntaxMappingBuilder::new(stmt_list.syntax().clone()); | ||
|
||
builder.map_children( | ||
input.into_iter(), | ||
stmt_list.statements().map(|it| it.syntax().clone()), | ||
); | ||
|
||
if let Some((input, output)) = tail_expr.zip(stmt_list.tail_expr()) { | ||
builder.map_node(input.syntax().clone(), output.syntax().clone()); | ||
} | ||
|
||
builder.finish(&mut mapping); | ||
} | ||
|
||
ast | ||
} | ||
|
||
pub fn expr_path(&self, path: ast::Path) -> ast::Expr { | ||
let ast::Expr::PathExpr(ast) = make::expr_path(path.clone()).clone_for_update() else { | ||
unreachable!() | ||
}; | ||
|
||
if let Some(mut mapping) = self.mappings() { | ||
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone()); | ||
builder.map_node(path.syntax().clone(), ast.path().unwrap().syntax().clone()); | ||
builder.finish(&mut mapping); | ||
} | ||
|
||
ast.into() | ||
} | ||
|
||
pub fn expr_ref(&self, expr: ast::Expr, exclusive: bool) -> ast::Expr { | ||
let ast::Expr::RefExpr(ast) = make::expr_ref(expr.clone(), exclusive).clone_for_update() | ||
else { | ||
unreachable!() | ||
}; | ||
|
||
if let Some(mut mapping) = self.mappings() { | ||
let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone()); | ||
builder.map_node(expr.syntax().clone(), ast.expr().unwrap().syntax().clone()); | ||
builder.finish(&mut mapping); | ||
} | ||
|
||
ast.into() | ||
} | ||
|
||
pub fn let_stmt( | ||
&self, | ||
pattern: ast::Pat, | ||
ty: Option<ast::Type>, | ||
initializer: Option<ast::Expr>, | ||
) -> ast::LetStmt { | ||
let ast = | ||
make::let_stmt(pattern.clone(), ty.clone(), initializer.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()); | ||
} | ||
if let Some(input) = initializer { | ||
builder | ||
.map_node(input.syntax().clone(), ast.initializer().unwrap().syntax().clone()); | ||
} | ||
builder.finish(&mut mapping); | ||
} | ||
|
||
ast | ||
} | ||
} |
Oops, something went wrong.