aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-17 19:10:55 +0100
committerAleksey Kladov <[email protected]>2018-08-17 19:10:55 +0100
commit70097504f78c4c41368a0b864a94df95fb9c27f7 (patch)
treec4034cab3c0d0b9488c0be538ae1d4c286570de9 /crates
parented7ae78c6fd9e508f6e959c6a164cf8481f6b377 (diff)
hide root
Diffstat (limited to 'crates')
-rw-r--r--crates/libanalysis/src/lib.rs6
-rw-r--r--crates/libeditor/src/code_actions.rs5
-rw-r--r--crates/libeditor/src/symbols.rs6
-rw-r--r--crates/libsyntax2/src/algo/visit.rs6
-rw-r--r--crates/libsyntax2/src/ast/generated.rs63
-rw-r--r--crates/libsyntax2/src/ast/mod.rs8
-rw-r--r--crates/libsyntax2/src/lib.rs2
-rw-r--r--crates/libsyntax2/src/utils.rs4
-rw-r--r--crates/libsyntax2/src/yellow/mod.rs40
-rw-r--r--crates/libsyntax2/src/yellow/syntax.rs16
10 files changed, 91 insertions, 65 deletions
diff --git a/crates/libanalysis/src/lib.rs b/crates/libanalysis/src/lib.rs
index b1093fe83..ae96a0d62 100644
--- a/crates/libanalysis/src/lib.rs
+++ b/crates/libanalysis/src/lib.rs
@@ -26,7 +26,7 @@ use std::{
26}; 26};
27 27
28use libsyntax2::{ 28use libsyntax2::{
29 TextUnit, TextRange, SyntaxRoot, 29 TextUnit, TextRange, RefRoot,
30 ast::{self, AstNode, NameOwner}, 30 ast::{self, AstNode, NameOwner},
31 SyntaxKind::*, 31 SyntaxKind::*,
32}; 32};
@@ -151,7 +151,7 @@ impl World {
151 Ok(vec![]) 151 Ok(vec![])
152 } 152 }
153 153
154 fn index_resolve(&self, name_ref: ast::NameRef<&SyntaxRoot>) -> Vec<(FileId, FileSymbol)> { 154 fn index_resolve(&self, name_ref: ast::NameRef<RefRoot>) -> Vec<(FileId, FileSymbol)> {
155 let name = name_ref.text(); 155 let name = name_ref.text();
156 let mut query = Query::new(name.to_string()); 156 let mut query = Query::new(name.to_string());
157 query.exact(); 157 query.exact();
@@ -159,7 +159,7 @@ impl World {
159 self.world_symbols(query) 159 self.world_symbols(query)
160 } 160 }
161 161
162 fn resolve_module(&self, id: FileId, module: ast::Module<&SyntaxRoot>) -> Vec<(FileId, FileSymbol)> { 162 fn resolve_module(&self, id: FileId, module: ast::Module<RefRoot>) -> Vec<(FileId, FileSymbol)> {
163 let name = match module.name() { 163 let name = match module.name() {
164 Some(name) => name.text(), 164 Some(name) => name.text(),
165 None => return Vec::new(), 165 None => return Vec::new(),
diff --git a/crates/libeditor/src/code_actions.rs b/crates/libeditor/src/code_actions.rs
index 4b2515835..bb6eb0d61 100644
--- a/crates/libeditor/src/code_actions.rs
+++ b/crates/libeditor/src/code_actions.rs
@@ -2,8 +2,7 @@ use {TextUnit, File, EditBuilder, Edit};
2use libsyntax2::{ 2use libsyntax2::{
3 ast::{self, AstNode, AttrsOwner}, 3 ast::{self, AstNode, AttrsOwner},
4 SyntaxKind::COMMA, 4 SyntaxKind::COMMA,
5 SyntaxNodeRef, 5 SyntaxNodeRef, RefRoot,
6 SyntaxRoot,
7 algo::{ 6 algo::{
8 Direction, siblings, 7 Direction, siblings,
9 find_leaf_at_offset, ancestors, 8 find_leaf_at_offset, ancestors,
@@ -71,7 +70,7 @@ fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<Synta
71 .find(|node| !node.kind().is_trivia()) 70 .find(|node| !node.kind().is_trivia())
72} 71}
73 72
74pub fn find_node<'a, N: AstNode<&'a SyntaxRoot>>(syntax: SyntaxNodeRef<'a>, offset: TextUnit) -> Option<N> { 73pub fn find_node<'a, N: AstNode<RefRoot<'a>>>(syntax: SyntaxNodeRef<'a>, offset: TextUnit) -> Option<N> {
75 let leaves = find_leaf_at_offset(syntax, offset); 74 let leaves = find_leaf_at_offset(syntax, offset);
76 let leaf = leaves.clone() 75 let leaf = leaves.clone()
77 .find(|leaf| !leaf.kind().is_trivia()) 76 .find(|leaf| !leaf.kind().is_trivia())
diff --git a/crates/libeditor/src/symbols.rs b/crates/libeditor/src/symbols.rs
index ce870430e..cf5bd2a41 100644
--- a/crates/libeditor/src/symbols.rs
+++ b/crates/libeditor/src/symbols.rs
@@ -1,6 +1,6 @@
1use smol_str::SmolStr; 1use smol_str::SmolStr;
2use libsyntax2::{ 2use libsyntax2::{
3 SyntaxKind, SyntaxNodeRef, SyntaxRoot, AstNode, 3 SyntaxKind, SyntaxNodeRef, AstNode, RefRoot,
4 ast::{self, NameOwner}, 4 ast::{self, NameOwner},
5 algo::{ 5 algo::{
6 visit::{visitor, Visitor}, 6 visit::{visitor, Visitor},
@@ -32,7 +32,7 @@ pub fn file_symbols(file: &ast::File) -> Vec<FileSymbol> {
32} 32}
33 33
34fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> { 34fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> {
35 fn decl<'a, N: NameOwner<&'a SyntaxRoot>>(node: N) -> Option<FileSymbol> { 35 fn decl<'a, N: NameOwner<RefRoot<'a>>>(node: N) -> Option<FileSymbol> {
36 let name = node.name()?; 36 let name = node.name()?;
37 Some(FileSymbol { 37 Some(FileSymbol {
38 name: name.text(), 38 name: name.text(),
@@ -80,7 +80,7 @@ pub fn file_structure(file: &ast::File) -> Vec<StructureNode> {
80} 80}
81 81
82fn structure_node(node: SyntaxNodeRef) -> Option<StructureNode> { 82fn structure_node(node: SyntaxNodeRef) -> Option<StructureNode> {
83 fn decl<'a, N: NameOwner<&'a SyntaxRoot>>(node: N) -> Option<StructureNode> { 83 fn decl<'a, N: NameOwner<RefRoot<'a>>>(node: N) -> Option<StructureNode> {
84 let name = node.name()?; 84 let name = node.name()?;
85 Some(StructureNode { 85 Some(StructureNode {
86 parent: None, 86 parent: None,
diff --git a/crates/libsyntax2/src/algo/visit.rs b/crates/libsyntax2/src/algo/visit.rs
index dc5afa5a9..55eb72f59 100644
--- a/crates/libsyntax2/src/algo/visit.rs
+++ b/crates/libsyntax2/src/algo/visit.rs
@@ -1,5 +1,5 @@
1use std::marker::PhantomData; 1use std::marker::PhantomData;
2use {SyntaxNodeRef, AstNode, SyntaxRoot}; 2use {SyntaxNodeRef, AstNode, RefRoot};
3 3
4 4
5pub fn visitor<'a, T>() -> impl Visitor<'a, Output=T> { 5pub fn visitor<'a, T>() -> impl Visitor<'a, Output=T> {
@@ -10,7 +10,7 @@ pub trait Visitor<'a>: Sized {
10 type Output; 10 type Output;
11 fn accept(self, node: SyntaxNodeRef<'a>) -> Option<Self::Output>; 11 fn accept(self, node: SyntaxNodeRef<'a>) -> Option<Self::Output>;
12 fn visit<N, F>(self, f: F) -> Vis<Self, N, F> 12 fn visit<N, F>(self, f: F) -> Vis<Self, N, F>
13 where N: AstNode<&'a SyntaxRoot>, 13 where N: AstNode<RefRoot<'a>>,
14 F: FnOnce(N) -> Self::Output, 14 F: FnOnce(N) -> Self::Output,
15 { 15 {
16 Vis { inner: self, f, ph: PhantomData } 16 Vis { inner: self, f, ph: PhantomData }
@@ -40,7 +40,7 @@ pub struct Vis<V, N, F> {
40impl<'a, V, N, F> Visitor<'a> for Vis<V, N, F> 40impl<'a, V, N, F> Visitor<'a> for Vis<V, N, F>
41 where 41 where
42 V: Visitor<'a>, 42 V: Visitor<'a>,
43 N: AstNode<&'a SyntaxRoot>, 43 N: AstNode<RefRoot<'a>>,
44 F: FnOnce(N) -> <V as Visitor<'a>>::Output, 44 F: FnOnce(N) -> <V as Visitor<'a>>::Output,
45{ 45{
46 type Output = <V as Visitor<'a>>::Output; 46 type Output = <V as Visitor<'a>>::Output;
diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs
index be3f73f7e..4fc405c8a 100644
--- a/crates/libsyntax2/src/ast/generated.rs
+++ b/crates/libsyntax2/src/ast/generated.rs
@@ -1,13 +1,12 @@
1use std::sync::Arc;
2use { 1use {
3 ast, 2 ast,
4 SyntaxNode, SyntaxRoot, TreeRoot, AstNode, 3 SyntaxNode, OwnedRoot, TreeRoot, AstNode,
5 SyntaxKind::*, 4 SyntaxKind::*,
6}; 5};
7 6
8// ArrayType 7// ArrayType
9#[derive(Debug, Clone, Copy)] 8#[derive(Debug, Clone, Copy)]
10pub struct ArrayType<R: TreeRoot = Arc<SyntaxRoot>> { 9pub struct ArrayType<R: TreeRoot = OwnedRoot> {
11 syntax: SyntaxNode<R>, 10 syntax: SyntaxNode<R>,
12} 11}
13 12
@@ -25,7 +24,7 @@ impl<R: TreeRoot> ArrayType<R> {}
25 24
26// Attr 25// Attr
27#[derive(Debug, Clone, Copy)] 26#[derive(Debug, Clone, Copy)]
28pub struct Attr<R: TreeRoot = Arc<SyntaxRoot>> { 27pub struct Attr<R: TreeRoot = OwnedRoot> {
29 syntax: SyntaxNode<R>, 28 syntax: SyntaxNode<R>,
30} 29}
31 30
@@ -50,7 +49,7 @@ impl<R: TreeRoot> Attr<R> {
50 49
51// ConstDef 50// ConstDef
52#[derive(Debug, Clone, Copy)] 51#[derive(Debug, Clone, Copy)]
53pub struct ConstDef<R: TreeRoot = Arc<SyntaxRoot>> { 52pub struct ConstDef<R: TreeRoot = OwnedRoot> {
54 syntax: SyntaxNode<R>, 53 syntax: SyntaxNode<R>,
55} 54}
56 55
@@ -70,7 +69,7 @@ impl<R: TreeRoot> ConstDef<R> {}
70 69
71// DynTraitType 70// DynTraitType
72#[derive(Debug, Clone, Copy)] 71#[derive(Debug, Clone, Copy)]
73pub struct DynTraitType<R: TreeRoot = Arc<SyntaxRoot>> { 72pub struct DynTraitType<R: TreeRoot = OwnedRoot> {
74 syntax: SyntaxNode<R>, 73 syntax: SyntaxNode<R>,
75} 74}
76 75
@@ -88,7 +87,7 @@ impl<R: TreeRoot> DynTraitType<R> {}
88 87
89// EnumDef 88// EnumDef
90#[derive(Debug, Clone, Copy)] 89#[derive(Debug, Clone, Copy)]
91pub struct EnumDef<R: TreeRoot = Arc<SyntaxRoot>> { 90pub struct EnumDef<R: TreeRoot = OwnedRoot> {
92 syntax: SyntaxNode<R>, 91 syntax: SyntaxNode<R>,
93} 92}
94 93
@@ -108,7 +107,7 @@ impl<R: TreeRoot> EnumDef<R> {}
108 107
109// File 108// File
110#[derive(Debug, Clone, Copy)] 109#[derive(Debug, Clone, Copy)]
111pub struct File<R: TreeRoot = Arc<SyntaxRoot>> { 110pub struct File<R: TreeRoot = OwnedRoot> {
112 syntax: SyntaxNode<R>, 111 syntax: SyntaxNode<R>,
113} 112}
114 113
@@ -132,7 +131,7 @@ impl<R: TreeRoot> File<R> {
132 131
133// FnDef 132// FnDef
134#[derive(Debug, Clone, Copy)] 133#[derive(Debug, Clone, Copy)]
135pub struct FnDef<R: TreeRoot = Arc<SyntaxRoot>> { 134pub struct FnDef<R: TreeRoot = OwnedRoot> {
136 syntax: SyntaxNode<R>, 135 syntax: SyntaxNode<R>,
137} 136}
138 137
@@ -152,7 +151,7 @@ impl<R: TreeRoot> FnDef<R> {}
152 151
153// FnPointerType 152// FnPointerType
154#[derive(Debug, Clone, Copy)] 153#[derive(Debug, Clone, Copy)]
155pub struct FnPointerType<R: TreeRoot = Arc<SyntaxRoot>> { 154pub struct FnPointerType<R: TreeRoot = OwnedRoot> {
156 syntax: SyntaxNode<R>, 155 syntax: SyntaxNode<R>,
157} 156}
158 157
@@ -170,7 +169,7 @@ impl<R: TreeRoot> FnPointerType<R> {}
170 169
171// ForType 170// ForType
172#[derive(Debug, Clone, Copy)] 171#[derive(Debug, Clone, Copy)]
173pub struct ForType<R: TreeRoot = Arc<SyntaxRoot>> { 172pub struct ForType<R: TreeRoot = OwnedRoot> {
174 syntax: SyntaxNode<R>, 173 syntax: SyntaxNode<R>,
175} 174}
176 175
@@ -188,7 +187,7 @@ impl<R: TreeRoot> ForType<R> {}
188 187
189// ImplItem 188// ImplItem
190#[derive(Debug, Clone, Copy)] 189#[derive(Debug, Clone, Copy)]
191pub struct ImplItem<R: TreeRoot = Arc<SyntaxRoot>> { 190pub struct ImplItem<R: TreeRoot = OwnedRoot> {
192 syntax: SyntaxNode<R>, 191 syntax: SyntaxNode<R>,
193} 192}
194 193
@@ -206,7 +205,7 @@ impl<R: TreeRoot> ImplItem<R> {}
206 205
207// ImplTraitType 206// ImplTraitType
208#[derive(Debug, Clone, Copy)] 207#[derive(Debug, Clone, Copy)]
209pub struct ImplTraitType<R: TreeRoot = Arc<SyntaxRoot>> { 208pub struct ImplTraitType<R: TreeRoot = OwnedRoot> {
210 syntax: SyntaxNode<R>, 209 syntax: SyntaxNode<R>,
211} 210}
212 211
@@ -224,7 +223,7 @@ impl<R: TreeRoot> ImplTraitType<R> {}
224 223
225// Module 224// Module
226#[derive(Debug, Clone, Copy)] 225#[derive(Debug, Clone, Copy)]
227pub struct Module<R: TreeRoot = Arc<SyntaxRoot>> { 226pub struct Module<R: TreeRoot = OwnedRoot> {
228 syntax: SyntaxNode<R>, 227 syntax: SyntaxNode<R>,
229} 228}
230 229
@@ -244,7 +243,7 @@ impl<R: TreeRoot> Module<R> {}
244 243
245// Name 244// Name
246#[derive(Debug, Clone, Copy)] 245#[derive(Debug, Clone, Copy)]
247pub struct Name<R: TreeRoot = Arc<SyntaxRoot>> { 246pub struct Name<R: TreeRoot = OwnedRoot> {
248 syntax: SyntaxNode<R>, 247 syntax: SyntaxNode<R>,
249} 248}
250 249
@@ -262,7 +261,7 @@ impl<R: TreeRoot> Name<R> {}
262 261
263// NameRef 262// NameRef
264#[derive(Debug, Clone, Copy)] 263#[derive(Debug, Clone, Copy)]
265pub struct NameRef<R: TreeRoot = Arc<SyntaxRoot>> { 264pub struct NameRef<R: TreeRoot = OwnedRoot> {
266 syntax: SyntaxNode<R>, 265 syntax: SyntaxNode<R>,
267} 266}
268 267
@@ -280,7 +279,7 @@ impl<R: TreeRoot> NameRef<R> {}
280 279
281// NamedField 280// NamedField
282#[derive(Debug, Clone, Copy)] 281#[derive(Debug, Clone, Copy)]
283pub struct NamedField<R: TreeRoot = Arc<SyntaxRoot>> { 282pub struct NamedField<R: TreeRoot = OwnedRoot> {
284 syntax: SyntaxNode<R>, 283 syntax: SyntaxNode<R>,
285} 284}
286 285
@@ -300,7 +299,7 @@ impl<R: TreeRoot> NamedField<R> {}
300 299
301// NeverType 300// NeverType
302#[derive(Debug, Clone, Copy)] 301#[derive(Debug, Clone, Copy)]
303pub struct NeverType<R: TreeRoot = Arc<SyntaxRoot>> { 302pub struct NeverType<R: TreeRoot = OwnedRoot> {
304 syntax: SyntaxNode<R>, 303 syntax: SyntaxNode<R>,
305} 304}
306 305
@@ -318,7 +317,7 @@ impl<R: TreeRoot> NeverType<R> {}
318 317
319// NominalDef 318// NominalDef
320#[derive(Debug, Clone, Copy)] 319#[derive(Debug, Clone, Copy)]
321pub enum NominalDef<R: TreeRoot = Arc<SyntaxRoot>> { 320pub enum NominalDef<R: TreeRoot = OwnedRoot> {
322 StructDef(StructDef<R>), 321 StructDef(StructDef<R>),
323 EnumDef(EnumDef<R>), 322 EnumDef(EnumDef<R>),
324} 323}
@@ -344,7 +343,7 @@ impl<R: TreeRoot> NominalDef<R> {}
344 343
345// ParenType 344// ParenType
346#[derive(Debug, Clone, Copy)] 345#[derive(Debug, Clone, Copy)]
347pub struct ParenType<R: TreeRoot = Arc<SyntaxRoot>> { 346pub struct ParenType<R: TreeRoot = OwnedRoot> {
348 syntax: SyntaxNode<R>, 347 syntax: SyntaxNode<R>,
349} 348}
350 349
@@ -362,7 +361,7 @@ impl<R: TreeRoot> ParenType<R> {}
362 361
363// PathType 362// PathType
364#[derive(Debug, Clone, Copy)] 363#[derive(Debug, Clone, Copy)]
365pub struct PathType<R: TreeRoot = Arc<SyntaxRoot>> { 364pub struct PathType<R: TreeRoot = OwnedRoot> {
366 syntax: SyntaxNode<R>, 365 syntax: SyntaxNode<R>,
367} 366}
368 367
@@ -380,7 +379,7 @@ impl<R: TreeRoot> PathType<R> {}
380 379
381// PlaceholderType 380// PlaceholderType
382#[derive(Debug, Clone, Copy)] 381#[derive(Debug, Clone, Copy)]
383pub struct PlaceholderType<R: TreeRoot = Arc<SyntaxRoot>> { 382pub struct PlaceholderType<R: TreeRoot = OwnedRoot> {
384 syntax: SyntaxNode<R>, 383 syntax: SyntaxNode<R>,
385} 384}
386 385
@@ -398,7 +397,7 @@ impl<R: TreeRoot> PlaceholderType<R> {}
398 397
399// PointerType 398// PointerType
400#[derive(Debug, Clone, Copy)] 399#[derive(Debug, Clone, Copy)]
401pub struct PointerType<R: TreeRoot = Arc<SyntaxRoot>> { 400pub struct PointerType<R: TreeRoot = OwnedRoot> {
402 syntax: SyntaxNode<R>, 401 syntax: SyntaxNode<R>,
403} 402}
404 403
@@ -416,7 +415,7 @@ impl<R: TreeRoot> PointerType<R> {}
416 415
417// ReferenceType 416// ReferenceType
418#[derive(Debug, Clone, Copy)] 417#[derive(Debug, Clone, Copy)]
419pub struct ReferenceType<R: TreeRoot = Arc<SyntaxRoot>> { 418pub struct ReferenceType<R: TreeRoot = OwnedRoot> {
420 syntax: SyntaxNode<R>, 419 syntax: SyntaxNode<R>,
421} 420}
422 421
@@ -434,7 +433,7 @@ impl<R: TreeRoot> ReferenceType<R> {}
434 433
435// SliceType 434// SliceType
436#[derive(Debug, Clone, Copy)] 435#[derive(Debug, Clone, Copy)]
437pub struct SliceType<R: TreeRoot = Arc<SyntaxRoot>> { 436pub struct SliceType<R: TreeRoot = OwnedRoot> {
438 syntax: SyntaxNode<R>, 437 syntax: SyntaxNode<R>,
439} 438}
440 439
@@ -452,7 +451,7 @@ impl<R: TreeRoot> SliceType<R> {}
452 451
453// StaticDef 452// StaticDef
454#[derive(Debug, Clone, Copy)] 453#[derive(Debug, Clone, Copy)]
455pub struct StaticDef<R: TreeRoot = Arc<SyntaxRoot>> { 454pub struct StaticDef<R: TreeRoot = OwnedRoot> {
456 syntax: SyntaxNode<R>, 455 syntax: SyntaxNode<R>,
457} 456}
458 457
@@ -472,7 +471,7 @@ impl<R: TreeRoot> StaticDef<R> {}
472 471
473// StructDef 472// StructDef
474#[derive(Debug, Clone, Copy)] 473#[derive(Debug, Clone, Copy)]
475pub struct StructDef<R: TreeRoot = Arc<SyntaxRoot>> { 474pub struct StructDef<R: TreeRoot = OwnedRoot> {
476 syntax: SyntaxNode<R>, 475 syntax: SyntaxNode<R>,
477} 476}
478 477
@@ -498,7 +497,7 @@ impl<R: TreeRoot> StructDef<R> {
498 497
499// TokenTree 498// TokenTree
500#[derive(Debug, Clone, Copy)] 499#[derive(Debug, Clone, Copy)]
501pub struct TokenTree<R: TreeRoot = Arc<SyntaxRoot>> { 500pub struct TokenTree<R: TreeRoot = OwnedRoot> {
502 syntax: SyntaxNode<R>, 501 syntax: SyntaxNode<R>,
503} 502}
504 503
@@ -516,7 +515,7 @@ impl<R: TreeRoot> TokenTree<R> {}
516 515
517// TraitDef 516// TraitDef
518#[derive(Debug, Clone, Copy)] 517#[derive(Debug, Clone, Copy)]
519pub struct TraitDef<R: TreeRoot = Arc<SyntaxRoot>> { 518pub struct TraitDef<R: TreeRoot = OwnedRoot> {
520 syntax: SyntaxNode<R>, 519 syntax: SyntaxNode<R>,
521} 520}
522 521
@@ -536,7 +535,7 @@ impl<R: TreeRoot> TraitDef<R> {}
536 535
537// TupleType 536// TupleType
538#[derive(Debug, Clone, Copy)] 537#[derive(Debug, Clone, Copy)]
539pub struct TupleType<R: TreeRoot = Arc<SyntaxRoot>> { 538pub struct TupleType<R: TreeRoot = OwnedRoot> {
540 syntax: SyntaxNode<R>, 539 syntax: SyntaxNode<R>,
541} 540}
542 541
@@ -554,7 +553,7 @@ impl<R: TreeRoot> TupleType<R> {}
554 553
555// TypeDef 554// TypeDef
556#[derive(Debug, Clone, Copy)] 555#[derive(Debug, Clone, Copy)]
557pub struct TypeDef<R: TreeRoot = Arc<SyntaxRoot>> { 556pub struct TypeDef<R: TreeRoot = OwnedRoot> {
558 syntax: SyntaxNode<R>, 557 syntax: SyntaxNode<R>,
559} 558}
560 559
@@ -574,7 +573,7 @@ impl<R: TreeRoot> TypeDef<R> {}
574 573
575// TypeRef 574// TypeRef
576#[derive(Debug, Clone, Copy)] 575#[derive(Debug, Clone, Copy)]
577pub enum TypeRef<R: TreeRoot = Arc<SyntaxRoot>> { 576pub enum TypeRef<R: TreeRoot = OwnedRoot> {
578 ParenType(ParenType<R>), 577 ParenType(ParenType<R>),
579 TupleType(TupleType<R>), 578 TupleType(TupleType<R>),
580 NeverType(NeverType<R>), 579 NeverType(NeverType<R>),
diff --git a/crates/libsyntax2/src/ast/mod.rs b/crates/libsyntax2/src/ast/mod.rs
index 9b9200f99..b52230e9c 100644
--- a/crates/libsyntax2/src/ast/mod.rs
+++ b/crates/libsyntax2/src/ast/mod.rs
@@ -1,12 +1,10 @@
1mod generated; 1mod generated;
2 2
3use std::sync::Arc;
4
5use itertools::Itertools; 3use itertools::Itertools;
6use smol_str::SmolStr; 4use smol_str::SmolStr;
7 5
8use { 6use {
9 SyntaxNode, SyntaxNodeRef, SyntaxRoot, TreeRoot, SyntaxError, 7 SyntaxNode, SyntaxNodeRef, OwnedRoot, TreeRoot, SyntaxError,
10 SyntaxKind::*, 8 SyntaxKind::*,
11}; 9};
12pub use self::generated::*; 10pub use self::generated::*;
@@ -37,7 +35,7 @@ pub trait AttrsOwner<R: TreeRoot>: AstNode<R> {
37 } 35 }
38} 36}
39 37
40impl File<Arc<SyntaxRoot>> { 38impl File<OwnedRoot> {
41 pub fn parse(text: &str) -> Self { 39 pub fn parse(text: &str) -> Self {
42 File::cast(::parse(text)).unwrap() 40 File::cast(::parse(text)).unwrap()
43 } 41 }
@@ -45,7 +43,7 @@ impl File<Arc<SyntaxRoot>> {
45 43
46impl<R: TreeRoot> File<R> { 44impl<R: TreeRoot> File<R> {
47 pub fn errors(&self) -> Vec<SyntaxError> { 45 pub fn errors(&self) -> Vec<SyntaxError> {
48 self.syntax().root.errors.clone() 46 self.syntax().root.syntax_root().errors.clone()
49 } 47 }
50} 48}
51 49
diff --git a/crates/libsyntax2/src/lib.rs b/crates/libsyntax2/src/lib.rs
index feef542c4..0e40fb65f 100644
--- a/crates/libsyntax2/src/lib.rs
+++ b/crates/libsyntax2/src/lib.rs
@@ -45,7 +45,7 @@ pub use {
45 lexer::{tokenize, Token}, 45 lexer::{tokenize, Token},
46 syntax_kinds::SyntaxKind, 46 syntax_kinds::SyntaxKind,
47 text_unit::{TextRange, TextUnit}, 47 text_unit::{TextRange, TextUnit},
48 yellow::{SyntaxNode, SyntaxNodeRef, SyntaxRoot, TreeRoot, SyntaxError}, 48 yellow::{SyntaxNode, SyntaxNodeRef, OwnedRoot, RefRoot, TreeRoot, SyntaxError},
49}; 49};
50 50
51 51
diff --git a/crates/libsyntax2/src/utils.rs b/crates/libsyntax2/src/utils.rs
index 1fbb872a5..4f60aa2a8 100644
--- a/crates/libsyntax2/src/utils.rs
+++ b/crates/libsyntax2/src/utils.rs
@@ -1,13 +1,13 @@
1use std::fmt::Write; 1use std::fmt::Write;
2use { 2use {
3 algo::walk::{walk, WalkEvent}, 3 algo::walk::{walk, WalkEvent},
4 SyntaxNode, 4 SyntaxNode, TreeRoot,
5}; 5};
6 6
7/// Parse a file and create a string representation of the resulting parse tree. 7/// Parse a file and create a string representation of the resulting parse tree.
8pub fn dump_tree(syntax: &SyntaxNode) -> String { 8pub fn dump_tree(syntax: &SyntaxNode) -> String {
9 let syntax = syntax.as_ref(); 9 let syntax = syntax.as_ref();
10 let mut errors: Vec<_> = syntax.root.errors.iter().cloned().collect(); 10 let mut errors: Vec<_> = syntax.root.syntax_root().errors.iter().cloned().collect();
11 errors.sort_by_key(|e| e.offset); 11 errors.sort_by_key(|e| e.offset);
12 let mut err_pos = 0; 12 let mut err_pos = 0;
13 let mut level = 0; 13 let mut level = 0;
diff --git a/crates/libsyntax2/src/yellow/mod.rs b/crates/libsyntax2/src/yellow/mod.rs
index ff3bb221b..3c4510fe7 100644
--- a/crates/libsyntax2/src/yellow/mod.rs
+++ b/crates/libsyntax2/src/yellow/mod.rs
@@ -4,7 +4,6 @@ mod red;
4mod syntax; 4mod syntax;
5 5
6use std::{ 6use std::{
7 ops::Deref,
8 sync::Arc, 7 sync::Arc,
9 ptr, 8 ptr,
10}; 9};
@@ -15,17 +14,48 @@ pub(crate) use self::{
15 red::RedNode, 14 red::RedNode,
16}; 15};
17 16
18pub trait TreeRoot: Deref<Target=SyntaxRoot> + Clone + Send + Sync {}
19
20#[derive(Debug)] 17#[derive(Debug)]
21pub struct SyntaxRoot { 18pub struct SyntaxRoot {
22 red: RedNode, 19 red: RedNode,
23 pub(crate) errors: Vec<SyntaxError>, 20 pub(crate) errors: Vec<SyntaxError>,
24} 21}
25 22
26impl TreeRoot for Arc<SyntaxRoot> {} 23pub trait TreeRoot: Clone + Send + Sync {
24 fn borrowed(&self) -> RefRoot;
25 fn owned(&self) -> OwnedRoot;
26
27 #[doc(hidden)]
28 fn syntax_root(&self) -> &SyntaxRoot;
29}
30#[derive(Clone, Debug)]
31pub struct OwnedRoot(Arc<SyntaxRoot>);
32#[derive(Clone, Copy, Debug)]
33pub struct RefRoot<'a>(&'a OwnedRoot); // TODO: shared_from_this instead of double indirection
27 34
28impl<'a> TreeRoot for &'a SyntaxRoot {} 35impl TreeRoot for OwnedRoot {
36 fn borrowed(&self) -> RefRoot {
37 RefRoot(&self)
38 }
39 fn owned(&self) -> OwnedRoot {
40 self.clone()
41 }
42
43 fn syntax_root(&self) -> &SyntaxRoot {
44 &*self.0
45 }
46}
47
48impl<'a> TreeRoot for RefRoot<'a> {
49 fn borrowed(&self) -> RefRoot {
50 *self
51 }
52 fn owned(&self) -> OwnedRoot {
53 self.0.clone()
54 }
55 fn syntax_root(&self) -> &SyntaxRoot {
56 self.0.syntax_root()
57 }
58}
29 59
30impl SyntaxRoot { 60impl SyntaxRoot {
31 pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SyntaxRoot { 61 pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SyntaxRoot {
diff --git a/crates/libsyntax2/src/yellow/syntax.rs b/crates/libsyntax2/src/yellow/syntax.rs
index bb390751a..ef82ba408 100644
--- a/crates/libsyntax2/src/yellow/syntax.rs
+++ b/crates/libsyntax2/src/yellow/syntax.rs
@@ -3,14 +3,14 @@ use std::{fmt, sync::Arc};
3use smol_str::SmolStr; 3use smol_str::SmolStr;
4 4
5use { 5use {
6 yellow::{RedNode, TreeRoot, SyntaxRoot, RedPtr}, 6 yellow::{RedNode, TreeRoot, SyntaxRoot, RedPtr, RefRoot, OwnedRoot},
7 SyntaxKind::{self, *}, 7 SyntaxKind::{self, *},
8 TextRange, TextUnit, 8 TextRange, TextUnit,
9}; 9};
10 10
11 11
12#[derive(Clone, Copy)] 12#[derive(Clone, Copy)]
13pub struct SyntaxNode<R: TreeRoot = Arc<SyntaxRoot>> { 13pub struct SyntaxNode<R: TreeRoot = OwnedRoot> {
14 pub(crate) root: R, 14 pub(crate) root: R,
15 // Guaranteed to not dangle, because `root` holds a 15 // Guaranteed to not dangle, because `root` holds a
16 // strong reference to red's ancestor 16 // strong reference to red's ancestor
@@ -28,7 +28,7 @@ impl<R1: TreeRoot, R2: TreeRoot> PartialEq<SyntaxNode<R1>> for SyntaxNode<R2> {
28 28
29impl<R: TreeRoot> Eq for SyntaxNode<R> {} 29impl<R: TreeRoot> Eq for SyntaxNode<R> {}
30 30
31pub type SyntaxNodeRef<'a> = SyntaxNode<&'a SyntaxRoot>; 31pub type SyntaxNodeRef<'a> = SyntaxNode<RefRoot<'a>>;
32 32
33#[test] 33#[test]
34fn syntax_node_ref_is_copy() { 34fn syntax_node_ref_is_copy() {
@@ -42,18 +42,18 @@ pub struct SyntaxError {
42 pub offset: TextUnit, 42 pub offset: TextUnit,
43} 43}
44 44
45impl SyntaxNode<Arc<SyntaxRoot>> { 45impl SyntaxNode<OwnedRoot> {
46 pub(crate) fn new_owned(root: SyntaxRoot) -> Self { 46 pub(crate) fn new_owned(root: SyntaxRoot) -> Self {
47 let root = Arc::new(root); 47 let root = OwnedRoot(Arc::new(root));
48 let red = RedPtr::new(&root.red); 48 let red = RedPtr::new(&root.syntax_root().red);
49 SyntaxNode { root, red } 49 SyntaxNode { root, red }
50 } 50 }
51} 51}
52 52
53impl<R: TreeRoot> SyntaxNode<R> { 53impl<R: TreeRoot> SyntaxNode<R> {
54 pub fn as_ref<'a>(&'a self) -> SyntaxNode<&'a SyntaxRoot> { 54 pub fn as_ref<'a>(&'a self) -> SyntaxNode<RefRoot<'a>> {
55 SyntaxNode { 55 SyntaxNode {
56 root: &*self.root, 56 root: self.root.borrowed(),
57 red: self.red, 57 red: self.red,
58 } 58 }
59 } 59 }