diff options
Diffstat (limited to 'crates/ra_syntax/src/syntax_node.rs')
-rw-r--r-- | crates/ra_syntax/src/syntax_node.rs | 48 |
1 files changed, 46 insertions, 2 deletions
diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs index a1bc0b499..1ca1c992b 100644 --- a/crates/ra_syntax/src/syntax_node.rs +++ b/crates/ra_syntax/src/syntax_node.rs | |||
@@ -6,12 +6,12 @@ | |||
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 | ||
9 | use std::{fmt, borrow::Borrow}; | 9 | use std::{fmt::{self, Write}, borrow::Borrow}; |
10 | 10 | ||
11 | use rowan::{Types, TransparentNewType}; | 11 | use rowan::{Types, TransparentNewType}; |
12 | 12 | ||
13 | use crate::{ | 13 | use crate::{ |
14 | SmolStr, SyntaxKind, TextRange, SyntaxText, | 14 | SmolStr, SyntaxKind, TextRange, SyntaxText, SourceFile, AstNode, |
15 | syntax_error::SyntaxError, | 15 | syntax_error::SyntaxError, |
16 | }; | 16 | }; |
17 | 17 | ||
@@ -134,6 +134,50 @@ impl SyntaxNode { | |||
134 | WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxNode::from_repr(n)), | 134 | WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxNode::from_repr(n)), |
135 | }) | 135 | }) |
136 | } | 136 | } |
137 | |||
138 | pub fn debug_dump(&self) -> String { | ||
139 | let mut errors: Vec<_> = match self.ancestors().find_map(SourceFile::cast) { | ||
140 | Some(file) => file.errors(), | ||
141 | None => self.root_data().to_vec(), | ||
142 | }; | ||
143 | errors.sort_by_key(|e| e.offset()); | ||
144 | let mut err_pos = 0; | ||
145 | let mut level = 0; | ||
146 | let mut buf = String::new(); | ||
147 | macro_rules! indent { | ||
148 | () => { | ||
149 | for _ in 0..level { | ||
150 | buf.push_str(" "); | ||
151 | } | ||
152 | }; | ||
153 | } | ||
154 | |||
155 | for event in self.preorder() { | ||
156 | match event { | ||
157 | WalkEvent::Enter(node) => { | ||
158 | indent!(); | ||
159 | writeln!(buf, "{:?}", node).unwrap(); | ||
160 | if node.first_child().is_none() { | ||
161 | let off = node.range().end(); | ||
162 | while err_pos < errors.len() && errors[err_pos].offset() <= off { | ||
163 | indent!(); | ||
164 | writeln!(buf, "err: `{}`", errors[err_pos]).unwrap(); | ||
165 | err_pos += 1; | ||
166 | } | ||
167 | } | ||
168 | level += 1; | ||
169 | } | ||
170 | WalkEvent::Leave(_) => level -= 1, | ||
171 | } | ||
172 | } | ||
173 | |||
174 | assert_eq!(level, 0); | ||
175 | for err in errors[err_pos..].iter() { | ||
176 | writeln!(buf, "err: `{}`", err).unwrap(); | ||
177 | } | ||
178 | |||
179 | buf | ||
180 | } | ||
137 | } | 181 | } |
138 | 182 | ||
139 | impl ToOwned for SyntaxNode { | 183 | impl ToOwned for SyntaxNode { |