Swtches to rust SSR query check

This commit is contained in:
Mikhail Modin 2020-03-15 21:23:18 +00:00
parent 530ff9f57f
commit b150965ed7
7 changed files with 63 additions and 20 deletions

View file

@ -112,10 +112,14 @@ pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr {
let token = token(op);
expr_from_text(&format!("{}{}", token, expr.syntax()))
}
pub fn expr_from_text(text: &str) -> ast::Expr {
fn expr_from_text(text: &str) -> ast::Expr {
ast_from_text(&format!("const C: () = {};", text))
}
pub fn try_expr_from_text(text: &str) -> Option<ast::Expr> {
try_ast_from_text(&format!("const C: () = {};", text))
}
pub fn bind_pat(name: ast::Name) -> ast::BindPat {
return from_text(name.text());
@ -239,6 +243,16 @@ fn ast_from_text<N: AstNode>(text: &str) -> N {
node
}
fn try_ast_from_text<N: AstNode>(text: &str) -> Option<N> {
let parse = SourceFile::parse(text);
let node = parse.tree().syntax().descendants().find_map(N::cast)?;
let node = node.syntax().clone();
let node = unroot(node);
let node = N::cast(node).unwrap();
assert_eq!(node.syntax().text_range().start(), 0.into());
Some(node)
}
fn unroot(n: SyntaxNode) -> SyntaxNode {
SyntaxNode::new_root(n.green().clone())
}