diff options
Diffstat (limited to 'crates/ra_syntax/src/ast')
-rw-r--r-- | crates/ra_syntax/src/ast/tokens.rs | 39 |
1 files changed, 18 insertions, 21 deletions
diff --git a/crates/ra_syntax/src/ast/tokens.rs b/crates/ra_syntax/src/ast/tokens.rs index aa34b682d..3865729b8 100644 --- a/crates/ra_syntax/src/ast/tokens.rs +++ b/crates/ra_syntax/src/ast/tokens.rs | |||
@@ -1,8 +1,10 @@ | |||
1 | //! There are many AstNodes, but only a few tokens, so we hand-write them here. | 1 | //! There are many AstNodes, but only a few tokens, so we hand-write them here. |
2 | 2 | ||
3 | use std::convert::{TryFrom, TryInto}; | ||
4 | |||
3 | use crate::{ | 5 | use crate::{ |
4 | ast::{AstToken, Comment, RawString, String, Whitespace}, | 6 | ast::{AstToken, Comment, RawString, String, Whitespace}, |
5 | TextRange, TextUnit, | 7 | TextRange, TextSize, |
6 | }; | 8 | }; |
7 | 9 | ||
8 | impl Comment { | 10 | impl Comment { |
@@ -56,6 +58,9 @@ const COMMENT_PREFIX_TO_KIND: &[(&str, CommentKind)] = { | |||
56 | }; | 58 | }; |
57 | 59 | ||
58 | fn kind_by_prefix(text: &str) -> CommentKind { | 60 | fn kind_by_prefix(text: &str) -> CommentKind { |
61 | if text == "/**/" { | ||
62 | return CommentKind { shape: CommentShape::Block, doc: None }; | ||
63 | } | ||
59 | for (prefix, kind) in COMMENT_PREFIX_TO_KIND.iter() { | 64 | for (prefix, kind) in COMMENT_PREFIX_TO_KIND.iter() { |
60 | if text.starts_with(prefix) { | 65 | if text.starts_with(prefix) { |
61 | return *kind; | 66 | return *kind; |
@@ -94,14 +99,14 @@ impl QuoteOffsets { | |||
94 | return None; | 99 | return None; |
95 | } | 100 | } |
96 | 101 | ||
97 | let start = TextUnit::from(0); | 102 | let start = TextSize::from(0); |
98 | let left_quote = TextUnit::from_usize(left_quote) + TextUnit::of_char('"'); | 103 | let left_quote = TextSize::try_from(left_quote).unwrap() + TextSize::of('"'); |
99 | let right_quote = TextUnit::from_usize(right_quote); | 104 | let right_quote = TextSize::try_from(right_quote).unwrap(); |
100 | let end = TextUnit::of_str(literal); | 105 | let end = TextSize::of(literal); |
101 | 106 | ||
102 | let res = QuoteOffsets { | 107 | let res = QuoteOffsets { |
103 | quotes: [TextRange::from_to(start, left_quote), TextRange::from_to(right_quote, end)], | 108 | quotes: [TextRange::new(start, left_quote), TextRange::new(right_quote, end)], |
104 | contents: TextRange::from_to(left_quote, right_quote), | 109 | contents: TextRange::new(left_quote, right_quote), |
105 | }; | 110 | }; |
106 | Some(res) | 111 | Some(res) |
107 | } | 112 | } |
@@ -168,7 +173,7 @@ impl HasStringValue for RawString { | |||
168 | impl RawString { | 173 | impl RawString { |
169 | pub fn map_range_up(&self, range: TextRange) -> Option<TextRange> { | 174 | pub fn map_range_up(&self, range: TextRange) -> Option<TextRange> { |
170 | let contents_range = self.text_range_between_quotes()?; | 175 | let contents_range = self.text_range_between_quotes()?; |
171 | assert!(range.is_subrange(&TextRange::offset_len(0.into(), contents_range.len()))); | 176 | assert!(TextRange::up_to(contents_range.len()).contains_range(range)); |
172 | Some(range + contents_range.start()) | 177 | Some(range + contents_range.start()) |
173 | } | 178 | } |
174 | } | 179 | } |
@@ -459,7 +464,7 @@ pub trait HasFormatSpecifier: AstToken { | |||
459 | while let Some((r, Ok(next_char))) = chars.peek() { | 464 | while let Some((r, Ok(next_char))) = chars.peek() { |
460 | if next_char.is_ascii_digit() { | 465 | if next_char.is_ascii_digit() { |
461 | chars.next(); | 466 | chars.next(); |
462 | range = range.extend_to(r); | 467 | range = range.cover(*r); |
463 | } else { | 468 | } else { |
464 | break; | 469 | break; |
465 | } | 470 | } |
@@ -477,7 +482,7 @@ pub trait HasFormatSpecifier: AstToken { | |||
477 | while let Some((r, Ok(next_char))) = chars.peek() { | 482 | while let Some((r, Ok(next_char))) = chars.peek() { |
478 | if *next_char == '_' || next_char.is_ascii_digit() || next_char.is_alphabetic() { | 483 | if *next_char == '_' || next_char.is_ascii_digit() || next_char.is_alphabetic() { |
479 | chars.next(); | 484 | chars.next(); |
480 | range = range.extend_to(r); | 485 | range = range.cover(*r); |
481 | } else { | 486 | } else { |
482 | break; | 487 | break; |
483 | } | 488 | } |
@@ -498,10 +503,8 @@ impl HasFormatSpecifier for String { | |||
498 | let mut res = Vec::with_capacity(text.len()); | 503 | let mut res = Vec::with_capacity(text.len()); |
499 | rustc_lexer::unescape::unescape_str(text, &mut |range, unescaped_char| { | 504 | rustc_lexer::unescape::unescape_str(text, &mut |range, unescaped_char| { |
500 | res.push(( | 505 | res.push(( |
501 | TextRange::from_to( | 506 | TextRange::new(range.start.try_into().unwrap(), range.end.try_into().unwrap()) |
502 | TextUnit::from_usize(range.start), | 507 | + offset, |
503 | TextUnit::from_usize(range.end), | ||
504 | ) + offset, | ||
505 | unescaped_char, | 508 | unescaped_char, |
506 | )) | 509 | )) |
507 | }); | 510 | }); |
@@ -520,13 +523,7 @@ impl HasFormatSpecifier for RawString { | |||
520 | 523 | ||
521 | let mut res = Vec::with_capacity(text.len()); | 524 | let mut res = Vec::with_capacity(text.len()); |
522 | for (idx, c) in text.char_indices() { | 525 | for (idx, c) in text.char_indices() { |
523 | res.push(( | 526 | res.push((TextRange::at(idx.try_into().unwrap(), TextSize::of(c)) + offset, Ok(c))); |
524 | TextRange::from_to( | ||
525 | TextUnit::from_usize(idx), | ||
526 | TextUnit::from_usize(idx + c.len_utf8()), | ||
527 | ) + offset, | ||
528 | Ok(c), | ||
529 | )); | ||
530 | } | 527 | } |
531 | Some(res) | 528 | Some(res) |
532 | } | 529 | } |