diff options
Diffstat (limited to 'crates/syntax/src/ast/token_ext.rs')
-rw-r--r-- | crates/syntax/src/ast/token_ext.rs | 52 |
1 files changed, 21 insertions, 31 deletions
diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs index 8d3fad5a6..6cd20b6a6 100644 --- a/crates/syntax/src/ast/token_ext.rs +++ b/crates/syntax/src/ast/token_ext.rs | |||
@@ -139,14 +139,31 @@ pub trait HasQuotes: AstToken { | |||
139 | } | 139 | } |
140 | 140 | ||
141 | impl HasQuotes for ast::String {} | 141 | impl HasQuotes for ast::String {} |
142 | impl HasQuotes for ast::RawString {} | ||
143 | 142 | ||
144 | pub trait HasStringValue: HasQuotes { | 143 | pub trait HasStringValue: HasQuotes { |
145 | fn value(&self) -> Option<Cow<'_, str>>; | 144 | fn value(&self) -> Option<Cow<'_, str>>; |
146 | } | 145 | } |
147 | 146 | ||
147 | impl ast::String { | ||
148 | pub fn is_raw(&self) -> bool { | ||
149 | self.text().starts_with('r') | ||
150 | } | ||
151 | pub fn map_range_up(&self, range: TextRange) -> Option<TextRange> { | ||
152 | let contents_range = self.text_range_between_quotes()?; | ||
153 | assert!(TextRange::up_to(contents_range.len()).contains_range(range)); | ||
154 | Some(range + contents_range.start()) | ||
155 | } | ||
156 | } | ||
157 | |||
148 | impl HasStringValue for ast::String { | 158 | impl HasStringValue for ast::String { |
149 | fn value(&self) -> Option<Cow<'_, str>> { | 159 | fn value(&self) -> Option<Cow<'_, str>> { |
160 | if self.is_raw() { | ||
161 | let text = self.text().as_str(); | ||
162 | let text = | ||
163 | &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; | ||
164 | return Some(Cow::Borrowed(text)); | ||
165 | } | ||
166 | |||
150 | let text = self.text().as_str(); | 167 | let text = self.text().as_str(); |
151 | let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; | 168 | let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; |
152 | 169 | ||
@@ -166,20 +183,9 @@ impl HasStringValue for ast::String { | |||
166 | } | 183 | } |
167 | } | 184 | } |
168 | 185 | ||
169 | // FIXME: merge `ast::RawString` and `ast::String`. | 186 | impl ast::ByteString { |
170 | impl HasStringValue for ast::RawString { | 187 | pub fn is_raw(&self) -> bool { |
171 | fn value(&self) -> Option<Cow<'_, str>> { | 188 | self.text().starts_with("br") |
172 | let text = self.text().as_str(); | ||
173 | let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; | ||
174 | Some(Cow::Borrowed(text)) | ||
175 | } | ||
176 | } | ||
177 | |||
178 | impl ast::RawString { | ||
179 | pub fn map_range_up(&self, range: TextRange) -> Option<TextRange> { | ||
180 | let contents_range = self.text_range_between_quotes()?; | ||
181 | assert!(TextRange::up_to(contents_range.len()).contains_range(range)); | ||
182 | Some(range + contents_range.start()) | ||
183 | } | 189 | } |
184 | } | 190 | } |
185 | 191 | ||
@@ -522,22 +528,6 @@ impl HasFormatSpecifier for ast::String { | |||
522 | } | 528 | } |
523 | } | 529 | } |
524 | 530 | ||
525 | impl HasFormatSpecifier for ast::RawString { | ||
526 | fn char_ranges( | ||
527 | &self, | ||
528 | ) -> Option<Vec<(TextRange, Result<char, rustc_lexer::unescape::EscapeError>)>> { | ||
529 | let text = self.text().as_str(); | ||
530 | let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; | ||
531 | let offset = self.text_range_between_quotes()?.start() - self.syntax().text_range().start(); | ||
532 | |||
533 | let mut res = Vec::with_capacity(text.len()); | ||
534 | for (idx, c) in text.char_indices() { | ||
535 | res.push((TextRange::at(idx.try_into().unwrap(), TextSize::of(c)) + offset, Ok(c))); | ||
536 | } | ||
537 | Some(res) | ||
538 | } | ||
539 | } | ||
540 | |||
541 | impl ast::IntNumber { | 531 | impl ast::IntNumber { |
542 | #[rustfmt::skip] | 532 | #[rustfmt::skip] |
543 | pub(crate) const SUFFIXES: &'static [&'static str] = &[ | 533 | pub(crate) const SUFFIXES: &'static [&'static str] = &[ |