Skip to content

Commit b98c16a

Browse files
committed
Categorize assists
1 parent 4cb8bf0 commit b98c16a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+721
-506
lines changed

crates/ra_assists/src/assist_context.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use ra_text_edit::TextEditBuilder;
1919

2020
use crate::{
2121
assist_config::{AssistConfig, SnippetCap},
22-
Assist, AssistId, GroupLabel, ResolvedAssist,
22+
Assist, AssistId, AssistKind, GroupLabel, ResolvedAssist,
2323
};
2424

2525
/// `AssistContext` allows to apply an assist or check if it could be applied.
@@ -135,22 +135,24 @@ impl Assists {
135135
pub(crate) fn add(
136136
&mut self,
137137
id: AssistId,
138+
kind: AssistKind,
138139
label: impl Into<String>,
139140
target: TextRange,
140141
f: impl FnOnce(&mut AssistBuilder),
141142
) -> Option<()> {
142-
let label = Assist::new(id, label.into(), None, target);
143+
let label = Assist::new(id, kind, label.into(), None, target);
143144
self.add_impl(label, f)
144145
}
145146
pub(crate) fn add_group(
146147
&mut self,
147148
group: &GroupLabel,
148149
id: AssistId,
150+
kind: AssistKind,
149151
label: impl Into<String>,
150152
target: TextRange,
151153
f: impl FnOnce(&mut AssistBuilder),
152154
) -> Option<()> {
153-
let label = Assist::new(id, label.into(), Some(group.clone()), target);
155+
let label = Assist::new(id, kind, label.into(), Some(group.clone()), target);
154156
self.add_impl(label, f)
155157
}
156158
fn add_impl(&mut self, label: Assist, f: impl FnOnce(&mut AssistBuilder)) -> Option<()> {

crates/ra_assists/src/handlers/add_custom_impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use stdx::SepBy;
88

99
use crate::{
1010
assist_context::{AssistContext, Assists},
11-
AssistId,
11+
AssistId, AssistKind,
1212
};
1313

1414
// Assist: add_custom_impl
@@ -52,7 +52,7 @@ pub(crate) fn add_custom_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<
5252
format!("Add custom impl `{}` for `{}`", trait_token.text().as_str(), annotated_name);
5353

5454
let target = attr.syntax().text_range();
55-
acc.add(AssistId("add_custom_impl"), label, target, |builder| {
55+
acc.add(AssistId("add_custom_impl"), AssistKind::Refactor, label, target, |builder| {
5656
let new_attr_input = input
5757
.syntax()
5858
.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
@@ -4,7 +4,7 @@ use ra_syntax::{
44
TextSize,
55
};
66

7-
use crate::{AssistContext, AssistId, Assists};
7+
use crate::{AssistContext, AssistId, AssistKind, Assists};
88

99
// Assist: add_derive
1010
//
@@ -29,7 +29,7 @@ pub(crate) fn add_derive(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
2929
let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?;
3030
let node_start = derive_insertion_offset(&nominal)?;
3131
let target = nominal.syntax().text_range();
32-
acc.add(AssistId("add_derive"), "Add `#[derive]`", target, |builder| {
32+
acc.add(AssistId("add_derive"), AssistKind::Refactor, "Add `#[derive]`", target, |builder| {
3333
let derive_attr = nominal
3434
.attrs()
3535
.filter_map(|x| x.as_simple_call())

crates/ra_assists/src/handlers/add_explicit_type.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ra_syntax::{
44
TextRange,
55
};
66

7-
use crate::{AssistContext, AssistId, Assists};
7+
use crate::{AssistContext, AssistId, AssistKind, Assists};
88

99
// Assist: add_explicit_type
1010
//
@@ -60,6 +60,7 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio
6060
let inferred_type = ty.display_source_code(ctx.db(), module.into()).ok()?;
6161
acc.add(
6262
AssistId("add_explicit_type"),
63+
AssistKind::RefactorRewrite,
6364
format!("Insert explicit type `{}`", inferred_type),
6465
pat_range,
6566
|builder| match ascribed_ty {

crates/ra_assists/src/handlers/add_from_impl_for_enum.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use ra_ide_db::RootDatabase;
22
use ra_syntax::ast::{self, AstNode, NameOwner};
33
use test_utils::mark;
44

5-
use crate::{utils::FamousDefs, AssistContext, AssistId, Assists};
5+
use crate::{utils::FamousDefs, AssistContext, AssistId, AssistKind, Assists};
66

77
// Assist: add_from_impl_for_enum
88
//
@@ -46,6 +46,7 @@ pub(crate) fn add_from_impl_for_enum(acc: &mut Assists, ctx: &AssistContext) ->
4646
let target = variant.syntax().text_range();
4747
acc.add(
4848
AssistId("add_from_impl_for_enum"),
49+
AssistKind::Refactor,
4950
"Add From impl for this enum variant",
5051
target,
5152
|edit| {

crates/ra_assists/src/handlers/add_function.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
1313
use crate::{
1414
assist_config::SnippetCap,
1515
utils::{render_snippet, Cursor},
16-
AssistContext, AssistId, Assists,
16+
AssistContext, AssistId, AssistKind, Assists,
1717
};
1818

1919
// Assist: add_function
@@ -62,15 +62,21 @@ pub(crate) fn add_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
6262
let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?;
6363

6464
let target = call.syntax().text_range();
65-
acc.add(AssistId("add_function"), "Add function", target, |builder| {
66-
let function_template = function_builder.render();
67-
builder.edit_file(function_template.file);
68-
let new_fn = function_template.to_string(ctx.config.snippet_cap);
69-
match ctx.config.snippet_cap {
70-
Some(cap) => builder.insert_snippet(cap, function_template.insert_offset, new_fn),
71-
None => builder.insert(function_template.insert_offset, new_fn),
72-
}
73-
})
65+
acc.add(
66+
AssistId("add_function"),
67+
AssistKind::RefactorExtract,
68+
"Add function",
69+
target,
70+
|builder| {
71+
let function_template = function_builder.render();
72+
builder.edit_file(function_template.file);
73+
let new_fn = function_template.to_string(ctx.config.snippet_cap);
74+
match ctx.config.snippet_cap {
75+
Some(cap) => builder.insert_snippet(cap, function_template.insert_offset, new_fn),
76+
None => builder.insert(function_template.insert_offset, new_fn),
77+
}
78+
},
79+
)
7480
}
7581

7682
struct FunctionTemplate {

crates/ra_assists/src/handlers/add_impl.rs

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use ra_syntax::ast::{self, AstNode, NameOwner, TypeParamsOwner};
22
use stdx::{format_to, SepBy};
33

4-
use crate::{AssistContext, AssistId, Assists};
4+
use crate::{AssistContext, AssistId, AssistKind, Assists};
55

66
// Assist: add_impl
77
//
@@ -26,38 +26,46 @@ pub(crate) fn add_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
2626
let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?;
2727
let name = nominal.name()?;
2828
let target = nominal.syntax().text_range();
29-
acc.add(AssistId("add_impl"), format!("Implement {}", name.text().as_str()), target, |edit| {
30-
let type_params = nominal.type_param_list();
31-
let start_offset = nominal.syntax().text_range().end();
32-
let mut buf = String::new();
33-
buf.push_str("\n\nimpl");
34-
if let Some(type_params) = &type_params {
35-
format_to!(buf, "{}", type_params.syntax());
36-
}
37-
buf.push_str(" ");
38-
buf.push_str(name.text().as_str());
39-
if let Some(type_params) = type_params {
40-
let lifetime_params = type_params
41-
.lifetime_params()
42-
.filter_map(|it| it.lifetime_token())
43-
.map(|it| it.text().clone());
44-
let type_params =
45-
type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone());
29+
acc.add(
30+
AssistId("add_impl"),
31+
AssistKind::Refactor,
32+
format!("Implement {}", name.text().as_str()),
33+
target,
34+
|edit| {
35+
let type_params = nominal.type_param_list();
36+
let start_offset = nominal.syntax().text_range().end();
37+
let mut buf = String::new();
38+
buf.push_str("\n\nimpl");
39+
if let Some(type_params) = &type_params {
40+
format_to!(buf, "{}", type_params.syntax());
41+
}
42+
buf.push_str(" ");
43+
buf.push_str(name.text().as_str());
44+
if let Some(type_params) = type_params {
45+
let lifetime_params = type_params
46+
.lifetime_params()
47+
.filter_map(|it| it.lifetime_token())
48+
.map(|it| it.text().clone());
49+
let type_params = type_params
50+
.type_params()
51+
.filter_map(|it| it.name())
52+
.map(|it| it.text().clone());
4653

47-
let generic_params = lifetime_params.chain(type_params).sep_by(", ");
48-
format_to!(buf, "<{}>", generic_params)
49-
}
50-
match ctx.config.snippet_cap {
51-
Some(cap) => {
52-
buf.push_str(" {\n $0\n}");
53-
edit.insert_snippet(cap, start_offset, buf);
54+
let generic_params = lifetime_params.chain(type_params).sep_by(", ");
55+
format_to!(buf, "<{}>", generic_params)
5456
}
55-
None => {
56-
buf.push_str(" {\n}");
57-
edit.insert(start_offset, buf);
57+
match ctx.config.snippet_cap {
58+
Some(cap) => {
59+
buf.push_str(" {\n $0\n}");
60+
edit.insert_snippet(cap, start_offset, buf);
61+
}
62+
None => {
63+
buf.push_str(" {\n}");
64+
edit.insert(start_offset, buf);
65+
}
5866
}
59-
}
60-
})
67+
},
68+
)
6169
}
6270

6371
#[cfg(test)]

crates/ra_assists/src/handlers/add_missing_impl_members.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
assist_context::{AssistContext, Assists},
1313
ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams},
1414
utils::{get_missing_assoc_items, render_snippet, resolve_target_trait, Cursor},
15-
AssistId,
15+
AssistId, AssistKind,
1616
};
1717

1818
#[derive(PartialEq)]
@@ -147,7 +147,7 @@ fn add_missing_impl_members_inner(
147147
}
148148

149149
let target = impl_def.syntax().text_range();
150-
acc.add(AssistId(assist_id), label, target, |builder| {
150+
acc.add(AssistId(assist_id), AssistKind::QuickFix, label, target, |builder| {
151151
let n_existing_items = impl_item_list.assoc_items().count();
152152
let source_scope = ctx.sema.scope_for_def(trait_);
153153
let target_scope = ctx.sema.scope(impl_item_list.syntax());

crates/ra_assists/src/handlers/add_new.rs

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use ra_syntax::{
77
};
88
use stdx::{format_to, SepBy};
99

10-
use crate::{AssistContext, AssistId, Assists};
10+
use crate::{AssistContext, AssistId, AssistKind, Assists};
1111

1212
// Assist: add_new
1313
//
@@ -42,50 +42,56 @@ pub(crate) fn add_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
4242
let impl_def = find_struct_impl(&ctx, &strukt)?;
4343

4444
let target = strukt.syntax().text_range();
45-
acc.add(AssistId("add_new"), "Add default constructor", target, |builder| {
46-
let mut buf = String::with_capacity(512);
47-
48-
if impl_def.is_some() {
49-
buf.push('\n');
50-
}
51-
52-
let vis = strukt.visibility().map_or(String::new(), |v| format!("{} ", v));
53-
54-
let params = field_list
55-
.fields()
56-
.filter_map(|f| {
57-
Some(format!("{}: {}", f.name()?.syntax(), f.ascribed_type()?.syntax()))
58-
})
59-
.sep_by(", ");
60-
let fields = field_list.fields().filter_map(|f| f.name()).sep_by(", ");
61-
62-
format_to!(buf, " {}fn new({}) -> Self {{ Self {{ {} }} }}", vis, params, fields);
63-
64-
let start_offset = impl_def
65-
.and_then(|impl_def| {
45+
acc.add(
46+
AssistId("add_new"),
47+
AssistKind::Refactor,
48+
"Add default constructor",
49+
target,
50+
|builder| {
51+
let mut buf = String::with_capacity(512);
52+
53+
if impl_def.is_some() {
6654
buf.push('\n');
67-
let start = impl_def
68-
.syntax()
69-
.descendants_with_tokens()
70-
.find(|t| t.kind() == T!['{'])?
71-
.text_range()
72-
.end();
73-
74-
Some(start)
75-
})
76-
.unwrap_or_else(|| {
77-
buf = generate_impl_text(&strukt, &buf);
78-
strukt.syntax().text_range().end()
79-
});
80-
81-
match ctx.config.snippet_cap {
82-
None => builder.insert(start_offset, buf),
83-
Some(cap) => {
84-
buf = buf.replace("fn new", "fn $0new");
85-
builder.insert_snippet(cap, start_offset, buf);
8655
}
87-
}
88-
})
56+
57+
let vis = strukt.visibility().map_or(String::new(), |v| format!("{} ", v));
58+
59+
let params = field_list
60+
.fields()
61+
.filter_map(|f| {
62+
Some(format!("{}: {}", f.name()?.syntax(), f.ascribed_type()?.syntax()))
63+
})
64+
.sep_by(", ");
65+
let fields = field_list.fields().filter_map(|f| f.name()).sep_by(", ");
66+
67+
format_to!(buf, " {}fn new({}) -> Self {{ Self {{ {} }} }}", vis, params, fields);
68+
69+
let start_offset = impl_def
70+
.and_then(|impl_def| {
71+
buf.push('\n');
72+
let start = impl_def
73+
.syntax()
74+
.descendants_with_tokens()
75+
.find(|t| t.kind() == T!['{'])?
76+
.text_range()
77+
.end();
78+
79+
Some(start)
80+
})
81+
.unwrap_or_else(|| {
82+
buf = generate_impl_text(&strukt, &buf);
83+
strukt.syntax().text_range().end()
84+
});
85+
86+
match ctx.config.snippet_cap {
87+
None => builder.insert(start_offset, buf),
88+
Some(cap) => {
89+
buf = buf.replace("fn new", "fn $0new");
90+
builder.insert_snippet(cap, start_offset, buf);
91+
}
92+
}
93+
},
94+
)
8995
}
9096

9197
// Generates the surrounding `impl Type { <code> }` including type and lifetime

crates/ra_assists/src/handlers/add_turbo_fish.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use test_utils::mark;
44

55
use crate::{
66
assist_context::{AssistContext, Assists},
7-
AssistId,
7+
AssistId, AssistKind,
88
};
99

1010
// Assist: add_turbo_fish
@@ -45,12 +45,16 @@ pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext) -> Option<(
4545
mark::hit!(add_turbo_fish_non_generic);
4646
return None;
4747
}
48-
acc.add(AssistId("add_turbo_fish"), "Add `::<>`", ident.text_range(), |builder| {
49-
match ctx.config.snippet_cap {
48+
acc.add(
49+
AssistId("add_turbo_fish"),
50+
AssistKind::RefactorRewrite,
51+
"Add `::<>`",
52+
ident.text_range(),
53+
|builder| match ctx.config.snippet_cap {
5054
Some(cap) => builder.insert_snippet(cap, ident.text_range().end(), "::<${0:_}>"),
5155
None => builder.insert(ident.text_range().end(), "::<_>"),
52-
}
53-
})
56+
},
57+
)
5458
}
5559

5660
#[cfg(test)]

0 commit comments

Comments
 (0)