mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 15:15:24 +00:00
add ssr fragment for expressions
This commit is contained in:
parent
2d373dc53c
commit
df2a996cb0
2 changed files with 16 additions and 2 deletions
|
@ -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 @@ impl ParsedRule {
|
|||
};
|
||||
|
||||
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());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue