Skip to content

Commit 4de3c3e

Browse files
bors[bot]matklad
andauthored
Merge #4341
4341: Move target to AssistLabel r=matklad a=matklad Target is used for assists sorting, so we need it before we compute the action. bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents ede8906 + 233f01c commit 4de3c3e

36 files changed

+292
-256
lines changed

crates/ra_assists/src/assist_ctx.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,10 @@ impl<'a> AssistCtx<'a> {
9494
self,
9595
id: AssistId,
9696
label: impl Into<String>,
97+
target: TextRange,
9798
f: impl FnOnce(&mut ActionBuilder),
9899
) -> Option<Assist> {
99-
let label = AssistLabel::new(id, label.into(), None);
100+
let label = AssistLabel::new(id, label.into(), None, target);
100101

101102
let mut info = AssistInfo::new(label);
102103
if self.should_compute_edit {
@@ -152,9 +153,10 @@ impl<'a> AssistGroup<'a> {
152153
&mut self,
153154
id: AssistId,
154155
label: impl Into<String>,
156+
target: TextRange,
155157
f: impl FnOnce(&mut ActionBuilder),
156158
) {
157-
let label = AssistLabel::new(id, label.into(), Some(self.group.clone()));
159+
let label = AssistLabel::new(id, label.into(), Some(self.group.clone()), target);
158160

159161
let mut info = AssistInfo::new(label).with_group(self.group.clone());
160162
if self.ctx.should_compute_edit {
@@ -181,7 +183,6 @@ impl<'a> AssistGroup<'a> {
181183
pub(crate) struct ActionBuilder<'a, 'b> {
182184
edit: TextEditBuilder,
183185
cursor_position: Option<TextSize>,
184-
target: Option<TextRange>,
185186
file: AssistFile,
186187
ctx: &'a AssistCtx<'b>,
187188
}
@@ -191,7 +192,6 @@ impl<'a, 'b> ActionBuilder<'a, 'b> {
191192
Self {
192193
edit: TextEditBuilder::default(),
193194
cursor_position: None,
194-
target: None,
195195
file: AssistFile::default(),
196196
ctx,
197197
}
@@ -237,14 +237,6 @@ impl<'a, 'b> ActionBuilder<'a, 'b> {
237237
self.cursor_position = Some(offset)
238238
}
239239

240-
/// Specify that the assist should be active withing the `target` range.
241-
///
242-
/// Target ranges are used to sort assists: the smaller the target range,
243-
/// the more specific assist is, and so it should be sorted first.
244-
pub(crate) fn target(&mut self, target: TextRange) {
245-
self.target = Some(target)
246-
}
247-
248240
/// Get access to the raw `TextEditBuilder`.
249241
pub(crate) fn text_edit_builder(&mut self) -> &mut TextEditBuilder {
250242
&mut self.edit
@@ -267,7 +259,6 @@ impl<'a, 'b> ActionBuilder<'a, 'b> {
267259
AssistAction {
268260
edit: self.edit.finish(),
269261
cursor_position: self.cursor_position,
270-
target: self.target,
271262
file: self.file,
272263
}
273264
}

crates/ra_assists/src/handlers/add_custom_impl.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ pub(crate) fn add_custom_impl(ctx: AssistCtx) -> Option<Assist> {
4848
let label =
4949
format!("Add custom impl '{}' for '{}'", trait_token.text().as_str(), annotated_name);
5050

51-
ctx.add_assist(AssistId("add_custom_impl"), label, |edit| {
52-
edit.target(attr.syntax().text_range());
53-
51+
let target = attr.syntax().text_range();
52+
ctx.add_assist(AssistId("add_custom_impl"), label, target, |edit| {
5453
let new_attr_input = input
5554
.syntax()
5655
.descendants_with_tokens()

crates/ra_assists/src/handlers/add_derive.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ use crate::{Assist, AssistCtx, AssistId};
2727
pub(crate) fn add_derive(ctx: AssistCtx) -> Option<Assist> {
2828
let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?;
2929
let node_start = derive_insertion_offset(&nominal)?;
30-
ctx.add_assist(AssistId("add_derive"), "Add `#[derive]`", |edit| {
30+
let target = nominal.syntax().text_range();
31+
ctx.add_assist(AssistId("add_derive"), "Add `#[derive]`", target, |edit| {
3132
let derive_attr = nominal
3233
.attrs()
3334
.filter_map(|x| x.as_simple_call())
@@ -41,7 +42,6 @@ pub(crate) fn add_derive(ctx: AssistCtx) -> Option<Assist> {
4142
}
4243
Some(tt) => tt.syntax().text_range().end() - TextSize::of(')'),
4344
};
44-
edit.target(nominal.syntax().text_range());
4545
edit.set_cursor(offset)
4646
})
4747
}

crates/ra_assists/src/handlers/add_explicit_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ pub(crate) fn add_explicit_type(ctx: AssistCtx) -> Option<Assist> {
6262
ctx.add_assist(
6363
AssistId("add_explicit_type"),
6464
format!("Insert explicit type '{}'", new_type_string),
65+
pat_range,
6566
|edit| {
66-
edit.target(pat_range);
6767
if let Some(ascribed_ty) = ascribed_ty {
6868
edit.replace(ascribed_ty.syntax().text_range(), new_type_string);
6969
} else {

crates/ra_assists/src/handlers/add_from_impl_for_enum.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ pub(crate) fn add_from_impl_for_enum(ctx: AssistCtx) -> Option<Assist> {
4747
return None;
4848
}
4949

50+
let target = variant.syntax().text_range();
5051
ctx.add_assist(
5152
AssistId("add_from_impl_for_enum"),
5253
"Add From impl for this enum variant",
54+
target,
5355
|edit| {
5456
let start_offset = variant.parent_enum().syntax().text_range().end();
5557
let mut buf = String::new();

crates/ra_assists/src/handlers/add_function.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ pub(crate) fn add_function(ctx: AssistCtx) -> Option<Assist> {
5757

5858
let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?;
5959

60-
ctx.add_assist(AssistId("add_function"), "Add function", |edit| {
61-
edit.target(call.syntax().text_range());
62-
60+
let target = call.syntax().text_range();
61+
// TODO: assert here?
62+
ctx.add_assist(AssistId("add_function"), "Add function", target, |edit| {
6363
if let Some(function_template) = function_builder.render() {
6464
edit.set_file(function_template.file);
6565
edit.set_cursor(function_template.cursor_offset);

crates/ra_assists/src/handlers/add_impl.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,40 @@ use crate::{Assist, AssistCtx, AssistId};
2828
pub(crate) fn add_impl(ctx: AssistCtx) -> Option<Assist> {
2929
let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?;
3030
let name = nominal.name()?;
31-
ctx.add_assist(AssistId("add_impl"), format!("Implement {}", name.text().as_str()), |edit| {
32-
edit.target(nominal.syntax().text_range());
33-
let type_params = nominal.type_param_list();
34-
let start_offset = nominal.syntax().text_range().end();
35-
let mut buf = String::new();
36-
buf.push_str("\n\nimpl");
37-
if let Some(type_params) = &type_params {
38-
format_to!(buf, "{}", type_params.syntax());
39-
}
40-
buf.push_str(" ");
41-
buf.push_str(name.text().as_str());
42-
if let Some(type_params) = type_params {
43-
let lifetime_params = type_params
44-
.lifetime_params()
45-
.filter_map(|it| it.lifetime_token())
46-
.map(|it| it.text().clone());
47-
let type_params =
48-
type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone());
31+
let target = nominal.syntax().text_range();
32+
ctx.add_assist(
33+
AssistId("add_impl"),
34+
format!("Implement {}", name.text().as_str()),
35+
target,
36+
|edit| {
37+
let type_params = nominal.type_param_list();
38+
let start_offset = nominal.syntax().text_range().end();
39+
let mut buf = String::new();
40+
buf.push_str("\n\nimpl");
41+
if let Some(type_params) = &type_params {
42+
format_to!(buf, "{}", type_params.syntax());
43+
}
44+
buf.push_str(" ");
45+
buf.push_str(name.text().as_str());
46+
if let Some(type_params) = type_params {
47+
let lifetime_params = type_params
48+
.lifetime_params()
49+
.filter_map(|it| it.lifetime_token())
50+
.map(|it| it.text().clone());
51+
let type_params = type_params
52+
.type_params()
53+
.filter_map(|it| it.name())
54+
.map(|it| it.text().clone());
4955

50-
let generic_params = lifetime_params.chain(type_params).sep_by(", ");
51-
format_to!(buf, "<{}>", generic_params)
52-
}
53-
buf.push_str(" {\n");
54-
edit.set_cursor(start_offset + TextSize::of(&buf));
55-
buf.push_str("\n}");
56-
edit.insert(start_offset, buf);
57-
})
56+
let generic_params = lifetime_params.chain(type_params).sep_by(", ");
57+
format_to!(buf, "<{}>", generic_params)
58+
}
59+
buf.push_str(" {\n");
60+
edit.set_cursor(start_offset + TextSize::of(&buf));
61+
buf.push_str("\n}");
62+
edit.insert(start_offset, buf);
63+
},
64+
)
5865
}
5966

6067
#[cfg(test)]

crates/ra_assists/src/handlers/add_missing_impl_members.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ fn add_missing_impl_members_inner(
107107
label: &'static str,
108108
) -> Option<Assist> {
109109
let _p = ra_prof::profile("add_missing_impl_members_inner");
110-
let impl_node = ctx.find_node_at_offset::<ast::ImplDef>()?;
111-
let impl_item_list = impl_node.item_list()?;
110+
let impl_def = ctx.find_node_at_offset::<ast::ImplDef>()?;
111+
let impl_item_list = impl_def.item_list()?;
112112

113-
let trait_ = resolve_target_trait(&ctx.sema, &impl_node)?;
113+
let trait_ = resolve_target_trait(&ctx.sema, &impl_def)?;
114114

115115
let def_name = |item: &ast::AssocItem| -> Option<SmolStr> {
116116
match item {
@@ -121,7 +121,7 @@ fn add_missing_impl_members_inner(
121121
.map(|it| it.text().clone())
122122
};
123123

124-
let missing_items = get_missing_assoc_items(&ctx.sema, &impl_node)
124+
let missing_items = get_missing_assoc_items(&ctx.sema, &impl_def)
125125
.iter()
126126
.map(|i| match i {
127127
hir::AssocItem::Function(i) => ast::AssocItem::FnDef(i.source(ctx.db).value),
@@ -143,13 +143,13 @@ fn add_missing_impl_members_inner(
143143
}
144144

145145
let sema = ctx.sema;
146-
147-
ctx.add_assist(AssistId(assist_id), label, |edit| {
146+
let target = impl_def.syntax().text_range();
147+
ctx.add_assist(AssistId(assist_id), label, target, |edit| {
148148
let n_existing_items = impl_item_list.assoc_items().count();
149149
let source_scope = sema.scope_for_def(trait_);
150150
let target_scope = sema.scope(impl_item_list.syntax());
151151
let ast_transform = QualifyPaths::new(&target_scope, &source_scope)
152-
.or(SubstituteTypeParams::for_trait_impl(&source_scope, trait_, impl_node));
152+
.or(SubstituteTypeParams::for_trait_impl(&source_scope, trait_, impl_def));
153153
let items = missing_items
154154
.into_iter()
155155
.map(|it| ast_transform::apply(&*ast_transform, it))

crates/ra_assists/src/handlers/add_new.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ pub(crate) fn add_new(ctx: AssistCtx) -> Option<Assist> {
4141
// Return early if we've found an existing new fn
4242
let impl_def = find_struct_impl(&ctx, &strukt)?;
4343

44-
ctx.add_assist(AssistId("add_new"), "Add default constructor", |edit| {
45-
edit.target(strukt.syntax().text_range());
46-
44+
let target = strukt.syntax().text_range();
45+
ctx.add_assist(AssistId("add_new"), "Add default constructor", target, |edit| {
4746
let mut buf = String::with_capacity(512);
4847

4948
if impl_def.is_some() {

crates/ra_assists/src/handlers/apply_demorgan.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ pub(crate) fn apply_demorgan(ctx: AssistCtx) -> Option<Assist> {
3939
let rhs_range = rhs.syntax().text_range();
4040
let not_rhs = invert_boolean_expression(rhs);
4141

42-
ctx.add_assist(AssistId("apply_demorgan"), "Apply De Morgan's law", |edit| {
43-
edit.target(op_range);
42+
ctx.add_assist(AssistId("apply_demorgan"), "Apply De Morgan's law", op_range, |edit| {
4443
edit.replace(op_range, opposite_op);
4544
edit.replace(lhs_range, format!("!({}", not_lhs.syntax().text()));
4645
edit.replace(rhs_range, format!("{})", not_rhs.syntax().text()));

0 commit comments

Comments
 (0)