This commit is contained in:
Aleksey Kladov 2020-05-06 13:08:37 +02:00
parent 4de3c3eedc
commit 1116c9a0e9
3 changed files with 17 additions and 19 deletions

View file

@ -256,10 +256,10 @@ impl<'a, 'b> ActionBuilder<'a, 'b> {
} }
fn build(self) -> AssistAction { fn build(self) -> AssistAction {
AssistAction { let edit = self.edit.finish();
edit: self.edit.finish(), if edit.is_empty() && self.cursor_position.is_none() {
cursor_position: self.cursor_position, panic!("Only call `add_assist` if the assist can be applied")
file: self.file,
} }
AssistAction { edit, cursor_position: self.cursor_position, file: self.file }
} }
} }

View file

@ -58,13 +58,11 @@ pub(crate) fn add_function(ctx: AssistCtx) -> Option<Assist> {
let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?; let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?;
let target = call.syntax().text_range(); let target = call.syntax().text_range();
// TODO: assert here?
ctx.add_assist(AssistId("add_function"), "Add function", target, |edit| { ctx.add_assist(AssistId("add_function"), "Add function", target, |edit| {
if let Some(function_template) = function_builder.render() { let function_template = function_builder.render();
edit.set_file(function_template.file); edit.set_file(function_template.file);
edit.set_cursor(function_template.cursor_offset); edit.set_cursor(function_template.cursor_offset);
edit.insert(function_template.insert_offset, function_template.fn_def.to_string()); 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 }) Some(Self { target, fn_name, type_params, params, file, needs_pub })
} }
fn render(self) -> Option<FunctionTemplate> { fn render(self) -> FunctionTemplate {
let placeholder_expr = ast::make::expr_todo(); let placeholder_expr = ast::make::expr_todo();
let fn_body = ast::make::block_expr(vec![], Some(placeholder_expr)); 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); 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 let placeholder_expr =
.syntax() fn_def.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
.descendants() let cursor_offset_from_fn_start = placeholder_expr.syntax().text_range().start();
.find_map(ast::MacroCall::cast)?
.syntax()
.text_range()
.start();
let cursor_offset = insert_offset + cursor_offset_from_fn_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 }
} }
} }

View file

@ -71,6 +71,10 @@ impl TextEdit {
TextEdit { indels } TextEdit { indels }
} }
pub fn is_empty(&self) -> bool {
self.indels.is_empty()
}
pub fn as_indels(&self) -> &[Indel] { pub fn as_indels(&self) -> &[Indel] {
&self.indels &self.indels
} }