aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_mbe/src/mbe_expander/matcher.rs2
-rw-r--r--crates/ra_mbe/src/subtree_source.rs8
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs92
-rw-r--r--crates/ra_tt/src/lib.rs6
4 files changed, 55 insertions, 53 deletions
diff --git a/crates/ra_mbe/src/mbe_expander/matcher.rs b/crates/ra_mbe/src/mbe_expander/matcher.rs
index c67ae4110..e36b5a412 100644
--- a/crates/ra_mbe/src/mbe_expander/matcher.rs
+++ b/crates/ra_mbe/src/mbe_expander/matcher.rs
@@ -106,7 +106,7 @@ fn match_subtree(
106 } 106 }
107 Op::TokenTree(tt::TokenTree::Subtree(lhs)) => { 107 Op::TokenTree(tt::TokenTree::Subtree(lhs)) => {
108 let rhs = src.expect_subtree().map_err(|()| err!("expected subtree"))?; 108 let rhs = src.expect_subtree().map_err(|()| err!("expected subtree"))?;
109 if lhs.delimiter.map(|it| it.kind) != rhs.delimiter.map(|it| it.kind) { 109 if lhs.delimiter_kind() != rhs.delimiter_kind() {
110 bail!("mismatched delimiter") 110 bail!("mismatched delimiter")
111 } 111 }
112 let mut src = TtIter::new(rhs); 112 let mut src = TtIter::new(rhs);
diff --git a/crates/ra_mbe/src/subtree_source.rs b/crates/ra_mbe/src/subtree_source.rs
index 5a03a372a..b841c39d3 100644
--- a/crates/ra_mbe/src/subtree_source.rs
+++ b/crates/ra_mbe/src/subtree_source.rs
@@ -70,11 +70,11 @@ impl<'a> SubtreeTokenSource<'a> {
70 } 70 }
71 Some(tt::TokenTree::Subtree(subtree)) => { 71 Some(tt::TokenTree::Subtree(subtree)) => {
72 self.cached_cursor.set(cursor.subtree().unwrap()); 72 self.cached_cursor.set(cursor.subtree().unwrap());
73 cached.push(Some(convert_delim(subtree.delimiter, false))); 73 cached.push(Some(convert_delim(subtree.delimiter_kind(), false)));
74 } 74 }
75 None => { 75 None => {
76 if let Some(subtree) = cursor.end() { 76 if let Some(subtree) = cursor.end() {
77 cached.push(Some(convert_delim(subtree.delimiter, true))); 77 cached.push(Some(convert_delim(subtree.delimiter_kind(), true)));
78 self.cached_cursor.set(cursor.bump()); 78 self.cached_cursor.set(cursor.bump());
79 } 79 }
80 } 80 }
@@ -114,8 +114,8 @@ impl<'a> TokenSource for SubtreeTokenSource<'a> {
114 } 114 }
115} 115}
116 116
117fn convert_delim(d: Option<tt::Delimiter>, closing: bool) -> TtToken { 117fn convert_delim(d: Option<tt::DelimiterKind>, closing: bool) -> TtToken {
118 let (kinds, texts) = match d.map(|it| it.kind) { 118 let (kinds, texts) = match d {
119 Some(tt::DelimiterKind::Parenthesis) => ([T!['('], T![')']], "()"), 119 Some(tt::DelimiterKind::Parenthesis) => ([T!['('], T![')']], "()"),
120 Some(tt::DelimiterKind::Brace) => ([T!['{'], T!['}']], "{}"), 120 Some(tt::DelimiterKind::Brace) => ([T!['{'], T!['}']], "{}"),
121 Some(tt::DelimiterKind::Bracket) => ([T!['['], T![']']], "[]"), 121 Some(tt::DelimiterKind::Bracket) => ([T!['['], T![']']], "[]"),
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs
index d585d57af..2c60430d1 100644
--- a/crates/ra_mbe/src/syntax_bridge.rs
+++ b/crates/ra_mbe/src/syntax_bridge.rs
@@ -269,35 +269,33 @@ impl Convertor {
269 .take(token.text().len() - 1) 269 .take(token.text().len() - 1)
270 .chain(std::iter::once(last_spacing)); 270 .chain(std::iter::once(last_spacing));
271 for (char, spacing) in token.text().chars().zip(spacing_iter) { 271 for (char, spacing) in token.text().chars().zip(spacing_iter) {
272 let id = self.alloc(token.text_range()); 272 token_trees.push(
273 token_trees 273 tt::Leaf::from(tt::Punct {
274 .push(tt::Leaf::from(tt::Punct { char, spacing, id }).into()); 274 char,
275 spacing,
276 id: self.alloc(token.text_range()),
277 })
278 .into(),
279 );
275 } 280 }
276 } else { 281 } else {
282 macro_rules! make_leaf {
283 ($i:ident) => {
284 tt::$i {
285 id: self.alloc(token.text_range()),
286 text: token.text().clone(),
287 }
288 .into()
289 };
290 }
291
277 let child: tt::Leaf = match token.kind() { 292 let child: tt::Leaf = match token.kind() {
278 T![true] | T![false] => { 293 T![true] | T![false] => make_leaf!(Literal),
279 let id = self.alloc(token.text_range()); 294 IDENT | LIFETIME => make_leaf!(Ident),
280 let text = token.text().clone(); 295 k if k.is_keyword() => make_leaf!(Ident),
281 tt::Literal { text, id }.into() 296 k if k.is_literal() => make_leaf!(Literal),
282 }
283 IDENT | LIFETIME => {
284 let id = self.alloc(token.text_range());
285 let text = token.text().clone();
286 tt::Ident { text, id }.into()
287 }
288 k if k.is_keyword() => {
289 let id = self.alloc(token.text_range());
290 let text = token.text().clone();
291 tt::Ident { text, id }.into()
292 }
293 k if k.is_literal() => {
294 let id = self.alloc(token.text_range());
295 let text = token.text().clone();
296 tt::Literal { text, id }.into()
297 }
298 _ => return None, 297 _ => return None,
299 }; 298 };
300
301 token_trees.push(child.into()); 299 token_trees.push(child.into());
302 } 300 }
303 } 301 }
@@ -370,8 +368,8 @@ impl<'a> TtTreeSink<'a> {
370 } 368 }
371} 369}
372 370
373fn delim_to_str(d: Option<tt::Delimiter>, closing: bool) -> SmolStr { 371fn delim_to_str(d: Option<tt::DelimiterKind>, closing: bool) -> SmolStr {
374 let texts = match d.map(|it| it.kind) { 372 let texts = match d {
375 Some(tt::DelimiterKind::Parenthesis) => "()", 373 Some(tt::DelimiterKind::Parenthesis) => "()",
376 Some(tt::DelimiterKind::Brace) => "{}", 374 Some(tt::DelimiterKind::Brace) => "{}",
377 Some(tt::DelimiterKind::Bracket) => "[]", 375 Some(tt::DelimiterKind::Bracket) => "[]",
@@ -395,7 +393,7 @@ impl<'a> TreeSink for TtTreeSink<'a> {
395 break; 393 break;
396 } 394 }
397 395
398 let text: Option<SmolStr> = match self.cursor.token_tree() { 396 let text: SmolStr = match self.cursor.token_tree() {
399 Some(tt::TokenTree::Leaf(leaf)) => { 397 Some(tt::TokenTree::Leaf(leaf)) => {
400 // Mark the range if needed 398 // Mark the range if needed
401 let id = match leaf { 399 let id = match leaf {
@@ -407,35 +405,35 @@ impl<'a> TreeSink for TtTreeSink<'a> {
407 let range = TextRange::offset_len(self.text_pos, TextUnit::of_str(&text)); 405 let range = TextRange::offset_len(self.text_pos, TextUnit::of_str(&text));
408 self.token_map.insert(id, range); 406 self.token_map.insert(id, range);
409 self.cursor = self.cursor.bump(); 407 self.cursor = self.cursor.bump();
410 Some(text) 408 text
411 } 409 }
412 Some(tt::TokenTree::Subtree(subtree)) => { 410 Some(tt::TokenTree::Subtree(subtree)) => {
413 self.cursor = self.cursor.subtree().unwrap(); 411 self.cursor = self.cursor.subtree().unwrap();
414 if let Some(id) = subtree.delimiter.map(|it| it.id) { 412 if let Some(id) = subtree.delimiter.map(|it| it.id) {
415 self.open_delims.insert(id, self.text_pos); 413 self.open_delims.insert(id, self.text_pos);
416 } 414 }
417 Some(delim_to_str(subtree.delimiter, false)) 415 delim_to_str(subtree.delimiter_kind(), false)
418 } 416 }
419 None => self.cursor.end().and_then(|parent| { 417 None => {
420 self.cursor = self.cursor.bump(); 418 if let Some(parent) = self.cursor.end() {
421 if let Some(id) = parent.delimiter.map(|it| it.id) { 419 self.cursor = self.cursor.bump();
422 if let Some(open_delim) = self.open_delims.get(&id) { 420 if let Some(id) = parent.delimiter.map(|it| it.id) {
423 let open_range = 421 if let Some(open_delim) = self.open_delims.get(&id) {
424 TextRange::offset_len(*open_delim, TextUnit::from_usize(1)); 422 let open_range =
425 let close_range = 423 TextRange::offset_len(*open_delim, TextUnit::from_usize(1));
426 TextRange::offset_len(self.text_pos, TextUnit::from_usize(1)); 424 let close_range =
427 self.token_map.insert_delim(id, open_range, close_range); 425 TextRange::offset_len(self.text_pos, TextUnit::from_usize(1));
426 self.token_map.insert_delim(id, open_range, close_range);
427 }
428 } 428 }
429 delim_to_str(parent.delimiter_kind(), true)
430 } else {
431 continue;
429 } 432 }
430 433 }
431 Some(delim_to_str(parent.delimiter, true))
432 }),
433 }; 434 };
434 435 self.buf += &text;
435 if let Some(text) = text { 436 self.text_pos += TextUnit::of_str(&text);
436 self.buf += &text;
437 self.text_pos += TextUnit::of_str(&text);
438 }
439 } 437 }
440 438
441 let text = SmolStr::new(self.buf.as_str()); 439 let text = SmolStr::new(self.buf.as_str());
@@ -583,7 +581,7 @@ mod tests {
583 let token_tree = ast::TokenTree::cast(token_tree).unwrap(); 581 let token_tree = ast::TokenTree::cast(token_tree).unwrap();
584 let tt = ast_to_token_tree(&token_tree).unwrap().0; 582 let tt = ast_to_token_tree(&token_tree).unwrap().0;
585 583
586 assert_eq!(tt.delimiter.map(|it| it.kind), Some(tt::DelimiterKind::Brace)); 584 assert_eq!(tt.delimiter_kind(), Some(tt::DelimiterKind::Brace));
587 } 585 }
588 586
589 #[test] 587 #[test]
diff --git a/crates/ra_tt/src/lib.rs b/crates/ra_tt/src/lib.rs
index 73d8395a8..10f424aae 100644
--- a/crates/ra_tt/src/lib.rs
+++ b/crates/ra_tt/src/lib.rs
@@ -103,7 +103,7 @@ impl fmt::Display for TokenTree {
103 103
104impl fmt::Display for Subtree { 104impl fmt::Display for Subtree {
105 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 105 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
106 let (l, r) = match self.delimiter.map(|it| it.kind) { 106 let (l, r) = match self.delimiter_kind() {
107 Some(DelimiterKind::Parenthesis) => ("(", ")"), 107 Some(DelimiterKind::Parenthesis) => ("(", ")"),
108 Some(DelimiterKind::Brace) => ("{", "}"), 108 Some(DelimiterKind::Brace) => ("{", "}"),
109 Some(DelimiterKind::Bracket) => ("[", "]"), 109 Some(DelimiterKind::Bracket) => ("[", "]"),
@@ -171,6 +171,10 @@ impl Subtree {
171 171
172 self.token_trees.len() + children_count 172 self.token_trees.len() + children_count
173 } 173 }
174
175 pub fn delimiter_kind(&self) -> Option<DelimiterKind> {
176 self.delimiter.map(|it| it.kind)
177 }
174} 178}
175 179
176pub mod buffer; 180pub mod buffer;