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.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/crates/ide_ssr/src/fragments.rs b/crates/ide_ssr/src/fragments.rs
new file mode 100644
index 0000000000..0abf9e4d98
--- /dev/null
+++ b/crates/ide_ssr/src/fragments.rs
@@ -0,0 +1,21 @@
+//! When specifying SSR rule, you generally want to map one *kind* of thing to
+//! the same kind of thing: path to path, expression to expression, type to
+//! type.
+//!
+//! The problem is, while this *kind* is generally obvious to the human, the ide
+//! needs to determine it somehow. We do this in a stupid way -- by pasting SSR
+//! rule into different contexts and checking what works.
+
+use parser::SyntaxKind;
+use syntax::{ast, AstNode, SyntaxNode};
+
+pub(crate) fn ty(s: &str) -> Result<SyntaxNode, ()> {
+ let template = "type T = {};";
+ 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::Type::cast).ok_or(())?;
+ Ok(node.syntax().clone())
+}