aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers/add_missing_impl_members.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-05-07 15:28:47 +0100
committerGitHub <[email protected]>2020-05-07 15:28:47 +0100
commitc7e305731c922a2d32eda89ff22cb636059bc4e7 (patch)
tree4f3ab3a70fbbb901ccec3cd162da00eaa9cbad09 /crates/ra_assists/src/handlers/add_missing_impl_members.rs
parentf4cd75ac06dff7f5a95065a6c3868669e5c2ab27 (diff)
parent4867968d22899395e6551f22641b3617e995140c (diff)
Merge #4350
4350: Refactor assists API to be more convenient for adding new assists r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_assists/src/handlers/add_missing_impl_members.rs')
-rw-r--r--crates/ra_assists/src/handlers/add_missing_impl_members.rs32
1 files changed, 17 insertions, 15 deletions
diff --git a/crates/ra_assists/src/handlers/add_missing_impl_members.rs b/crates/ra_assists/src/handlers/add_missing_impl_members.rs
index 7df786590..3482a75bf 100644
--- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs
+++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs
@@ -9,9 +9,10 @@ use ra_syntax::{
9}; 9};
10 10
11use crate::{ 11use crate::{
12 assist_context::{AssistContext, Assists},
12 ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams}, 13 ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams},
13 utils::{get_missing_assoc_items, resolve_target_trait}, 14 utils::{get_missing_assoc_items, resolve_target_trait},
14 Assist, AssistCtx, AssistId, 15 AssistId,
15}; 16};
16 17
17#[derive(PartialEq)] 18#[derive(PartialEq)]
@@ -50,8 +51,9 @@ enum AddMissingImplMembersMode {
50// 51//
51// } 52// }
52// ``` 53// ```
53pub(crate) fn add_missing_impl_members(ctx: AssistCtx) -> Option<Assist> { 54pub(crate) fn add_missing_impl_members(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
54 add_missing_impl_members_inner( 55 add_missing_impl_members_inner(
56 acc,
55 ctx, 57 ctx,
56 AddMissingImplMembersMode::NoDefaultMethods, 58 AddMissingImplMembersMode::NoDefaultMethods,
57 "add_impl_missing_members", 59 "add_impl_missing_members",
@@ -91,8 +93,9 @@ pub(crate) fn add_missing_impl_members(ctx: AssistCtx) -> Option<Assist> {
91// 93//
92// } 94// }
93// ``` 95// ```
94pub(crate) fn add_missing_default_members(ctx: AssistCtx) -> Option<Assist> { 96pub(crate) fn add_missing_default_members(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
95 add_missing_impl_members_inner( 97 add_missing_impl_members_inner(
98 acc,
96 ctx, 99 ctx,
97 AddMissingImplMembersMode::DefaultMethodsOnly, 100 AddMissingImplMembersMode::DefaultMethodsOnly,
98 "add_impl_default_members", 101 "add_impl_default_members",
@@ -101,11 +104,12 @@ pub(crate) fn add_missing_default_members(ctx: AssistCtx) -> Option<Assist> {
101} 104}
102 105
103fn add_missing_impl_members_inner( 106fn add_missing_impl_members_inner(
104 ctx: AssistCtx, 107 acc: &mut Assists,
108 ctx: &AssistContext,
105 mode: AddMissingImplMembersMode, 109 mode: AddMissingImplMembersMode,
106 assist_id: &'static str, 110 assist_id: &'static str,
107 label: &'static str, 111 label: &'static str,
108) -> Option<Assist> { 112) -> Option<()> {
109 let _p = ra_prof::profile("add_missing_impl_members_inner"); 113 let _p = ra_prof::profile("add_missing_impl_members_inner");
110 let impl_def = ctx.find_node_at_offset::<ast::ImplDef>()?; 114 let impl_def = ctx.find_node_at_offset::<ast::ImplDef>()?;
111 let impl_item_list = impl_def.item_list()?; 115 let impl_item_list = impl_def.item_list()?;
@@ -142,12 +146,11 @@ fn add_missing_impl_members_inner(
142 return None; 146 return None;
143 } 147 }
144 148
145 let sema = ctx.sema;
146 let target = impl_def.syntax().text_range(); 149 let target = impl_def.syntax().text_range();
147 ctx.add_assist(AssistId(assist_id), label, target, |edit| { 150 acc.add(AssistId(assist_id), label, target, |edit| {
148 let n_existing_items = impl_item_list.assoc_items().count(); 151 let n_existing_items = impl_item_list.assoc_items().count();
149 let source_scope = sema.scope_for_def(trait_); 152 let source_scope = ctx.sema.scope_for_def(trait_);
150 let target_scope = sema.scope(impl_item_list.syntax()); 153 let target_scope = ctx.sema.scope(impl_item_list.syntax());
151 let ast_transform = QualifyPaths::new(&target_scope, &source_scope) 154 let ast_transform = QualifyPaths::new(&target_scope, &source_scope)
152 .or(SubstituteTypeParams::for_trait_impl(&source_scope, trait_, impl_def)); 155 .or(SubstituteTypeParams::for_trait_impl(&source_scope, trait_, impl_def));
153 let items = missing_items 156 let items = missing_items
@@ -170,13 +173,12 @@ fn add_missing_impl_members_inner(
170} 173}
171 174
172fn add_body(fn_def: ast::FnDef) -> ast::FnDef { 175fn add_body(fn_def: ast::FnDef) -> ast::FnDef {
173 if fn_def.body().is_none() { 176 if fn_def.body().is_some() {
174 let body = make::block_expr(None, Some(make::expr_todo())); 177 return fn_def;
175 let body = IndentLevel(1).increase_indent(body);
176 fn_def.with_body(body)
177 } else {
178 fn_def
179 } 178 }
179 let body = make::block_expr(None, Some(make::expr_todo()));
180 let body = IndentLevel(1).increase_indent(body);
181 fn_def.with_body(body)
180} 182}
181 183
182#[cfg(test)] 184#[cfg(test)]