diff options
author | Aleksey Kladov <[email protected]> | 2020-01-15 17:48:28 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-01-15 17:48:28 +0000 |
commit | ef1326ee19c9ff707dcf96ce289102deedcf4d5f (patch) | |
tree | 13315f0f9fdbe1d69d228d745147b28a5edd99ab /crates/ra_syntax/src | |
parent | 7d2d3ac3db6ea7bbb3d77569495176da3b2992e6 (diff) |
More orthogonal path editing
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r-- | crates/ra_syntax/src/ast/edit.rs | 42 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/make.rs | 18 |
2 files changed, 44 insertions, 16 deletions
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index d88a0cf4b..0e78d8b63 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs | |||
@@ -207,6 +207,48 @@ impl ast::TypeParam { | |||
207 | } | 207 | } |
208 | } | 208 | } |
209 | 209 | ||
210 | impl ast::Path { | ||
211 | #[must_use] | ||
212 | pub fn with_segment(&self, segment: ast::PathSegment) -> ast::Path { | ||
213 | if let Some(old) = self.segment() { | ||
214 | return replace_children( | ||
215 | self, | ||
216 | single_node(old.syntax().clone()), | ||
217 | iter::once(segment.syntax().clone().into()), | ||
218 | ); | ||
219 | } | ||
220 | self.clone() | ||
221 | } | ||
222 | } | ||
223 | |||
224 | impl ast::PathSegment { | ||
225 | #[must_use] | ||
226 | pub fn with_type_args(&self, type_args: ast::TypeArgList) -> ast::PathSegment { | ||
227 | self._with_type_args(type_args, false) | ||
228 | } | ||
229 | |||
230 | #[must_use] | ||
231 | pub fn with_turbo_fish(&self, type_args: ast::TypeArgList) -> ast::PathSegment { | ||
232 | self._with_type_args(type_args, true) | ||
233 | } | ||
234 | |||
235 | fn _with_type_args(&self, type_args: ast::TypeArgList, turbo: bool) -> ast::PathSegment { | ||
236 | if let Some(old) = self.type_arg_list() { | ||
237 | return replace_children( | ||
238 | self, | ||
239 | single_node(old.syntax().clone()), | ||
240 | iter::once(type_args.syntax().clone().into()), | ||
241 | ); | ||
242 | } | ||
243 | let mut to_insert: ArrayVec<[SyntaxElement; 2]> = ArrayVec::new(); | ||
244 | if turbo { | ||
245 | to_insert.push(make::token(T![::]).into()); | ||
246 | } | ||
247 | to_insert.push(type_args.syntax().clone().into()); | ||
248 | insert_children(self, InsertPosition::Last, to_insert) | ||
249 | } | ||
250 | } | ||
251 | |||
210 | #[must_use] | 252 | #[must_use] |
211 | pub fn strip_attrs_and_docs<N: ast::AttrsOwner>(node: &N) -> N { | 253 | pub fn strip_attrs_and_docs<N: ast::AttrsOwner>(node: &N) -> N { |
212 | N::cast(strip_attrs_and_docs_inner(node.syntax().clone())).unwrap() | 254 | N::cast(strip_attrs_and_docs_inner(node.syntax().clone())).unwrap() |
diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 4a79d0dec..36e648180 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs | |||
@@ -2,7 +2,7 @@ | |||
2 | //! of smaller pieces. | 2 | //! of smaller pieces. |
3 | use itertools::Itertools; | 3 | use itertools::Itertools; |
4 | 4 | ||
5 | use crate::{algo, ast, AstNode, SourceFile, SyntaxKind, SyntaxToken}; | 5 | use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxToken}; |
6 | 6 | ||
7 | pub fn name(text: &str) -> ast::Name { | 7 | pub fn name(text: &str) -> ast::Name { |
8 | ast_from_text(&format!("mod {};", text)) | 8 | ast_from_text(&format!("mod {};", text)) |
@@ -21,20 +21,6 @@ pub fn path_qualified(qual: ast::Path, name_ref: ast::NameRef) -> ast::Path { | |||
21 | fn path_from_text(text: &str) -> ast::Path { | 21 | fn path_from_text(text: &str) -> ast::Path { |
22 | ast_from_text(text) | 22 | ast_from_text(text) |
23 | } | 23 | } |
24 | pub fn path_with_type_arg_list(path: ast::Path, args: Option<ast::TypeArgList>) -> ast::Path { | ||
25 | if let Some(args) = args { | ||
26 | let syntax = path.syntax(); | ||
27 | // FIXME: remove existing type args | ||
28 | let new_syntax = algo::insert_children( | ||
29 | syntax, | ||
30 | crate::algo::InsertPosition::Last, | ||
31 | &mut Some(args).into_iter().map(|n| n.syntax().clone().into()), | ||
32 | ); | ||
33 | ast::Path::cast(new_syntax).unwrap() | ||
34 | } else { | ||
35 | path | ||
36 | } | ||
37 | } | ||
38 | 24 | ||
39 | pub fn record_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField { | 25 | pub fn record_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField { |
40 | return match expr { | 26 | return match expr { |
@@ -201,7 +187,7 @@ pub mod tokens { | |||
201 | use once_cell::sync::Lazy; | 187 | use once_cell::sync::Lazy; |
202 | 188 | ||
203 | pub(super) static SOURCE_FILE: Lazy<Parse<SourceFile>> = | 189 | pub(super) static SOURCE_FILE: Lazy<Parse<SourceFile>> = |
204 | Lazy::new(|| SourceFile::parse("const C: () = (1 != 1, 2 == 2)\n;")); | 190 | Lazy::new(|| SourceFile::parse("const C: <()>::Item = (1 != 1, 2 == 2)\n;")); |
205 | 191 | ||
206 | pub fn comma() -> SyntaxToken { | 192 | pub fn comma() -> SyntaxToken { |
207 | SOURCE_FILE | 193 | SOURCE_FILE |