From d91a98ec843ec8562c58ccb01a1e29d00cc744dc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 7 Jan 2019 16:15:47 +0300 Subject: switch ra_syntax to new rowan API --- crates/ra_syntax/src/algo.rs | 16 +- crates/ra_syntax/src/algo/visit.rs | 30 ++-- crates/ra_syntax/src/ast.rs | 199 +++++++++++-------------- crates/ra_syntax/src/ast/generated.rs.tera | 93 ++++++------ crates/ra_syntax/src/lib.rs | 38 ++--- crates/ra_syntax/src/reparsing.rs | 35 ++--- crates/ra_syntax/src/utils.rs | 17 +-- crates/ra_syntax/src/validation.rs | 15 +- crates/ra_syntax/src/validation/byte.rs | 8 +- crates/ra_syntax/src/validation/byte_string.rs | 8 +- crates/ra_syntax/src/validation/char.rs | 8 +- crates/ra_syntax/src/validation/string.rs | 8 +- crates/ra_syntax/src/yellow.rs | 104 +++++-------- crates/ra_syntax/src/yellow/syntax_text.rs | 6 +- crates/ra_syntax/tests/test.rs | 8 +- 15 files changed, 266 insertions(+), 327 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs index 4b3548ea9..13f50d2ef 100644 --- a/crates/ra_syntax/src/algo.rs +++ b/crates/ra_syntax/src/algo.rs @@ -1,19 +1,23 @@ pub mod visit; -use crate::{SyntaxNode, SyntaxNodeRef, TextRange, TextUnit}; +use rowan::TransparentNewType; + +use crate::{SyntaxNode, TextRange, TextUnit}; pub use rowan::LeafAtOffset; -pub fn find_leaf_at_offset(node: SyntaxNodeRef, offset: TextUnit) -> LeafAtOffset { +pub fn find_leaf_at_offset(node: &SyntaxNode, offset: TextUnit) -> LeafAtOffset<&SyntaxNode> { match node.0.leaf_at_offset(offset) { LeafAtOffset::None => LeafAtOffset::None, - LeafAtOffset::Single(n) => LeafAtOffset::Single(SyntaxNode(n)), - LeafAtOffset::Between(l, r) => LeafAtOffset::Between(SyntaxNode(l), SyntaxNode(r)), + LeafAtOffset::Single(n) => LeafAtOffset::Single(SyntaxNode::from_repr(n)), + LeafAtOffset::Between(l, r) => { + LeafAtOffset::Between(SyntaxNode::from_repr(l), SyntaxNode::from_repr(r)) + } } } -pub fn find_covering_node(root: SyntaxNodeRef, range: TextRange) -> SyntaxNodeRef { - SyntaxNode(root.0.covering_node(range)) +pub fn find_covering_node(root: &SyntaxNode, range: TextRange) -> &SyntaxNode { + SyntaxNode::from_repr(root.0.covering_node(range)) } pub fn generate(seed: Option, step: impl Fn(&T) -> Option) -> impl Iterator { diff --git a/crates/ra_syntax/src/algo/visit.rs b/crates/ra_syntax/src/algo/visit.rs index c021f464c..38f21594c 100644 --- a/crates/ra_syntax/src/algo/visit.rs +++ b/crates/ra_syntax/src/algo/visit.rs @@ -1,4 +1,4 @@ -use crate::{AstNode, SyntaxNodeRef}; +use crate::{AstNode, SyntaxNode}; use std::marker::PhantomData; @@ -15,11 +15,11 @@ pub fn visitor_ctx<'a, T, C>(ctx: C) -> impl VisitorCtx<'a, Output = T, Ctx = C> pub trait Visitor<'a>: Sized { type Output; - fn accept(self, node: SyntaxNodeRef<'a>) -> Option; + fn accept(self, node: &'a SyntaxNode) -> Option; fn visit(self, f: F) -> Vis where - N: AstNode<'a>, - F: FnOnce(N) -> Self::Output, + N: AstNode + 'a, + F: FnOnce(&'a N) -> Self::Output, { Vis { inner: self, @@ -32,11 +32,11 @@ pub trait Visitor<'a>: Sized { pub trait VisitorCtx<'a>: Sized { type Output; type Ctx; - fn accept(self, node: SyntaxNodeRef<'a>) -> Result; + fn accept(self, node: &'a SyntaxNode) -> Result; fn visit(self, f: F) -> VisCtx where - N: AstNode<'a>, - F: FnOnce(N, Self::Ctx) -> Self::Output, + N: AstNode + 'a, + F: FnOnce(&'a N, Self::Ctx) -> Self::Output, { VisCtx { inner: self, @@ -54,7 +54,7 @@ struct EmptyVisitor { impl<'a, T> Visitor<'a> for EmptyVisitor { type Output = T; - fn accept(self, _node: SyntaxNodeRef<'a>) -> Option { + fn accept(self, _node: &'a SyntaxNode) -> Option { None } } @@ -69,7 +69,7 @@ impl<'a, T, C> VisitorCtx<'a> for EmptyVisitorCtx { type Output = T; type Ctx = C; - fn accept(self, _node: SyntaxNodeRef<'a>) -> Result { + fn accept(self, _node: &'a SyntaxNode) -> Result { Err(self.ctx) } } @@ -84,12 +84,12 @@ pub struct Vis { impl<'a, V, N, F> Visitor<'a> for Vis where V: Visitor<'a>, - N: AstNode<'a>, - F: FnOnce(N) -> >::Output, + N: AstNode + 'a, + F: FnOnce(&'a N) -> >::Output, { type Output = >::Output; - fn accept(self, node: SyntaxNodeRef<'a>) -> Option { + fn accept(self, node: &'a SyntaxNode) -> Option { let Vis { inner, f, .. } = self; inner.accept(node).or_else(|| N::cast(node).map(f)) } @@ -105,13 +105,13 @@ pub struct VisCtx { impl<'a, V, N, F> VisitorCtx<'a> for VisCtx where V: VisitorCtx<'a>, - N: AstNode<'a>, - F: FnOnce(N, >::Ctx) -> >::Output, + N: AstNode + 'a, + F: FnOnce(&'a N, >::Ctx) -> >::Output, { type Output = >::Output; type Ctx = >::Ctx; - fn accept(self, node: SyntaxNodeRef<'a>) -> Result { + fn accept(self, node: &'a SyntaxNode) -> Result { let VisCtx { inner, f, .. } = self; inner.accept(node).or_else(|ctx| match N::cast(node) { None => Err(ctx), diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 9ab59738f..285dda1e0 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -1,119 +1,88 @@ mod generated; use std::marker::PhantomData; -use std::string::String as RustString; use itertools::Itertools; pub use self::generated::*; use crate::{ - yellow::{RefRoot, SyntaxNodeChildren}, + yellow::{SyntaxNode, SyntaxNodeChildren, TreePtr, RaTypes}, SmolStr, SyntaxKind::*, - SyntaxNodeRef, }; /// The main trait to go from untyped `SyntaxNode` to a typed ast. The /// conversion itself has zero runtime cost: ast and syntax nodes have exactly /// the same representation: a pointer to the tree root and a pointer to the /// node itself. -pub trait AstNode<'a>: Clone + Copy + 'a { - fn cast(syntax: SyntaxNodeRef<'a>) -> Option +pub trait AstNode: rowan::TransparentNewType> { + fn cast(syntax: &SyntaxNode) -> Option<&Self> where Self: Sized; - fn syntax(self) -> SyntaxNodeRef<'a>; + fn syntax(&self) -> &SyntaxNode; + fn to_owned(&self) -> TreePtr; } -pub trait NameOwner<'a>: AstNode<'a> { - fn name(self) -> Option> { +pub trait NameOwner: AstNode { + fn name(&self) -> Option<&Name> { child_opt(self) } } -pub trait VisibilityOwner<'a>: AstNode<'a> { - fn visibility(self) -> Option> { +pub trait VisibilityOwner: AstNode { + fn visibility(&self) -> Option<&Visibility> { child_opt(self) } } -pub trait LoopBodyOwner<'a>: AstNode<'a> { - fn loop_body(self) -> Option> { +pub trait LoopBodyOwner: AstNode { + fn loop_body(&self) -> Option<&Block> { child_opt(self) } } -pub trait ArgListOwner<'a>: AstNode<'a> { - fn arg_list(self) -> Option> { +pub trait ArgListOwner: AstNode { + fn arg_list(&self) -> Option<&ArgList> { child_opt(self) } } -pub trait FnDefOwner<'a>: AstNode<'a> { - fn functions(self) -> AstChildren<'a, FnDef<'a>> { +pub trait FnDefOwner: AstNode { + fn functions(&self) -> AstChildren { children(self) } } -// ModuleItem -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum ItemOrMacro<'a> { - Item(ModuleItem<'a>), - Macro(MacroCall<'a>), -} - -impl<'a> AstNode<'a> for ItemOrMacro<'a> { - fn cast(syntax: SyntaxNodeRef<'a>) -> Option { - let res = if let Some(item) = ModuleItem::cast(syntax) { - ItemOrMacro::Item(item) - } else if let Some(macro_call) = MacroCall::cast(syntax) { - ItemOrMacro::Macro(macro_call) - } else { - return None; - }; - Some(res) - } - fn syntax(self) -> SyntaxNodeRef<'a> { - match self { - ItemOrMacro::Item(it) => it.syntax(), - ItemOrMacro::Macro(it) => it.syntax(), - } - } -} - -pub trait ModuleItemOwner<'a>: AstNode<'a> { - fn items(self) -> AstChildren<'a, ModuleItem<'a>> { - children(self) - } - - fn items_with_macros(self) -> AstChildren<'a, ItemOrMacro<'a>> { +pub trait ModuleItemOwner: AstNode { + fn items(&self) -> AstChildren { children(self) } } -pub trait TypeParamsOwner<'a>: AstNode<'a> { - fn type_param_list(self) -> Option> { +pub trait TypeParamsOwner: AstNode { + fn type_param_list(&self) -> Option<&TypeParamList> { child_opt(self) } - fn where_clause(self) -> Option> { + fn where_clause(&self) -> Option<&WhereClause> { child_opt(self) } } -pub trait AttrsOwner<'a>: AstNode<'a> { - fn attrs(self) -> AstChildren<'a, Attr<'a>> { +pub trait AttrsOwner: AstNode { + fn attrs(&self) -> AstChildren { children(self) } } -pub trait DocCommentsOwner<'a>: AstNode<'a> { - fn doc_comments(self) -> AstChildren<'a, Comment<'a>> { +pub trait DocCommentsOwner: AstNode { + fn doc_comments(&self) -> AstChildren { children(self) } /// Returns the textual content of a doc comment block as a single string. /// That is, strips leading `///` and joins lines - fn doc_comment_text(self) -> RustString { + fn doc_comment_text(&self) -> std::string::String { self.doc_comments() .filter(|comment| comment.is_doc_comment()) .map(|comment| { @@ -130,13 +99,13 @@ pub trait DocCommentsOwner<'a>: AstNode<'a> { } } -impl<'a> FnDef<'a> { +impl FnDef { pub fn has_atom_attr(&self, atom: &str) -> bool { self.attrs().filter_map(|x| x.as_atom()).any(|x| x == atom) } } -impl<'a> Attr<'a> { +impl Attr { pub fn as_atom(&self) -> Option { let tt = self.value()?; let (_bra, attr, _ket) = tt.syntax().children().collect_tuple()?; @@ -147,7 +116,7 @@ impl<'a> Attr<'a> { } } - pub fn as_call(&self) -> Option<(SmolStr, TokenTree<'a>)> { + pub fn as_call(&self) -> Option<(SmolStr, &TokenTree)> { let tt = self.value()?; let (_bra, attr, args, _ket) = tt.syntax().children().collect_tuple()?; let args = TokenTree::cast(args)?; @@ -159,37 +128,37 @@ impl<'a> Attr<'a> { } } -impl<'a> Lifetime<'a> { +impl Lifetime { pub fn text(&self) -> SmolStr { self.syntax().leaf_text().unwrap().clone() } } -impl<'a> Char<'a> { +impl Char { pub fn text(&self) -> &SmolStr { &self.syntax().leaf_text().unwrap() } } -impl<'a> Byte<'a> { +impl Byte { pub fn text(&self) -> &SmolStr { &self.syntax().leaf_text().unwrap() } } -impl<'a> ByteString<'a> { +impl ByteString { pub fn text(&self) -> &SmolStr { &self.syntax().leaf_text().unwrap() } } -impl<'a> String<'a> { +impl String { pub fn text(&self) -> &SmolStr { &self.syntax().leaf_text().unwrap() } } -impl<'a> Comment<'a> { +impl Comment { pub fn text(&self) -> &SmolStr { self.syntax().leaf_text().unwrap() } @@ -251,7 +220,7 @@ impl CommentFlavor { } } -impl<'a> Whitespace<'a> { +impl Whitespace { pub fn text(&self) -> &SmolStr { &self.syntax().leaf_text().unwrap() } @@ -265,36 +234,36 @@ impl<'a> Whitespace<'a> { } } -impl<'a> Name<'a> { +impl Name { pub fn text(&self) -> SmolStr { let ident = self.syntax().first_child().unwrap(); ident.leaf_text().unwrap().clone() } } -impl<'a> NameRef<'a> { +impl NameRef { pub fn text(&self) -> SmolStr { let ident = self.syntax().first_child().unwrap(); ident.leaf_text().unwrap().clone() } } -impl<'a> ImplBlock<'a> { - pub fn target_type(self) -> Option> { +impl ImplBlock { + pub fn target_type(&self) -> Option<&TypeRef> { match self.target() { (Some(t), None) | (_, Some(t)) => Some(t), _ => None, } } - pub fn target_trait(self) -> Option> { + pub fn target_trait(&self) -> Option<&TypeRef> { match self.target() { (Some(t), Some(_)) => Some(t), _ => None, } } - fn target(self) -> (Option>, Option>) { + fn target(&self) -> (Option<&TypeRef>, Option<&TypeRef>) { let mut types = children(self); let first = types.next(); let second = types.next(); @@ -302,8 +271,8 @@ impl<'a> ImplBlock<'a> { } } -impl<'a> Module<'a> { - pub fn has_semi(self) -> bool { +impl Module { + pub fn has_semi(&self) -> bool { match self.syntax().last_child() { None => false, Some(node) => node.kind() == SEMI, @@ -311,8 +280,8 @@ impl<'a> Module<'a> { } } -impl<'a> LetStmt<'a> { - pub fn has_semi(self) -> bool { +impl LetStmt { + pub fn has_semi(&self) -> bool { match self.syntax().last_child() { None => false, Some(node) => node.kind() == SEMI, @@ -320,35 +289,35 @@ impl<'a> LetStmt<'a> { } } -impl<'a> IfExpr<'a> { - pub fn then_branch(self) -> Option> { +impl IfExpr { + pub fn then_branch(&self) -> Option<&Block> { self.blocks().nth(0) } - pub fn else_branch(self) -> Option> { + pub fn else_branch(&self) -> Option<&Block> { self.blocks().nth(1) } - fn blocks(self) -> AstChildren<'a, Block<'a>> { + fn blocks(&self) -> AstChildren { children(self) } } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum PathSegmentKind<'a> { - Name(NameRef<'a>), + Name(&'a NameRef), SelfKw, SuperKw, CrateKw, } -impl<'a> PathSegment<'a> { - pub fn parent_path(self) -> Path<'a> { +impl PathSegment { + pub fn parent_path(&self) -> &Path { self.syntax() .parent() .and_then(Path::cast) .expect("segments are always nested in paths") } - pub fn kind(self) -> Option> { + pub fn kind(&self) -> Option { let res = if let Some(name_ref) = self.name_ref() { PathSegmentKind::Name(name_ref) } else { @@ -363,20 +332,20 @@ impl<'a> PathSegment<'a> { } } -impl<'a> Path<'a> { - pub fn parent_path(self) -> Option> { +impl Path { + pub fn parent_path(&self) -> Option<&Path> { self.syntax().parent().and_then(Path::cast) } } -impl<'a> UseTree<'a> { - pub fn has_star(self) -> bool { +impl UseTree { + pub fn has_star(&self) -> bool { self.syntax().children().any(|it| it.kind() == STAR) } } -impl<'a> UseTreeList<'a> { - pub fn parent_use_tree(self) -> UseTree<'a> { +impl UseTreeList { + pub fn parent_use_tree(&self) -> &UseTree { self.syntax() .parent() .and_then(UseTree::cast) @@ -384,22 +353,22 @@ impl<'a> UseTreeList<'a> { } } -fn child_opt<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> Option { +fn child_opt(parent: &P) -> Option<&C> { children(parent).next() } -fn children<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> AstChildren<'a, C> { +fn children(parent: &P) -> AstChildren { AstChildren::new(parent.syntax()) } #[derive(Debug)] pub struct AstChildren<'a, N> { - inner: SyntaxNodeChildren>, + inner: SyntaxNodeChildren<'a>, ph: PhantomData, } impl<'a, N> AstChildren<'a, N> { - fn new(parent: SyntaxNodeRef<'a>) -> Self { + fn new(parent: &'a SyntaxNode) -> Self { AstChildren { inner: parent.children(), ph: PhantomData, @@ -407,9 +376,9 @@ impl<'a, N> AstChildren<'a, N> { } } -impl<'a, N: AstNode<'a>> Iterator for AstChildren<'a, N> { - type Item = N; - fn next(&mut self) -> Option { +impl<'a, N: AstNode + 'a> Iterator for AstChildren<'a, N> { + type Item = &'a N; + fn next(&mut self) -> Option<&'a N> { loop { if let Some(n) = N::cast(self.inner.next()?) { return Some(n); @@ -420,13 +389,13 @@ impl<'a, N: AstNode<'a>> Iterator for AstChildren<'a, N> { #[derive(Debug, Clone, PartialEq, Eq)] pub enum StructFlavor<'a> { - Tuple(PosFieldList<'a>), - Named(NamedFieldDefList<'a>), + Tuple(&'a PosFieldList), + Named(&'a NamedFieldDefList), Unit, } -impl<'a> StructFlavor<'a> { - fn from_node>(node: N) -> StructFlavor<'a> { +impl StructFlavor<'_> { + fn from_node(node: &N) -> StructFlavor { if let Some(nfdl) = child_opt::<_, NamedFieldDefList>(node) { StructFlavor::Named(nfdl) } else if let Some(pfl) = child_opt::<_, PosFieldList>(node) { @@ -437,31 +406,31 @@ impl<'a> StructFlavor<'a> { } } -impl<'a> StructDef<'a> { - pub fn flavor(self) -> StructFlavor<'a> { +impl StructDef { + pub fn flavor(&self) -> StructFlavor { StructFlavor::from_node(self) } } -impl<'a> EnumVariant<'a> { - pub fn flavor(self) -> StructFlavor<'a> { +impl EnumVariant { + pub fn flavor(&self) -> StructFlavor { StructFlavor::from_node(self) } } -impl<'a> PointerType<'a> { +impl PointerType { pub fn is_mut(&self) -> bool { self.syntax().children().any(|n| n.kind() == MUT_KW) } } -impl<'a> ReferenceType<'a> { +impl ReferenceType { pub fn is_mut(&self) -> bool { self.syntax().children().any(|n| n.kind() == MUT_KW) } } -impl<'a> RefExpr<'a> { +impl RefExpr { pub fn is_mut(&self) -> bool { self.syntax().children().any(|n| n.kind() == MUT_KW) } @@ -477,7 +446,7 @@ pub enum PrefixOp { Neg, } -impl<'a> PrefixExpr<'a> { +impl PrefixExpr { pub fn op(&self) -> Option { match self.syntax().first_child()?.kind() { STAR => Some(PrefixOp::Deref), @@ -552,7 +521,7 @@ pub enum BinOp { BitXorAssign, } -impl<'a> BinExpr<'a> { +impl BinExpr { pub fn op(&self) -> Option { self.syntax() .children() @@ -592,15 +561,15 @@ impl<'a> BinExpr<'a> { .next() } - pub fn lhs(self) -> Option> { + pub fn lhs(&self) -> Option<&Expr> { children(self).nth(0) } - pub fn rhs(self) -> Option> { + pub fn rhs(&self) -> Option<&Expr> { children(self).nth(1) } - pub fn sub_exprs(self) -> (Option>, Option>) { + pub fn sub_exprs(&self) -> (Option<&Expr>, Option<&Expr>) { let mut children = children(self); let first = children.next(); let second = children.next(); @@ -618,7 +587,7 @@ pub enum SelfParamFlavor { MutRef, } -impl<'a> SelfParam<'a> { +impl SelfParam { pub fn flavor(&self) -> SelfParamFlavor { let borrowed = self.syntax().children().any(|n| n.kind() == AMP); if borrowed { @@ -641,7 +610,7 @@ impl<'a> SelfParam<'a> { #[test] fn test_doc_comment_of_items() { - let file = SourceFileNode::parse( + let file = SourceFile::parse( r#" //! doc // non-doc diff --git a/crates/ra_syntax/src/ast/generated.rs.tera b/crates/ra_syntax/src/ast/generated.rs.tera index 131ee09ec..0a20fc78e 100644 --- a/crates/ra_syntax/src/ast/generated.rs.tera +++ b/crates/ra_syntax/src/ast/generated.rs.tera @@ -11,89 +11,92 @@ the below applies to the result of this template #![cfg_attr(rustfmt, rustfmt_skip)] -use std::hash::{Hash, Hasher}; +use rowan::TransparentNewType; use crate::{ - ast, - SyntaxNode, SyntaxNodeRef, AstNode, - yellow::{TreeRoot, RaTypes, OwnedRoot, RefRoot}, - SyntaxKind::*, + SyntaxNode, SyntaxKind::*, + yellow::{RaTypes, TreePtr}, + ast::{self, AstNode}, }; {% for node, methods in ast %} // {{ node }} {%- if methods.enum %} +#[derive(Debug, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct {{ node }} { + pub(crate) syntax: SyntaxNode, +} +unsafe impl TransparentNewType for {{ node }} { + type Repr = rowan::SyntaxNode; +} + #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum {{ node }}<'a> { +pub enum {{ node }}Kind<'a> { {%- for kind in methods.enum %} - {{ kind }}({{ kind }}<'a>), + {{ kind }}(&'a {{ kind }}), {%- endfor %} } -impl<'a> AstNode<'a> for {{ node }}<'a> { - fn cast(syntax: SyntaxNodeRef<'a>) -> Option { +impl AstNode for {{ node }} { + fn cast(syntax: &SyntaxNode) -> Option<&Self> { match syntax.kind() { -{%- for kind in methods.enum %} - {{ kind | SCREAM }} => Some({{ node }}::{{ kind }}({{ kind }} { syntax })), -{%- endfor %} + {%- for kind in methods.enum %} + | {{ kind | SCREAM }} + {%- endfor %} => Some({{ node }}::from_repr(syntax.into_repr())), _ => None, } } - fn syntax(self) -> SyntaxNodeRef<'a> { - match self { -{%- for kind in methods.enum %} - {{ node }}::{{ kind }}(inner) => inner.syntax(), -{%- endfor %} + fn syntax(&self) -> &SyntaxNode { &self.syntax } + fn to_owned(&self) -> TreePtr<{{ node }}> { TreePtr::cast(self.syntax.to_owned()) } +} + +impl {{ node }} { + pub fn kind(&self) -> {{ node }}Kind { + match self.syntax.kind() { + {%- for kind in methods.enum %} + {{ kind | SCREAM }} => {{ node }}Kind::{{ kind }}({{ kind }}::cast(&self.syntax).unwrap()), + {%- endfor %} + _ => unreachable!(), } } } {% else %} -#[derive(Debug, Clone, Copy,)] -pub struct {{ node }}Node = OwnedRoot> { - pub(crate) syntax: SyntaxNode, +#[derive(Debug, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct {{ node }} { + pub(crate) syntax: SyntaxNode, } -pub type {{ node }}<'a> = {{ node }}Node>; - -impl, R2: TreeRoot> PartialEq<{{node}}Node> for {{node}}Node { - fn eq(&self, other: &{{node}}Node) -> bool { self.syntax == other.syntax } -} -impl> Eq for {{node}}Node {} -impl> Hash for {{node}}Node { - fn hash(&self, state: &mut H) { self.syntax.hash(state) } +unsafe impl TransparentNewType for {{ node }} { + type Repr = rowan::SyntaxNode; } -impl<'a> AstNode<'a> for {{ node }}<'a> { - fn cast(syntax: SyntaxNodeRef<'a>) -> Option { +impl AstNode for {{ node }} { + fn cast(syntax: &SyntaxNode) -> Option<&Self> { match syntax.kind() { - {{ node | SCREAM }} => Some({{ node }} { syntax }), + {{ node | SCREAM }} => Some({{ node }}::from_repr(syntax.into_repr())), _ => None, } } - fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } -} - -impl> {{ node }}Node { - pub fn borrowed(&self) -> {{ node }} { - {{ node }}Node { syntax: self.syntax.borrowed() } - } - pub fn owned(&self) -> {{ node }}Node { - {{ node }}Node { syntax: self.syntax.owned() } - } + fn syntax(&self) -> &SyntaxNode { &self.syntax } + fn to_owned(&self) -> TreePtr<{{ node }}> { TreePtr::cast(self.syntax.to_owned()) } } {% endif %} {% if methods.traits -%} + {%- for t in methods.traits -%} -impl<'a> ast::{{ t }}<'a> for {{ node }}<'a> {} +impl ast::{{ t }} for {{ node }} {} {% endfor -%} + {%- endif -%} -impl<'a> {{ node }}<'a> { +impl {{ node }} { {%- if methods.collections -%} {%- for m in methods.collections -%} {%- set method_name = m.0 -%} {%- set ChildName = m.1 %} - pub fn {{ method_name }}(self) -> impl Iterator> + 'a { + pub fn {{ method_name }}(&self) -> impl Iterator { super::children(self) } {% endfor -%} @@ -109,7 +112,7 @@ impl<'a> {{ node }}<'a> { {%- set method_name = m.0 -%} {%- set ChildName = m.1 %} {%- endif %} - pub fn {{ method_name }}(self) -> Option<{{ ChildName }}<'a>> { + pub fn {{ method_name }}(&self) -> Option<&{{ ChildName }}> { super::child_opt(self) } {% endfor -%} diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index 6753c513f..a75e641ea 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -42,52 +42,42 @@ pub use crate::{ ast::AstNode, lexer::{tokenize, Token}, syntax_kinds::SyntaxKind, - yellow::{ - Direction, OwnedRoot, RefRoot, SyntaxError, SyntaxNode, SyntaxNodeRef, TreeRoot, WalkEvent, Location, - }, + yellow::{Direction, SyntaxError, SyntaxNode, WalkEvent, Location, TreePtr}, }; use ra_text_edit::AtomTextEdit; use crate::yellow::GreenNode; -/// `SourceFileNode` represents a parse tree for a single Rust file. -pub use crate::ast::{SourceFile, SourceFileNode}; +/// `SourceFile` represents a parse tree for a single Rust file. +pub use crate::ast::SourceFile; -impl SourceFileNode { - fn new(green: GreenNode, errors: Vec) -> SourceFileNode { +impl SourceFile { + fn new(green: GreenNode, errors: Vec) -> TreePtr { let root = SyntaxNode::new(green, errors); if cfg!(debug_assertions) { - utils::validate_block_structure(root.borrowed()); + utils::validate_block_structure(&root); } assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE); - ast::SourceFileNode { syntax: root } + TreePtr::cast(root) } - pub fn parse(text: &str) -> SourceFileNode { + pub fn parse(text: &str) -> TreePtr { let tokens = tokenize(&text); let (green, errors) = parser_impl::parse_with(yellow::GreenBuilder::new(), text, &tokens, grammar::root); - SourceFileNode::new(green, errors) + SourceFile::new(green, errors) } - pub fn reparse(&self, edit: &AtomTextEdit) -> SourceFileNode { + pub fn reparse(&self, edit: &AtomTextEdit) -> TreePtr { self.incremental_reparse(edit) .unwrap_or_else(|| self.full_reparse(edit)) } - pub fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option { + pub fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option> { reparsing::incremental_reparse(self.syntax(), edit, self.errors()) - .map(|(green_node, errors)| SourceFileNode::new(green_node, errors)) + .map(|(green_node, errors)| SourceFile::new(green_node, errors)) } - fn full_reparse(&self, edit: &AtomTextEdit) -> SourceFileNode { + fn full_reparse(&self, edit: &AtomTextEdit) -> TreePtr { let text = text_utils::replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert); - SourceFileNode::parse(&text) - } - /// Typed AST representation of the parse tree. - pub fn ast(&self) -> ast::SourceFile { - self.borrowed() - } - /// Untyped homogeneous representation of the parse tree. - pub fn syntax(&self) -> SyntaxNodeRef { - self.syntax.borrowed() + SourceFile::parse(&text) } pub fn errors(&self) -> Vec { let mut errors = self.syntax.root_data().clone(); diff --git a/crates/ra_syntax/src/reparsing.rs b/crates/ra_syntax/src/reparsing.rs index 7ee71a1b6..d5d72e1f8 100644 --- a/crates/ra_syntax/src/reparsing.rs +++ b/crates/ra_syntax/src/reparsing.rs @@ -4,12 +4,12 @@ use crate::lexer::{tokenize, Token}; use crate::parser_api::Parser; use crate::parser_impl; use crate::text_utils::replace_range; -use crate::yellow::{self, GreenNode, SyntaxError, SyntaxNodeRef}; +use crate::yellow::{self, GreenNode, SyntaxError, SyntaxNode}; use crate::{SyntaxKind::*, TextRange, TextUnit}; use ra_text_edit::AtomTextEdit; pub(crate) fn incremental_reparse( - node: SyntaxNodeRef, + node: &SyntaxNode, edit: &AtomTextEdit, errors: Vec, ) -> Option<(GreenNode, Vec)> { @@ -21,9 +21,9 @@ pub(crate) fn incremental_reparse( } fn reparse_leaf<'node>( - node: SyntaxNodeRef<'node>, + node: &'node SyntaxNode, edit: &AtomTextEdit, -) -> Option<(SyntaxNodeRef<'node>, GreenNode, Vec)> { +) -> Option<(&'node SyntaxNode, GreenNode, Vec)> { let node = algo::find_covering_node(node, edit.delete); match node.kind() { WHITESPACE | COMMENT | IDENT | STRING | RAW_STRING => { @@ -47,9 +47,9 @@ fn reparse_leaf<'node>( } fn reparse_block<'node>( - node: SyntaxNodeRef<'node>, + node: &'node SyntaxNode, edit: &AtomTextEdit, -) -> Option<(SyntaxNodeRef<'node>, GreenNode, Vec)> { +) -> Option<(&'node SyntaxNode, GreenNode, Vec)> { let (node, reparser) = find_reparsable_node(node, edit.delete)?; let text = get_text_after_edit(node, &edit); let tokens = tokenize(&text); @@ -61,7 +61,7 @@ fn reparse_block<'node>( Some((node, green, new_errors)) } -fn get_text_after_edit(node: SyntaxNodeRef, edit: &AtomTextEdit) -> String { +fn get_text_after_edit(node: &SyntaxNode, edit: &AtomTextEdit) -> String { replace_range( node.text().to_string(), edit.delete - node.range().start(), @@ -77,17 +77,14 @@ fn is_contextual_kw(text: &str) -> bool { } type ParseFn = fn(&mut Parser); -fn find_reparsable_node( - node: SyntaxNodeRef<'_>, - range: TextRange, -) -> Option<(SyntaxNodeRef<'_>, ParseFn)> { +fn find_reparsable_node(node: &SyntaxNode, range: TextRange) -> Option<(&SyntaxNode, ParseFn)> { let node = algo::find_covering_node(node, range); return node .ancestors() .filter_map(|node| reparser(node).map(|r| (node, r))) .next(); - fn reparser(node: SyntaxNodeRef) -> Option { + fn reparser(node: &SyntaxNode) -> Option { let res = match node.kind() { BLOCK => grammar::block, NAMED_FIELD_DEF_LIST => grammar::named_field_def_list, @@ -138,7 +135,7 @@ fn is_balanced(tokens: &[Token]) -> bool { fn merge_errors( old_errors: Vec, new_errors: Vec, - old_node: SyntaxNodeRef, + old_node: &SyntaxNode, edit: &AtomTextEdit, ) -> Vec { let mut res = Vec::new(); @@ -159,22 +156,22 @@ fn merge_errors( mod tests { use test_utils::{extract_range, assert_eq_text}; - use crate::{SourceFileNode, text_utils::replace_range, utils::dump_tree }; + use crate::{SourceFile, AstNode, text_utils::replace_range, utils::dump_tree}; use super::*; fn do_check(before: &str, replace_with: &str, reparser: F) where for<'a> F: Fn( - SyntaxNodeRef<'a>, + &'a SyntaxNode, &AtomTextEdit, - ) -> Option<(SyntaxNodeRef<'a>, GreenNode, Vec)>, + ) -> Option<(&'a SyntaxNode, GreenNode, Vec)>, { let (range, before) = extract_range(before); let after = replace_range(before.clone(), range, replace_with); - let fully_reparsed = SourceFileNode::parse(&after); + let fully_reparsed = SourceFile::parse(&after); let incrementally_reparsed = { - let f = SourceFileNode::parse(&before); + let f = SourceFile::parse(&before); let edit = AtomTextEdit { delete: range, insert: replace_with.to_string(), @@ -183,7 +180,7 @@ mod tests { reparser(f.syntax(), &edit).expect("cannot incrementally reparse"); let green_root = node.replace_with(green); let errors = super::merge_errors(f.errors(), new_errors, node, &edit); - SourceFileNode::new(green_root, errors) + SourceFile::new(green_root, errors) }; assert_eq_text!( diff --git a/crates/ra_syntax/src/utils.rs b/crates/ra_syntax/src/utils.rs index 0a2b6afbc..2e1b42da0 100644 --- a/crates/ra_syntax/src/utils.rs +++ b/crates/ra_syntax/src/utils.rs @@ -1,11 +1,11 @@ -use crate::{SourceFileNode, SyntaxKind, SyntaxNodeRef, WalkEvent, AstNode}; -use std::fmt::Write; -use std::str; +use std::{str, fmt::Write}; + +use crate::{SourceFile, SyntaxKind, WalkEvent, AstNode, SyntaxNode}; /// Parse a file and create a string representation of the resulting parse tree. -pub fn dump_tree(syntax: SyntaxNodeRef) -> String { - let mut errors: Vec<_> = match syntax.ancestors().find_map(SourceFileNode::cast) { - Some(file) => file.owned().errors(), +pub fn dump_tree(syntax: &SyntaxNode) -> String { + let mut errors: Vec<_> = match syntax.ancestors().find_map(SourceFile::cast) { + Some(file) => file.errors(), None => syntax.root_data().to_vec(), }; errors.sort_by_key(|e| e.offset()); @@ -48,14 +48,13 @@ pub fn dump_tree(syntax: SyntaxNodeRef) -> String { } pub fn check_fuzz_invariants(text: &str) { - let file = SourceFileNode::parse(text); + let file = SourceFile::parse(text); let root = file.syntax(); validate_block_structure(root); - let _ = file.ast(); let _ = file.errors(); } -pub(crate) fn validate_block_structure(root: SyntaxNodeRef) { +pub(crate) fn validate_block_structure(root: &SyntaxNode) { let mut stack = Vec::new(); for node in root.descendants() { match node.kind() { diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs index bdee8120c..73e1d20b9 100644 --- a/crates/ra_syntax/src/validation.rs +++ b/crates/ra_syntax/src/validation.rs @@ -1,16 +1,15 @@ -use crate::{ - algo::visit::{visitor_ctx, VisitorCtx}, - ast, - SourceFileNode, - yellow::SyntaxError, -}; - mod byte; mod byte_string; mod char; mod string; -pub(crate) fn validate(file: &SourceFileNode) -> Vec { +use crate::{ + SourceFile, yellow::SyntaxError, AstNode, + ast, + algo::visit::{visitor_ctx, VisitorCtx}, +}; + +pub(crate) fn validate(file: &SourceFile) -> Vec { let mut errors = Vec::new(); for node in file.syntax().descendants() { let _ = visitor_ctx(&mut errors) diff --git a/crates/ra_syntax/src/validation/byte.rs b/crates/ra_syntax/src/validation/byte.rs index 714224b09..4deb302a7 100644 --- a/crates/ra_syntax/src/validation/byte.rs +++ b/crates/ra_syntax/src/validation/byte.rs @@ -11,7 +11,7 @@ use crate::{ }, }; -pub(super) fn validate_byte_node(node: ast::Byte, errors: &mut Vec) { +pub(super) fn validate_byte_node(node: &ast::Byte, errors: &mut Vec) { let literal_text = node.text(); let literal_range = node.syntax().range(); let mut components = string_lexing::parse_byte_literal(literal_text); @@ -106,11 +106,11 @@ fn validate_byte_code_escape(text: &str, range: TextRange, errors: &mut Vec SourceFileNode { + fn build_file(literal: &str) -> TreePtr { let src = format!("const C: u8 = b'{}';", literal); - SourceFileNode::parse(&src) + SourceFile::parse(&src) } fn assert_valid_byte(literal: &str) { diff --git a/crates/ra_syntax/src/validation/byte_string.rs b/crates/ra_syntax/src/validation/byte_string.rs index f7a4fb156..670c43a09 100644 --- a/crates/ra_syntax/src/validation/byte_string.rs +++ b/crates/ra_syntax/src/validation/byte_string.rs @@ -9,7 +9,7 @@ use crate::{ use super::byte; -pub(crate) fn validate_byte_string_node(node: ast::ByteString, errors: &mut Vec) { +pub(crate) fn validate_byte_string_node(node: &ast::ByteString, errors: &mut Vec) { let literal_text = node.text(); let literal_range = node.syntax().range(); let mut components = string_lexing::parse_byte_string_literal(literal_text); @@ -43,12 +43,12 @@ pub(crate) fn validate_byte_string_node(node: ast::ByteString, errors: &mut Vec< #[cfg(test)] mod test { - use crate::SourceFileNode; + use crate::{SourceFile, TreePtr}; - fn build_file(literal: &str) -> SourceFileNode { + fn build_file(literal: &str) -> TreePtr { let src = format!(r#"const S: &'static [u8] = b"{}";"#, literal); println!("Source: {}", src); - SourceFileNode::parse(&src) + SourceFile::parse(&src) } fn assert_valid_str(literal: &str) { diff --git a/crates/ra_syntax/src/validation/char.rs b/crates/ra_syntax/src/validation/char.rs index 1d6fe8837..9cbd43fba 100644 --- a/crates/ra_syntax/src/validation/char.rs +++ b/crates/ra_syntax/src/validation/char.rs @@ -14,7 +14,7 @@ use crate::{ }, }; -pub(super) fn validate_char_node(node: ast::Char, errors: &mut Vec) { +pub(super) fn validate_char_node(node: &ast::Char, errors: &mut Vec) { let literal_text = node.text(); let literal_range = node.syntax().range(); let mut components = string_lexing::parse_char_literal(literal_text); @@ -175,11 +175,11 @@ fn validate_unicode_escape(text: &str, range: TextRange, errors: &mut Vec SourceFileNode { + fn build_file(literal: &str) -> TreePtr { let src = format!("const C: char = '{}';", literal); - SourceFileNode::parse(&src) + SourceFile::parse(&src) } fn assert_valid_char(literal: &str) { diff --git a/crates/ra_syntax/src/validation/string.rs b/crates/ra_syntax/src/validation/string.rs index 1371bb1f0..7b2a68d12 100644 --- a/crates/ra_syntax/src/validation/string.rs +++ b/crates/ra_syntax/src/validation/string.rs @@ -9,7 +9,7 @@ use crate::{ use super::char; -pub(crate) fn validate_string_node(node: ast::String, errors: &mut Vec) { +pub(crate) fn validate_string_node(node: &ast::String, errors: &mut Vec) { let literal_text = node.text(); let literal_range = node.syntax().range(); let mut components = string_lexing::parse_string_literal(literal_text); @@ -38,12 +38,12 @@ pub(crate) fn validate_string_node(node: ast::String, errors: &mut Vec SourceFileNode { + fn build_file(literal: &str) -> TreePtr { let src = format!(r#"const S: &'static str = "{}";"#, literal); println!("Source: {}", src); - SourceFileNode::parse(&src) + SourceFile::parse(&src) } fn assert_valid_str(literal: &str) { diff --git a/crates/ra_syntax/src/yellow.rs b/crates/ra_syntax/src/yellow.rs index cacd89dc8..6dc846f33 100644 --- a/crates/ra_syntax/src/yellow.rs +++ b/crates/ra_syntax/src/yellow.rs @@ -4,15 +4,12 @@ mod syntax_text; use self::syntax_text::SyntaxText; use crate::{SmolStr, SyntaxKind, TextRange}; -use rowan::Types; -use std::{ - fmt, - hash::{Hash, Hasher}, -}; +use rowan::{Types, TransparentNewType}; +use std::fmt; pub(crate) use self::builder::GreenBuilder; pub use self::syntax_error::{SyntaxError, SyntaxErrorKind, Location}; -pub use rowan::{TreeRoot, WalkEvent}; +pub use rowan::WalkEvent; #[derive(Debug, Clone, Copy)] pub enum RaTypes {} @@ -21,35 +18,19 @@ impl Types for RaTypes { type RootData = Vec; } -pub type OwnedRoot = ::rowan::OwnedRoot; -pub type RefRoot<'a> = ::rowan::RefRoot<'a, RaTypes>; - pub type GreenNode = ::rowan::GreenNode; +pub type TreePtr = ::rowan::TreePtr; -#[derive(Clone, Copy)] -pub struct SyntaxNode = OwnedRoot>(pub(crate) ::rowan::SyntaxNode); -pub type SyntaxNodeRef<'a> = SyntaxNode>; - -impl PartialEq> for SyntaxNode -where - R1: TreeRoot, - R2: TreeRoot, -{ - fn eq(&self, other: &SyntaxNode) -> bool { - self.0 == other.0 - } -} - -impl> Eq for SyntaxNode {} -impl> Hash for SyntaxNode { - fn hash(&self, state: &mut H) { - self.0.hash(state) - } +#[derive(PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct SyntaxNode(pub(crate) ::rowan::SyntaxNode); +unsafe impl TransparentNewType for SyntaxNode { + type Repr = ::rowan::SyntaxNode; } impl SyntaxNode { - pub(crate) fn new(green: GreenNode, errors: Vec) -> SyntaxNode { - SyntaxNode(::rowan::SyntaxNode::new(green, errors)) + pub(crate) fn new(green: GreenNode, errors: Vec) -> TreePtr { + TreePtr::cast(::rowan::SyntaxNode::new(green, errors)) } } @@ -59,45 +40,42 @@ pub enum Direction { Prev, } -impl<'a> SyntaxNodeRef<'a> { - pub fn leaf_text(self) -> Option<&'a SmolStr> { +impl SyntaxNode { + pub fn leaf_text(&self) -> Option<&SmolStr> { self.0.leaf_text() } - pub fn ancestors(self) -> impl Iterator> { + pub fn ancestors(&self) -> impl Iterator { crate::algo::generate(Some(self), |&node| node.parent()) } - pub fn descendants(self) -> impl Iterator> { + pub fn descendants(&self) -> impl Iterator { self.preorder().filter_map(|event| match event { WalkEvent::Enter(node) => Some(node), WalkEvent::Leave(_) => None, }) } - pub fn siblings(self, direction: Direction) -> impl Iterator> { + pub fn siblings(&self, direction: Direction) -> impl Iterator { crate::algo::generate(Some(self), move |&node| match direction { Direction::Next => node.next_sibling(), Direction::Prev => node.prev_sibling(), }) } - pub fn preorder(self) -> impl Iterator>> { + pub fn preorder(&self) -> impl Iterator> { self.0.preorder().map(|event| match event { - WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode(n)), - WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxNode(n)), + WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode::from_repr(n)), + WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxNode::from_repr(n)), }) } } -impl> SyntaxNode { +impl SyntaxNode { pub(crate) fn root_data(&self) -> &Vec { self.0.root_data() } pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode { - self.0.replace_with(replacement) - } - pub fn borrowed<'a>(&'a self) -> SyntaxNode> { - SyntaxNode(self.0.borrowed()) + self.0.replace_self(replacement) } - pub fn owned(&self) -> SyntaxNode { - SyntaxNode(self.0.owned()) + pub fn to_owned(&self) -> TreePtr { + TreePtr::cast(self.0.to_owned()) } pub fn kind(&self) -> SyntaxKind { self.0.kind() @@ -106,32 +84,32 @@ impl> SyntaxNode { self.0.range() } pub fn text(&self) -> SyntaxText { - SyntaxText::new(self.borrowed()) + SyntaxText::new(self) } pub fn is_leaf(&self) -> bool { self.0.is_leaf() } - pub fn parent(&self) -> Option> { - self.0.parent().map(SyntaxNode) + pub fn parent(&self) -> Option<&SyntaxNode> { + self.0.parent().map(SyntaxNode::from_repr) } - pub fn first_child(&self) -> Option> { - self.0.first_child().map(SyntaxNode) + pub fn first_child(&self) -> Option<&SyntaxNode> { + self.0.first_child().map(SyntaxNode::from_repr) } - pub fn last_child(&self) -> Option> { - self.0.last_child().map(SyntaxNode) + pub fn last_child(&self) -> Option<&SyntaxNode> { + self.0.last_child().map(SyntaxNode::from_repr) } - pub fn next_sibling(&self) -> Option> { - self.0.next_sibling().map(SyntaxNode) + pub fn next_sibling(&self) -> Option<&SyntaxNode> { + self.0.next_sibling().map(SyntaxNode::from_repr) } - pub fn prev_sibling(&self) -> Option> { - self.0.prev_sibling().map(SyntaxNode) + pub fn prev_sibling(&self) -> Option<&SyntaxNode> { + self.0.prev_sibling().map(SyntaxNode::from_repr) } - pub fn children(&self) -> SyntaxNodeChildren { + pub fn children(&self) -> SyntaxNodeChildren { SyntaxNodeChildren(self.0.children()) } } -impl> fmt::Debug for SyntaxNode { +impl fmt::Debug for SyntaxNode { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { write!(fmt, "{:?}@{:?}", self.kind(), self.range())?; if has_short_text(self.kind()) { @@ -142,13 +120,13 @@ impl> fmt::Debug for SyntaxNode { } #[derive(Debug)] -pub struct SyntaxNodeChildren>(::rowan::SyntaxNodeChildren); +pub struct SyntaxNodeChildren<'a>(::rowan::SyntaxNodeChildren<'a, RaTypes>); -impl> Iterator for SyntaxNodeChildren { - type Item = SyntaxNode; +impl<'a> Iterator for SyntaxNodeChildren<'a> { + type Item = &'a SyntaxNode; - fn next(&mut self) -> Option> { - self.0.next().map(SyntaxNode) + fn next(&mut self) -> Option<&'a SyntaxNode> { + self.0.next().map(SyntaxNode::from_repr) } } diff --git a/crates/ra_syntax/src/yellow/syntax_text.rs b/crates/ra_syntax/src/yellow/syntax_text.rs index 279a83b61..31db0fdab 100644 --- a/crates/ra_syntax/src/yellow/syntax_text.rs +++ b/crates/ra_syntax/src/yellow/syntax_text.rs @@ -3,17 +3,17 @@ use std::{fmt, ops}; use ra_text_edit::text_utils::contains_offset_nonstrict; use crate::{ text_utils::intersect, - SyntaxNodeRef, TextRange, TextUnit, + SyntaxNode, TextRange, TextUnit, }; #[derive(Clone)] pub struct SyntaxText<'a> { - node: SyntaxNodeRef<'a>, + node: &'a SyntaxNode, range: TextRange, } impl<'a> SyntaxText<'a> { - pub(crate) fn new(node: SyntaxNodeRef<'a>) -> SyntaxText<'a> { + pub(crate) fn new(node: &'a SyntaxNode) -> SyntaxText<'a> { SyntaxText { node, range: node.range(), diff --git a/crates/ra_syntax/tests/test.rs b/crates/ra_syntax/tests/test.rs index 2235dc401..3243b27ae 100644 --- a/crates/ra_syntax/tests/test.rs +++ b/crates/ra_syntax/tests/test.rs @@ -9,8 +9,8 @@ use std::{ use test_utils::{project_dir, dir_tests, read_text, collect_tests}; use ra_syntax::{ + SourceFile, AstNode, utils::{check_fuzz_invariants, dump_tree}, - SourceFileNode, }; #[test] @@ -27,7 +27,7 @@ fn parser_tests() { &test_data_dir(), &["parser/inline/ok", "parser/ok"], |text, path| { - let file = SourceFileNode::parse(text); + let file = SourceFile::parse(text); let errors = file.errors(); assert_eq!( &*errors, @@ -42,7 +42,7 @@ fn parser_tests() { &test_data_dir(), &["parser/err", "parser/inline/err"], |text, path| { - let file = SourceFileNode::parse(text); + let file = SourceFile::parse(text); let errors = file.errors(); assert_ne!( &*errors, @@ -85,7 +85,7 @@ fn self_hosting_parsing() { { count += 1; let text = read_text(entry.path()); - let node = SourceFileNode::parse(&text); + let node = SourceFile::parse(&text); let errors = node.errors(); assert_eq!( &*errors, -- cgit v1.2.3