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.rs68
1 files changed, 30 insertions, 38 deletions
diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs
index 51bae04de..c42045d77 100644
--- a/crates/ra_syntax/src/syntax_node.rs
+++ b/crates/ra_syntax/src/syntax_node.rs
@@ -6,11 +6,7 @@
6//! The *real* implementation is in the (language-agnostic) `rowan` crate, this 6//! The *real* implementation is in the (language-agnostic) `rowan` crate, this
7//! modules just wraps its API. 7//! modules just wraps its API.
8 8
9use std::{ 9use std::{fmt, iter::successors, ops::RangeInclusive};
10 fmt::{self, Write},
11 iter::successors,
12 ops::RangeInclusive,
13};
14 10
15use ra_parser::ParseError; 11use ra_parser::ParseError;
16use rowan::GreenNodeBuilder; 12use rowan::GreenNodeBuilder;
@@ -36,8 +32,29 @@ pub enum InsertPosition<T> {
36pub struct SyntaxNode(pub(crate) rowan::cursor::SyntaxNode); 32pub struct SyntaxNode(pub(crate) rowan::cursor::SyntaxNode);
37 33
38impl fmt::Debug for SyntaxNode { 34impl fmt::Debug for SyntaxNode {
39 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { 35 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40 write!(fmt, "{:?}@{:?}", self.kind(), self.range()) 36 if f.alternate() {
37 let mut level = 0;
38 for event in self.preorder_with_tokens() {
39 match event {
40 WalkEvent::Enter(element) => {
41 for _ in 0..level {
42 write!(f, " ")?;
43 }
44 match element {
45 SyntaxElement::Node(node) => writeln!(f, "{:?}", node)?,
46 SyntaxElement::Token(token) => writeln!(f, "{:?}", token)?,
47 }
48 level += 1;
49 }
50 WalkEvent::Leave(_) => level -= 1,
51 }
52 }
53 assert_eq!(level, 0);
54 Ok(())
55 } else {
56 write!(f, "{:?}@{:?}", self.kind(), self.text_range())
57 }
41 } 58 }
42} 59}
43 60
@@ -63,7 +80,7 @@ impl SyntaxNode {
63 self.0.kind().0.into() 80 self.0.kind().0.into()
64 } 81 }
65 82
66 pub fn range(&self) -> TextRange { 83 pub fn text_range(&self) -> TextRange {
67 self.0.text_range() 84 self.0.text_range()
68 } 85 }
69 86
@@ -173,31 +190,6 @@ impl SyntaxNode {
173 }) 190 })
174 } 191 }
175 192
176 pub fn debug_dump(&self) -> String {
177 let mut level = 0;
178 let mut buf = String::new();
179
180 for event in self.preorder_with_tokens() {
181 match event {
182 WalkEvent::Enter(element) => {
183 for _ in 0..level {
184 buf.push_str(" ");
185 }
186 match element {
187 SyntaxElement::Node(node) => writeln!(buf, "{:?}", node).unwrap(),
188 SyntaxElement::Token(token) => writeln!(buf, "{:?}", token).unwrap(),
189 }
190 level += 1;
191 }
192 WalkEvent::Leave(_) => level -= 1,
193 }
194 }
195
196 assert_eq!(level, 0);
197
198 buf
199 }
200
201 pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode { 193 pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode {
202 self.0.replace_with(replacement) 194 self.0.replace_with(replacement)
203 } 195 }
@@ -299,7 +291,7 @@ pub struct SyntaxToken(pub(crate) rowan::cursor::SyntaxToken);
299 291
300impl fmt::Debug for SyntaxToken { 292impl fmt::Debug for SyntaxToken {
301 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { 293 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
302 write!(fmt, "{:?}@{:?}", self.kind(), self.range())?; 294 write!(fmt, "{:?}@{:?}", self.kind(), self.text_range())?;
303 if self.text().len() < 25 { 295 if self.text().len() < 25 {
304 return write!(fmt, " {:?}", self.text()); 296 return write!(fmt, " {:?}", self.text());
305 } 297 }
@@ -329,7 +321,7 @@ impl SyntaxToken {
329 self.0.text() 321 self.0.text()
330 } 322 }
331 323
332 pub fn range(&self) -> TextRange { 324 pub fn text_range(&self) -> TextRange {
333 self.0.text_range() 325 self.0.text_range()
334 } 326 }
335 327
@@ -461,10 +453,10 @@ impl SyntaxElement {
461 .ancestors() 453 .ancestors()
462 } 454 }
463 455
464 pub fn range(&self) -> TextRange { 456 pub fn text_range(&self) -> TextRange {
465 match self { 457 match self {
466 SyntaxElement::Node(it) => it.range(), 458 SyntaxElement::Node(it) => it.text_range(),
467 SyntaxElement::Token(it) => it.range(), 459 SyntaxElement::Token(it) => it.text_range(),
468 } 460 }
469 } 461 }
470 462