hide root

This commit is contained in:
Aleksey Kladov 2018-08-17 21:10:55 +03:00
parent ed7ae78c6f
commit 70097504f7
10 changed files with 91 additions and 65 deletions

View file

@ -26,7 +26,7 @@ use std::{
}; };
use libsyntax2::{ use libsyntax2::{
TextUnit, TextRange, SyntaxRoot, TextUnit, TextRange, RefRoot,
ast::{self, AstNode, NameOwner}, ast::{self, AstNode, NameOwner},
SyntaxKind::*, SyntaxKind::*,
}; };
@ -151,7 +151,7 @@ impl World {
Ok(vec![]) Ok(vec![])
} }
fn index_resolve(&self, name_ref: ast::NameRef<&SyntaxRoot>) -> Vec<(FileId, FileSymbol)> { fn index_resolve(&self, name_ref: ast::NameRef<RefRoot>) -> Vec<(FileId, FileSymbol)> {
let name = name_ref.text(); let name = name_ref.text();
let mut query = Query::new(name.to_string()); let mut query = Query::new(name.to_string());
query.exact(); query.exact();
@ -159,7 +159,7 @@ impl World {
self.world_symbols(query) self.world_symbols(query)
} }
fn resolve_module(&self, id: FileId, module: ast::Module<&SyntaxRoot>) -> Vec<(FileId, FileSymbol)> { fn resolve_module(&self, id: FileId, module: ast::Module<RefRoot>) -> Vec<(FileId, FileSymbol)> {
let name = match module.name() { let name = match module.name() {
Some(name) => name.text(), Some(name) => name.text(),
None => return Vec::new(), None => return Vec::new(),

View file

@ -2,8 +2,7 @@ use {TextUnit, File, EditBuilder, Edit};
use libsyntax2::{ use libsyntax2::{
ast::{self, AstNode, AttrsOwner}, ast::{self, AstNode, AttrsOwner},
SyntaxKind::COMMA, SyntaxKind::COMMA,
SyntaxNodeRef, SyntaxNodeRef, RefRoot,
SyntaxRoot,
algo::{ algo::{
Direction, siblings, Direction, siblings,
find_leaf_at_offset, ancestors, find_leaf_at_offset, ancestors,
@ -71,7 +70,7 @@ fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<Synta
.find(|node| !node.kind().is_trivia()) .find(|node| !node.kind().is_trivia())
} }
pub fn find_node<'a, N: AstNode<&'a SyntaxRoot>>(syntax: SyntaxNodeRef<'a>, offset: TextUnit) -> Option<N> { pub fn find_node<'a, N: AstNode<RefRoot<'a>>>(syntax: SyntaxNodeRef<'a>, offset: TextUnit) -> Option<N> {
let leaves = find_leaf_at_offset(syntax, offset); let leaves = find_leaf_at_offset(syntax, offset);
let leaf = leaves.clone() let leaf = leaves.clone()
.find(|leaf| !leaf.kind().is_trivia()) .find(|leaf| !leaf.kind().is_trivia())

View file

@ -1,6 +1,6 @@
use smol_str::SmolStr; use smol_str::SmolStr;
use libsyntax2::{ use libsyntax2::{
SyntaxKind, SyntaxNodeRef, SyntaxRoot, AstNode, SyntaxKind, SyntaxNodeRef, AstNode, RefRoot,
ast::{self, NameOwner}, ast::{self, NameOwner},
algo::{ algo::{
visit::{visitor, Visitor}, visit::{visitor, Visitor},
@ -32,7 +32,7 @@ pub fn file_symbols(file: &ast::File) -> Vec<FileSymbol> {
} }
fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> { fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> {
fn decl<'a, N: NameOwner<&'a SyntaxRoot>>(node: N) -> Option<FileSymbol> { fn decl<'a, N: NameOwner<RefRoot<'a>>>(node: N) -> Option<FileSymbol> {
let name = node.name()?; let name = node.name()?;
Some(FileSymbol { Some(FileSymbol {
name: name.text(), name: name.text(),
@ -80,7 +80,7 @@ pub fn file_structure(file: &ast::File) -> Vec<StructureNode> {
} }
fn structure_node(node: SyntaxNodeRef) -> Option<StructureNode> { fn structure_node(node: SyntaxNodeRef) -> Option<StructureNode> {
fn decl<'a, N: NameOwner<&'a SyntaxRoot>>(node: N) -> Option<StructureNode> { fn decl<'a, N: NameOwner<RefRoot<'a>>>(node: N) -> Option<StructureNode> {
let name = node.name()?; let name = node.name()?;
Some(StructureNode { Some(StructureNode {
parent: None, parent: None,

View file

@ -1,5 +1,5 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use {SyntaxNodeRef, AstNode, SyntaxRoot}; use {SyntaxNodeRef, AstNode, RefRoot};
pub fn visitor<'a, T>() -> impl Visitor<'a, Output=T> { pub fn visitor<'a, T>() -> impl Visitor<'a, Output=T> {
@ -10,7 +10,7 @@ pub trait Visitor<'a>: Sized {
type Output; type Output;
fn accept(self, node: SyntaxNodeRef<'a>) -> Option<Self::Output>; fn accept(self, node: SyntaxNodeRef<'a>) -> Option<Self::Output>;
fn visit<N, F>(self, f: F) -> Vis<Self, N, F> fn visit<N, F>(self, f: F) -> Vis<Self, N, F>
where N: AstNode<&'a SyntaxRoot>, where N: AstNode<RefRoot<'a>>,
F: FnOnce(N) -> Self::Output, F: FnOnce(N) -> Self::Output,
{ {
Vis { inner: self, f, ph: PhantomData } Vis { inner: self, f, ph: PhantomData }
@ -40,7 +40,7 @@ pub struct Vis<V, N, F> {
impl<'a, V, N, F> Visitor<'a> for Vis<V, N, F> impl<'a, V, N, F> Visitor<'a> for Vis<V, N, F>
where where
V: Visitor<'a>, V: Visitor<'a>,
N: AstNode<&'a SyntaxRoot>, N: AstNode<RefRoot<'a>>,
F: FnOnce(N) -> <V as Visitor<'a>>::Output, F: FnOnce(N) -> <V as Visitor<'a>>::Output,
{ {
type Output = <V as Visitor<'a>>::Output; type Output = <V as Visitor<'a>>::Output;

View file

@ -1,13 +1,12 @@
use std::sync::Arc;
use { use {
ast, ast,
SyntaxNode, SyntaxRoot, TreeRoot, AstNode, SyntaxNode, OwnedRoot, TreeRoot, AstNode,
SyntaxKind::*, SyntaxKind::*,
}; };
// ArrayType // ArrayType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct ArrayType<R: TreeRoot = Arc<SyntaxRoot>> { pub struct ArrayType<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -25,7 +24,7 @@ impl<R: TreeRoot> ArrayType<R> {}
// Attr // Attr
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct Attr<R: TreeRoot = Arc<SyntaxRoot>> { pub struct Attr<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -50,7 +49,7 @@ impl<R: TreeRoot> Attr<R> {
// ConstDef // ConstDef
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct ConstDef<R: TreeRoot = Arc<SyntaxRoot>> { pub struct ConstDef<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -70,7 +69,7 @@ impl<R: TreeRoot> ConstDef<R> {}
// DynTraitType // DynTraitType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct DynTraitType<R: TreeRoot = Arc<SyntaxRoot>> { pub struct DynTraitType<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -88,7 +87,7 @@ impl<R: TreeRoot> DynTraitType<R> {}
// EnumDef // EnumDef
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct EnumDef<R: TreeRoot = Arc<SyntaxRoot>> { pub struct EnumDef<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -108,7 +107,7 @@ impl<R: TreeRoot> EnumDef<R> {}
// File // File
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct File<R: TreeRoot = Arc<SyntaxRoot>> { pub struct File<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -132,7 +131,7 @@ impl<R: TreeRoot> File<R> {
// FnDef // FnDef
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct FnDef<R: TreeRoot = Arc<SyntaxRoot>> { pub struct FnDef<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -152,7 +151,7 @@ impl<R: TreeRoot> FnDef<R> {}
// FnPointerType // FnPointerType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct FnPointerType<R: TreeRoot = Arc<SyntaxRoot>> { pub struct FnPointerType<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -170,7 +169,7 @@ impl<R: TreeRoot> FnPointerType<R> {}
// ForType // ForType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct ForType<R: TreeRoot = Arc<SyntaxRoot>> { pub struct ForType<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -188,7 +187,7 @@ impl<R: TreeRoot> ForType<R> {}
// ImplItem // ImplItem
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct ImplItem<R: TreeRoot = Arc<SyntaxRoot>> { pub struct ImplItem<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -206,7 +205,7 @@ impl<R: TreeRoot> ImplItem<R> {}
// ImplTraitType // ImplTraitType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct ImplTraitType<R: TreeRoot = Arc<SyntaxRoot>> { pub struct ImplTraitType<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -224,7 +223,7 @@ impl<R: TreeRoot> ImplTraitType<R> {}
// Module // Module
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct Module<R: TreeRoot = Arc<SyntaxRoot>> { pub struct Module<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -244,7 +243,7 @@ impl<R: TreeRoot> Module<R> {}
// Name // Name
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct Name<R: TreeRoot = Arc<SyntaxRoot>> { pub struct Name<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -262,7 +261,7 @@ impl<R: TreeRoot> Name<R> {}
// NameRef // NameRef
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct NameRef<R: TreeRoot = Arc<SyntaxRoot>> { pub struct NameRef<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -280,7 +279,7 @@ impl<R: TreeRoot> NameRef<R> {}
// NamedField // NamedField
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct NamedField<R: TreeRoot = Arc<SyntaxRoot>> { pub struct NamedField<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -300,7 +299,7 @@ impl<R: TreeRoot> NamedField<R> {}
// NeverType // NeverType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct NeverType<R: TreeRoot = Arc<SyntaxRoot>> { pub struct NeverType<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -318,7 +317,7 @@ impl<R: TreeRoot> NeverType<R> {}
// NominalDef // NominalDef
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum NominalDef<R: TreeRoot = Arc<SyntaxRoot>> { pub enum NominalDef<R: TreeRoot = OwnedRoot> {
StructDef(StructDef<R>), StructDef(StructDef<R>),
EnumDef(EnumDef<R>), EnumDef(EnumDef<R>),
} }
@ -344,7 +343,7 @@ impl<R: TreeRoot> NominalDef<R> {}
// ParenType // ParenType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct ParenType<R: TreeRoot = Arc<SyntaxRoot>> { pub struct ParenType<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -362,7 +361,7 @@ impl<R: TreeRoot> ParenType<R> {}
// PathType // PathType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct PathType<R: TreeRoot = Arc<SyntaxRoot>> { pub struct PathType<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -380,7 +379,7 @@ impl<R: TreeRoot> PathType<R> {}
// PlaceholderType // PlaceholderType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct PlaceholderType<R: TreeRoot = Arc<SyntaxRoot>> { pub struct PlaceholderType<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -398,7 +397,7 @@ impl<R: TreeRoot> PlaceholderType<R> {}
// PointerType // PointerType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct PointerType<R: TreeRoot = Arc<SyntaxRoot>> { pub struct PointerType<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -416,7 +415,7 @@ impl<R: TreeRoot> PointerType<R> {}
// ReferenceType // ReferenceType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct ReferenceType<R: TreeRoot = Arc<SyntaxRoot>> { pub struct ReferenceType<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -434,7 +433,7 @@ impl<R: TreeRoot> ReferenceType<R> {}
// SliceType // SliceType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct SliceType<R: TreeRoot = Arc<SyntaxRoot>> { pub struct SliceType<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -452,7 +451,7 @@ impl<R: TreeRoot> SliceType<R> {}
// StaticDef // StaticDef
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct StaticDef<R: TreeRoot = Arc<SyntaxRoot>> { pub struct StaticDef<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -472,7 +471,7 @@ impl<R: TreeRoot> StaticDef<R> {}
// StructDef // StructDef
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct StructDef<R: TreeRoot = Arc<SyntaxRoot>> { pub struct StructDef<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -498,7 +497,7 @@ impl<R: TreeRoot> StructDef<R> {
// TokenTree // TokenTree
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct TokenTree<R: TreeRoot = Arc<SyntaxRoot>> { pub struct TokenTree<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -516,7 +515,7 @@ impl<R: TreeRoot> TokenTree<R> {}
// TraitDef // TraitDef
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct TraitDef<R: TreeRoot = Arc<SyntaxRoot>> { pub struct TraitDef<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -536,7 +535,7 @@ impl<R: TreeRoot> TraitDef<R> {}
// TupleType // TupleType
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct TupleType<R: TreeRoot = Arc<SyntaxRoot>> { pub struct TupleType<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -554,7 +553,7 @@ impl<R: TreeRoot> TupleType<R> {}
// TypeDef // TypeDef
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct TypeDef<R: TreeRoot = Arc<SyntaxRoot>> { pub struct TypeDef<R: TreeRoot = OwnedRoot> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
@ -574,7 +573,7 @@ impl<R: TreeRoot> TypeDef<R> {}
// TypeRef // TypeRef
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum TypeRef<R: TreeRoot = Arc<SyntaxRoot>> { pub enum TypeRef<R: TreeRoot = OwnedRoot> {
ParenType(ParenType<R>), ParenType(ParenType<R>),
TupleType(TupleType<R>), TupleType(TupleType<R>),
NeverType(NeverType<R>), NeverType(NeverType<R>),

View file

@ -1,12 +1,10 @@
mod generated; mod generated;
use std::sync::Arc;
use itertools::Itertools; use itertools::Itertools;
use smol_str::SmolStr; use smol_str::SmolStr;
use { use {
SyntaxNode, SyntaxNodeRef, SyntaxRoot, TreeRoot, SyntaxError, SyntaxNode, SyntaxNodeRef, OwnedRoot, TreeRoot, SyntaxError,
SyntaxKind::*, SyntaxKind::*,
}; };
pub use self::generated::*; pub use self::generated::*;
@ -37,7 +35,7 @@ pub trait AttrsOwner<R: TreeRoot>: AstNode<R> {
} }
} }
impl File<Arc<SyntaxRoot>> { impl File<OwnedRoot> {
pub fn parse(text: &str) -> Self { pub fn parse(text: &str) -> Self {
File::cast(::parse(text)).unwrap() File::cast(::parse(text)).unwrap()
} }
@ -45,7 +43,7 @@ impl File<Arc<SyntaxRoot>> {
impl<R: TreeRoot> File<R> { impl<R: TreeRoot> File<R> {
pub fn errors(&self) -> Vec<SyntaxError> { pub fn errors(&self) -> Vec<SyntaxError> {
self.syntax().root.errors.clone() self.syntax().root.syntax_root().errors.clone()
} }
} }

View file

@ -45,7 +45,7 @@ pub use {
lexer::{tokenize, Token}, lexer::{tokenize, Token},
syntax_kinds::SyntaxKind, syntax_kinds::SyntaxKind,
text_unit::{TextRange, TextUnit}, text_unit::{TextRange, TextUnit},
yellow::{SyntaxNode, SyntaxNodeRef, SyntaxRoot, TreeRoot, SyntaxError}, yellow::{SyntaxNode, SyntaxNodeRef, OwnedRoot, RefRoot, TreeRoot, SyntaxError},
}; };

View file

@ -1,13 +1,13 @@
use std::fmt::Write; use std::fmt::Write;
use { use {
algo::walk::{walk, WalkEvent}, algo::walk::{walk, WalkEvent},
SyntaxNode, SyntaxNode, TreeRoot,
}; };
/// Parse a file and create a string representation of the resulting parse tree. /// Parse a file and create a string representation of the resulting parse tree.
pub fn dump_tree(syntax: &SyntaxNode) -> String { pub fn dump_tree(syntax: &SyntaxNode) -> String {
let syntax = syntax.as_ref(); let syntax = syntax.as_ref();
let mut errors: Vec<_> = syntax.root.errors.iter().cloned().collect(); let mut errors: Vec<_> = syntax.root.syntax_root().errors.iter().cloned().collect();
errors.sort_by_key(|e| e.offset); errors.sort_by_key(|e| e.offset);
let mut err_pos = 0; let mut err_pos = 0;
let mut level = 0; let mut level = 0;

View file

@ -4,7 +4,6 @@ mod red;
mod syntax; mod syntax;
use std::{ use std::{
ops::Deref,
sync::Arc, sync::Arc,
ptr, ptr,
}; };
@ -15,17 +14,48 @@ pub(crate) use self::{
red::RedNode, red::RedNode,
}; };
pub trait TreeRoot: Deref<Target=SyntaxRoot> + Clone + Send + Sync {}
#[derive(Debug)] #[derive(Debug)]
pub struct SyntaxRoot { pub struct SyntaxRoot {
red: RedNode, red: RedNode,
pub(crate) errors: Vec<SyntaxError>, pub(crate) errors: Vec<SyntaxError>,
} }
impl TreeRoot for Arc<SyntaxRoot> {} pub trait TreeRoot: Clone + Send + Sync {
fn borrowed(&self) -> RefRoot;
fn owned(&self) -> OwnedRoot;
impl<'a> TreeRoot for &'a SyntaxRoot {} #[doc(hidden)]
fn syntax_root(&self) -> &SyntaxRoot;
}
#[derive(Clone, Debug)]
pub struct OwnedRoot(Arc<SyntaxRoot>);
#[derive(Clone, Copy, Debug)]
pub struct RefRoot<'a>(&'a OwnedRoot); // TODO: shared_from_this instead of double indirection
impl TreeRoot for OwnedRoot {
fn borrowed(&self) -> RefRoot {
RefRoot(&self)
}
fn owned(&self) -> OwnedRoot {
self.clone()
}
fn syntax_root(&self) -> &SyntaxRoot {
&*self.0
}
}
impl<'a> TreeRoot for RefRoot<'a> {
fn borrowed(&self) -> RefRoot {
*self
}
fn owned(&self) -> OwnedRoot {
self.0.clone()
}
fn syntax_root(&self) -> &SyntaxRoot {
self.0.syntax_root()
}
}
impl SyntaxRoot { impl SyntaxRoot {
pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SyntaxRoot { pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SyntaxRoot {

View file

@ -3,14 +3,14 @@ use std::{fmt, sync::Arc};
use smol_str::SmolStr; use smol_str::SmolStr;
use { use {
yellow::{RedNode, TreeRoot, SyntaxRoot, RedPtr}, yellow::{RedNode, TreeRoot, SyntaxRoot, RedPtr, RefRoot, OwnedRoot},
SyntaxKind::{self, *}, SyntaxKind::{self, *},
TextRange, TextUnit, TextRange, TextUnit,
}; };
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct SyntaxNode<R: TreeRoot = Arc<SyntaxRoot>> { pub struct SyntaxNode<R: TreeRoot = OwnedRoot> {
pub(crate) root: R, pub(crate) root: R,
// Guaranteed to not dangle, because `root` holds a // Guaranteed to not dangle, because `root` holds a
// strong reference to red's ancestor // strong reference to red's ancestor
@ -28,7 +28,7 @@ impl<R1: TreeRoot, R2: TreeRoot> PartialEq<SyntaxNode<R1>> for SyntaxNode<R2> {
impl<R: TreeRoot> Eq for SyntaxNode<R> {} impl<R: TreeRoot> Eq for SyntaxNode<R> {}
pub type SyntaxNodeRef<'a> = SyntaxNode<&'a SyntaxRoot>; pub type SyntaxNodeRef<'a> = SyntaxNode<RefRoot<'a>>;
#[test] #[test]
fn syntax_node_ref_is_copy() { fn syntax_node_ref_is_copy() {
@ -42,18 +42,18 @@ pub struct SyntaxError {
pub offset: TextUnit, pub offset: TextUnit,
} }
impl SyntaxNode<Arc<SyntaxRoot>> { impl SyntaxNode<OwnedRoot> {
pub(crate) fn new_owned(root: SyntaxRoot) -> Self { pub(crate) fn new_owned(root: SyntaxRoot) -> Self {
let root = Arc::new(root); let root = OwnedRoot(Arc::new(root));
let red = RedPtr::new(&root.red); let red = RedPtr::new(&root.syntax_root().red);
SyntaxNode { root, red } SyntaxNode { root, red }
} }
} }
impl<R: TreeRoot> SyntaxNode<R> { impl<R: TreeRoot> SyntaxNode<R> {
pub fn as_ref<'a>(&'a self) -> SyntaxNode<&'a SyntaxRoot> { pub fn as_ref<'a>(&'a self) -> SyntaxNode<RefRoot<'a>> {
SyntaxNode { SyntaxNode {
root: &*self.root, root: self.root.borrowed(),
red: self.red, red: self.red,
} }
} }