diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-09-30 08:16:23 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2019-09-30 08:16:23 +0100 |
commit | c913b48928107710d6ec87a455b1ae6891297c2b (patch) | |
tree | b0272446dd887cbfab9168e2a0379de1c6f10a6d /crates/ra_assists/src/assists | |
parent | dbdf0e24d51ce425c0066a76a0efc723e41e5071 (diff) | |
parent | 4acadbdca61e77368061a0c53125e164912ab5d5 (diff) |
Merge #1936
1936: cleanup editor r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_assists/src/assists')
-rw-r--r-- | crates/ra_assists/src/assists/add_missing_impl_members.rs | 36 | ||||
-rw-r--r-- | crates/ra_assists/src/assists/move_bounds.rs | 12 |
2 files changed, 21 insertions, 27 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 682455bce..6fd1c3753 100644 --- a/crates/ra_assists/src/assists/add_missing_impl_members.rs +++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs | |||
@@ -1,10 +1,10 @@ | |||
1 | use hir::{db::HirDatabase, HasSource}; | 1 | use hir::{db::HirDatabase, HasSource}; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | ast::{self, make, AstNode, NameOwner}, | 3 | ast::{self, edit, make, AstNode, NameOwner}, |
4 | SmolStr, | 4 | SmolStr, |
5 | }; | 5 | }; |
6 | 6 | ||
7 | use crate::{ast_editor::AstEditor, Assist, AssistCtx, AssistId}; | 7 | use crate::{Assist, AssistCtx, AssistId}; |
8 | 8 | ||
9 | #[derive(PartialEq)] | 9 | #[derive(PartialEq)] |
10 | enum AddMissingImplMembersMode { | 10 | enum AddMissingImplMembersMode { |
@@ -75,30 +75,26 @@ fn add_missing_impl_members_inner( | |||
75 | 75 | ||
76 | ctx.add_action(AssistId(assist_id), label, |edit| { | 76 | ctx.add_action(AssistId(assist_id), label, |edit| { |
77 | let n_existing_items = impl_item_list.impl_items().count(); | 77 | let n_existing_items = impl_item_list.impl_items().count(); |
78 | let items = missing_items.into_iter().map(|it| match it { | 78 | let items = missing_items |
79 | ast::ImplItem::FnDef(def) => strip_docstring(add_body(def).into()), | 79 | .into_iter() |
80 | _ => strip_docstring(it), | 80 | .map(|it| match it { |
81 | }); | 81 | ast::ImplItem::FnDef(def) => ast::ImplItem::FnDef(add_body(def)), |
82 | let mut ast_editor = AstEditor::new(impl_item_list); | 82 | _ => it, |
83 | 83 | }) | |
84 | ast_editor.append_items(items); | 84 | .map(|it| edit::strip_attrs_and_docs(&it)); |
85 | 85 | let new_impl_item_list = impl_item_list.append_items(items); | |
86 | let first_new_item = ast_editor.ast().impl_items().nth(n_existing_items).unwrap(); | 86 | let cursor_position = { |
87 | let cursor_position = first_new_item.syntax().text_range().start(); | 87 | let first_new_item = new_impl_item_list.impl_items().nth(n_existing_items).unwrap(); |
88 | ast_editor.into_text_edit(edit.text_edit_builder()); | 88 | first_new_item.syntax().text_range().start() |
89 | 89 | }; | |
90 | |||
91 | edit.replace_ast(impl_item_list, new_impl_item_list); | ||
90 | edit.set_cursor(cursor_position); | 92 | edit.set_cursor(cursor_position); |
91 | }); | 93 | }); |
92 | 94 | ||
93 | ctx.build() | 95 | ctx.build() |
94 | } | 96 | } |
95 | 97 | ||
96 | fn strip_docstring(item: ast::ImplItem) -> ast::ImplItem { | ||
97 | let mut ast_editor = AstEditor::new(item); | ||
98 | ast_editor.strip_attrs_and_docs(); | ||
99 | ast_editor.ast().to_owned() | ||
100 | } | ||
101 | |||
102 | fn add_body(fn_def: ast::FnDef) -> ast::FnDef { | 98 | fn add_body(fn_def: ast::FnDef) -> ast::FnDef { |
103 | if fn_def.body().is_none() { | 99 | if fn_def.body().is_none() { |
104 | fn_def.with_body(make::block_from_expr(make::expr_unimplemented())) | 100 | fn_def.with_body(make::block_from_expr(make::expr_unimplemented())) |
diff --git a/crates/ra_assists/src/assists/move_bounds.rs b/crates/ra_assists/src/assists/move_bounds.rs index fd4bdc55c..39ff51233 100644 --- a/crates/ra_assists/src/assists/move_bounds.rs +++ b/crates/ra_assists/src/assists/move_bounds.rs | |||
@@ -1,11 +1,11 @@ | |||
1 | use hir::db::HirDatabase; | 1 | use hir::db::HirDatabase; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | ast::{self, make, AstNode, NameOwner, TypeBoundsOwner}, | 3 | ast::{self, edit, make, AstNode, NameOwner, TypeBoundsOwner}, |
4 | SyntaxElement, | 4 | SyntaxElement, |
5 | SyntaxKind::*, | 5 | SyntaxKind::*, |
6 | }; | 6 | }; |
7 | 7 | ||
8 | use crate::{ast_editor::AstEditor, Assist, AssistCtx, AssistId}; | 8 | use crate::{Assist, AssistCtx, AssistId}; |
9 | 9 | ||
10 | pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 10 | pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
11 | let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?; | 11 | let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?; |
@@ -39,14 +39,12 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) | |||
39 | .type_params() | 39 | .type_params() |
40 | .filter(|it| it.type_bound_list().is_some()) | 40 | .filter(|it| it.type_bound_list().is_some()) |
41 | .map(|type_param| { | 41 | .map(|type_param| { |
42 | let without_bounds = | 42 | let without_bounds = type_param.remove_bounds(); |
43 | AstEditor::new(type_param.clone()).remove_bounds().ast().clone(); | ||
44 | (type_param, without_bounds) | 43 | (type_param, without_bounds) |
45 | }); | 44 | }); |
46 | 45 | ||
47 | let mut ast_editor = AstEditor::new(type_param_list.clone()); | 46 | let new_type_param_list = edit::replace_descendants(&type_param_list, new_params); |
48 | ast_editor.replace_descendants(new_params); | 47 | edit.replace_ast(type_param_list.clone(), new_type_param_list); |
49 | ast_editor.into_text_edit(edit.text_edit_builder()); | ||
50 | 48 | ||
51 | let where_clause = { | 49 | let where_clause = { |
52 | let predicates = type_param_list.type_params().filter_map(build_predicate); | 50 | let predicates = type_param_list.type_params().filter_map(build_predicate); |