diff --git a/crates/ide_ssr/src/fragments.rs b/crates/ide_ssr/src/fragments.rs index 512b3ace01a4..4c768a970b19 100644 --- a/crates/ide_ssr/src/fragments.rs +++ b/crates/ide_ssr/src/fragments.rs @@ -19,7 +19,7 @@ pub(crate) fn ty(s: &str) -> Result { if node.to_string() != s { return Err(()); } - Ok(node.syntax().clone()) + Ok(node.syntax().clone_subtree()) } pub(crate) fn item(s: &str) -> Result { @@ -33,7 +33,7 @@ pub(crate) fn item(s: &str) -> Result { if node.to_string() != s { return Err(()); } - Ok(node.syntax().clone()) + Ok(node.syntax().clone_subtree()) } pub(crate) fn expr(s: &str) -> Result { @@ -47,5 +47,24 @@ pub(crate) fn expr(s: &str) -> Result { if node.to_string() != s { return Err(()); } - Ok(node.syntax().clone()) + Ok(node.syntax().clone_subtree()) +} + +pub(crate) fn stmt(s: &str) -> Result { + let template = "const _: () = { {}; };"; + let input = template.replace("{}", s); + let parse = syntax::SourceFile::parse(&input); + if !parse.errors().is_empty() { + return Err(()); + } + let mut node = + parse.tree().syntax().descendants().skip(2).find_map(ast::Stmt::cast).ok_or(())?; + if !s.ends_with(';') && node.to_string().ends_with(';') { + node = node.clone_for_update(); + node.syntax().last_token().map(|it| it.detach()); + } + if node.to_string() != s { + return Err(()); + } + Ok(node.syntax().clone_subtree()) } diff --git a/crates/ide_ssr/src/parsing.rs b/crates/ide_ssr/src/parsing.rs index 03c7fed03959..7d1947dda8ba 100644 --- a/crates/ide_ssr/src/parsing.rs +++ b/crates/ide_ssr/src/parsing.rs @@ -73,17 +73,17 @@ impl ParsedRule { rules: Vec::new(), }; - let raw_template_stmt = raw_template.map(ast::Stmt::parse); + let raw_template_stmt = raw_template.map(fragments::stmt); 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()); + builder.try_add2(fragments::expr(&raw_pattern), raw_template_stmt.clone()); } builder.try_add2(fragments::ty(&raw_pattern), raw_template.map(fragments::ty)); builder.try_add2(fragments::item(&raw_pattern), raw_template.map(fragments::item)); builder.try_add(ast::Path::parse(&raw_pattern), raw_template.map(ast::Path::parse)); builder.try_add(ast::Pat::parse(&raw_pattern), raw_template.map(ast::Pat::parse)); - builder.try_add(ast::Stmt::parse(&raw_pattern), raw_template_stmt); + builder.try_add2(fragments::stmt(&raw_pattern), raw_template_stmt); builder.build() } }