Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide_ssr/src/fragments.rs')
-rw-r--r--crates/ide_ssr/src/fragments.rs25
1 files changed, 22 insertions, 3 deletions
diff --git a/crates/ide_ssr/src/fragments.rs b/crates/ide_ssr/src/fragments.rs
index 512b3ace01..4c768a970b 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<SyntaxNode, ()> {
if node.to_string() != s {
return Err(());
}
- Ok(node.syntax().clone())
+ Ok(node.syntax().clone_subtree())
}
pub(crate) fn item(s: &str) -> Result<SyntaxNode, ()> {
@@ -33,7 +33,7 @@ pub(crate) fn item(s: &str) -> Result<SyntaxNode, ()> {
if node.to_string() != s {
return Err(());
}
- Ok(node.syntax().clone())
+ Ok(node.syntax().clone_subtree())
}
pub(crate) fn expr(s: &str) -> Result<SyntaxNode, ()> {
@@ -47,5 +47,24 @@ pub(crate) fn expr(s: &str) -> Result<SyntaxNode, ()> {
if node.to_string() != s {
return Err(());
}
- Ok(node.syntax().clone())
+ Ok(node.syntax().clone_subtree())
+}
+
+pub(crate) fn stmt(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 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())
}