aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/syntax_node.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/syntax_node.rs')
-rw-r--r--crates/ra_syntax/src/syntax_node.rs62
1 files changed, 30 insertions, 32 deletions
diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs
index a88a348ad..64d884287 100644
--- a/crates/ra_syntax/src/syntax_node.rs
+++ b/crates/ra_syntax/src/syntax_node.rs
@@ -8,11 +8,12 @@
8 8
9use std::{ 9use std::{
10 fmt::{self, Write}, 10 fmt::{self, Write},
11 any::Any,
11 borrow::Borrow, 12 borrow::Borrow,
12}; 13};
13 14
14use ra_parser::ParseError; 15use ra_parser::ParseError;
15use rowan::{Types, TransparentNewType, GreenNodeBuilder}; 16use rowan::{TransparentNewType, GreenNodeBuilder};
16 17
17use crate::{ 18use crate::{
18 SmolStr, SyntaxKind, TextUnit, TextRange, SyntaxText, SourceFile, AstNode, 19 SmolStr, SyntaxKind, TextUnit, TextRange, SyntaxText, SourceFile, AstNode,
@@ -20,26 +21,15 @@ use crate::{
20}; 21};
21 22
22pub use rowan::WalkEvent; 23pub use rowan::WalkEvent;
23 24pub(crate) use rowan::{GreenNode, GreenToken};
24#[derive(Debug, Clone, Copy)]
25pub enum RaTypes {}
26impl Types for RaTypes {
27 type Kind = SyntaxKind;
28 type RootData = Vec<SyntaxError>;
29}
30
31pub(crate) type GreenNode = rowan::GreenNode<RaTypes>;
32pub(crate) type GreenToken = rowan::GreenToken<RaTypes>;
33#[allow(unused)]
34pub(crate) type GreenElement = rowan::GreenElement<RaTypes>;
35 25
36/// Marker trait for CST and AST nodes 26/// Marker trait for CST and AST nodes
37pub trait SyntaxNodeWrapper: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>> {} 27pub trait SyntaxNodeWrapper: TransparentNewType<Repr = rowan::SyntaxNode> {}
38impl<T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>> SyntaxNodeWrapper for T {} 28impl<T: TransparentNewType<Repr = rowan::SyntaxNode>> SyntaxNodeWrapper for T {}
39 29
40/// An owning smart pointer for CST or AST node. 30/// An owning smart pointer for CST or AST node.
41#[derive(PartialEq, Eq, Hash)] 31#[derive(PartialEq, Eq, Hash)]
42pub struct TreeArc<T: SyntaxNodeWrapper>(pub(crate) rowan::TreeArc<RaTypes, T>); 32pub struct TreeArc<T: SyntaxNodeWrapper>(pub(crate) rowan::TreeArc<T>);
43 33
44impl<T: SyntaxNodeWrapper> Borrow<T> for TreeArc<T> { 34impl<T: SyntaxNodeWrapper> Borrow<T> for TreeArc<T> {
45 fn borrow(&self) -> &T { 35 fn borrow(&self) -> &T {
@@ -101,9 +91,9 @@ where
101 91
102#[derive(PartialEq, Eq, Hash)] 92#[derive(PartialEq, Eq, Hash)]
103#[repr(transparent)] 93#[repr(transparent)]
104pub struct SyntaxNode(pub(crate) rowan::SyntaxNode<RaTypes>); 94pub struct SyntaxNode(pub(crate) rowan::SyntaxNode);
105unsafe impl TransparentNewType for SyntaxNode { 95unsafe impl TransparentNewType for SyntaxNode {
106 type Repr = rowan::SyntaxNode<RaTypes>; 96 type Repr = rowan::SyntaxNode;
107} 97}
108 98
109impl ToOwned for SyntaxNode { 99impl ToOwned for SyntaxNode {
@@ -134,12 +124,14 @@ pub enum Direction {
134 124
135impl SyntaxNode { 125impl SyntaxNode {
136 pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> TreeArc<SyntaxNode> { 126 pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> TreeArc<SyntaxNode> {
127 let errors: Option<Box<Any + Send + Sync>> =
128 if errors.is_empty() { None } else { Some(Box::new(errors)) };
137 let ptr = TreeArc(rowan::SyntaxNode::new(green, errors)); 129 let ptr = TreeArc(rowan::SyntaxNode::new(green, errors));
138 TreeArc::cast(ptr) 130 TreeArc::cast(ptr)
139 } 131 }
140 132
141 pub fn kind(&self) -> SyntaxKind { 133 pub fn kind(&self) -> SyntaxKind {
142 self.0.kind() 134 self.0.kind().0.into()
143 } 135 }
144 136
145 pub fn range(&self) -> TextRange { 137 pub fn range(&self) -> TextRange {
@@ -303,8 +295,14 @@ impl SyntaxNode {
303 buf 295 buf
304 } 296 }
305 297
306 pub(crate) fn root_data(&self) -> &Vec<SyntaxError> { 298 pub(crate) fn root_data(&self) -> &[SyntaxError] {
307 self.0.root_data() 299 match self.0.root_data() {
300 None => &[],
301 Some(data) => {
302 let data: &Vec<SyntaxError> = std::any::Any::downcast_ref(data).unwrap();
303 data.as_slice()
304 }
305 }
308 } 306 }
309 307
310 pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode { 308 pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode {
@@ -313,7 +311,7 @@ impl SyntaxNode {
313} 311}
314 312
315#[derive(Clone, Copy, PartialEq, Eq, Hash)] 313#[derive(Clone, Copy, PartialEq, Eq, Hash)]
316pub struct SyntaxToken<'a>(pub(crate) rowan::SyntaxToken<'a, RaTypes>); 314pub struct SyntaxToken<'a>(pub(crate) rowan::SyntaxToken<'a>);
317 315
318//FIXME: always output text 316//FIXME: always output text
319impl<'a> fmt::Debug for SyntaxToken<'a> { 317impl<'a> fmt::Debug for SyntaxToken<'a> {
@@ -339,15 +337,15 @@ impl<'a> fmt::Display for SyntaxToken<'a> {
339 } 337 }
340} 338}
341 339
342impl<'a> From<rowan::SyntaxToken<'a, RaTypes>> for SyntaxToken<'a> { 340impl<'a> From<rowan::SyntaxToken<'a>> for SyntaxToken<'a> {
343 fn from(t: rowan::SyntaxToken<'a, RaTypes>) -> Self { 341 fn from(t: rowan::SyntaxToken<'a>) -> Self {
344 SyntaxToken(t) 342 SyntaxToken(t)
345 } 343 }
346} 344}
347 345
348impl<'a> SyntaxToken<'a> { 346impl<'a> SyntaxToken<'a> {
349 pub fn kind(&self) -> SyntaxKind { 347 pub fn kind(&self) -> SyntaxKind {
350 self.0.kind() 348 self.0.kind().0.into()
351 } 349 }
352 350
353 pub fn text(&self) -> &'a SmolStr { 351 pub fn text(&self) -> &'a SmolStr {
@@ -454,8 +452,8 @@ impl<'a> SyntaxElement<'a> {
454 } 452 }
455} 453}
456 454
457impl<'a> From<rowan::SyntaxElement<'a, RaTypes>> for SyntaxElement<'a> { 455impl<'a> From<rowan::SyntaxElement<'a>> for SyntaxElement<'a> {
458 fn from(el: rowan::SyntaxElement<'a, RaTypes>) -> Self { 456 fn from(el: rowan::SyntaxElement<'a>) -> Self {
459 match el { 457 match el {
460 rowan::SyntaxElement::Node(n) => SyntaxElement::Node(SyntaxNode::from_repr(n)), 458 rowan::SyntaxElement::Node(n) => SyntaxElement::Node(SyntaxNode::from_repr(n)),
461 rowan::SyntaxElement::Token(t) => SyntaxElement::Token(t.into()), 459 rowan::SyntaxElement::Token(t) => SyntaxElement::Token(t.into()),
@@ -485,7 +483,7 @@ impl<'a> SyntaxElement<'a> {
485} 483}
486 484
487#[derive(Debug)] 485#[derive(Debug)]
488pub struct SyntaxNodeChildren<'a>(rowan::SyntaxNodeChildren<'a, RaTypes>); 486pub struct SyntaxNodeChildren<'a>(rowan::SyntaxNodeChildren<'a>);
489 487
490impl<'a> Iterator for SyntaxNodeChildren<'a> { 488impl<'a> Iterator for SyntaxNodeChildren<'a> {
491 type Item = &'a SyntaxNode; 489 type Item = &'a SyntaxNode;
@@ -496,7 +494,7 @@ impl<'a> Iterator for SyntaxNodeChildren<'a> {
496} 494}
497 495
498#[derive(Debug)] 496#[derive(Debug)]
499pub struct SyntaxElementChildren<'a>(rowan::SyntaxElementChildren<'a, RaTypes>); 497pub struct SyntaxElementChildren<'a>(rowan::SyntaxElementChildren<'a>);
500 498
501impl<'a> Iterator for SyntaxElementChildren<'a> { 499impl<'a> Iterator for SyntaxElementChildren<'a> {
502 type Item = SyntaxElement<'a>; 500 type Item = SyntaxElement<'a>;
@@ -508,7 +506,7 @@ impl<'a> Iterator for SyntaxElementChildren<'a> {
508 506
509pub struct SyntaxTreeBuilder { 507pub struct SyntaxTreeBuilder {
510 errors: Vec<SyntaxError>, 508 errors: Vec<SyntaxError>,
511 inner: GreenNodeBuilder<RaTypes>, 509 inner: GreenNodeBuilder,
512} 510}
513 511
514impl Default for SyntaxTreeBuilder { 512impl Default for SyntaxTreeBuilder {
@@ -533,11 +531,11 @@ impl SyntaxTreeBuilder {
533 } 531 }
534 532
535 pub fn token(&mut self, kind: SyntaxKind, text: SmolStr) { 533 pub fn token(&mut self, kind: SyntaxKind, text: SmolStr) {
536 self.inner.token(kind, text) 534 self.inner.token(rowan::SyntaxKind(kind.into()), text)
537 } 535 }
538 536
539 pub fn start_node(&mut self, kind: SyntaxKind) { 537 pub fn start_node(&mut self, kind: SyntaxKind) {
540 self.inner.start_node(kind) 538 self.inner.start_node(rowan::SyntaxKind(kind.into()))
541 } 539 }
542 540
543 pub fn finish_node(&mut self) { 541 pub fn finish_node(&mut self) {