From 1116c9a0e9992d0dea8dac87de95c8a74c093cff Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 6 May 2020 13:08:37 +0200 Subject: Fix TODO --- crates/ra_assists/src/assist_ctx.rs | 8 ++++---- crates/ra_assists/src/handlers/add_function.rs | 24 +++++++++--------------- crates/ra_text_edit/src/lib.rs | 4 ++++ 3 files changed, 17 insertions(+), 19 deletions(-) (limited to 'crates') 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> { } fn build(self) -> AssistAction { - AssistAction { - edit: self.edit.finish(), - cursor_position: self.cursor_position, - file: self.file, + let edit = self.edit.finish(); + if edit.is_empty() && self.cursor_position.is_none() { + panic!("Only call `add_assist` if the assist can be applied") } + AssistAction { edit, cursor_position: self.cursor_position, file: self.file } } } 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 { let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?; let target = call.syntax().text_range(); - // TODO: assert here? ctx.add_assist(AssistId("add_function"), "Add function", target, |edit| { - if let Some(function_template) = function_builder.render() { - edit.set_file(function_template.file); - edit.set_cursor(function_template.cursor_offset); - edit.insert(function_template.insert_offset, function_template.fn_def.to_string()); - } + let function_template = function_builder.render(); + edit.set_file(function_template.file); + edit.set_cursor(function_template.cursor_offset); + edit.insert(function_template.insert_offset, function_template.fn_def.to_string()); }) } @@ -107,7 +105,7 @@ impl FunctionBuilder { Some(Self { target, fn_name, type_params, params, file, needs_pub }) } - fn render(self) -> Option { + fn render(self) -> FunctionTemplate { let placeholder_expr = ast::make::expr_todo(); let fn_body = ast::make::block_expr(vec![], Some(placeholder_expr)); let mut fn_def = ast::make::fn_def(self.fn_name, self.type_params, self.params, fn_body); @@ -133,15 +131,11 @@ impl FunctionBuilder { } }; - let cursor_offset_from_fn_start = fn_def - .syntax() - .descendants() - .find_map(ast::MacroCall::cast)? - .syntax() - .text_range() - .start(); + let placeholder_expr = + fn_def.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); + let cursor_offset_from_fn_start = placeholder_expr.syntax().text_range().start(); let cursor_offset = insert_offset + cursor_offset_from_fn_start; - Some(FunctionTemplate { insert_offset, cursor_offset, fn_def, file: self.file }) + FunctionTemplate { insert_offset, cursor_offset, fn_def, file: self.file } } } 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 { TextEdit { indels } } + pub fn is_empty(&self) -> bool { + self.indels.is_empty() + } + pub fn as_indels(&self) -> &[Indel] { &self.indels } -- cgit v1.2.3