diff options
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r-- | crates/ra_syntax/src/ast/make.rs | 16 | ||||
-rw-r--r-- | crates/ra_syntax/src/tests.rs | 1 |
2 files changed, 16 insertions, 1 deletions
diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index ae8829807..9f6f1cc53 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs | |||
@@ -112,10 +112,14 @@ pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr { | |||
112 | let token = token(op); | 112 | let token = token(op); |
113 | expr_from_text(&format!("{}{}", token, expr.syntax())) | 113 | expr_from_text(&format!("{}{}", token, expr.syntax())) |
114 | } | 114 | } |
115 | pub fn expr_from_text(text: &str) -> ast::Expr { | 115 | fn expr_from_text(text: &str) -> ast::Expr { |
116 | ast_from_text(&format!("const C: () = {};", text)) | 116 | ast_from_text(&format!("const C: () = {};", text)) |
117 | } | 117 | } |
118 | 118 | ||
119 | pub fn try_expr_from_text(text: &str) -> Option<ast::Expr> { | ||
120 | try_ast_from_text(&format!("const C: () = {};", text)) | ||
121 | } | ||
122 | |||
119 | pub fn bind_pat(name: ast::Name) -> ast::BindPat { | 123 | pub fn bind_pat(name: ast::Name) -> ast::BindPat { |
120 | return from_text(name.text()); | 124 | return from_text(name.text()); |
121 | 125 | ||
@@ -239,6 +243,16 @@ fn ast_from_text<N: AstNode>(text: &str) -> N { | |||
239 | node | 243 | node |
240 | } | 244 | } |
241 | 245 | ||
246 | fn try_ast_from_text<N: AstNode>(text: &str) -> Option<N> { | ||
247 | let parse = SourceFile::parse(text); | ||
248 | let node = parse.tree().syntax().descendants().find_map(N::cast)?; | ||
249 | let node = node.syntax().clone(); | ||
250 | let node = unroot(node); | ||
251 | let node = N::cast(node).unwrap(); | ||
252 | assert_eq!(node.syntax().text_range().start(), 0.into()); | ||
253 | Some(node) | ||
254 | } | ||
255 | |||
242 | fn unroot(n: SyntaxNode) -> SyntaxNode { | 256 | fn unroot(n: SyntaxNode) -> SyntaxNode { |
243 | SyntaxNode::new_root(n.green().clone()) | 257 | SyntaxNode::new_root(n.green().clone()) |
244 | } | 258 | } |
diff --git a/crates/ra_syntax/src/tests.rs b/crates/ra_syntax/src/tests.rs index d331d541e..6a8cb6bb5 100644 --- a/crates/ra_syntax/src/tests.rs +++ b/crates/ra_syntax/src/tests.rs | |||
@@ -34,6 +34,7 @@ fn main() { | |||
34 | "##; | 34 | "##; |
35 | 35 | ||
36 | let parse = SourceFile::parse(code); | 36 | let parse = SourceFile::parse(code); |
37 | // eprintln!("{:#?}", parse.syntax_node()); | ||
37 | assert!(parse.ok().is_ok()); | 38 | assert!(parse.ok().is_ok()); |
38 | } | 39 | } |
39 | 40 | ||