Skip to content

Commit f51b0cf

Browse files
bors[bot]kjeremy
andauthored
Merge #5116
5116: Categorize assists r=matklad a=kjeremy Categorize assists so that editors can use them. Follows the LSP spec pretty close (and some things may need adjustments) but this populates the Refactor menu in vscode and pushes quickfixes through again. This is a prerequisite to filtering out assists that the client doesn't care about. Fixes #4147 Co-authored-by: Jeremy Kolb <[email protected]> Co-authored-by: kjeremy <[email protected]>
2 parents ef6a6d7 + 4c9347e commit f51b0cf

Some content is hidden

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

43 files changed

+593
-442
lines changed

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::None), "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 & 2 deletions
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
//
@@ -59,7 +59,7 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio
5959

6060
let inferred_type = ty.display_source_code(ctx.db(), module.into()).ok()?;
6161
acc.add(
62-
AssistId("add_explicit_type"),
62+
AssistId("add_explicit_type", AssistKind::RefactorRewrite),
6363
format!("Insert explicit type `{}`", inferred_type),
6464
pat_range,
6565
|builder| match ascribed_ty {

crates/ra_assists/src/handlers/add_from_impl_for_enum.rs

Lines changed: 2 additions & 2 deletions
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
//
@@ -45,7 +45,7 @@ pub(crate) fn add_from_impl_for_enum(acc: &mut Assists, ctx: &AssistContext) ->
4545

4646
let target = variant.syntax().text_range();
4747
acc.add(
48-
AssistId("add_from_impl_for_enum"),
48+
AssistId("add_from_impl_for_enum", AssistKind::Refactor),
4949
"Add From impl for this enum variant",
5050
target,
5151
|edit| {

crates/ra_assists/src/handlers/add_function.rs

Lines changed: 2 additions & 2 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,7 +62,7 @@ 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| {
65+
acc.add(AssistId("add_function", AssistKind::None), "Add function", target, |builder| {
6666
let function_template = function_builder.render();
6767
builder.edit_file(function_template.file);
6868
let new_fn = function_template.to_string(ctx.config.snippet_cap);

crates/ra_assists/src/handlers/add_impl.rs

Lines changed: 37 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,45 @@ 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", AssistKind::Refactor),
31+
format!("Implement {}", name.text().as_str()),
32+
target,
33+
|edit| {
34+
let type_params = nominal.type_param_list();
35+
let start_offset = nominal.syntax().text_range().end();
36+
let mut buf = String::new();
37+
buf.push_str("\n\nimpl");
38+
if let Some(type_params) = &type_params {
39+
format_to!(buf, "{}", type_params.syntax());
40+
}
41+
buf.push_str(" ");
42+
buf.push_str(name.text().as_str());
43+
if let Some(type_params) = type_params {
44+
let lifetime_params = type_params
45+
.lifetime_params()
46+
.filter_map(|it| it.lifetime_token())
47+
.map(|it| it.text().clone());
48+
let type_params = type_params
49+
.type_params()
50+
.filter_map(|it| it.name())
51+
.map(|it| it.text().clone());
4652

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);
53+
let generic_params = lifetime_params.chain(type_params).sep_by(", ");
54+
format_to!(buf, "<{}>", generic_params)
5455
}
55-
None => {
56-
buf.push_str(" {\n}");
57-
edit.insert(start_offset, buf);
56+
match ctx.config.snippet_cap {
57+
Some(cap) => {
58+
buf.push_str(" {\n $0\n}");
59+
edit.insert_snippet(cap, start_offset, buf);
60+
}
61+
None => {
62+
buf.push_str(" {\n}");
63+
edit.insert(start_offset, buf);
64+
}
5865
}
59-
}
60-
})
66+
},
67+
)
6168
}
6269

6370
#[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: 2 additions & 2 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,7 +42,7 @@ 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| {
45+
acc.add(AssistId("add_new", AssistKind::None), "Add default constructor", target, |builder| {
4646
let mut buf = String::with_capacity(512);
4747

4848
if impl_def.is_some() {

crates/ra_assists/src/handlers/add_turbo_fish.rs

Lines changed: 8 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,15 @@ 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", AssistKind::RefactorRewrite),
50+
"Add `::<>`",
51+
ident.text_range(),
52+
|builder| match ctx.config.snippet_cap {
5053
Some(cap) => builder.insert_snippet(cap, ident.text_range().end(), "::<${0:_}>"),
5154
None => builder.insert(ident.text_range().end(), "::<_>"),
52-
}
53-
})
55+
},
56+
)
5457
}
5558

5659
#[cfg(test)]

crates/ra_assists/src/handlers/apply_demorgan.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use ra_syntax::ast::{self, AstNode};
22

3-
use crate::{utils::invert_boolean_expression, AssistContext, AssistId, Assists};
3+
use crate::{utils::invert_boolean_expression, AssistContext, AssistId, AssistKind, Assists};
44

55
// Assist: apply_demorgan
66
//
@@ -39,11 +39,16 @@ pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext) -> Option<(
3939
let rhs_range = rhs.syntax().text_range();
4040
let not_rhs = invert_boolean_expression(rhs);
4141

42-
acc.add(AssistId("apply_demorgan"), "Apply De Morgan's law", op_range, |edit| {
43-
edit.replace(op_range, opposite_op);
44-
edit.replace(lhs_range, format!("!({}", not_lhs.syntax().text()));
45-
edit.replace(rhs_range, format!("{})", not_rhs.syntax().text()));
46-
})
42+
acc.add(
43+
AssistId("apply_demorgan", AssistKind::RefactorRewrite),
44+
"Apply De Morgan's law",
45+
op_range,
46+
|edit| {
47+
edit.replace(op_range, opposite_op);
48+
edit.replace(lhs_range, format!("!({}", not_lhs.syntax().text()));
49+
edit.replace(rhs_range, format!("{})", not_rhs.syntax().text()));
50+
},
51+
)
4752
}
4853

4954
// Return the opposite text for a given logical operator, if it makes sense

crates/ra_assists/src/handlers/auto_import.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use ra_syntax::{
1313
};
1414
use rustc_hash::FxHashSet;
1515

16-
use crate::{utils::insert_use_statement, AssistContext, AssistId, Assists, GroupLabel};
16+
use crate::{
17+
utils::insert_use_statement, AssistContext, AssistId, AssistKind, Assists, GroupLabel,
18+
};
1719

1820
// Assist: auto_import
1921
//
@@ -46,7 +48,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
4648
for import in proposed_imports {
4749
acc.add_group(
4850
&group,
49-
AssistId("auto_import"),
51+
AssistId("auto_import", AssistKind::QuickFix),
5052
format!("Import `{}`", &import),
5153
range,
5254
|builder| {

crates/ra_assists/src/handlers/change_return_type_to_result.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use ra_syntax::{
33
AstNode, SyntaxNode,
44
};
55

6-
use crate::{AssistContext, AssistId, Assists};
6+
use crate::{AssistContext, AssistId, AssistKind, Assists};
77
use test_utils::mark;
88

99
// Assist: change_return_type_to_result
@@ -35,7 +35,7 @@ pub(crate) fn change_return_type_to_result(acc: &mut Assists, ctx: &AssistContex
3535
let block_expr = &fn_def.body()?;
3636

3737
acc.add(
38-
AssistId("change_return_type_to_result"),
38+
AssistId("change_return_type_to_result", AssistKind::RefactorRewrite),
3939
"Change return type to Result",
4040
type_ref.syntax().text_range(),
4141
|builder| {

crates/ra_assists/src/handlers/change_visibility.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use ra_syntax::{
66
};
77
use test_utils::mark;
88

9-
use crate::{utils::vis_offset, AssistContext, AssistId, Assists};
9+
use crate::{utils::vis_offset, AssistContext, AssistId, AssistKind, Assists};
1010

1111
// Assist: change_visibility
1212
//
@@ -62,16 +62,21 @@ fn add_vis(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
6262
return None;
6363
};
6464

65-
acc.add(AssistId("change_visibility"), "Change visibility to pub(crate)", target, |edit| {
66-
edit.insert(offset, "pub(crate) ");
67-
})
65+
acc.add(
66+
AssistId("change_visibility", AssistKind::RefactorRewrite),
67+
"Change visibility to pub(crate)",
68+
target,
69+
|edit| {
70+
edit.insert(offset, "pub(crate) ");
71+
},
72+
)
6873
}
6974

7075
fn change_vis(acc: &mut Assists, vis: ast::Visibility) -> Option<()> {
7176
if vis.syntax().text() == "pub" {
7277
let target = vis.syntax().text_range();
7378
return acc.add(
74-
AssistId("change_visibility"),
79+
AssistId("change_visibility", AssistKind::RefactorRewrite),
7580
"Change Visibility to pub(crate)",
7681
target,
7782
|edit| {
@@ -82,7 +87,7 @@ fn change_vis(acc: &mut Assists, vis: ast::Visibility) -> Option<()> {
8287
if vis.syntax().text() == "pub(crate)" {
8388
let target = vis.syntax().text_range();
8489
return acc.add(
85-
AssistId("change_visibility"),
90+
AssistId("change_visibility", AssistKind::RefactorRewrite),
8691
"Change visibility to pub",
8792
target,
8893
|edit| {

0 commit comments

Comments
 (0)