aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r--crates/ra_syntax/src/algo.rs2
-rw-r--r--crates/ra_syntax/src/fuzz.rs8
-rw-r--r--crates/ra_syntax/src/lib.rs4
-rw-r--r--crates/ra_syntax/src/parsing/reparsing.rs16
-rw-r--r--crates/ra_syntax/src/ptr.rs6
-rw-r--r--crates/ra_syntax/src/syntax_node.rs68
-rw-r--r--crates/ra_syntax/src/syntax_text.rs8
-rw-r--r--crates/ra_syntax/src/validation.rs12
-rw-r--r--crates/ra_syntax/src/validation/block.rs5
-rw-r--r--crates/ra_syntax/src/validation/field_expr.rs2
10 files changed, 63 insertions, 68 deletions
diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs
index e2de5e0e3..f47e11e66 100644
--- a/crates/ra_syntax/src/algo.rs
+++ b/crates/ra_syntax/src/algo.rs
@@ -25,7 +25,7 @@ pub fn ancestors_at_offset(
25) -> impl Iterator<Item = SyntaxNode> { 25) -> impl Iterator<Item = SyntaxNode> {
26 find_token_at_offset(node, offset) 26 find_token_at_offset(node, offset)
27 .map(|token| token.parent().ancestors()) 27 .map(|token| token.parent().ancestors())
28 .kmerge_by(|node1, node2| node1.range().len() < node2.range().len()) 28 .kmerge_by(|node1, node2| node1.text_range().len() < node2.text_range().len())
29} 29}
30 30
31/// Finds a node of specific Ast type at offset. Note that this is slightly 31/// Finds a node of specific Ast type at offset. Note that this is slightly
diff --git a/crates/ra_syntax/src/fuzz.rs b/crates/ra_syntax/src/fuzz.rs
index 716925b2f..698a624ec 100644
--- a/crates/ra_syntax/src/fuzz.rs
+++ b/crates/ra_syntax/src/fuzz.rs
@@ -51,10 +51,10 @@ impl CheckReparse {
51 for (a, b) in 51 for (a, b) in
52 new_parse.tree().syntax().descendants().zip(full_reparse.tree().syntax().descendants()) 52 new_parse.tree().syntax().descendants().zip(full_reparse.tree().syntax().descendants())
53 { 53 {
54 if (a.kind(), a.range()) != (b.kind(), b.range()) { 54 if (a.kind(), a.text_range()) != (b.kind(), b.text_range()) {
55 eprint!("original:\n{}", parse.tree().syntax().debug_dump()); 55 eprint!("original:\n{:#?}", parse.tree().syntax());
56 eprint!("reparsed:\n{}", new_parse.tree().syntax().debug_dump()); 56 eprint!("reparsed:\n{:#?}", new_parse.tree().syntax());
57 eprint!("full reparse:\n{}", full_reparse.tree().syntax().debug_dump()); 57 eprint!("full reparse:\n{:#?}", full_reparse.tree().syntax());
58 assert_eq!( 58 assert_eq!(
59 format!("{:?}", a), 59 format!("{:?}", a),
60 format!("{:?}", b), 60 format!("{:?}", b),
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs
index 604abe5c6..8af04c136 100644
--- a/crates/ra_syntax/src/lib.rs
+++ b/crates/ra_syntax/src/lib.rs
@@ -114,7 +114,7 @@ impl Parse<SyntaxNode> {
114 114
115impl Parse<SourceFile> { 115impl Parse<SourceFile> {
116 pub fn debug_dump(&self) -> String { 116 pub fn debug_dump(&self) -> String {
117 let mut buf = self.tree().syntax().debug_dump(); 117 let mut buf = format!("{:#?}", self.tree().syntax());
118 for err in self.errors.iter() { 118 for err in self.errors.iter() {
119 writeln!(buf, "error {:?}: {}", err.location(), err.kind()).unwrap(); 119 writeln!(buf, "error {:?}: {}", err.location(), err.kind()).unwrap();
120 } 120 }
@@ -234,7 +234,7 @@ fn api_walkthrough() {
234 assert_eq!(expr_syntax.kind(), SyntaxKind::BIN_EXPR); 234 assert_eq!(expr_syntax.kind(), SyntaxKind::BIN_EXPR);
235 235
236 // And text range: 236 // And text range:
237 assert_eq!(expr_syntax.range(), TextRange::from_to(32.into(), 37.into())); 237 assert_eq!(expr_syntax.text_range(), TextRange::from_to(32.into(), 37.into()));
238 238
239 // You can get node's text as a `SyntaxText` object, which will traverse the 239 // You can get node's text as a `SyntaxText` object, which will traverse the
240 // tree collecting token's text: 240 // tree collecting token's text:
diff --git a/crates/ra_syntax/src/parsing/reparsing.rs b/crates/ra_syntax/src/parsing/reparsing.rs
index b4ad9e019..2f388bdfe 100644
--- a/crates/ra_syntax/src/parsing/reparsing.rs
+++ b/crates/ra_syntax/src/parsing/reparsing.rs
@@ -46,7 +46,8 @@ fn reparse_token<'node>(
46 WHITESPACE | COMMENT | IDENT | STRING | RAW_STRING => { 46 WHITESPACE | COMMENT | IDENT | STRING | RAW_STRING => {
47 if token.kind() == WHITESPACE || token.kind() == COMMENT { 47 if token.kind() == WHITESPACE || token.kind() == COMMENT {
48 // removing a new line may extends previous token 48 // removing a new line may extends previous token
49 if token.text().to_string()[edit.delete - token.range().start()].contains('\n') { 49 if token.text().to_string()[edit.delete - token.text_range().start()].contains('\n')
50 {
50 return None; 51 return None;
51 } 52 }
52 } 53 }
@@ -62,7 +63,7 @@ fn reparse_token<'node>(
62 return None; 63 return None;
63 } 64 }
64 65
65 if let Some(next_char) = root.text().char_at(token.range().end()) { 66 if let Some(next_char) = root.text().char_at(token.text_range().end()) {
66 let tokens_with_next_char = tokenize(&format!("{}{}", text, next_char)); 67 let tokens_with_next_char = tokenize(&format!("{}{}", text, next_char));
67 if tokens_with_next_char.len() == 1 { 68 if tokens_with_next_char.len() == 1 {
68 return None; 69 return None;
@@ -70,7 +71,7 @@ fn reparse_token<'node>(
70 } 71 }
71 72
72 let new_token = GreenToken::new(rowan::SyntaxKind(token.kind().into()), text.into()); 73 let new_token = GreenToken::new(rowan::SyntaxKind(token.kind().into()), text.into());
73 Some((token.replace_with(new_token), token.range())) 74 Some((token.replace_with(new_token), token.text_range()))
74 } 75 }
75 _ => None, 76 _ => None,
76 } 77 }
@@ -90,11 +91,12 @@ fn reparse_block<'node>(
90 let mut tree_sink = TextTreeSink::new(&text, &tokens); 91 let mut tree_sink = TextTreeSink::new(&text, &tokens);
91 reparser.parse(&mut token_source, &mut tree_sink); 92 reparser.parse(&mut token_source, &mut tree_sink);
92 let (green, new_errors) = tree_sink.finish(); 93 let (green, new_errors) = tree_sink.finish();
93 Some((node.replace_with(green), new_errors, node.range())) 94 Some((node.replace_with(green), new_errors, node.text_range()))
94} 95}
95 96
96fn get_text_after_edit(element: SyntaxElement, edit: &AtomTextEdit) -> String { 97fn get_text_after_edit(element: SyntaxElement, edit: &AtomTextEdit) -> String {
97 let edit = AtomTextEdit::replace(edit.delete - element.range().start(), edit.insert.clone()); 98 let edit =
99 AtomTextEdit::replace(edit.delete - element.text_range().start(), edit.insert.clone());
98 let text = match element { 100 let text = match element {
99 SyntaxElement::Token(token) => token.text().to_string(), 101 SyntaxElement::Token(token) => token.text().to_string(),
100 SyntaxElement::Node(node) => node.text().to_string(), 102 SyntaxElement::Node(node) => node.text().to_string(),
@@ -188,8 +190,8 @@ mod tests {
188 }; 190 };
189 191
190 assert_eq_text!( 192 assert_eq_text!(
191 &fully_reparsed.tree().syntax().debug_dump(), 193 &format!("{:#?}", fully_reparsed.tree().syntax()),
192 &incrementally_reparsed.tree().syntax().debug_dump(), 194 &format!("{:#?}", incrementally_reparsed.tree().syntax()),
193 ); 195 );
194 } 196 }
195 197
diff --git a/crates/ra_syntax/src/ptr.rs b/crates/ra_syntax/src/ptr.rs
index d1b30a2c9..8665c8976 100644
--- a/crates/ra_syntax/src/ptr.rs
+++ b/crates/ra_syntax/src/ptr.rs
@@ -12,15 +12,15 @@ pub struct SyntaxNodePtr {
12 12
13impl SyntaxNodePtr { 13impl SyntaxNodePtr {
14 pub fn new(node: &SyntaxNode) -> SyntaxNodePtr { 14 pub fn new(node: &SyntaxNode) -> SyntaxNodePtr {
15 SyntaxNodePtr { range: node.range(), kind: node.kind() } 15 SyntaxNodePtr { range: node.text_range(), kind: node.kind() }
16 } 16 }
17 17
18 pub fn to_node(self, root: &SyntaxNode) -> SyntaxNode { 18 pub fn to_node(self, root: &SyntaxNode) -> SyntaxNode {
19 assert!(root.parent().is_none()); 19 assert!(root.parent().is_none());
20 successors(Some(root.clone()), |node| { 20 successors(Some(root.clone()), |node| {
21 node.children().find(|it| self.range.is_subrange(&it.range())) 21 node.children().find(|it| self.range.is_subrange(&it.text_range()))
22 }) 22 })
23 .find(|it| it.range() == self.range && it.kind() == self.kind) 23 .find(|it| it.text_range() == self.range && it.kind() == self.kind)
24 .unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self)) 24 .unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self))
25 } 25 }
26 26
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
diff --git a/crates/ra_syntax/src/syntax_text.rs b/crates/ra_syntax/src/syntax_text.rs
index 2ad98809b..f8ddff48e 100644
--- a/crates/ra_syntax/src/syntax_text.rs
+++ b/crates/ra_syntax/src/syntax_text.rs
@@ -13,7 +13,7 @@ pub struct SyntaxText {
13 13
14impl SyntaxText { 14impl SyntaxText {
15 pub(crate) fn new(node: SyntaxNode) -> SyntaxText { 15 pub(crate) fn new(node: SyntaxNode) -> SyntaxText {
16 let range = node.range(); 16 let range = node.text_range();
17 SyntaxText { node, range } 17 SyntaxText { node, range }
18 } 18 }
19 19
@@ -24,14 +24,14 @@ impl SyntaxText {
24 self.node.descendants_with_tokens().try_fold(init, move |acc, element| { 24 self.node.descendants_with_tokens().try_fold(init, move |acc, element| {
25 let res = match element { 25 let res = match element {
26 SyntaxElement::Token(token) => { 26 SyntaxElement::Token(token) => {
27 let range = match self.range.intersection(&token.range()) { 27 let range = match self.range.intersection(&token.text_range()) {
28 None => return Ok(acc), 28 None => return Ok(acc),
29 Some(it) => it, 29 Some(it) => it,
30 }; 30 };
31 let slice = if range == token.range() { 31 let slice = if range == token.text_range() {
32 token.text() 32 token.text()
33 } else { 33 } else {
34 let range = range - token.range().start(); 34 let range = range - token.text_range().start();
35 &token.text()[range] 35 &token.text()[range]
36 }; 36 };
37 f(acc, slice)? 37 f(acc, slice)?
diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs
index 7140d10c3..19bdafef2 100644
--- a/crates/ra_syntax/src/validation.rs
+++ b/crates/ra_syntax/src/validation.rs
@@ -33,7 +33,7 @@ fn validate_literal(literal: ast::Literal, acc: &mut Vec<SyntaxError>) {
33 if let Some(end) = text.rfind('\'') { 33 if let Some(end) = text.rfind('\'') {
34 if let Some(without_quotes) = text.get(2..end) { 34 if let Some(without_quotes) = text.get(2..end) {
35 if let Err((off, err)) = unescape::unescape_byte(without_quotes) { 35 if let Err((off, err)) = unescape::unescape_byte(without_quotes) {
36 let off = token.range().start() + TextUnit::from_usize(off + 2); 36 let off = token.text_range().start() + TextUnit::from_usize(off + 2);
37 acc.push(SyntaxError::new(err.into(), off)) 37 acc.push(SyntaxError::new(err.into(), off))
38 } 38 }
39 } 39 }
@@ -43,7 +43,7 @@ fn validate_literal(literal: ast::Literal, acc: &mut Vec<SyntaxError>) {
43 if let Some(end) = text.rfind('\'') { 43 if let Some(end) = text.rfind('\'') {
44 if let Some(without_quotes) = text.get(1..end) { 44 if let Some(without_quotes) = text.get(1..end) {
45 if let Err((off, err)) = unescape::unescape_char(without_quotes) { 45 if let Err((off, err)) = unescape::unescape_char(without_quotes) {
46 let off = token.range().start() + TextUnit::from_usize(off + 1); 46 let off = token.text_range().start() + TextUnit::from_usize(off + 1);
47 acc.push(SyntaxError::new(err.into(), off)) 47 acc.push(SyntaxError::new(err.into(), off))
48 } 48 }
49 } 49 }
@@ -55,7 +55,7 @@ fn validate_literal(literal: ast::Literal, acc: &mut Vec<SyntaxError>) {
55 unescape::unescape_byte_str(without_quotes, &mut |range, char| { 55 unescape::unescape_byte_str(without_quotes, &mut |range, char| {
56 if let Err(err) = char { 56 if let Err(err) = char {
57 let off = range.start; 57 let off = range.start;
58 let off = token.range().start() + TextUnit::from_usize(off + 2); 58 let off = token.text_range().start() + TextUnit::from_usize(off + 2);
59 acc.push(SyntaxError::new(err.into(), off)) 59 acc.push(SyntaxError::new(err.into(), off))
60 } 60 }
61 }) 61 })
@@ -68,7 +68,7 @@ fn validate_literal(literal: ast::Literal, acc: &mut Vec<SyntaxError>) {
68 unescape::unescape_str(without_quotes, &mut |range, char| { 68 unescape::unescape_str(without_quotes, &mut |range, char| {
69 if let Err(err) = char { 69 if let Err(err) = char {
70 let off = range.start; 70 let off = range.start;
71 let off = token.range().start() + TextUnit::from_usize(off + 1); 71 let off = token.text_range().start() + TextUnit::from_usize(off + 1);
72 acc.push(SyntaxError::new(err.into(), off)) 72 acc.push(SyntaxError::new(err.into(), off))
73 } 73 }
74 }) 74 })
@@ -89,9 +89,9 @@ pub(crate) fn validate_block_structure(root: &SyntaxNode) {
89 assert_eq!( 89 assert_eq!(
90 node.parent(), 90 node.parent(),
91 pair.parent(), 91 pair.parent(),
92 "\nunpaired curleys:\n{}\n{}\n", 92 "\nunpaired curleys:\n{}\n{:#?}\n",
93 root.text(), 93 root.text(),
94 root.debug_dump(), 94 root,
95 ); 95 );
96 assert!( 96 assert!(
97 node.next_sibling().is_none() && pair.prev_sibling().is_none(), 97 node.next_sibling().is_none() && pair.prev_sibling().is_none(),
diff --git a/crates/ra_syntax/src/validation/block.rs b/crates/ra_syntax/src/validation/block.rs
index f5573bd8f..c5588658d 100644
--- a/crates/ra_syntax/src/validation/block.rs
+++ b/crates/ra_syntax/src/validation/block.rs
@@ -16,6 +16,7 @@ pub(crate) fn validate_block_node(node: ast::Block, errors: &mut Vec<SyntaxError
16 _ => {} 16 _ => {}
17 } 17 }
18 } 18 }
19 errors 19 errors.extend(
20 .extend(node.attrs().map(|attr| SyntaxError::new(InvalidBlockAttr, attr.syntax().range()))) 20 node.attrs().map(|attr| SyntaxError::new(InvalidBlockAttr, attr.syntax().text_range())),
21 )
21} 22}
diff --git a/crates/ra_syntax/src/validation/field_expr.rs b/crates/ra_syntax/src/validation/field_expr.rs
index 0e18bd9ca..004f199fd 100644
--- a/crates/ra_syntax/src/validation/field_expr.rs
+++ b/crates/ra_syntax/src/validation/field_expr.rs
@@ -7,7 +7,7 @@ use crate::{
7pub(crate) fn validate_field_expr_node(node: ast::FieldExpr, errors: &mut Vec<SyntaxError>) { 7pub(crate) fn validate_field_expr_node(node: ast::FieldExpr, errors: &mut Vec<SyntaxError>) {
8 if let Some(FieldKind::Index(idx)) = node.field_access() { 8 if let Some(FieldKind::Index(idx)) = node.field_access() {
9 if idx.text().chars().any(|c| c < '0' || c > '9') { 9 if idx.text().chars().any(|c| c < '0' || c > '9') {
10 errors.push(SyntaxError::new(InvalidTupleIndexFormat, idx.range())); 10 errors.push(SyntaxError::new(InvalidTupleIndexFormat, idx.text_range()));
11 } 11 }
12 } 12 }
13} 13}