mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-04 19:29:07 +00:00
Move AssistKind into AssistId
This commit is contained in:
parent
1d58e16824
commit
36cc81ac71
@ -19,7 +19,7 @@ use ra_text_edit::TextEditBuilder;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
assist_config::{AssistConfig, SnippetCap},
|
assist_config::{AssistConfig, SnippetCap},
|
||||||
Assist, AssistId, AssistKind, GroupLabel, ResolvedAssist,
|
Assist, AssistId, GroupLabel, ResolvedAssist,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// `AssistContext` allows to apply an assist or check if it could be applied.
|
/// `AssistContext` allows to apply an assist or check if it could be applied.
|
||||||
@ -135,24 +135,22 @@ impl Assists {
|
|||||||
pub(crate) fn add(
|
pub(crate) fn add(
|
||||||
&mut self,
|
&mut self,
|
||||||
id: AssistId,
|
id: AssistId,
|
||||||
kind: AssistKind,
|
|
||||||
label: impl Into<String>,
|
label: impl Into<String>,
|
||||||
target: TextRange,
|
target: TextRange,
|
||||||
f: impl FnOnce(&mut AssistBuilder),
|
f: impl FnOnce(&mut AssistBuilder),
|
||||||
) -> Option<()> {
|
) -> Option<()> {
|
||||||
let label = Assist::new(id, kind, label.into(), None, target);
|
let label = Assist::new(id, label.into(), None, target);
|
||||||
self.add_impl(label, f)
|
self.add_impl(label, f)
|
||||||
}
|
}
|
||||||
pub(crate) fn add_group(
|
pub(crate) fn add_group(
|
||||||
&mut self,
|
&mut self,
|
||||||
group: &GroupLabel,
|
group: &GroupLabel,
|
||||||
id: AssistId,
|
id: AssistId,
|
||||||
kind: AssistKind,
|
|
||||||
label: impl Into<String>,
|
label: impl Into<String>,
|
||||||
target: TextRange,
|
target: TextRange,
|
||||||
f: impl FnOnce(&mut AssistBuilder),
|
f: impl FnOnce(&mut AssistBuilder),
|
||||||
) -> Option<()> {
|
) -> Option<()> {
|
||||||
let label = Assist::new(id, kind, label.into(), Some(group.clone()), target);
|
let label = Assist::new(id, label.into(), Some(group.clone()), target);
|
||||||
self.add_impl(label, f)
|
self.add_impl(label, f)
|
||||||
}
|
}
|
||||||
fn add_impl(&mut self, label: Assist, f: impl FnOnce(&mut AssistBuilder)) -> Option<()> {
|
fn add_impl(&mut self, label: Assist, f: impl FnOnce(&mut AssistBuilder)) -> Option<()> {
|
||||||
|
@ -52,7 +52,7 @@ pub(crate) fn add_custom_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<
|
|||||||
format!("Add custom impl `{}` for `{}`", trait_token.text().as_str(), annotated_name);
|
format!("Add custom impl `{}` for `{}`", trait_token.text().as_str(), annotated_name);
|
||||||
|
|
||||||
let target = attr.syntax().text_range();
|
let target = attr.syntax().text_range();
|
||||||
acc.add(AssistId("add_custom_impl"), AssistKind::Refactor, label, target, |builder| {
|
acc.add(AssistId("add_custom_impl", AssistKind::Refactor), label, target, |builder| {
|
||||||
let new_attr_input = input
|
let new_attr_input = input
|
||||||
.syntax()
|
.syntax()
|
||||||
.descendants_with_tokens()
|
.descendants_with_tokens()
|
||||||
|
@ -29,7 +29,7 @@ pub(crate) fn add_derive(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
|||||||
let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?;
|
let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?;
|
||||||
let node_start = derive_insertion_offset(&nominal)?;
|
let node_start = derive_insertion_offset(&nominal)?;
|
||||||
let target = nominal.syntax().text_range();
|
let target = nominal.syntax().text_range();
|
||||||
acc.add(AssistId("add_derive"), AssistKind::Refactor, "Add `#[derive]`", target, |builder| {
|
acc.add(AssistId("add_derive", AssistKind::Refactor), "Add `#[derive]`", target, |builder| {
|
||||||
let derive_attr = nominal
|
let derive_attr = nominal
|
||||||
.attrs()
|
.attrs()
|
||||||
.filter_map(|x| x.as_simple_call())
|
.filter_map(|x| x.as_simple_call())
|
||||||
|
@ -59,8 +59,7 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio
|
|||||||
|
|
||||||
let inferred_type = ty.display_source_code(ctx.db(), module.into()).ok()?;
|
let inferred_type = ty.display_source_code(ctx.db(), module.into()).ok()?;
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("add_explicit_type"),
|
AssistId("add_explicit_type", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
format!("Insert explicit type `{}`", inferred_type),
|
format!("Insert explicit type `{}`", inferred_type),
|
||||||
pat_range,
|
pat_range,
|
||||||
|builder| match ascribed_ty {
|
|builder| match ascribed_ty {
|
||||||
|
@ -45,8 +45,7 @@ pub(crate) fn add_from_impl_for_enum(acc: &mut Assists, ctx: &AssistContext) ->
|
|||||||
|
|
||||||
let target = variant.syntax().text_range();
|
let target = variant.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("add_from_impl_for_enum"),
|
AssistId("add_from_impl_for_enum", AssistKind::Refactor),
|
||||||
AssistKind::Refactor,
|
|
||||||
"Add From impl for this enum variant",
|
"Add From impl for this enum variant",
|
||||||
target,
|
target,
|
||||||
|edit| {
|
|edit| {
|
||||||
|
@ -63,8 +63,7 @@ pub(crate) fn add_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
|
|||||||
|
|
||||||
let target = call.syntax().text_range();
|
let target = call.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("add_function"),
|
AssistId("add_function", AssistKind::RefactorExtract),
|
||||||
AssistKind::RefactorExtract,
|
|
||||||
"Add function",
|
"Add function",
|
||||||
target,
|
target,
|
||||||
|builder| {
|
|builder| {
|
||||||
|
@ -27,8 +27,7 @@ pub(crate) fn add_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
|||||||
let name = nominal.name()?;
|
let name = nominal.name()?;
|
||||||
let target = nominal.syntax().text_range();
|
let target = nominal.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("add_impl"),
|
AssistId("add_impl", AssistKind::Refactor),
|
||||||
AssistKind::Refactor,
|
|
||||||
format!("Implement {}", name.text().as_str()),
|
format!("Implement {}", name.text().as_str()),
|
||||||
target,
|
target,
|
||||||
|edit| {
|
|edit| {
|
||||||
|
@ -147,7 +147,7 @@ fn add_missing_impl_members_inner(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let target = impl_def.syntax().text_range();
|
let target = impl_def.syntax().text_range();
|
||||||
acc.add(AssistId(assist_id), AssistKind::QuickFix, label, target, |builder| {
|
acc.add(AssistId(assist_id, AssistKind::QuickFix), label, target, |builder| {
|
||||||
let n_existing_items = impl_item_list.assoc_items().count();
|
let n_existing_items = impl_item_list.assoc_items().count();
|
||||||
let source_scope = ctx.sema.scope_for_def(trait_);
|
let source_scope = ctx.sema.scope_for_def(trait_);
|
||||||
let target_scope = ctx.sema.scope(impl_item_list.syntax());
|
let target_scope = ctx.sema.scope(impl_item_list.syntax());
|
||||||
|
@ -43,8 +43,7 @@ pub(crate) fn add_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
|||||||
|
|
||||||
let target = strukt.syntax().text_range();
|
let target = strukt.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("add_new"),
|
AssistId("add_new", AssistKind::Refactor),
|
||||||
AssistKind::Refactor,
|
|
||||||
"Add default constructor",
|
"Add default constructor",
|
||||||
target,
|
target,
|
||||||
|builder| {
|
|builder| {
|
||||||
|
@ -46,8 +46,7 @@ pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext) -> Option<(
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("add_turbo_fish"),
|
AssistId("add_turbo_fish", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Add `::<>`",
|
"Add `::<>`",
|
||||||
ident.text_range(),
|
ident.text_range(),
|
||||||
|builder| match ctx.config.snippet_cap {
|
|builder| match ctx.config.snippet_cap {
|
||||||
|
@ -40,8 +40,7 @@ pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext) -> Option<(
|
|||||||
let not_rhs = invert_boolean_expression(rhs);
|
let not_rhs = invert_boolean_expression(rhs);
|
||||||
|
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("apply_demorgan"),
|
AssistId("apply_demorgan", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Apply De Morgan's law",
|
"Apply De Morgan's law",
|
||||||
op_range,
|
op_range,
|
||||||
|edit| {
|
|edit| {
|
||||||
|
@ -48,8 +48,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
|
|||||||
for import in proposed_imports {
|
for import in proposed_imports {
|
||||||
acc.add_group(
|
acc.add_group(
|
||||||
&group,
|
&group,
|
||||||
AssistId("auto_import"),
|
AssistId("auto_import", AssistKind::QuickFix),
|
||||||
AssistKind::QuickFix,
|
|
||||||
format!("Import `{}`", &import),
|
format!("Import `{}`", &import),
|
||||||
range,
|
range,
|
||||||
|builder| {
|
|builder| {
|
||||||
|
@ -35,8 +35,7 @@ pub(crate) fn change_return_type_to_result(acc: &mut Assists, ctx: &AssistContex
|
|||||||
let block_expr = &fn_def.body()?;
|
let block_expr = &fn_def.body()?;
|
||||||
|
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("change_return_type_to_result"),
|
AssistId("change_return_type_to_result", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Change return type to Result",
|
"Change return type to Result",
|
||||||
type_ref.syntax().text_range(),
|
type_ref.syntax().text_range(),
|
||||||
|builder| {
|
|builder| {
|
||||||
|
@ -63,8 +63,7 @@ fn add_vis(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("change_visibility"),
|
AssistId("change_visibility", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Change visibility to pub(crate)",
|
"Change visibility to pub(crate)",
|
||||||
target,
|
target,
|
||||||
|edit| {
|
|edit| {
|
||||||
@ -77,8 +76,7 @@ fn change_vis(acc: &mut Assists, vis: ast::Visibility) -> Option<()> {
|
|||||||
if vis.syntax().text() == "pub" {
|
if vis.syntax().text() == "pub" {
|
||||||
let target = vis.syntax().text_range();
|
let target = vis.syntax().text_range();
|
||||||
return acc.add(
|
return acc.add(
|
||||||
AssistId("change_visibility"),
|
AssistId("change_visibility", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Change Visibility to pub(crate)",
|
"Change Visibility to pub(crate)",
|
||||||
target,
|
target,
|
||||||
|edit| {
|
|edit| {
|
||||||
@ -89,8 +87,7 @@ fn change_vis(acc: &mut Assists, vis: ast::Visibility) -> Option<()> {
|
|||||||
if vis.syntax().text() == "pub(crate)" {
|
if vis.syntax().text() == "pub(crate)" {
|
||||||
let target = vis.syntax().text_range();
|
let target = vis.syntax().text_range();
|
||||||
return acc.add(
|
return acc.add(
|
||||||
AssistId("change_visibility"),
|
AssistId("change_visibility", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Change visibility to pub",
|
"Change visibility to pub",
|
||||||
target,
|
target,
|
||||||
|edit| {
|
|edit| {
|
||||||
|
@ -100,8 +100,7 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext)
|
|||||||
|
|
||||||
let target = if_expr.syntax().text_range();
|
let target = if_expr.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("convert_to_guarded_return"),
|
AssistId("convert_to_guarded_return", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Convert to guarded return",
|
"Convert to guarded return",
|
||||||
target,
|
target,
|
||||||
|edit| {
|
|edit| {
|
||||||
|
@ -49,8 +49,7 @@ pub(crate) fn extract_struct_from_enum_variant(
|
|||||||
let current_module = enum_hir.module(ctx.db());
|
let current_module = enum_hir.module(ctx.db());
|
||||||
let target = variant.syntax().text_range();
|
let target = variant.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("extract_struct_from_enum_variant"),
|
AssistId("extract_struct_from_enum_variant", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Extract struct from enum variant",
|
"Extract struct from enum variant",
|
||||||
target,
|
target,
|
||||||
|builder| {
|
|builder| {
|
||||||
|
@ -44,8 +44,7 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext) -> Option
|
|||||||
}
|
}
|
||||||
let target = expr.syntax().text_range();
|
let target = expr.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("extract_variable"),
|
AssistId("extract_variable", AssistKind::RefactorExtract),
|
||||||
AssistKind::RefactorExtract,
|
|
||||||
"Extract into variable",
|
"Extract into variable",
|
||||||
target,
|
target,
|
||||||
move |edit| {
|
move |edit| {
|
||||||
|
@ -104,8 +104,7 @@ pub(crate) fn fill_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option<
|
|||||||
|
|
||||||
let target = match_expr.syntax().text_range();
|
let target = match_expr.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("fill_match_arms"),
|
AssistId("fill_match_arms", AssistKind::QuickFix),
|
||||||
AssistKind::QuickFix,
|
|
||||||
"Fill match arms",
|
"Fill match arms",
|
||||||
target,
|
target,
|
||||||
|builder| {
|
|builder| {
|
||||||
|
@ -58,7 +58,7 @@ fn add_vis_to_referenced_module_def(acc: &mut Assists, ctx: &AssistContext) -> O
|
|||||||
Some(name) => format!("Change visibility of {} to {}", name, missing_visibility),
|
Some(name) => format!("Change visibility of {} to {}", name, missing_visibility),
|
||||||
};
|
};
|
||||||
|
|
||||||
acc.add(AssistId("fix_visibility"), AssistKind::QuickFix, assist_label, target, |builder| {
|
acc.add(AssistId("fix_visibility", AssistKind::QuickFix), assist_label, target, |builder| {
|
||||||
builder.edit_file(target_file);
|
builder.edit_file(target_file);
|
||||||
match ctx.config.snippet_cap {
|
match ctx.config.snippet_cap {
|
||||||
Some(cap) => builder.insert_snippet(cap, offset, format!("$0{} ", missing_visibility)),
|
Some(cap) => builder.insert_snippet(cap, offset, format!("$0{} ", missing_visibility)),
|
||||||
@ -101,7 +101,7 @@ fn add_vis_to_referenced_record_field(acc: &mut Assists, ctx: &AssistContext) ->
|
|||||||
let assist_label =
|
let assist_label =
|
||||||
format!("Change visibility of {}.{} to {}", parent_name, target_name, missing_visibility);
|
format!("Change visibility of {}.{} to {}", parent_name, target_name, missing_visibility);
|
||||||
|
|
||||||
acc.add(AssistId("fix_visibility"), AssistKind::QuickFix, assist_label, target, |builder| {
|
acc.add(AssistId("fix_visibility", AssistKind::QuickFix), assist_label, target, |builder| {
|
||||||
builder.edit_file(target_file);
|
builder.edit_file(target_file);
|
||||||
match ctx.config.snippet_cap {
|
match ctx.config.snippet_cap {
|
||||||
Some(cap) => builder.insert_snippet(cap, offset, format!("$0{} ", missing_visibility)),
|
Some(cap) => builder.insert_snippet(cap, offset, format!("$0{} ", missing_visibility)),
|
||||||
|
@ -34,8 +34,7 @@ pub(crate) fn flip_binexpr(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
|
|||||||
}
|
}
|
||||||
|
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("flip_binexpr"),
|
AssistId("flip_binexpr", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Flip binary expression",
|
"Flip binary expression",
|
||||||
op_range,
|
op_range,
|
||||||
|edit| {
|
|edit| {
|
||||||
|
@ -29,8 +29,7 @@ pub(crate) fn flip_comma(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("flip_comma"),
|
AssistId("flip_comma", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Flip comma",
|
"Flip comma",
|
||||||
comma.text_range(),
|
comma.text_range(),
|
||||||
|edit| {
|
|edit| {
|
||||||
|
@ -34,8 +34,7 @@ pub(crate) fn flip_trait_bound(acc: &mut Assists, ctx: &AssistContext) -> Option
|
|||||||
|
|
||||||
let target = plus.text_range();
|
let target = plus.text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("flip_trait_bound"),
|
AssistId("flip_trait_bound", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Flip trait bounds",
|
"Flip trait bounds",
|
||||||
target,
|
target,
|
||||||
|edit| {
|
|edit| {
|
||||||
|
@ -111,8 +111,7 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O
|
|||||||
|
|
||||||
let target = bind_pat.syntax().text_range();
|
let target = bind_pat.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("inline_local_variable"),
|
AssistId("inline_local_variable", AssistKind::RefactorInline),
|
||||||
AssistKind::RefactorInline,
|
|
||||||
"Inline variable",
|
"Inline variable",
|
||||||
target,
|
target,
|
||||||
move |builder| {
|
move |builder| {
|
||||||
|
@ -83,7 +83,7 @@ fn generate_fn_def_assist(
|
|||||||
_ => return None,
|
_ => return None,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
acc.add(AssistId(ASSIST_NAME), AssistKind::Refactor, ASSIST_LABEL, lifetime_loc, |builder| {
|
acc.add(AssistId(ASSIST_NAME, AssistKind::Refactor), ASSIST_LABEL, lifetime_loc, |builder| {
|
||||||
add_lifetime_param(fn_def, builder, end_of_fn_ident, new_lifetime_param);
|
add_lifetime_param(fn_def, builder, end_of_fn_ident, new_lifetime_param);
|
||||||
builder.replace(lifetime_loc, format!("'{}", new_lifetime_param));
|
builder.replace(lifetime_loc, format!("'{}", new_lifetime_param));
|
||||||
loc_needing_lifetime.map(|loc| builder.insert(loc, format!("'{} ", new_lifetime_param)));
|
loc_needing_lifetime.map(|loc| builder.insert(loc, format!("'{} ", new_lifetime_param)));
|
||||||
@ -98,7 +98,7 @@ fn generate_impl_def_assist(
|
|||||||
) -> Option<()> {
|
) -> Option<()> {
|
||||||
let new_lifetime_param = generate_unique_lifetime_param_name(&impl_def.type_param_list())?;
|
let new_lifetime_param = generate_unique_lifetime_param_name(&impl_def.type_param_list())?;
|
||||||
let end_of_impl_kw = impl_def.impl_token()?.text_range().end();
|
let end_of_impl_kw = impl_def.impl_token()?.text_range().end();
|
||||||
acc.add(AssistId(ASSIST_NAME), AssistKind::Refactor, ASSIST_LABEL, lifetime_loc, |builder| {
|
acc.add(AssistId(ASSIST_NAME, AssistKind::Refactor), ASSIST_LABEL, lifetime_loc, |builder| {
|
||||||
add_lifetime_param(impl_def, builder, end_of_impl_kw, new_lifetime_param);
|
add_lifetime_param(impl_def, builder, end_of_impl_kw, new_lifetime_param);
|
||||||
builder.replace(lifetime_loc, format!("'{}", new_lifetime_param));
|
builder.replace(lifetime_loc, format!("'{}", new_lifetime_param));
|
||||||
})
|
})
|
||||||
|
@ -54,7 +54,7 @@ pub(crate) fn invert_if(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
|||||||
let else_node = else_block.syntax();
|
let else_node = else_block.syntax();
|
||||||
let else_range = else_node.text_range();
|
let else_range = else_node.text_range();
|
||||||
let then_range = then_node.text_range();
|
let then_range = then_node.text_range();
|
||||||
acc.add(AssistId("invert_if"), AssistKind::RefactorRewrite, "Invert if", if_range, |edit| {
|
acc.add(AssistId("invert_if", AssistKind::RefactorRewrite), "Invert if", if_range, |edit| {
|
||||||
edit.replace(cond_range, flip_cond.syntax().text());
|
edit.replace(cond_range, flip_cond.syntax().text());
|
||||||
edit.replace(else_range, then_node.text());
|
edit.replace(else_range, then_node.text());
|
||||||
edit.replace(then_range, else_node.text());
|
edit.replace(then_range, else_node.text());
|
||||||
|
@ -57,8 +57,7 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext) -> Option<()
|
|||||||
|
|
||||||
let target = tree.syntax().text_range();
|
let target = tree.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("merge_imports"),
|
AssistId("merge_imports", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Merge imports",
|
"Merge imports",
|
||||||
target,
|
target,
|
||||||
|builder| {
|
|builder| {
|
||||||
|
@ -60,8 +60,7 @@ pub(crate) fn merge_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option
|
|||||||
}
|
}
|
||||||
|
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("merge_match_arms"),
|
AssistId("merge_match_arms", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Merge match arms",
|
"Merge match arms",
|
||||||
current_text_range,
|
current_text_range,
|
||||||
|edit| {
|
|edit| {
|
||||||
|
@ -51,8 +51,7 @@ pub(crate) fn move_bounds_to_where_clause(acc: &mut Assists, ctx: &AssistContext
|
|||||||
|
|
||||||
let target = type_param_list.syntax().text_range();
|
let target = type_param_list.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("move_bounds_to_where_clause"),
|
AssistId("move_bounds_to_where_clause", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Move to where clause",
|
"Move to where clause",
|
||||||
target,
|
target,
|
||||||
|edit| {
|
|edit| {
|
||||||
|
@ -41,8 +41,7 @@ pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext) ->
|
|||||||
|
|
||||||
let target = guard.syntax().text_range();
|
let target = guard.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("move_guard_to_arm_body"),
|
AssistId("move_guard_to_arm_body", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Move guard to arm body",
|
"Move guard to arm body",
|
||||||
target,
|
target,
|
||||||
|edit| {
|
|edit| {
|
||||||
@ -106,8 +105,7 @@ pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContex
|
|||||||
|
|
||||||
let target = if_expr.syntax().text_range();
|
let target = if_expr.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("move_arm_cond_to_match_guard"),
|
AssistId("move_arm_cond_to_match_guard", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Move condition to match guard",
|
"Move condition to match guard",
|
||||||
target,
|
target,
|
||||||
|edit| {
|
|edit| {
|
||||||
|
@ -27,8 +27,7 @@ pub(crate) fn make_raw_string(acc: &mut Assists, ctx: &AssistContext) -> Option<
|
|||||||
let value = token.value()?;
|
let value = token.value()?;
|
||||||
let target = token.syntax().text_range();
|
let target = token.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("make_raw_string"),
|
AssistId("make_raw_string", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Rewrite as raw string",
|
"Rewrite as raw string",
|
||||||
target,
|
target,
|
||||||
|edit| {
|
|edit| {
|
||||||
@ -65,8 +64,7 @@ pub(crate) fn make_usual_string(acc: &mut Assists, ctx: &AssistContext) -> Optio
|
|||||||
let value = token.value()?;
|
let value = token.value()?;
|
||||||
let target = token.syntax().text_range();
|
let target = token.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("make_usual_string"),
|
AssistId("make_usual_string", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Rewrite as regular string",
|
"Rewrite as regular string",
|
||||||
target,
|
target,
|
||||||
|edit| {
|
|edit| {
|
||||||
@ -95,7 +93,7 @@ pub(crate) fn make_usual_string(acc: &mut Assists, ctx: &AssistContext) -> Optio
|
|||||||
pub(crate) fn add_hash(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
pub(crate) fn add_hash(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
||||||
let token = ctx.find_token_at_offset(RAW_STRING)?;
|
let token = ctx.find_token_at_offset(RAW_STRING)?;
|
||||||
let target = token.text_range();
|
let target = token.text_range();
|
||||||
acc.add(AssistId("add_hash"), AssistKind::Refactor, "Add # to raw string", target, |edit| {
|
acc.add(AssistId("add_hash", AssistKind::Refactor), "Add # to raw string", target, |edit| {
|
||||||
edit.insert(token.text_range().start() + TextSize::of('r'), "#");
|
edit.insert(token.text_range().start() + TextSize::of('r'), "#");
|
||||||
edit.insert(token.text_range().end(), "#");
|
edit.insert(token.text_range().end(), "#");
|
||||||
})
|
})
|
||||||
@ -125,8 +123,7 @@ pub(crate) fn remove_hash(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
|
|||||||
}
|
}
|
||||||
let target = token.text_range();
|
let target = token.text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("remove_hash"),
|
AssistId("remove_hash", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Remove hash from raw string",
|
"Remove hash from raw string",
|
||||||
target,
|
target,
|
||||||
|edit| {
|
|edit| {
|
||||||
|
@ -38,7 +38,7 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let target = macro_call.syntax().text_range();
|
let target = macro_call.syntax().text_range();
|
||||||
acc.add(AssistId("remove_dbg"), AssistKind::Refactor, "Remove dbg!()", target, |builder| {
|
acc.add(AssistId("remove_dbg", AssistKind::Refactor), "Remove dbg!()", target, |builder| {
|
||||||
builder.replace(macro_range, macro_content);
|
builder.replace(macro_range, macro_content);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -27,8 +27,7 @@ pub(crate) fn remove_mut(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
|||||||
|
|
||||||
let target = mut_token.text_range();
|
let target = mut_token.text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("remove_mut"),
|
AssistId("remove_mut", AssistKind::Refactor),
|
||||||
AssistKind::Refactor,
|
|
||||||
"Remove `mut` keyword",
|
"Remove `mut` keyword",
|
||||||
target,
|
target,
|
||||||
|builder| {
|
|builder| {
|
||||||
|
@ -43,8 +43,7 @@ fn reorder<R: AstNode>(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
|||||||
|
|
||||||
let target = record.syntax().text_range();
|
let target = record.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("reorder_fields"),
|
AssistId("reorder_fields", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Reorder record fields",
|
"Reorder record fields",
|
||||||
target,
|
target,
|
||||||
|edit| {
|
|edit| {
|
||||||
|
@ -49,8 +49,7 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext)
|
|||||||
|
|
||||||
let target = if_expr.syntax().text_range();
|
let target = if_expr.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("replace_if_let_with_match"),
|
AssistId("replace_if_let_with_match", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Replace with match",
|
"Replace with match",
|
||||||
target,
|
target,
|
||||||
move |edit| {
|
move |edit| {
|
||||||
|
@ -45,8 +45,7 @@ pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) ->
|
|||||||
|
|
||||||
let target = let_kw.text_range();
|
let target = let_kw.text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("replace_let_with_if_let"),
|
AssistId("replace_let_with_if_let", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Replace with if-let",
|
"Replace with if-let",
|
||||||
target,
|
target,
|
||||||
|edit| {
|
|edit| {
|
||||||
|
@ -37,8 +37,7 @@ pub(crate) fn replace_qualified_name_with_use(
|
|||||||
|
|
||||||
let target = path.syntax().text_range();
|
let target = path.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("replace_qualified_name_with_use"),
|
AssistId("replace_qualified_name_with_use", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Replace qualified path with use",
|
"Replace qualified path with use",
|
||||||
target,
|
target,
|
||||||
|builder| {
|
|builder| {
|
||||||
|
@ -47,8 +47,7 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext)
|
|||||||
let happy_variant = TryEnum::from_ty(&ctx.sema, &ty)?.happy_case();
|
let happy_variant = TryEnum::from_ty(&ctx.sema, &ty)?.happy_case();
|
||||||
let target = method_call.syntax().text_range();
|
let target = method_call.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("replace_unwrap_with_match"),
|
AssistId("replace_unwrap_with_match", AssistKind::RefactorRewrite),
|
||||||
AssistKind::RefactorRewrite,
|
|
||||||
"Replace unwrap with match",
|
"Replace unwrap with match",
|
||||||
target,
|
target,
|
||||||
|builder| {
|
|builder| {
|
||||||
|
@ -28,7 +28,7 @@ pub(crate) fn split_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
|
|||||||
}
|
}
|
||||||
|
|
||||||
let target = colon_colon.text_range();
|
let target = colon_colon.text_range();
|
||||||
acc.add(AssistId("split_import"), AssistKind::RefactorRewrite, "Split import", target, |edit| {
|
acc.add(AssistId("split_import", AssistKind::RefactorRewrite), "Split import", target, |edit| {
|
||||||
edit.replace_ast(use_tree, new_tree);
|
edit.replace_ast(use_tree, new_tree);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
|
|||||||
// }
|
// }
|
||||||
// ```
|
// ```
|
||||||
pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
||||||
let assist_id = AssistId("unwrap_block");
|
let assist_id = AssistId("unwrap_block", AssistKind::RefactorRewrite);
|
||||||
let assist_label = "Unwrap block";
|
let assist_label = "Unwrap block";
|
||||||
|
|
||||||
let l_curly_token = ctx.find_token_at_offset(T!['{'])?;
|
let l_curly_token = ctx.find_token_at_offset(T!['{'])?;
|
||||||
@ -50,47 +50,35 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
|
|||||||
let ancestor_then_branch = ancestor.then_branch()?;
|
let ancestor_then_branch = ancestor.then_branch()?;
|
||||||
|
|
||||||
let target = then_branch.syntax().text_range();
|
let target = then_branch.syntax().text_range();
|
||||||
return acc.add(
|
return acc.add(assist_id, assist_label, target, |edit| {
|
||||||
assist_id,
|
let range_to_del_else_if = TextRange::new(
|
||||||
AssistKind::Refactor,
|
ancestor_then_branch.syntax().text_range().end(),
|
||||||
assist_label,
|
l_curly_token.text_range().start(),
|
||||||
target,
|
);
|
||||||
|edit| {
|
let range_to_del_rest = TextRange::new(
|
||||||
let range_to_del_else_if = TextRange::new(
|
then_branch.syntax().text_range().end(),
|
||||||
ancestor_then_branch.syntax().text_range().end(),
|
if_expr.syntax().text_range().end(),
|
||||||
l_curly_token.text_range().start(),
|
);
|
||||||
);
|
|
||||||
let range_to_del_rest = TextRange::new(
|
|
||||||
then_branch.syntax().text_range().end(),
|
|
||||||
if_expr.syntax().text_range().end(),
|
|
||||||
);
|
|
||||||
|
|
||||||
edit.delete(range_to_del_rest);
|
edit.delete(range_to_del_rest);
|
||||||
edit.delete(range_to_del_else_if);
|
edit.delete(range_to_del_else_if);
|
||||||
edit.replace(
|
edit.replace(
|
||||||
target,
|
target,
|
||||||
update_expr_string(then_branch.to_string(), &[' ', '{']),
|
update_expr_string(then_branch.to_string(), &[' ', '{']),
|
||||||
);
|
);
|
||||||
},
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let target = block.syntax().text_range();
|
let target = block.syntax().text_range();
|
||||||
return acc.add(
|
return acc.add(assist_id, assist_label, target, |edit| {
|
||||||
assist_id,
|
let range_to_del = TextRange::new(
|
||||||
AssistKind::RefactorRewrite,
|
then_branch.syntax().text_range().end(),
|
||||||
assist_label,
|
l_curly_token.text_range().start(),
|
||||||
target,
|
);
|
||||||
|edit| {
|
|
||||||
let range_to_del = TextRange::new(
|
|
||||||
then_branch.syntax().text_range().end(),
|
|
||||||
l_curly_token.text_range().start(),
|
|
||||||
);
|
|
||||||
|
|
||||||
edit.delete(range_to_del);
|
edit.delete(range_to_del);
|
||||||
edit.replace(target, update_expr_string(block.to_string(), &[' ', '{']));
|
edit.replace(target, update_expr_string(block.to_string(), &[' ', '{']));
|
||||||
},
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => return None,
|
_ => return None,
|
||||||
@ -98,7 +86,7 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
|
|||||||
|
|
||||||
let unwrapped = unwrap_trivial_block(block);
|
let unwrapped = unwrap_trivial_block(block);
|
||||||
let target = unwrapped.syntax().text_range();
|
let target = unwrapped.syntax().text_range();
|
||||||
acc.add(assist_id, AssistKind::RefactorRewrite, assist_label, target, |builder| {
|
acc.add(assist_id, assist_label, target, |builder| {
|
||||||
builder.replace(
|
builder.replace(
|
||||||
parent.syntax().text_range(),
|
parent.syntax().text_range(),
|
||||||
update_expr_string(unwrapped.to_string(), &[' ', '{', '\n']),
|
update_expr_string(unwrapped.to_string(), &[' ', '{', '\n']),
|
||||||
|
@ -26,10 +26,22 @@ pub(crate) use crate::assist_context::{AssistContext, Assists};
|
|||||||
|
|
||||||
pub use assist_config::AssistConfig;
|
pub use assist_config::AssistConfig;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum AssistKind {
|
||||||
|
None,
|
||||||
|
QuickFix,
|
||||||
|
Refactor,
|
||||||
|
RefactorExtract,
|
||||||
|
RefactorInline,
|
||||||
|
RefactorRewrite,
|
||||||
|
Source,
|
||||||
|
OrganizeImports,
|
||||||
|
}
|
||||||
|
|
||||||
/// Unique identifier of the assist, should not be shown to the user
|
/// Unique identifier of the assist, should not be shown to the user
|
||||||
/// directly.
|
/// directly.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub struct AssistId(pub &'static str);
|
pub struct AssistId(pub &'static str, pub AssistKind);
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct GroupLabel(pub String);
|
pub struct GroupLabel(pub String);
|
||||||
@ -37,7 +49,6 @@ pub struct GroupLabel(pub String);
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Assist {
|
pub struct Assist {
|
||||||
pub id: AssistId,
|
pub id: AssistId,
|
||||||
pub kind: AssistKind,
|
|
||||||
/// Short description of the assist, as shown in the UI.
|
/// Short description of the assist, as shown in the UI.
|
||||||
pub label: String,
|
pub label: String,
|
||||||
pub group: Option<GroupLabel>,
|
pub group: Option<GroupLabel>,
|
||||||
@ -52,18 +63,6 @@ pub struct ResolvedAssist {
|
|||||||
pub source_change: SourceChange,
|
pub source_change: SourceChange,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
|
||||||
pub enum AssistKind {
|
|
||||||
None,
|
|
||||||
QuickFix,
|
|
||||||
Refactor,
|
|
||||||
RefactorExtract,
|
|
||||||
RefactorInline,
|
|
||||||
RefactorRewrite,
|
|
||||||
Source,
|
|
||||||
OrganizeImports,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Assist {
|
impl Assist {
|
||||||
/// Return all the assists applicable at the given position.
|
/// Return all the assists applicable at the given position.
|
||||||
///
|
///
|
||||||
@ -99,14 +98,13 @@ impl Assist {
|
|||||||
|
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
id: AssistId,
|
id: AssistId,
|
||||||
kind: AssistKind,
|
|
||||||
label: String,
|
label: String,
|
||||||
group: Option<GroupLabel>,
|
group: Option<GroupLabel>,
|
||||||
target: TextRange,
|
target: TextRange,
|
||||||
) -> Assist {
|
) -> Assist {
|
||||||
// FIXME: make fields private, so that this invariant can't be broken
|
// FIXME: make fields private, so that this invariant can't be broken
|
||||||
assert!(label.starts_with(|c: char| c.is_uppercase()));
|
assert!(label.starts_with(|c: char| c.is_uppercase()));
|
||||||
Assist { id, kind, label, group, target }
|
Assist { id, label, group, target }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -650,7 +650,7 @@ pub(crate) fn unresolved_code_action(
|
|||||||
title: assist.label,
|
title: assist.label,
|
||||||
id: Some(format!("{}:{}", assist.id.0.to_owned(), index.to_string())),
|
id: Some(format!("{}:{}", assist.id.0.to_owned(), index.to_string())),
|
||||||
group: assist.group.filter(|_| snap.config.client_caps.code_action_group).map(|gr| gr.0),
|
group: assist.group.filter(|_| snap.config.client_caps.code_action_group).map(|gr| gr.0),
|
||||||
kind: Some(code_action_kind(assist.kind)),
|
kind: Some(code_action_kind(assist.id.1)),
|
||||||
edit: None,
|
edit: None,
|
||||||
command: None,
|
command: None,
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user