aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/assist_ctx.rs8
-rw-r--r--crates/ra_assists/src/handlers/add_function.rs24
-rw-r--r--crates/ra_text_edit/src/lib.rs4
3 files changed, 17 insertions, 19 deletions
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs
index 600e5689c..cbf1963b7 100644
--- a/crates/ra_assists/src/assist_ctx.rs
+++ b/crates/ra_assists/src/assist_ctx.rs
@@ -256,10 +256,10 @@ impl<'a, 'b> ActionBuilder<'a, 'b> {
256 } 256 }
257 257
258 fn build(self) -> AssistAction { 258 fn build(self) -> AssistAction {
259 AssistAction { 259 let edit = self.edit.finish();
260 edit: self.edit.finish(), 260 if edit.is_empty() && self.cursor_position.is_none() {
261 cursor_position: self.cursor_position, 261 panic!("Only call `add_assist` if the assist can be applied")
262 file: self.file,
263 } 262 }
263 AssistAction { edit, cursor_position: self.cursor_position, file: self.file }
264 } 264 }
265} 265}
diff --git a/crates/ra_assists/src/handlers/add_function.rs b/crates/ra_assists/src/handlers/add_function.rs
index 76c0f9783..1d9d4e638 100644
--- a/crates/ra_assists/src/handlers/add_function.rs
+++ b/crates/ra_assists/src/handlers/add_function.rs
@@ -58,13 +58,11 @@ pub(crate) fn add_function(ctx: AssistCtx) -> Option<Assist> {
58 let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?; 58 let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?;
59 59
60 let target = call.syntax().text_range(); 60 let target = call.syntax().text_range();
61 // TODO: assert here?
62 ctx.add_assist(AssistId("add_function"), "Add function", target, |edit| { 61 ctx.add_assist(AssistId("add_function"), "Add function", target, |edit| {
63 if let Some(function_template) = function_builder.render() { 62 let function_template = function_builder.render();
64 edit.set_file(function_template.file); 63 edit.set_file(function_template.file);
65 edit.set_cursor(function_template.cursor_offset); 64 edit.set_cursor(function_template.cursor_offset);
66 edit.insert(function_template.insert_offset, function_template.fn_def.to_string()); 65 edit.insert(function_template.insert_offset, function_template.fn_def.to_string());
67 }
68 }) 66 })
69} 67}
70 68
@@ -107,7 +105,7 @@ impl FunctionBuilder {
107 Some(Self { target, fn_name, type_params, params, file, needs_pub }) 105 Some(Self { target, fn_name, type_params, params, file, needs_pub })
108 } 106 }
109 107
110 fn render(self) -> Option<FunctionTemplate> { 108 fn render(self) -> FunctionTemplate {
111 let placeholder_expr = ast::make::expr_todo(); 109 let placeholder_expr = ast::make::expr_todo();
112 let fn_body = ast::make::block_expr(vec![], Some(placeholder_expr)); 110 let fn_body = ast::make::block_expr(vec![], Some(placeholder_expr));
113 let mut fn_def = ast::make::fn_def(self.fn_name, self.type_params, self.params, fn_body); 111 let mut fn_def = ast::make::fn_def(self.fn_name, self.type_params, self.params, fn_body);
@@ -133,15 +131,11 @@ impl FunctionBuilder {
133 } 131 }
134 }; 132 };
135 133
136 let cursor_offset_from_fn_start = fn_def 134 let placeholder_expr =
137 .syntax() 135 fn_def.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
138 .descendants() 136 let cursor_offset_from_fn_start = placeholder_expr.syntax().text_range().start();
139 .find_map(ast::MacroCall::cast)?
140 .syntax()
141 .text_range()
142 .start();
143 let cursor_offset = insert_offset + cursor_offset_from_fn_start; 137 let cursor_offset = insert_offset + cursor_offset_from_fn_start;
144 Some(FunctionTemplate { insert_offset, cursor_offset, fn_def, file: self.file }) 138 FunctionTemplate { insert_offset, cursor_offset, fn_def, file: self.file }
145 } 139 }
146} 140}
147 141
diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs
index 64b67f2ad..3409713ff 100644
--- a/crates/ra_text_edit/src/lib.rs
+++ b/crates/ra_text_edit/src/lib.rs
@@ -71,6 +71,10 @@ impl TextEdit {
71 TextEdit { indels } 71 TextEdit { indels }
72 } 72 }
73 73
74 pub fn is_empty(&self) -> bool {
75 self.indels.is_empty()
76 }
77
74 pub fn as_indels(&self) -> &[Indel] { 78 pub fn as_indels(&self) -> &[Indel] {
75 &self.indels 79 &self.indels
76 } 80 }