aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src')
-rw-r--r--crates/ra_assists/src/add_derive.rs4
-rw-r--r--crates/ra_assists/src/add_impl.rs4
-rw-r--r--crates/ra_assists/src/assist_ctx.rs5
-rw-r--r--crates/ra_assists/src/auto_import.rs16
-rw-r--r--crates/ra_assists/src/change_visibility.rs8
-rw-r--r--crates/ra_assists/src/fill_match_arms.rs4
-rw-r--r--crates/ra_assists/src/flip_comma.rs4
-rw-r--r--crates/ra_assists/src/introduce_variable.rs4
-rw-r--r--crates/ra_assists/src/lib.rs6
-rw-r--r--crates/ra_assists/src/remove_dbg.rs4
-rw-r--r--crates/ra_assists/src/replace_if_let_with_match.rs4
-rw-r--r--crates/ra_assists/src/split_import.rs4
12 files changed, 41 insertions, 26 deletions
diff --git a/crates/ra_assists/src/add_derive.rs b/crates/ra_assists/src/add_derive.rs
index 0556dd69c..e91b5eb8d 100644
--- a/crates/ra_assists/src/add_derive.rs
+++ b/crates/ra_assists/src/add_derive.rs
@@ -5,12 +5,12 @@ use ra_syntax::{
5 TextUnit, 5 TextUnit,
6}; 6};
7 7
8use crate::{AssistCtx, Assist}; 8use crate::{AssistCtx, Assist, AssistId};
9 9
10pub(crate) fn add_derive(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 10pub(crate) fn add_derive(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
11 let nominal = ctx.node_at_offset::<ast::NominalDef>()?; 11 let nominal = ctx.node_at_offset::<ast::NominalDef>()?;
12 let node_start = derive_insertion_offset(nominal)?; 12 let node_start = derive_insertion_offset(nominal)?;
13 ctx.add_action("add `#[derive]`", |edit| { 13 ctx.add_action(AssistId("add_derive"), "add `#[derive]`", |edit| {
14 let derive_attr = nominal 14 let derive_attr = nominal
15 .attrs() 15 .attrs()
16 .filter_map(|x| x.as_call()) 16 .filter_map(|x| x.as_call())
diff --git a/crates/ra_assists/src/add_impl.rs b/crates/ra_assists/src/add_impl.rs
index b40b9cc0c..b292f188d 100644
--- a/crates/ra_assists/src/add_impl.rs
+++ b/crates/ra_assists/src/add_impl.rs
@@ -5,12 +5,12 @@ use ra_syntax::{
5 TextUnit, 5 TextUnit,
6}; 6};
7 7
8use crate::{AssistCtx, Assist}; 8use crate::{AssistCtx, Assist, AssistId};
9 9
10pub(crate) fn add_impl(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 10pub(crate) fn add_impl(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
11 let nominal = ctx.node_at_offset::<ast::NominalDef>()?; 11 let nominal = ctx.node_at_offset::<ast::NominalDef>()?;
12 let name = nominal.name()?; 12 let name = nominal.name()?;
13 ctx.add_action("add impl", |edit| { 13 ctx.add_action(AssistId("add_impl"), "add impl", |edit| {
14 edit.target(nominal.syntax().range()); 14 edit.target(nominal.syntax().range());
15 let type_params = nominal.type_param_list(); 15 let type_params = nominal.type_param_list();
16 let start_offset = nominal.syntax().range().end(); 16 let start_offset = nominal.syntax().range().end();
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs
index e9c4f0a23..4ad21c74b 100644
--- a/crates/ra_assists/src/assist_ctx.rs
+++ b/crates/ra_assists/src/assist_ctx.rs
@@ -7,7 +7,7 @@ use ra_syntax::{
7}; 7};
8use ra_fmt::{leading_indent, reindent}; 8use ra_fmt::{leading_indent, reindent};
9 9
10use crate::{AssistLabel, AssistAction}; 10use crate::{AssistLabel, AssistAction, AssistId};
11 11
12#[derive(Clone, Debug)] 12#[derive(Clone, Debug)]
13pub(crate) enum Assist { 13pub(crate) enum Assist {
@@ -81,10 +81,11 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> {
81 81
82 pub(crate) fn add_action( 82 pub(crate) fn add_action(
83 &mut self, 83 &mut self,
84 id: AssistId,
84 label: impl Into<String>, 85 label: impl Into<String>,
85 f: impl FnOnce(&mut AssistBuilder), 86 f: impl FnOnce(&mut AssistBuilder),
86 ) -> &mut Self { 87 ) -> &mut Self {
87 let label = AssistLabel { label: label.into() }; 88 let label = AssistLabel { label: label.into(), id };
88 match &mut self.assist { 89 match &mut self.assist {
89 Assist::Unresolved(labels) => labels.push(label), 90 Assist::Unresolved(labels) => labels.push(label),
90 Assist::Resolved(labels_actions) => { 91 Assist::Resolved(labels_actions) => {
diff --git a/crates/ra_assists/src/auto_import.rs b/crates/ra_assists/src/auto_import.rs
index 105c888d5..685dbed06 100644
--- a/crates/ra_assists/src/auto_import.rs
+++ b/crates/ra_assists/src/auto_import.rs
@@ -4,7 +4,10 @@ use ra_syntax::{
4 ast::{ self, NameOwner }, AstNode, SyntaxNode, Direction, TextRange, 4 ast::{ self, NameOwner }, AstNode, SyntaxNode, Direction, TextRange,
5 SyntaxKind::{ PATH, PATH_SEGMENT, COLONCOLON, COMMA } 5 SyntaxKind::{ PATH, PATH_SEGMENT, COLONCOLON, COMMA }
6}; 6};
7use crate::assist_ctx::{AssistCtx, Assist, AssistBuilder}; 7use crate::{
8 AssistId,
9 assist_ctx::{AssistCtx, Assist, AssistBuilder},
10};
8 11
9fn collect_path_segments(path: &ast::Path) -> Option<Vec<&ast::PathSegment>> { 12fn collect_path_segments(path: &ast::Path) -> Option<Vec<&ast::PathSegment>> {
10 let mut v = Vec::new(); 13 let mut v = Vec::new();
@@ -526,6 +529,7 @@ pub(crate) fn auto_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist
526 if let Some(module) = path.syntax().ancestors().find_map(ast::Module::cast) { 529 if let Some(module) = path.syntax().ancestors().find_map(ast::Module::cast) {
527 if let (Some(item_list), Some(name)) = (module.item_list(), module.name()) { 530 if let (Some(item_list), Some(name)) = (module.item_list(), module.name()) {
528 ctx.add_action( 531 ctx.add_action(
532 AssistId("auto_import"),
529 format!("import {} in mod {}", fmt_segments(&segments), name.text()), 533 format!("import {} in mod {}", fmt_segments(&segments), name.text()),
530 |edit| { 534 |edit| {
531 apply_auto_import(item_list.syntax(), path, &segments, edit); 535 apply_auto_import(item_list.syntax(), path, &segments, edit);
@@ -534,9 +538,13 @@ pub(crate) fn auto_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist
534 } 538 }
535 } else { 539 } else {
536 let current_file = node.ancestors().find_map(ast::SourceFile::cast)?; 540 let current_file = node.ancestors().find_map(ast::SourceFile::cast)?;
537 ctx.add_action(format!("import {} in the current file", fmt_segments(&segments)), |edit| { 541 ctx.add_action(
538 apply_auto_import(current_file.syntax(), path, &segments, edit); 542 AssistId("auto_import"),
539 }); 543 format!("import {} in the current file", fmt_segments(&segments)),
544 |edit| {
545 apply_auto_import(current_file.syntax(), path, &segments, edit);
546 },
547 );
540 } 548 }
541 549
542 ctx.build() 550 ctx.build()
diff --git a/crates/ra_assists/src/change_visibility.rs b/crates/ra_assists/src/change_visibility.rs
index c2ba897a4..50c1be5ae 100644
--- a/crates/ra_assists/src/change_visibility.rs
+++ b/crates/ra_assists/src/change_visibility.rs
@@ -5,7 +5,7 @@ use ra_syntax::{
5 SyntaxKind::{VISIBILITY, FN_KW, MOD_KW, STRUCT_KW, ENUM_KW, TRAIT_KW, FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF, IDENT, WHITESPACE, COMMENT, ATTR}, 5 SyntaxKind::{VISIBILITY, FN_KW, MOD_KW, STRUCT_KW, ENUM_KW, TRAIT_KW, FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF, IDENT, WHITESPACE, COMMENT, ATTR},
6}; 6};
7 7
8use crate::{AssistCtx, Assist}; 8use crate::{AssistCtx, Assist, AssistId};
9 9
10pub(crate) fn change_visibility(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 10pub(crate) fn change_visibility(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
11 if let Some(vis) = ctx.node_at_offset::<ast::Visibility>() { 11 if let Some(vis) = ctx.node_at_offset::<ast::Visibility>() {
@@ -41,7 +41,7 @@ fn add_vis(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
41 (vis_offset(field.syntax()), ident.range()) 41 (vis_offset(field.syntax()), ident.range())
42 }; 42 };
43 43
44 ctx.add_action("make pub(crate)", |edit| { 44 ctx.add_action(AssistId("change_visibility"), "make pub(crate)", |edit| {
45 edit.target(target); 45 edit.target(target);
46 edit.insert(offset, "pub(crate) "); 46 edit.insert(offset, "pub(crate) ");
47 edit.set_cursor(offset); 47 edit.set_cursor(offset);
@@ -63,7 +63,7 @@ fn vis_offset(node: &SyntaxNode) -> TextUnit {
63 63
64fn change_vis(mut ctx: AssistCtx<impl HirDatabase>, vis: &ast::Visibility) -> Option<Assist> { 64fn change_vis(mut ctx: AssistCtx<impl HirDatabase>, vis: &ast::Visibility) -> Option<Assist> {
65 if vis.syntax().text() == "pub" { 65 if vis.syntax().text() == "pub" {
66 ctx.add_action("change to pub(crate)", |edit| { 66 ctx.add_action(AssistId("change_visibility"), "change to pub(crate)", |edit| {
67 edit.target(vis.syntax().range()); 67 edit.target(vis.syntax().range());
68 edit.replace(vis.syntax().range(), "pub(crate)"); 68 edit.replace(vis.syntax().range(), "pub(crate)");
69 edit.set_cursor(vis.syntax().range().start()) 69 edit.set_cursor(vis.syntax().range().start())
@@ -72,7 +72,7 @@ fn change_vis(mut ctx: AssistCtx<impl HirDatabase>, vis: &ast::Visibility) -> Op
72 return ctx.build(); 72 return ctx.build();
73 } 73 }
74 if vis.syntax().text() == "pub(crate)" { 74 if vis.syntax().text() == "pub(crate)" {
75 ctx.add_action("change to pub", |edit| { 75 ctx.add_action(AssistId("change_visibility"), "change to pub", |edit| {
76 edit.target(vis.syntax().range()); 76 edit.target(vis.syntax().range());
77 edit.replace(vis.syntax().range(), "pub"); 77 edit.replace(vis.syntax().range(), "pub");
78 edit.set_cursor(vis.syntax().range().start()); 78 edit.set_cursor(vis.syntax().range().start());
diff --git a/crates/ra_assists/src/fill_match_arms.rs b/crates/ra_assists/src/fill_match_arms.rs
index 6bf6e7332..30020b56e 100644
--- a/crates/ra_assists/src/fill_match_arms.rs
+++ b/crates/ra_assists/src/fill_match_arms.rs
@@ -6,7 +6,7 @@ use hir::{
6}; 6};
7use ra_syntax::ast::{self, AstNode}; 7use ra_syntax::ast::{self, AstNode};
8 8
9use crate::{AssistCtx, Assist}; 9use crate::{AssistCtx, Assist, AssistId};
10 10
11pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 11pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
12 let match_expr = ctx.node_at_offset::<ast::MatchExpr>()?; 12 let match_expr = ctx.node_at_offset::<ast::MatchExpr>()?;
@@ -37,7 +37,7 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
37 let enum_name = enum_def.name(ctx.db)?; 37 let enum_name = enum_def.name(ctx.db)?;
38 let db = ctx.db; 38 let db = ctx.db;
39 39
40 ctx.add_action("fill match arms", |edit| { 40 ctx.add_action(AssistId("fill_match_arms"), "fill match arms", |edit| {
41 let mut buf = format!("match {} {{\n", expr.syntax().text().to_string()); 41 let mut buf = format!("match {} {{\n", expr.syntax().text().to_string());
42 let variants = enum_def.variants(db); 42 let variants = enum_def.variants(db);
43 for variant in variants { 43 for variant in variants {
diff --git a/crates/ra_assists/src/flip_comma.rs b/crates/ra_assists/src/flip_comma.rs
index 0d4a789fc..6b98cac68 100644
--- a/crates/ra_assists/src/flip_comma.rs
+++ b/crates/ra_assists/src/flip_comma.rs
@@ -5,13 +5,13 @@ use ra_syntax::{
5 algo::non_trivia_sibling, 5 algo::non_trivia_sibling,
6}; 6};
7 7
8use crate::{AssistCtx, Assist}; 8use crate::{AssistCtx, Assist, AssistId};
9 9
10pub(crate) fn flip_comma(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 10pub(crate) fn flip_comma(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
11 let comma = ctx.leaf_at_offset().find(|leaf| leaf.kind() == COMMA)?; 11 let comma = ctx.leaf_at_offset().find(|leaf| leaf.kind() == COMMA)?;
12 let prev = non_trivia_sibling(comma, Direction::Prev)?; 12 let prev = non_trivia_sibling(comma, Direction::Prev)?;
13 let next = non_trivia_sibling(comma, Direction::Next)?; 13 let next = non_trivia_sibling(comma, Direction::Next)?;
14 ctx.add_action("flip comma", |edit| { 14 ctx.add_action(AssistId("flip_comma"), "flip comma", |edit| {
15 edit.target(comma.range()); 15 edit.target(comma.range());
16 edit.replace(prev.range(), next.text()); 16 edit.replace(prev.range(), next.text());
17 edit.replace(next.range(), prev.text()); 17 edit.replace(next.range(), prev.text());
diff --git a/crates/ra_assists/src/introduce_variable.rs b/crates/ra_assists/src/introduce_variable.rs
index f0e012105..3d708ebb2 100644
--- a/crates/ra_assists/src/introduce_variable.rs
+++ b/crates/ra_assists/src/introduce_variable.rs
@@ -6,7 +6,7 @@ use ra_syntax::{
6 }, SyntaxNode, TextUnit, 6 }, SyntaxNode, TextUnit,
7}; 7};
8 8
9use crate::{AssistCtx, Assist}; 9use crate::{AssistCtx, Assist, AssistId};
10 10
11pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 11pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
12 let node = ctx.covering_node(); 12 let node = ctx.covering_node();
@@ -19,7 +19,7 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option
19 if indent.kind() != WHITESPACE { 19 if indent.kind() != WHITESPACE {
20 return None; 20 return None;
21 } 21 }
22 ctx.add_action("introduce variable", move |edit| { 22 ctx.add_action(AssistId("introduce_variable"), "introduce variable", move |edit| {
23 let mut buf = String::new(); 23 let mut buf = String::new();
24 24
25 let cursor_offset = if wrap_in_block { 25 let cursor_offset = if wrap_in_block {
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index e1e899edc..56d276867 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -16,10 +16,16 @@ use hir::db::HirDatabase;
16 16
17pub(crate) use crate::assist_ctx::{AssistCtx, Assist}; 17pub(crate) use crate::assist_ctx::{AssistCtx, Assist};
18 18
19/// Unique identifier of the assist, should not be shown to the user
20/// directly.
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub struct AssistId(pub &'static str);
23
19#[derive(Debug, Clone)] 24#[derive(Debug, Clone)]
20pub struct AssistLabel { 25pub struct AssistLabel {
21 /// Short description of the assist, as shown in the UI. 26 /// Short description of the assist, as shown in the UI.
22 pub label: String, 27 pub label: String,
28 pub id: AssistId,
23} 29}
24 30
25#[derive(Debug, Clone)] 31#[derive(Debug, Clone)]
diff --git a/crates/ra_assists/src/remove_dbg.rs b/crates/ra_assists/src/remove_dbg.rs
index 2bed270a1..6ea48d909 100644
--- a/crates/ra_assists/src/remove_dbg.rs
+++ b/crates/ra_assists/src/remove_dbg.rs
@@ -6,7 +6,7 @@ use ra_syntax::{
6 L_PAREN, R_PAREN, L_CURLY, R_CURLY, L_BRACK, R_BRACK, EXCL 6 L_PAREN, R_PAREN, L_CURLY, R_CURLY, L_BRACK, R_BRACK, EXCL
7 }, 7 },
8}; 8};
9use crate::{AssistCtx, Assist}; 9use crate::{AssistCtx, Assist, AssistId};
10 10
11pub(crate) fn remove_dbg(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 11pub(crate) fn remove_dbg(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
12 let macro_call = ctx.node_at_offset::<ast::MacroCall>()?; 12 let macro_call = ctx.node_at_offset::<ast::MacroCall>()?;
@@ -46,7 +46,7 @@ pub(crate) fn remove_dbg(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist>
46 macro_args.text().slice(start..end).to_string() 46 macro_args.text().slice(start..end).to_string()
47 }; 47 };
48 48
49 ctx.add_action("remove dbg!()", |edit| { 49 ctx.add_action(AssistId("remove_dbg"), "remove dbg!()", |edit| {
50 edit.target(macro_call.syntax().range()); 50 edit.target(macro_call.syntax().range());
51 edit.replace(macro_range, macro_content); 51 edit.replace(macro_range, macro_content);
52 edit.set_cursor(cursor_pos); 52 edit.set_cursor(cursor_pos);
diff --git a/crates/ra_assists/src/replace_if_let_with_match.rs b/crates/ra_assists/src/replace_if_let_with_match.rs
index 87a2c1185..230573499 100644
--- a/crates/ra_assists/src/replace_if_let_with_match.rs
+++ b/crates/ra_assists/src/replace_if_let_with_match.rs
@@ -2,7 +2,7 @@ use ra_syntax::{AstNode, ast};
2use ra_fmt::extract_trivial_expression; 2use ra_fmt::extract_trivial_expression;
3use hir::db::HirDatabase; 3use hir::db::HirDatabase;
4 4
5use crate::{AssistCtx, Assist}; 5use crate::{AssistCtx, Assist, AssistId};
6 6
7pub(crate) fn replace_if_let_with_match(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 7pub(crate) fn replace_if_let_with_match(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
8 let if_expr: &ast::IfExpr = ctx.node_at_offset()?; 8 let if_expr: &ast::IfExpr = ctx.node_at_offset()?;
@@ -15,7 +15,7 @@ pub(crate) fn replace_if_let_with_match(mut ctx: AssistCtx<impl HirDatabase>) ->
15 ast::ElseBranchFlavor::IfExpr(_) => return None, 15 ast::ElseBranchFlavor::IfExpr(_) => return None,
16 }; 16 };
17 17
18 ctx.add_action("replace with match", |edit| { 18 ctx.add_action(AssistId("replace_if_let_with_match"), "replace with match", |edit| {
19 let match_expr = build_match_expr(expr, pat, then_block, else_block); 19 let match_expr = build_match_expr(expr, pat, then_block, else_block);
20 edit.target(if_expr.syntax().range()); 20 edit.target(if_expr.syntax().range());
21 edit.replace_node_and_indent(if_expr.syntax(), match_expr); 21 edit.replace_node_and_indent(if_expr.syntax(), match_expr);
diff --git a/crates/ra_assists/src/split_import.rs b/crates/ra_assists/src/split_import.rs
index f043be636..dd5be4e91 100644
--- a/crates/ra_assists/src/split_import.rs
+++ b/crates/ra_assists/src/split_import.rs
@@ -5,7 +5,7 @@ use ra_syntax::{
5 algo::generate, 5 algo::generate,
6}; 6};
7 7
8use crate::{AssistCtx, Assist}; 8use crate::{AssistCtx, Assist, AssistId};
9 9
10pub(crate) fn split_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 10pub(crate) fn split_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
11 let colon_colon = ctx.leaf_at_offset().find(|leaf| leaf.kind() == COLONCOLON)?; 11 let colon_colon = ctx.leaf_at_offset().find(|leaf| leaf.kind() == COLONCOLON)?;
@@ -23,7 +23,7 @@ pub(crate) fn split_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assis
23 None => top_path.syntax().range().end(), 23 None => top_path.syntax().range().end(),
24 }; 24 };
25 25
26 ctx.add_action("split import", |edit| { 26 ctx.add_action(AssistId("split_import"), "split import", |edit| {
27 edit.target(colon_colon.range()); 27 edit.target(colon_colon.range());
28 edit.insert(l_curly, "{"); 28 edit.insert(l_curly, "{");
29 edit.insert(r_curly, "}"); 29 edit.insert(r_curly, "}");