mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
internal: Add SyntaxFactory
to ease generating nodes with syntax mappings
This commit is contained in:
parent
af9a658864
commit
05b48e4005
5 changed files with 186 additions and 101 deletions
45
crates/syntax/src/ast/syntax_factory.rs
Normal file
45
crates/syntax/src/ast/syntax_factory.rs
Normal file
|
@ -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()
|
||||
}
|
||||
}
|
110
crates/syntax/src/ast/syntax_factory/constructors.rs
Normal file
110
crates/syntax/src/ast/syntax_factory/constructors.rs
Normal file
|
@ -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
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue