Skip to content

Commit df2a996

Browse files
committed
add ssr fragment for expressions
1 parent 2d373dc commit df2a996

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

crates/ide_ssr/src/fragments.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,17 @@ pub(crate) fn item(s: &str) -> Result<SyntaxNode, ()> {
3535
}
3636
Ok(node.syntax().clone())
3737
}
38+
39+
pub(crate) fn expr(s: &str) -> Result<SyntaxNode, ()> {
40+
let template = "const _: () = {};";
41+
let input = template.replace("{}", s);
42+
let parse = syntax::SourceFile::parse(&input);
43+
if !parse.errors().is_empty() {
44+
return Err(());
45+
}
46+
let node = parse.tree().syntax().descendants().find_map(ast::Expr::cast).ok_or(())?;
47+
if node.to_string() != s {
48+
return Err(());
49+
}
50+
Ok(node.syntax().clone())
51+
}

crates/ide_ssr/src/parsing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ impl ParsedRule {
7474
};
7575

7676
let raw_template_stmt = raw_template.map(ast::Stmt::parse);
77-
if let raw_template_expr @ Some(Ok(_)) = raw_template.map(ast::Expr::parse) {
78-
builder.try_add(ast::Expr::parse(&raw_pattern), raw_template_expr);
77+
if let raw_template_expr @ Some(Ok(_)) = raw_template.map(fragments::expr) {
78+
builder.try_add2(fragments::expr(&raw_pattern), raw_template_expr);
7979
} else {
8080
builder.try_add(ast::Expr::parse(&raw_pattern), raw_template_stmt.clone());
8181
}

0 commit comments

Comments
 (0)