aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax/src/ast/token_ext.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-11-06 21:39:02 +0000
committerGitHub <[email protected]>2020-11-06 21:39:02 +0000
commit7f12a1f225c7d3397f27964ce039b55d680772d3 (patch)
tree26043b20588eae4510e28249f11a094aacaf190d /crates/syntax/src/ast/token_ext.rs
parentcdddcaee851be1cff1eeb23599f5a58f1b30a927 (diff)
parent6158304f8b64ef7cdf58b14bc675baf33a27a853 (diff)
Merge #6485
6485: Remove RAW literals r=matklad a=matklad bors r+ 🤖 closes https://github.com/rust-analyzer/rust-analyzer/issues/6308 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/syntax/src/ast/token_ext.rs')
-rw-r--r--crates/syntax/src/ast/token_ext.rs90
1 files changed, 34 insertions, 56 deletions
diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs
index 8d3fad5a6..bf0035986 100644
--- a/crates/syntax/src/ast/token_ext.rs
+++ b/crates/syntax/src/ast/token_ext.rs
@@ -114,39 +114,24 @@ impl QuoteOffsets {
114 } 114 }
115} 115}
116 116
117pub trait HasQuotes: AstToken { 117impl ast::String {
118 fn quote_offsets(&self) -> Option<QuoteOffsets> { 118 pub fn is_raw(&self) -> bool {
119 let text = self.text().as_str(); 119 self.text().starts_with('r')
120 let offsets = QuoteOffsets::new(text)?;
121 let o = self.syntax().text_range().start();
122 let offsets = QuoteOffsets {
123 quotes: (offsets.quotes.0 + o, offsets.quotes.1 + o),
124 contents: offsets.contents + o,
125 };
126 Some(offsets)
127 }
128 fn open_quote_text_range(&self) -> Option<TextRange> {
129 self.quote_offsets().map(|it| it.quotes.0)
130 } 120 }
131 121 pub fn map_range_up(&self, range: TextRange) -> Option<TextRange> {
132 fn close_quote_text_range(&self) -> Option<TextRange> { 122 let contents_range = self.text_range_between_quotes()?;
133 self.quote_offsets().map(|it| it.quotes.1) 123 assert!(TextRange::up_to(contents_range.len()).contains_range(range));
134 } 124 Some(range + contents_range.start())
135
136 fn text_range_between_quotes(&self) -> Option<TextRange> {
137 self.quote_offsets().map(|it| it.contents)
138 } 125 }
139}
140
141impl HasQuotes for ast::String {}
142impl HasQuotes for ast::RawString {}
143 126
144pub trait HasStringValue: HasQuotes { 127 pub fn value(&self) -> Option<Cow<'_, str>> {
145 fn value(&self) -> Option<Cow<'_, str>>; 128 if self.is_raw() {
146} 129 let text = self.text().as_str();
130 let text =
131 &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
132 return Some(Cow::Borrowed(text));
133 }
147 134
148impl HasStringValue for ast::String {
149 fn value(&self) -> Option<Cow<'_, str>> {
150 let text = self.text().as_str(); 135 let text = self.text().as_str();
151 let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; 136 let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()];
152 137
@@ -164,22 +149,31 @@ impl HasStringValue for ast::String {
164 let res = if buf == text { Cow::Borrowed(text) } else { Cow::Owned(buf) }; 149 let res = if buf == text { Cow::Borrowed(text) } else { Cow::Owned(buf) };
165 Some(res) 150 Some(res)
166 } 151 }
167}
168 152
169// FIXME: merge `ast::RawString` and `ast::String`. 153 pub fn quote_offsets(&self) -> Option<QuoteOffsets> {
170impl HasStringValue for ast::RawString {
171 fn value(&self) -> Option<Cow<'_, str>> {
172 let text = self.text().as_str(); 154 let text = self.text().as_str();
173 let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; 155 let offsets = QuoteOffsets::new(text)?;
174 Some(Cow::Borrowed(text)) 156 let o = self.syntax().text_range().start();
157 let offsets = QuoteOffsets {
158 quotes: (offsets.quotes.0 + o, offsets.quotes.1 + o),
159 contents: offsets.contents + o,
160 };
161 Some(offsets)
162 }
163 pub fn text_range_between_quotes(&self) -> Option<TextRange> {
164 self.quote_offsets().map(|it| it.contents)
165 }
166 pub fn open_quote_text_range(&self) -> Option<TextRange> {
167 self.quote_offsets().map(|it| it.quotes.0)
168 }
169 pub fn close_quote_text_range(&self) -> Option<TextRange> {
170 self.quote_offsets().map(|it| it.quotes.1)
175 } 171 }
176} 172}
177 173
178impl ast::RawString { 174impl ast::ByteString {
179 pub fn map_range_up(&self, range: TextRange) -> Option<TextRange> { 175 pub fn is_raw(&self) -> bool {
180 let contents_range = self.text_range_between_quotes()?; 176 self.text().starts_with("br")
181 assert!(TextRange::up_to(contents_range.len()).contains_range(range));
182 Some(range + contents_range.start())
183 } 177 }
184} 178}
185 179
@@ -522,22 +516,6 @@ impl HasFormatSpecifier for ast::String {
522 } 516 }
523} 517}
524 518
525impl 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
541impl ast::IntNumber { 519impl ast::IntNumber {
542 #[rustfmt::skip] 520 #[rustfmt::skip]
543 pub(crate) const SUFFIXES: &'static [&'static str] = &[ 521 pub(crate) const SUFFIXES: &'static [&'static str] = &[