aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/assists
diff options
context:
space:
mode:
authorAlexander Andreev <[email protected]>2019-09-30 10:07:26 +0100
committerAlexander Andreev <[email protected]>2019-09-30 10:07:26 +0100
commit81efd696ccb369d6c4813bad1107bfd9e270385b (patch)
treef91b652c3ee8e4503c146bf4f76f129fb9ef1428 /crates/ra_assists/src/assists
parentfdbd6bb11a0c47bf9ba1428e6bd432cd2ce72045 (diff)
parent733f1d8b709788225bd06f8c0aee1819db92620b (diff)
Merge branch 'master' into feature/issue/1856
# Conflicts: # crates/ra_assists/src/ast_editor.rs
Diffstat (limited to 'crates/ra_assists/src/assists')
-rw-r--r--crates/ra_assists/src/assists/add_derive.rs2
-rw-r--r--crates/ra_assists/src/assists/add_missing_impl_members.rs36
-rw-r--r--crates/ra_assists/src/assists/move_bounds.rs12
3 files changed, 22 insertions, 28 deletions
diff --git a/crates/ra_assists/src/assists/add_derive.rs b/crates/ra_assists/src/assists/add_derive.rs
index 45814838f..77ecc33c9 100644
--- a/crates/ra_assists/src/assists/add_derive.rs
+++ b/crates/ra_assists/src/assists/add_derive.rs
@@ -15,7 +15,7 @@ pub(crate) fn add_derive(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist>
15 ctx.add_action(AssistId("add_derive"), "add `#[derive]`", |edit| { 15 ctx.add_action(AssistId("add_derive"), "add `#[derive]`", |edit| {
16 let derive_attr = nominal 16 let derive_attr = nominal
17 .attrs() 17 .attrs()
18 .filter_map(|x| x.as_call()) 18 .filter_map(|x| x.as_simple_call())
19 .filter(|(name, _arg)| name == "derive") 19 .filter(|(name, _arg)| name == "derive")
20 .map(|(_name, arg)| arg) 20 .map(|(_name, arg)| arg)
21 .next(); 21 .next();
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 9edf1b21a..565b96fb5 100644
--- a/crates/ra_assists/src/assists/add_missing_impl_members.rs
+++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs
@@ -2,11 +2,11 @@
2 2
3use hir::{db::HirDatabase, HasSource}; 3use hir::{db::HirDatabase, HasSource};
4use ra_syntax::{ 4use ra_syntax::{
5 ast::{self, make, AstNode, NameOwner}, 5 ast::{self, edit, make, AstNode, NameOwner},
6 SmolStr, 6 SmolStr,
7}; 7};
8 8
9use crate::{ast_editor::AstEditor, Assist, AssistCtx, AssistId}; 9use crate::{Assist, AssistCtx, AssistId};
10 10
11#[derive(PartialEq)] 11#[derive(PartialEq)]
12enum AddMissingImplMembersMode { 12enum AddMissingImplMembersMode {
@@ -77,30 +77,26 @@ fn add_missing_impl_members_inner(
77 77
78 ctx.add_action(AssistId(assist_id), label, |edit| { 78 ctx.add_action(AssistId(assist_id), label, |edit| {
79 let n_existing_items = impl_item_list.impl_items().count(); 79 let n_existing_items = impl_item_list.impl_items().count();
80 let items = missing_items.into_iter().map(|it| match it { 80 let items = missing_items
81 ast::ImplItem::FnDef(def) => strip_docstring(add_body(def).into()), 81 .into_iter()
82 _ => strip_docstring(it), 82 .map(|it| match it {
83 }); 83 ast::ImplItem::FnDef(def) => ast::ImplItem::FnDef(add_body(def)),
84 let mut ast_editor = AstEditor::new(impl_item_list); 84 _ => it,
85 85 })
86 ast_editor.append_items(items); 86 .map(|it| edit::strip_attrs_and_docs(&it));
87 87 let new_impl_item_list = impl_item_list.append_items(items);
88 let first_new_item = ast_editor.ast().impl_items().nth(n_existing_items).unwrap(); 88 let cursor_position = {
89 let cursor_position = first_new_item.syntax().text_range().start(); 89 let first_new_item = new_impl_item_list.impl_items().nth(n_existing_items).unwrap();
90 ast_editor.into_text_edit(edit.text_edit_builder()); 90 first_new_item.syntax().text_range().start()
91 91 };
92
93 edit.replace_ast(impl_item_list, new_impl_item_list);
92 edit.set_cursor(cursor_position); 94 edit.set_cursor(cursor_position);
93 }); 95 });
94 96
95 ctx.build() 97 ctx.build()
96} 98}
97 99
98fn strip_docstring(item: ast::ImplItem) -> ast::ImplItem {
99 let mut ast_editor = AstEditor::new(item);
100 ast_editor.strip_attrs_and_docs();
101 ast_editor.ast().to_owned()
102}
103
104fn add_body(fn_def: ast::FnDef) -> ast::FnDef { 100fn add_body(fn_def: ast::FnDef) -> ast::FnDef {
105 if fn_def.body().is_none() { 101 if fn_def.body().is_none() {
106 fn_def.with_body(make::block_from_expr(make::expr_unimplemented())) 102 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 32fab03c9..f791d22b0 100644
--- a/crates/ra_assists/src/assists/move_bounds.rs
+++ b/crates/ra_assists/src/assists/move_bounds.rs
@@ -2,12 +2,12 @@
2 2
3use hir::db::HirDatabase; 3use hir::db::HirDatabase;
4use ra_syntax::{ 4use ra_syntax::{
5 ast::{self, make, AstNode, NameOwner, TypeBoundsOwner}, 5 ast::{self, edit, make, AstNode, NameOwner, TypeBoundsOwner},
6 SyntaxElement, 6 SyntaxElement,
7 SyntaxKind::*, 7 SyntaxKind::*,
8}; 8};
9 9
10use crate::{ast_editor::AstEditor, Assist, AssistCtx, AssistId}; 10use crate::{Assist, AssistCtx, AssistId};
11 11
12pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 12pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
13 let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?; 13 let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?;
@@ -41,14 +41,12 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>)
41 .type_params() 41 .type_params()
42 .filter(|it| it.type_bound_list().is_some()) 42 .filter(|it| it.type_bound_list().is_some())
43 .map(|type_param| { 43 .map(|type_param| {
44 let without_bounds = 44 let without_bounds = type_param.remove_bounds();
45 AstEditor::new(type_param.clone()).remove_bounds().ast().clone();
46 (type_param, without_bounds) 45 (type_param, without_bounds)
47 }); 46 });
48 47
49 let mut ast_editor = AstEditor::new(type_param_list.clone()); 48 let new_type_param_list = edit::replace_descendants(&type_param_list, new_params);
50 ast_editor.replace_descendants(new_params); 49 edit.replace_ast(type_param_list.clone(), new_type_param_list);
51 ast_editor.into_text_edit(edit.text_edit_builder());
52 50
53 let where_clause = { 51 let where_clause = {
54 let predicates = type_param_list.type_params().filter_map(build_predicate); 52 let predicates = type_param_list.type_params().filter_map(build_predicate);