diff options
Diffstat (limited to 'crates/ra_syntax/src/ast')
-rw-r--r-- | crates/ra_syntax/src/ast/edit.rs | 23 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/tokens.rs | 17 |
2 files changed, 28 insertions, 12 deletions
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index 2ef173a03..abc7a646c 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs | |||
@@ -189,6 +189,21 @@ impl ast::RecordFieldList { | |||
189 | } | 189 | } |
190 | } | 190 | } |
191 | 191 | ||
192 | impl ast::TypeAliasDef { | ||
193 | #[must_use] | ||
194 | pub fn remove_bounds(&self) -> ast::TypeAliasDef { | ||
195 | let colon = match self.colon_token() { | ||
196 | Some(it) => it, | ||
197 | None => return self.clone(), | ||
198 | }; | ||
199 | let end = match self.type_bound_list() { | ||
200 | Some(it) => it.syntax().clone().into(), | ||
201 | None => colon.clone().into(), | ||
202 | }; | ||
203 | self.replace_children(colon.into()..=end, iter::empty()) | ||
204 | } | ||
205 | } | ||
206 | |||
192 | impl ast::TypeParam { | 207 | impl ast::TypeParam { |
193 | #[must_use] | 208 | #[must_use] |
194 | pub fn remove_bounds(&self) -> ast::TypeParam { | 209 | pub fn remove_bounds(&self) -> ast::TypeParam { |
@@ -299,12 +314,8 @@ impl ast::UseTree { | |||
299 | Some(it) => it, | 314 | Some(it) => it, |
300 | None => return self.clone(), | 315 | None => return self.clone(), |
301 | }; | 316 | }; |
302 | let use_tree = make::use_tree( | 317 | let use_tree = |
303 | suffix.clone(), | 318 | make::use_tree(suffix, self.use_tree_list(), self.alias(), self.star_token().is_some()); |
304 | self.use_tree_list(), | ||
305 | self.alias(), | ||
306 | self.star_token().is_some(), | ||
307 | ); | ||
308 | let nested = make::use_tree_list(iter::once(use_tree)); | 319 | let nested = make::use_tree_list(iter::once(use_tree)); |
309 | return make::use_tree(prefix.clone(), Some(nested), None, false); | 320 | return make::use_tree(prefix.clone(), Some(nested), None, false); |
310 | 321 | ||
diff --git a/crates/ra_syntax/src/ast/tokens.rs b/crates/ra_syntax/src/ast/tokens.rs index 2e72d4927..045f69133 100644 --- a/crates/ra_syntax/src/ast/tokens.rs +++ b/crates/ra_syntax/src/ast/tokens.rs | |||
@@ -1,6 +1,9 @@ | |||
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}; | 3 | use std::{ |
4 | borrow::Cow, | ||
5 | convert::{TryFrom, TryInto}, | ||
6 | }; | ||
4 | 7 | ||
5 | use crate::{ | 8 | use crate::{ |
6 | ast::{AstToken, Comment, RawString, String, Whitespace}, | 9 | ast::{AstToken, Comment, RawString, String, Whitespace}, |
@@ -138,11 +141,11 @@ impl HasQuotes for String {} | |||
138 | impl HasQuotes for RawString {} | 141 | impl HasQuotes for RawString {} |
139 | 142 | ||
140 | pub trait HasStringValue: HasQuotes { | 143 | pub trait HasStringValue: HasQuotes { |
141 | fn value(&self) -> Option<std::string::String>; | 144 | fn value(&self) -> Option<Cow<'_, str>>; |
142 | } | 145 | } |
143 | 146 | ||
144 | impl HasStringValue for String { | 147 | impl HasStringValue for String { |
145 | fn value(&self) -> Option<std::string::String> { | 148 | fn value(&self) -> Option<Cow<'_, str>> { |
146 | let text = self.text().as_str(); | 149 | let text = self.text().as_str(); |
147 | let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; | 150 | let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; |
148 | 151 | ||
@@ -156,15 +159,17 @@ impl HasStringValue for String { | |||
156 | if has_error { | 159 | if has_error { |
157 | return None; | 160 | return None; |
158 | } | 161 | } |
159 | Some(buf) | 162 | // FIXME: don't actually allocate for borrowed case |
163 | let res = if buf == text { Cow::Borrowed(text) } else { Cow::Owned(buf) }; | ||
164 | Some(res) | ||
160 | } | 165 | } |
161 | } | 166 | } |
162 | 167 | ||
163 | impl HasStringValue for RawString { | 168 | impl HasStringValue for RawString { |
164 | fn value(&self) -> Option<std::string::String> { | 169 | fn value(&self) -> Option<Cow<'_, str>> { |
165 | let text = self.text().as_str(); | 170 | let text = self.text().as_str(); |
166 | let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; | 171 | let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; |
167 | Some(text.to_string()) | 172 | Some(Cow::Borrowed(text)) |
168 | } | 173 | } |
169 | } | 174 | } |
170 | 175 | ||