diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-09-26 20:31:28 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2019-09-26 20:31:28 +0100 |
commit | 1002e470747fe5887a2915689e21d9be3a1ca5d8 (patch) | |
tree | 3fd5903b67b498a39660b2dafdc929f33d41900e /crates/ra_assists | |
parent | 53a30d9e69ee5149e4fdb1c6fe4081281e879d0e (diff) | |
parent | d847d53e36571c8f7925b72cedf66bb203976148 (diff) |
Merge #1921
1921: WIP: start simplifying editing API r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_assists')
-rw-r--r-- | crates/ra_assists/src/assists/add_missing_impl_members.rs | 8 | ||||
-rw-r--r-- | crates/ra_assists/src/assists/auto_import.rs | 1 | ||||
-rw-r--r-- | crates/ra_assists/src/ast_editor.rs | 72 | ||||
-rw-r--r-- | crates/ra_assists/src/lib.rs | 1 |
4 files changed, 4 insertions, 78 deletions
diff --git a/crates/ra_assists/src/assists/add_missing_impl_members.rs b/crates/ra_assists/src/assists/add_missing_impl_members.rs index 23da1e65f..682455bce 100644 --- a/crates/ra_assists/src/assists/add_missing_impl_members.rs +++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs | |||
@@ -100,12 +100,11 @@ fn strip_docstring(item: ast::ImplItem) -> ast::ImplItem { | |||
100 | } | 100 | } |
101 | 101 | ||
102 | fn add_body(fn_def: ast::FnDef) -> ast::FnDef { | 102 | fn add_body(fn_def: ast::FnDef) -> ast::FnDef { |
103 | let mut ast_editor = AstEditor::new(fn_def.clone()); | ||
104 | if fn_def.body().is_none() { | 103 | if fn_def.body().is_none() { |
105 | let body = make::block_from_expr(make::expr_unimplemented()); | 104 | fn_def.with_body(make::block_from_expr(make::expr_unimplemented())) |
106 | ast_editor.set_body(&body); | 105 | } else { |
106 | fn_def | ||
107 | } | 107 | } |
108 | ast_editor.ast().to_owned() | ||
109 | } | 108 | } |
110 | 109 | ||
111 | /// Given an `ast::ImplBlock`, resolves the target trait (the one being | 110 | /// Given an `ast::ImplBlock`, resolves the target trait (the one being |
@@ -332,5 +331,4 @@ impl Foo for S { | |||
332 | }", | 331 | }", |
333 | ) | 332 | ) |
334 | } | 333 | } |
335 | |||
336 | } | 334 | } |
diff --git a/crates/ra_assists/src/assists/auto_import.rs b/crates/ra_assists/src/assists/auto_import.rs index 5aae98546..a91c170b9 100644 --- a/crates/ra_assists/src/assists/auto_import.rs +++ b/crates/ra_assists/src/assists/auto_import.rs | |||
@@ -448,7 +448,6 @@ fn make_assist_add_in_tree_list( | |||
448 | fmt_segments_raw(target, &mut buf); | 448 | fmt_segments_raw(target, &mut buf); |
449 | edit.insert(offset, buf); | 449 | edit.insert(offset, buf); |
450 | } else { | 450 | } else { |
451 | |||
452 | } | 451 | } |
453 | } | 452 | } |
454 | 453 | ||
diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs index 2a685f26e..72c8c478a 100644 --- a/crates/ra_assists/src/ast_editor.rs +++ b/crates/ra_assists/src/ast_editor.rs | |||
@@ -6,7 +6,7 @@ use rustc_hash::FxHashMap; | |||
6 | use ra_fmt::leading_indent; | 6 | use ra_fmt::leading_indent; |
7 | use ra_syntax::{ | 7 | use ra_syntax::{ |
8 | algo, | 8 | algo, |
9 | ast::{self, TypeBoundsOwner}, | 9 | ast::{self, make::tokens, TypeBoundsOwner}, |
10 | AstNode, Direction, InsertPosition, SyntaxElement, | 10 | AstNode, Direction, InsertPosition, SyntaxElement, |
11 | SyntaxKind::*, | 11 | SyntaxKind::*, |
12 | T, | 12 | T, |
@@ -229,26 +229,6 @@ impl AstEditor<ast::ImplItem> { | |||
229 | } | 229 | } |
230 | } | 230 | } |
231 | 231 | ||
232 | impl AstEditor<ast::FnDef> { | ||
233 | pub fn set_body(&mut self, body: &ast::Block) { | ||
234 | let mut to_insert: ArrayVec<[SyntaxElement; 2]> = ArrayVec::new(); | ||
235 | let old_body_or_semi: SyntaxElement = if let Some(old_body) = self.ast().body() { | ||
236 | old_body.syntax().clone().into() | ||
237 | } else if let Some(semi) = self.ast().semicolon_token() { | ||
238 | to_insert.push(tokens::single_space().into()); | ||
239 | semi.into() | ||
240 | } else { | ||
241 | to_insert.push(tokens::single_space().into()); | ||
242 | to_insert.push(body.syntax().clone().into()); | ||
243 | self.ast = self.insert_children(InsertPosition::Last, to_insert.into_iter()); | ||
244 | return; | ||
245 | }; | ||
246 | to_insert.push(body.syntax().clone().into()); | ||
247 | let replace_range = RangeInclusive::new(old_body_or_semi.clone(), old_body_or_semi); | ||
248 | self.ast = self.replace_children(replace_range, to_insert.into_iter()) | ||
249 | } | ||
250 | } | ||
251 | |||
252 | impl AstEditor<ast::TypeParam> { | 232 | impl AstEditor<ast::TypeParam> { |
253 | pub fn remove_bounds(&mut self) -> &mut Self { | 233 | pub fn remove_bounds(&mut self) -> &mut Self { |
254 | let colon = match self.ast.colon_token() { | 234 | let colon = match self.ast.colon_token() { |
@@ -263,53 +243,3 @@ impl AstEditor<ast::TypeParam> { | |||
263 | self | 243 | self |
264 | } | 244 | } |
265 | } | 245 | } |
266 | |||
267 | mod tokens { | ||
268 | use once_cell::sync::Lazy; | ||
269 | use ra_syntax::{AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken, T}; | ||
270 | |||
271 | static SOURCE_FILE: Lazy<Parse<SourceFile>> = Lazy::new(|| SourceFile::parse(",\n; ;")); | ||
272 | |||
273 | pub(crate) fn comma() -> SyntaxToken { | ||
274 | SOURCE_FILE | ||
275 | .tree() | ||
276 | .syntax() | ||
277 | .descendants_with_tokens() | ||
278 | .filter_map(|it| it.into_token()) | ||
279 | .find(|it| it.kind() == T![,]) | ||
280 | .unwrap() | ||
281 | } | ||
282 | |||
283 | pub(crate) fn single_space() -> SyntaxToken { | ||
284 | SOURCE_FILE | ||
285 | .tree() | ||
286 | .syntax() | ||
287 | .descendants_with_tokens() | ||
288 | .filter_map(|it| it.into_token()) | ||
289 | .find(|it| it.kind() == WHITESPACE && it.text().as_str() == " ") | ||
290 | .unwrap() | ||
291 | } | ||
292 | |||
293 | #[allow(unused)] | ||
294 | pub(crate) fn single_newline() -> SyntaxToken { | ||
295 | SOURCE_FILE | ||
296 | .tree() | ||
297 | .syntax() | ||
298 | .descendants_with_tokens() | ||
299 | .filter_map(|it| it.into_token()) | ||
300 | .find(|it| it.kind() == WHITESPACE && it.text().as_str() == "\n") | ||
301 | .unwrap() | ||
302 | } | ||
303 | |||
304 | pub(crate) struct WsBuilder(SourceFile); | ||
305 | |||
306 | impl WsBuilder { | ||
307 | pub(crate) fn new(text: &str) -> WsBuilder { | ||
308 | WsBuilder(SourceFile::parse(text).ok().unwrap()) | ||
309 | } | ||
310 | pub(crate) fn ws(&self) -> SyntaxToken { | ||
311 | self.0.syntax().first_child_or_token().unwrap().into_token().unwrap() | ||
312 | } | ||
313 | } | ||
314 | |||
315 | } | ||
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 897af2b02..3ca3320f7 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -340,5 +340,4 @@ mod tests { | |||
340 | assert_eq!(assists.next().expect("expected assist").0.label, "introduce variable"); | 340 | assert_eq!(assists.next().expect("expected assist").0.label, "introduce variable"); |
341 | assert_eq!(assists.next().expect("expected assist").0.label, "replace with match"); | 341 | assert_eq!(assists.next().expect("expected assist").0.label, "replace with match"); |
342 | } | 342 | } |
343 | |||
344 | } | 343 | } |