mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 01:17:27 +00:00
keep ast creation API simple
This commit is contained in:
parent
d6bbdfefa7
commit
183a38fb50
9 changed files with 213 additions and 250 deletions
|
@ -1,10 +1,10 @@
|
|||
use hir::{db::HirDatabase, HasSource};
|
||||
use ra_syntax::{
|
||||
ast::{self, AstNode, NameOwner},
|
||||
ast::{self, make, AstNode, NameOwner},
|
||||
SmolStr,
|
||||
};
|
||||
|
||||
use crate::{ast_builder::Make, ast_editor::AstEditor, Assist, AssistCtx, AssistId};
|
||||
use crate::{ast_editor::AstEditor, Assist, AssistCtx, AssistId};
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum AddMissingImplMembersMode {
|
||||
|
@ -102,7 +102,8 @@ fn strip_docstring(item: ast::ImplItem) -> ast::ImplItem {
|
|||
fn add_body(fn_def: ast::FnDef) -> ast::FnDef {
|
||||
let mut ast_editor = AstEditor::new(fn_def.clone());
|
||||
if fn_def.body().is_none() {
|
||||
ast_editor.set_body(&Make::<ast::Block>::single_expr(Make::<ast::Expr>::unimplemented()));
|
||||
let body = make::block_from_expr(make::expr_unimplemented());
|
||||
ast_editor.set_body(&body);
|
||||
}
|
||||
ast_editor.ast().to_owned()
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use std::iter;
|
||||
|
||||
use hir::{db::HirDatabase, Adt, HasSource};
|
||||
use ra_syntax::ast::{self, AstNode, NameOwner};
|
||||
use ra_syntax::ast::{self, make, AstNode, NameOwner};
|
||||
|
||||
use crate::{ast_builder::Make, Assist, AssistCtx, AssistId};
|
||||
use crate::{Assist, AssistCtx, AssistId};
|
||||
|
||||
pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||
let match_expr = ctx.node_at_offset::<ast::MatchExpr>()?;
|
||||
|
@ -31,8 +31,8 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
|
|||
let variants = variant_list.variants();
|
||||
let arms = variants
|
||||
.filter_map(build_pat)
|
||||
.map(|pat| Make::<ast::MatchArm>::from(iter::once(pat), Make::<ast::Expr>::unit()));
|
||||
let new_arm_list = Make::<ast::MatchArmList>::from_arms(arms);
|
||||
.map(|pat| make::match_arm(iter::once(pat), make::expr_unit()));
|
||||
let new_arm_list = make::match_arm_list(arms);
|
||||
|
||||
edit.target(match_expr.syntax().text_range());
|
||||
edit.set_cursor(expr.syntax().text_range().start());
|
||||
|
@ -63,21 +63,22 @@ fn resolve_enum_def(
|
|||
}
|
||||
|
||||
fn build_pat(var: ast::EnumVariant) -> Option<ast::Pat> {
|
||||
let path = Make::<ast::Path>::from(var.parent_enum().name()?, var.name()?);
|
||||
let path = make::path_qualified(
|
||||
make::path_from_name_ref(make::name_ref(&var.parent_enum().name()?.syntax().to_string())),
|
||||
make::name_ref(&var.name()?.syntax().to_string()),
|
||||
);
|
||||
|
||||
let pat: ast::Pat = match var.kind() {
|
||||
ast::StructKind::Tuple(field_list) => {
|
||||
let pats = iter::repeat(Make::<ast::PlaceholderPat>::placeholder().into())
|
||||
.take(field_list.fields().count());
|
||||
Make::<ast::TupleStructPat>::from(path, pats).into()
|
||||
let pats =
|
||||
iter::repeat(make::placeholder_pat().into()).take(field_list.fields().count());
|
||||
make::tuple_struct_pat(path, pats).into()
|
||||
}
|
||||
ast::StructKind::Named(field_list) => {
|
||||
let pats = field_list
|
||||
.fields()
|
||||
.map(|f| Make::<ast::BindPat>::from_name(f.name().unwrap()).into());
|
||||
Make::<ast::RecordPat>::from(path, pats).into()
|
||||
let pats = field_list.fields().map(|f| make::bind_pat(f.name().unwrap()).into());
|
||||
make::record_pat(path, pats).into()
|
||||
}
|
||||
ast::StructKind::Unit => Make::<ast::PathPat>::from_path(path).into(),
|
||||
ast::StructKind::Unit => make::path_pat(path).into(),
|
||||
};
|
||||
|
||||
Some(pat)
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
use hir::db::HirDatabase;
|
||||
use ra_syntax::{
|
||||
ast::{self, AstNode, NameOwner, TypeBoundsOwner},
|
||||
ast::{self, make, AstNode, NameOwner, TypeBoundsOwner},
|
||||
SyntaxElement,
|
||||
SyntaxKind::*,
|
||||
};
|
||||
|
||||
use crate::{ast_builder::Make, ast_editor::AstEditor, Assist, AssistCtx, AssistId};
|
||||
use crate::{ast_editor::AstEditor, Assist, AssistCtx, AssistId};
|
||||
|
||||
pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||
let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?;
|
||||
|
@ -50,7 +50,7 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>)
|
|||
|
||||
let where_clause = {
|
||||
let predicates = type_param_list.type_params().filter_map(build_predicate);
|
||||
Make::<ast::WhereClause>::from_predicates(predicates)
|
||||
make::where_clause(predicates)
|
||||
};
|
||||
|
||||
let to_insert = match anchor.prev_sibling_or_token() {
|
||||
|
@ -68,8 +68,8 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>)
|
|||
}
|
||||
|
||||
fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> {
|
||||
let path = Make::<ast::Path>::from_name(param.name()?);
|
||||
let predicate = Make::<ast::WherePred>::from(path, param.type_bound_list()?.bounds());
|
||||
let path = make::path_from_name_ref(make::name_ref(¶m.name()?.syntax().to_string()));
|
||||
let predicate = make::where_pred(path, param.type_bound_list()?.bounds());
|
||||
Some(predicate)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,218 +0,0 @@
|
|||
use itertools::Itertools;
|
||||
|
||||
use ra_syntax::{ast, AstNode, SourceFile};
|
||||
|
||||
pub struct Make<N: AstNode> {
|
||||
_phantom: std::marker::PhantomData<N>,
|
||||
}
|
||||
|
||||
impl Make<ast::RecordField> {
|
||||
pub fn from(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField {
|
||||
match expr {
|
||||
Some(expr) => Self::from_text(&format!("{}: {}", name.syntax(), expr.syntax())),
|
||||
None => Self::from_text(&name.syntax().to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::RecordField {
|
||||
ast_node_from_file_text(&format!("fn f() {{ S {{ {}, }} }}", text))
|
||||
}
|
||||
}
|
||||
|
||||
impl Make<ast::Block> {
|
||||
pub fn single_expr(e: ast::Expr) -> ast::Block {
|
||||
Self::from_text(&format!("{{ {} }}", e.syntax()))
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::Block {
|
||||
ast_node_from_file_text(&format!("fn f() {}", text))
|
||||
}
|
||||
}
|
||||
|
||||
impl Make<ast::Expr> {
|
||||
pub fn unit() -> ast::Expr {
|
||||
Self::from_text("()")
|
||||
}
|
||||
|
||||
pub fn unimplemented() -> ast::Expr {
|
||||
Self::from_text("unimplemented!()")
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::Expr {
|
||||
ast_node_from_file_text(&format!("const C: () = {};", text))
|
||||
}
|
||||
}
|
||||
|
||||
impl Make<ast::NameRef> {
|
||||
pub fn from(text: &str) -> ast::NameRef {
|
||||
ast_node_from_file_text(&format!("fn f() {{ {}; }}", text))
|
||||
}
|
||||
}
|
||||
|
||||
impl Make<ast::Path> {
|
||||
pub fn from_name(name: ast::Name) -> ast::Path {
|
||||
let name = name.syntax().to_string();
|
||||
Self::from_text(name.as_str())
|
||||
}
|
||||
|
||||
pub fn from(enum_name: ast::Name, var_name: ast::Name) -> ast::Path {
|
||||
Self::from_text(&format!("{}::{}", enum_name.syntax(), var_name.syntax()))
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::Path {
|
||||
ast_node_from_file_text(text)
|
||||
}
|
||||
}
|
||||
|
||||
impl Make<ast::BindPat> {
|
||||
pub fn from_name(name: ast::Name) -> ast::BindPat {
|
||||
Self::from_text(name.text())
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::BindPat {
|
||||
ast_node_from_file_text(&format!("fn f({}: ())", text))
|
||||
}
|
||||
}
|
||||
|
||||
impl Make<ast::PlaceholderPat> {
|
||||
pub fn placeholder() -> ast::PlaceholderPat {
|
||||
Self::from_text("_")
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::PlaceholderPat {
|
||||
ast_node_from_file_text(&format!("fn f({}: ())", text))
|
||||
}
|
||||
}
|
||||
|
||||
impl Make<ast::TupleStructPat> {
|
||||
pub fn from(path: ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::TupleStructPat {
|
||||
let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", ");
|
||||
Self::from_text(&format!("{}({})", path.syntax(), pats_str))
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::TupleStructPat {
|
||||
ast_node_from_file_text(&format!("fn f({}: ())", text))
|
||||
}
|
||||
}
|
||||
|
||||
impl Make<ast::RecordPat> {
|
||||
pub fn from(path: ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::RecordPat {
|
||||
let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", ");
|
||||
Self::from_text(&format!("{}{{ {} }}", path.syntax(), pats_str))
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::RecordPat {
|
||||
ast_node_from_file_text(&format!("fn f({}: ())", text))
|
||||
}
|
||||
}
|
||||
|
||||
impl Make<ast::PathPat> {
|
||||
pub fn from_path(path: ast::Path) -> ast::PathPat {
|
||||
let path_str = path.syntax().text().to_string();
|
||||
Self::from_text(path_str.as_str())
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::PathPat {
|
||||
ast_node_from_file_text(&format!("fn f({}: ())", text))
|
||||
}
|
||||
}
|
||||
|
||||
impl Make<ast::MatchArm> {
|
||||
pub fn from(pats: impl Iterator<Item = ast::Pat>, expr: ast::Expr) -> ast::MatchArm {
|
||||
let pats_str = pats.map(|p| p.syntax().to_string()).join(" | ");
|
||||
Self::from_text(&format!("{} => {}", pats_str, expr.syntax()))
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::MatchArm {
|
||||
ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text))
|
||||
}
|
||||
}
|
||||
|
||||
impl Make<ast::MatchArmList> {
|
||||
pub fn from_arms(arms: impl Iterator<Item = ast::MatchArm>) -> ast::MatchArmList {
|
||||
let arms_str = arms.map(|arm| format!("\n {}", arm.syntax())).join(",");
|
||||
Self::from_text(&format!("{},\n", arms_str))
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::MatchArmList {
|
||||
ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text))
|
||||
}
|
||||
}
|
||||
|
||||
impl Make<ast::WherePred> {
|
||||
pub fn from(path: ast::Path, bounds: impl Iterator<Item = ast::TypeBound>) -> ast::WherePred {
|
||||
let bounds = bounds.map(|b| b.syntax().to_string()).collect::<Vec<_>>().join(" + ");
|
||||
Self::from_text(&format!("{}: {}", path.syntax(), bounds))
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::WherePred {
|
||||
ast_node_from_file_text(&format!("fn f() where {} {{ }}", text))
|
||||
}
|
||||
}
|
||||
|
||||
impl Make<ast::WhereClause> {
|
||||
pub fn from_predicates(preds: impl Iterator<Item = ast::WherePred>) -> ast::WhereClause {
|
||||
let preds = preds.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", ");
|
||||
Self::from_text(preds.as_str())
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::WhereClause {
|
||||
ast_node_from_file_text(&format!("fn f() where {} {{ }}", text))
|
||||
}
|
||||
}
|
||||
|
||||
fn ast_node_from_file_text<N: AstNode>(text: &str) -> N {
|
||||
let parse = SourceFile::parse(text);
|
||||
let res = parse.tree().syntax().descendants().find_map(N::cast).unwrap();
|
||||
res
|
||||
}
|
||||
|
||||
pub(crate) mod tokens {
|
||||
use once_cell::sync::Lazy;
|
||||
use ra_syntax::{AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken, T};
|
||||
|
||||
static SOURCE_FILE: Lazy<Parse<SourceFile>> = Lazy::new(|| SourceFile::parse(",\n; ;"));
|
||||
|
||||
pub(crate) fn comma() -> SyntaxToken {
|
||||
SOURCE_FILE
|
||||
.tree()
|
||||
.syntax()
|
||||
.descendants_with_tokens()
|
||||
.filter_map(|it| it.into_token())
|
||||
.find(|it| it.kind() == T![,])
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub(crate) fn single_space() -> SyntaxToken {
|
||||
SOURCE_FILE
|
||||
.tree()
|
||||
.syntax()
|
||||
.descendants_with_tokens()
|
||||
.filter_map(|it| it.into_token())
|
||||
.find(|it| it.kind() == WHITESPACE && it.text().as_str() == " ")
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub(crate) fn single_newline() -> SyntaxToken {
|
||||
SOURCE_FILE
|
||||
.tree()
|
||||
.syntax()
|
||||
.descendants_with_tokens()
|
||||
.filter_map(|it| it.into_token())
|
||||
.find(|it| it.kind() == WHITESPACE && it.text().as_str() == "\n")
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub(crate) struct WsBuilder(SourceFile);
|
||||
|
||||
impl WsBuilder {
|
||||
pub(crate) fn new(text: &str) -> WsBuilder {
|
||||
WsBuilder(SourceFile::parse(text).ok().unwrap())
|
||||
}
|
||||
pub(crate) fn ws(&self) -> SyntaxToken {
|
||||
self.0.syntax().first_child_or_token().unwrap().into_token().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -13,8 +13,6 @@ use ra_syntax::{
|
|||
};
|
||||
use ra_text_edit::TextEditBuilder;
|
||||
|
||||
use crate::ast_builder::tokens;
|
||||
|
||||
pub struct AstEditor<N: AstNode> {
|
||||
original_ast: N,
|
||||
ast: N,
|
||||
|
@ -286,3 +284,53 @@ impl AstEditor<ast::TypeParam> {
|
|||
self
|
||||
}
|
||||
}
|
||||
|
||||
mod tokens {
|
||||
use once_cell::sync::Lazy;
|
||||
use ra_syntax::{AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken, T};
|
||||
|
||||
static SOURCE_FILE: Lazy<Parse<SourceFile>> = Lazy::new(|| SourceFile::parse(",\n; ;"));
|
||||
|
||||
pub(crate) fn comma() -> SyntaxToken {
|
||||
SOURCE_FILE
|
||||
.tree()
|
||||
.syntax()
|
||||
.descendants_with_tokens()
|
||||
.filter_map(|it| it.into_token())
|
||||
.find(|it| it.kind() == T![,])
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub(crate) fn single_space() -> SyntaxToken {
|
||||
SOURCE_FILE
|
||||
.tree()
|
||||
.syntax()
|
||||
.descendants_with_tokens()
|
||||
.filter_map(|it| it.into_token())
|
||||
.find(|it| it.kind() == WHITESPACE && it.text().as_str() == " ")
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub(crate) fn single_newline() -> SyntaxToken {
|
||||
SOURCE_FILE
|
||||
.tree()
|
||||
.syntax()
|
||||
.descendants_with_tokens()
|
||||
.filter_map(|it| it.into_token())
|
||||
.find(|it| it.kind() == WHITESPACE && it.text().as_str() == "\n")
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub(crate) struct WsBuilder(SourceFile);
|
||||
|
||||
impl WsBuilder {
|
||||
pub(crate) fn new(text: &str) -> WsBuilder {
|
||||
WsBuilder(SourceFile::parse(text).ok().unwrap())
|
||||
}
|
||||
pub(crate) fn ws(&self) -> SyntaxToken {
|
||||
self.0.syntax().first_child_or_token().unwrap().into_token().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,11 +8,9 @@
|
|||
mod assist_ctx;
|
||||
mod marks;
|
||||
pub mod ast_editor;
|
||||
pub mod ast_builder;
|
||||
|
||||
use itertools::Itertools;
|
||||
|
||||
use hir::db::HirDatabase;
|
||||
use itertools::Itertools;
|
||||
use ra_db::FileRange;
|
||||
use ra_syntax::{TextRange, TextUnit};
|
||||
use ra_text_edit::TextEdit;
|
||||
|
|
|
@ -2,11 +2,11 @@ use std::cell::RefCell;
|
|||
|
||||
use hir::diagnostics::{AstDiagnostic, Diagnostic as _, DiagnosticSink};
|
||||
use itertools::Itertools;
|
||||
use ra_assists::{ast_builder::Make, ast_editor::AstEditor};
|
||||
use ra_assists::ast_editor::AstEditor;
|
||||
use ra_db::SourceDatabase;
|
||||
use ra_prof::profile;
|
||||
use ra_syntax::{
|
||||
ast::{self, AstNode},
|
||||
ast::{self, make, AstNode},
|
||||
Location, SyntaxNode, TextRange, T,
|
||||
};
|
||||
use ra_text_edit::{TextEdit, TextEditBuilder};
|
||||
|
@ -59,10 +59,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
|
|||
let node = d.ast(db);
|
||||
let mut ast_editor = AstEditor::new(node);
|
||||
for f in d.missed_fields.iter() {
|
||||
let field = Make::<ast::RecordField>::from(
|
||||
Make::<ast::NameRef>::from(&f.to_string()),
|
||||
Some(Make::<ast::Expr>::unit()),
|
||||
);
|
||||
let field = make::record_field(make::name_ref(&f.to_string()), Some(make::expr_unit()));
|
||||
ast_editor.append_field(&field);
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ mod traits;
|
|||
mod tokens;
|
||||
mod extensions;
|
||||
mod expr_extensions;
|
||||
pub mod make;
|
||||
|
||||
use std::marker::PhantomData;
|
||||
|
||||
|
|
135
crates/ra_syntax/src/ast/make.rs
Normal file
135
crates/ra_syntax/src/ast/make.rs
Normal file
|
@ -0,0 +1,135 @@
|
|||
//! This module contains free-standing functions for creating AST fragments out
|
||||
//! of smaller pieces.
|
||||
use itertools::Itertools;
|
||||
|
||||
use crate::{ast, AstNode, SourceFile};
|
||||
|
||||
pub fn name_ref(text: &str) -> ast::NameRef {
|
||||
ast_from_text(&format!("fn f() {{ {}; }}", text))
|
||||
}
|
||||
|
||||
pub fn path_from_name_ref(name_ref: ast::NameRef) -> ast::Path {
|
||||
path_from_text(&name_ref.syntax().to_string())
|
||||
}
|
||||
pub fn path_qualified(qual: ast::Path, name_ref: ast::NameRef) -> ast::Path {
|
||||
path_from_text(&format!("{}::{}", qual.syntax(), name_ref.syntax()))
|
||||
}
|
||||
fn path_from_text(text: &str) -> ast::Path {
|
||||
ast_from_text(text)
|
||||
}
|
||||
|
||||
pub fn record_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField {
|
||||
return match expr {
|
||||
Some(expr) => from_text(&format!("{}: {}", name.syntax(), expr.syntax())),
|
||||
None => from_text(&name.syntax().to_string()),
|
||||
};
|
||||
|
||||
fn from_text(text: &str) -> ast::RecordField {
|
||||
ast_from_text(&format!("fn f() {{ S {{ {}, }} }}", text))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn block_from_expr(e: ast::Expr) -> ast::Block {
|
||||
return from_text(&format!("{{ {} }}", e.syntax()));
|
||||
|
||||
fn from_text(text: &str) -> ast::Block {
|
||||
ast_from_text(&format!("fn f() {}", text))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expr_unit() -> ast::Expr {
|
||||
expr_from_text("()")
|
||||
}
|
||||
pub fn expr_unimplemented() -> ast::Expr {
|
||||
expr_from_text("unimplemented!()")
|
||||
}
|
||||
fn expr_from_text(text: &str) -> ast::Expr {
|
||||
ast_from_text(&format!("const C: () = {};", text))
|
||||
}
|
||||
|
||||
pub fn bind_pat(name: ast::Name) -> ast::BindPat {
|
||||
return from_text(name.text());
|
||||
|
||||
fn from_text(text: &str) -> ast::BindPat {
|
||||
ast_from_text(&format!("fn f({}: ())", text))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn placeholder_pat() -> ast::PlaceholderPat {
|
||||
return from_text("_");
|
||||
|
||||
fn from_text(text: &str) -> ast::PlaceholderPat {
|
||||
ast_from_text(&format!("fn f({}: ())", text))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tuple_struct_pat(
|
||||
path: ast::Path,
|
||||
pats: impl Iterator<Item = ast::Pat>,
|
||||
) -> ast::TupleStructPat {
|
||||
let pats_str = pats.map(|p| p.syntax().to_string()).join(", ");
|
||||
return from_text(&format!("{}({})", path.syntax(), pats_str));
|
||||
|
||||
fn from_text(text: &str) -> ast::TupleStructPat {
|
||||
ast_from_text(&format!("fn f({}: ())", text))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn record_pat(path: ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::RecordPat {
|
||||
let pats_str = pats.map(|p| p.syntax().to_string()).join(", ");
|
||||
return from_text(&format!("{}{{ {} }}", path.syntax(), pats_str));
|
||||
|
||||
fn from_text(text: &str) -> ast::RecordPat {
|
||||
ast_from_text(&format!("fn f({}: ())", text))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn path_pat(path: ast::Path) -> ast::PathPat {
|
||||
let path_str = path.syntax().text().to_string();
|
||||
return from_text(path_str.as_str());
|
||||
fn from_text(text: &str) -> ast::PathPat {
|
||||
ast_from_text(&format!("fn f({}: ())", text))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn match_arm(pats: impl Iterator<Item = ast::Pat>, expr: ast::Expr) -> ast::MatchArm {
|
||||
let pats_str = pats.map(|p| p.syntax().to_string()).join(" | ");
|
||||
return from_text(&format!("{} => {}", pats_str, expr.syntax()));
|
||||
|
||||
fn from_text(text: &str) -> ast::MatchArm {
|
||||
ast_from_text(&format!("fn f() {{ match () {{{}}} }}", text))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn match_arm_list(arms: impl Iterator<Item = ast::MatchArm>) -> ast::MatchArmList {
|
||||
let arms_str = arms.map(|arm| format!("\n {}", arm.syntax())).join(",");
|
||||
return from_text(&format!("{},\n", arms_str));
|
||||
|
||||
fn from_text(text: &str) -> ast::MatchArmList {
|
||||
ast_from_text(&format!("fn f() {{ match () {{{}}} }}", text))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn where_pred(path: ast::Path, bounds: impl Iterator<Item = ast::TypeBound>) -> ast::WherePred {
|
||||
let bounds = bounds.map(|b| b.syntax().to_string()).join(" + ");
|
||||
return from_text(&format!("{}: {}", path.syntax(), bounds));
|
||||
|
||||
fn from_text(text: &str) -> ast::WherePred {
|
||||
ast_from_text(&format!("fn f() where {} {{ }}", text))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn where_clause(preds: impl Iterator<Item = ast::WherePred>) -> ast::WhereClause {
|
||||
let preds = preds.map(|p| p.syntax().to_string()).join(", ");
|
||||
return from_text(preds.as_str());
|
||||
|
||||
fn from_text(text: &str) -> ast::WhereClause {
|
||||
ast_from_text(&format!("fn f() where {} {{ }}", text))
|
||||
}
|
||||
}
|
||||
|
||||
fn ast_from_text<N: AstNode>(text: &str) -> N {
|
||||
let parse = SourceFile::parse(text);
|
||||
let res = parse.tree().syntax().descendants().find_map(N::cast).unwrap();
|
||||
res
|
||||
}
|
Loading…
Reference in a new issue