From 4ea0c12cf1520bc7d0f5def211a7cacadb45b7af Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 4 Feb 2020 13:22:32 +0100 Subject: Make sure that newly created nodes are the root of the tree --- crates/ra_syntax/src/ast/make.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 36e648180..38c0e9a66 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -2,7 +2,7 @@ //! of smaller pieces. use itertools::Itertools; -use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxToken}; +use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxToken}; pub fn name(text: &str) -> ast::Name { ast_from_text(&format!("mod {};", text)) @@ -179,7 +179,16 @@ pub fn token(kind: SyntaxKind) -> SyntaxToken { fn ast_from_text(text: &str) -> N { let parse = SourceFile::parse(text); - parse.tree().syntax().descendants().find_map(N::cast).unwrap() + let node = parse.tree().syntax().descendants().find_map(N::cast).unwrap(); + let node = node.syntax().clone(); + let node = unroot(node); + let node = N::cast(node).unwrap(); + assert_eq!(node.syntax().text_range().start(), 0.into()); + node +} + +fn unroot(n: SyntaxNode) -> SyntaxNode { + SyntaxNode::new_root(n.green().clone()) } pub mod tokens { -- cgit v1.2.3