Skip to content

Commit 1116c9a

Browse files
committed
Fix TODO
1 parent 4de3c3e commit 1116c9a

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

crates/ra_assists/src/assist_ctx.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,10 @@ impl<'a, 'b> ActionBuilder<'a, 'b> {
256256
}
257257

258258
fn build(self) -> AssistAction {
259-
AssistAction {
260-
edit: self.edit.finish(),
261-
cursor_position: self.cursor_position,
262-
file: self.file,
259+
let edit = self.edit.finish();
260+
if edit.is_empty() && self.cursor_position.is_none() {
261+
panic!("Only call `add_assist` if the assist can be applied")
263262
}
263+
AssistAction { edit, cursor_position: self.cursor_position, file: self.file }
264264
}
265265
}

crates/ra_assists/src/handlers/add_function.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,11 @@ pub(crate) fn add_function(ctx: AssistCtx) -> Option<Assist> {
5858
let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?;
5959

6060
let target = call.syntax().text_range();
61-
// TODO: assert here?
6261
ctx.add_assist(AssistId("add_function"), "Add function", target, |edit| {
63-
if let Some(function_template) = function_builder.render() {
64-
edit.set_file(function_template.file);
65-
edit.set_cursor(function_template.cursor_offset);
66-
edit.insert(function_template.insert_offset, function_template.fn_def.to_string());
67-
}
62+
let function_template = function_builder.render();
63+
edit.set_file(function_template.file);
64+
edit.set_cursor(function_template.cursor_offset);
65+
edit.insert(function_template.insert_offset, function_template.fn_def.to_string());
6866
})
6967
}
7068

@@ -107,7 +105,7 @@ impl FunctionBuilder {
107105
Some(Self { target, fn_name, type_params, params, file, needs_pub })
108106
}
109107

110-
fn render(self) -> Option<FunctionTemplate> {
108+
fn render(self) -> FunctionTemplate {
111109
let placeholder_expr = ast::make::expr_todo();
112110
let fn_body = ast::make::block_expr(vec![], Some(placeholder_expr));
113111
let mut fn_def = ast::make::fn_def(self.fn_name, self.type_params, self.params, fn_body);
@@ -133,15 +131,11 @@ impl FunctionBuilder {
133131
}
134132
};
135133

136-
let cursor_offset_from_fn_start = fn_def
137-
.syntax()
138-
.descendants()
139-
.find_map(ast::MacroCall::cast)?
140-
.syntax()
141-
.text_range()
142-
.start();
134+
let placeholder_expr =
135+
fn_def.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
136+
let cursor_offset_from_fn_start = placeholder_expr.syntax().text_range().start();
143137
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 }
145139
}
146140
}
147141

crates/ra_text_edit/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ impl TextEdit {
7171
TextEdit { indels }
7272
}
7373

74+
pub fn is_empty(&self) -> bool {
75+
self.indels.is_empty()
76+
}
77+
7478
pub fn as_indels(&self) -> &[Indel] {
7579
&self.indels
7680
}

0 commit comments

Comments
 (0)