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 @@ pub(crate) fn set_file(&mut self, assist_file: AssistFile) {
}
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 }
}
}

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 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 @@ fn from_call(
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 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 @@ fn render(self) -> Option<FunctionTemplate> {
}
};
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 }
}
}

View File

@ -71,6 +71,10 @@ pub(crate) fn from_indels(mut indels: Vec<Indel>) -> TextEdit {
TextEdit { indels }
}
pub fn is_empty(&self) -> bool {
self.indels.is_empty()
}
pub fn as_indels(&self) -> &[Indel] {
&self.indels
}