Unnamed repository; edit this file 'description' to name the repository.
add ssr fragment for expressions
Aleksey Kladov 2021-12-28
parent 2d373dc · commit df2a996
-rw-r--r--crates/ide_ssr/src/fragments.rs14
-rw-r--r--crates/ide_ssr/src/parsing.rs4
2 files changed, 16 insertions, 2 deletions
diff --git a/crates/ide_ssr/src/fragments.rs b/crates/ide_ssr/src/fragments.rs
index 72d26d53a3..512b3ace01 100644
--- a/crates/ide_ssr/src/fragments.rs
+++ b/crates/ide_ssr/src/fragments.rs
@@ -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())
+}
diff --git a/crates/ide_ssr/src/parsing.rs b/crates/ide_ssr/src/parsing.rs
index 1d5633cfe0..03c7fed039 100644
--- a/crates/ide_ssr/src/parsing.rs
+++ b/crates/ide_ssr/src/parsing.rs
@@ -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());
}