mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-05 03:24:06 +03:00
add ssr fragment for expressions
This commit is contained in:
@@ -35,3 +35,17 @@ pub(crate) fn item(s: &str) -> Result<SyntaxNode, ()> {
|
||||
}
|
||||
Ok(node.syntax().clone())
|
||||
}
|
||||
|
||||
pub(crate) fn expr(s: &str) -> Result<SyntaxNode, ()> {
|
||||
let template = "const _: () = {};";
|
||||
let input = template.replace("{}", s);
|
||||
let parse = syntax::SourceFile::parse(&input);
|
||||
if !parse.errors().is_empty() {
|
||||
return Err(());
|
||||
}
|
||||
let node = parse.tree().syntax().descendants().find_map(ast::Expr::cast).ok_or(())?;
|
||||
if node.to_string() != s {
|
||||
return Err(());
|
||||
}
|
||||
Ok(node.syntax().clone())
|
||||
}
|
||||
|
||||
@@ -74,8 +74,8 @@ fn new(
|
||||
};
|
||||
|
||||
let raw_template_stmt = raw_template.map(ast::Stmt::parse);
|
||||
if let raw_template_expr @ Some(Ok(_)) = raw_template.map(ast::Expr::parse) {
|
||||
builder.try_add(ast::Expr::parse(&raw_pattern), raw_template_expr);
|
||||
if let raw_template_expr @ Some(Ok(_)) = raw_template.map(fragments::expr) {
|
||||
builder.try_add2(fragments::expr(&raw_pattern), raw_template_expr);
|
||||
} else {
|
||||
builder.try_add(ast::Expr::parse(&raw_pattern), raw_template_stmt.clone());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user