aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax/src/ast/token_ext.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/syntax/src/ast/token_ext.rs')
-rw-r--r--crates/syntax/src/ast/token_ext.rs54
1 files changed, 21 insertions, 33 deletions
diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs
index 6cd20b6a6..bf0035986 100644
--- a/crates/syntax/src/ast/token_ext.rs
+++ b/crates/syntax/src/ast/token_ext.rs
@@ -114,36 +114,6 @@ impl QuoteOffsets {
114 } 114 }
115} 115}
116 116
117pub trait HasQuotes: AstToken {
118 fn quote_offsets(&self) -> Option<QuoteOffsets> {
119 let text = self.text().as_str();
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 }
131
132 fn close_quote_text_range(&self) -> Option<TextRange> {
133 self.quote_offsets().map(|it| it.quotes.1)
134 }
135
136 fn text_range_between_quotes(&self) -> Option<TextRange> {
137 self.quote_offsets().map(|it| it.contents)
138 }
139}
140
141impl HasQuotes for ast::String {}
142
143pub trait HasStringValue: HasQuotes {
144 fn value(&self) -> Option<Cow<'_, str>>;
145}
146
147impl ast::String { 117impl ast::String {
148 pub fn is_raw(&self) -> bool { 118 pub fn is_raw(&self) -> bool {
149 self.text().starts_with('r') 119 self.text().starts_with('r')
@@ -153,10 +123,8 @@ impl ast::String {
153 assert!(TextRange::up_to(contents_range.len()).contains_range(range)); 123 assert!(TextRange::up_to(contents_range.len()).contains_range(range));
154 Some(range + contents_range.start()) 124 Some(range + contents_range.start())
155 } 125 }
156}
157 126
158impl HasStringValue for ast::String { 127 pub fn value(&self) -> Option<Cow<'_, str>> {
159 fn value(&self) -> Option<Cow<'_, str>> {
160 if self.is_raw() { 128 if self.is_raw() {
161 let text = self.text().as_str(); 129 let text = self.text().as_str();
162 let text = 130 let text =
@@ -181,6 +149,26 @@ impl HasStringValue for ast::String {
181 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) };
182 Some(res) 150 Some(res)
183 } 151 }
152
153 pub fn quote_offsets(&self) -> Option<QuoteOffsets> {
154 let text = self.text().as_str();
155 let offsets = QuoteOffsets::new(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)
171 }
184} 172}
185 173
186impl ast::ByteString { 174impl ast::ByteString {