diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-01-15 17:54:41 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-01-15 17:54:41 +0000 |
commit | 01422cc31d1917aaef4b1f402eda05abfff1e75f (patch) | |
tree | 13315f0f9fdbe1d69d228d745147b28a5edd99ab /crates | |
parent | 2f1df3cd74603bf1ba7f2e1c8833407a176cc66e (diff) | |
parent | ef1326ee19c9ff707dcf96ce289102deedcf4d5f (diff) |
Merge #2856
2856: More orthogonal path editing r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_assists/src/ast_transform.rs | 13 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/edit.rs | 42 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/make.rs | 18 |
3 files changed, 54 insertions, 19 deletions
diff --git a/crates/ra_assists/src/ast_transform.rs b/crates/ra_assists/src/ast_transform.rs index eac2903d1..56b7588ef 100644 --- a/crates/ra_assists/src/ast_transform.rs +++ b/crates/ra_assists/src/ast_transform.rs | |||
@@ -2,7 +2,7 @@ | |||
2 | use rustc_hash::FxHashMap; | 2 | use rustc_hash::FxHashMap; |
3 | 3 | ||
4 | use hir::{db::HirDatabase, InFile, PathResolution}; | 4 | use hir::{db::HirDatabase, InFile, PathResolution}; |
5 | use ra_syntax::ast::{self, make, AstNode}; | 5 | use ra_syntax::ast::{self, AstNode}; |
6 | 6 | ||
7 | pub trait AstTransform<'a> { | 7 | pub trait AstTransform<'a> { |
8 | fn get_substitution( | 8 | fn get_substitution( |
@@ -134,11 +134,18 @@ impl<'a, DB: HirDatabase> QualifyPaths<'a, DB> { | |||
134 | match resolution { | 134 | match resolution { |
135 | PathResolution::Def(def) => { | 135 | PathResolution::Def(def) => { |
136 | let found_path = from.find_use_path(self.db, def)?; | 136 | let found_path = from.find_use_path(self.db, def)?; |
137 | let args = p | 137 | let mut path = path_to_ast(found_path); |
138 | |||
139 | let type_args = p | ||
138 | .segment() | 140 | .segment() |
139 | .and_then(|s| s.type_arg_list()) | 141 | .and_then(|s| s.type_arg_list()) |
140 | .map(|arg_list| apply(self, node.with_value(arg_list))); | 142 | .map(|arg_list| apply(self, node.with_value(arg_list))); |
141 | Some(make::path_with_type_arg_list(path_to_ast(found_path), args).syntax().clone()) | 143 | if let Some(type_args) = type_args { |
144 | let last_segment = path.segment().unwrap(); | ||
145 | path = path.with_segment(last_segment.with_type_args(type_args)) | ||
146 | } | ||
147 | |||
148 | Some(path.syntax().clone()) | ||
142 | } | 149 | } |
143 | PathResolution::Local(_) | 150 | PathResolution::Local(_) |
144 | | PathResolution::TypeParam(_) | 151 | | PathResolution::TypeParam(_) |
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 |