diff options
Diffstat (limited to 'crates/assists')
-rw-r--r-- | crates/assists/src/handlers/extract_struct_from_enum_variant.rs | 59 | ||||
-rw-r--r-- | crates/assists/src/handlers/inline_local_variable.rs | 99 | ||||
-rw-r--r-- | crates/assists/src/handlers/remove_dbg.rs | 12 | ||||
-rw-r--r-- | crates/assists/src/handlers/remove_unused_param.rs | 41 | ||||
-rw-r--r-- | crates/assists/src/handlers/reorder_impl.rs | 201 | ||||
-rw-r--r-- | crates/assists/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/assists/src/tests/generated.rs | 35 |
7 files changed, 351 insertions, 98 deletions
diff --git a/crates/assists/src/handlers/extract_struct_from_enum_variant.rs b/crates/assists/src/handlers/extract_struct_from_enum_variant.rs index 40028fc01..e3ef04932 100644 --- a/crates/assists/src/handlers/extract_struct_from_enum_variant.rs +++ b/crates/assists/src/handlers/extract_struct_from_enum_variant.rs | |||
@@ -2,12 +2,16 @@ use std::iter; | |||
2 | 2 | ||
3 | use either::Either; | 3 | use either::Either; |
4 | use hir::{AsName, Module, ModuleDef, Name, Variant}; | 4 | use hir::{AsName, Module, ModuleDef, Name, Variant}; |
5 | use ide_db::helpers::{ | 5 | use ide_db::{ |
6 | insert_use::{insert_use, ImportScope}, | 6 | defs::Definition, |
7 | mod_path_to_ast, | 7 | helpers::{ |
8 | insert_use::{insert_use, ImportScope}, | ||
9 | mod_path_to_ast, | ||
10 | }, | ||
11 | search::FileReference, | ||
12 | RootDatabase, | ||
8 | }; | 13 | }; |
9 | use ide_db::{defs::Definition, search::Reference, RootDatabase}; | 14 | use rustc_hash::FxHashSet; |
10 | use rustc_hash::{FxHashMap, FxHashSet}; | ||
11 | use syntax::{ | 15 | use syntax::{ |
12 | algo::{find_node_at_offset, SyntaxRewriter}, | 16 | algo::{find_node_at_offset, SyntaxRewriter}, |
13 | ast::{self, edit::IndentLevel, make, AstNode, NameOwner, VisibilityOwner}, | 17 | ast::{self, edit::IndentLevel, make, AstNode, NameOwner, VisibilityOwner}, |
@@ -58,29 +62,29 @@ pub(crate) fn extract_struct_from_enum_variant( | |||
58 | let mut visited_modules_set = FxHashSet::default(); | 62 | let mut visited_modules_set = FxHashSet::default(); |
59 | let current_module = enum_hir.module(ctx.db()); | 63 | let current_module = enum_hir.module(ctx.db()); |
60 | visited_modules_set.insert(current_module); | 64 | visited_modules_set.insert(current_module); |
61 | let mut rewriters = FxHashMap::default(); | 65 | let mut def_rewriter = None; |
62 | for reference in usages { | 66 | for (file_id, references) in usages { |
63 | let rewriter = rewriters | 67 | let mut rewriter = SyntaxRewriter::default(); |
64 | .entry(reference.file_range.file_id) | 68 | let source_file = ctx.sema.parse(file_id); |
65 | .or_insert_with(SyntaxRewriter::default); | 69 | for reference in references { |
66 | let source_file = ctx.sema.parse(reference.file_range.file_id); | 70 | update_reference( |
67 | update_reference( | 71 | ctx, |
68 | ctx, | 72 | &mut rewriter, |
69 | rewriter, | 73 | reference, |
70 | reference, | 74 | &source_file, |
71 | &source_file, | 75 | &enum_module_def, |
72 | &enum_module_def, | 76 | &variant_hir_name, |
73 | &variant_hir_name, | 77 | &mut visited_modules_set, |
74 | &mut visited_modules_set, | 78 | ); |
75 | ); | 79 | } |
76 | } | 80 | if file_id == ctx.frange.file_id { |
77 | let mut rewriter = | 81 | def_rewriter = Some(rewriter); |
78 | rewriters.remove(&ctx.frange.file_id).unwrap_or_else(SyntaxRewriter::default); | 82 | continue; |
79 | for (file_id, rewriter) in rewriters { | 83 | } |
80 | builder.edit_file(file_id); | 84 | builder.edit_file(file_id); |
81 | builder.rewrite(rewriter); | 85 | builder.rewrite(rewriter); |
82 | } | 86 | } |
83 | builder.edit_file(ctx.frange.file_id); | 87 | let mut rewriter = def_rewriter.unwrap_or_default(); |
84 | update_variant(&mut rewriter, &variant); | 88 | update_variant(&mut rewriter, &variant); |
85 | extract_struct_def( | 89 | extract_struct_def( |
86 | &mut rewriter, | 90 | &mut rewriter, |
@@ -90,6 +94,7 @@ pub(crate) fn extract_struct_from_enum_variant( | |||
90 | &variant.parent_enum().syntax().clone().into(), | 94 | &variant.parent_enum().syntax().clone().into(), |
91 | enum_ast.visibility(), | 95 | enum_ast.visibility(), |
92 | ); | 96 | ); |
97 | builder.edit_file(ctx.frange.file_id); | ||
93 | builder.rewrite(rewriter); | 98 | builder.rewrite(rewriter); |
94 | }, | 99 | }, |
95 | ) | 100 | ) |
@@ -205,13 +210,13 @@ fn update_variant(rewriter: &mut SyntaxRewriter, variant: &ast::Variant) -> Opti | |||
205 | fn update_reference( | 210 | fn update_reference( |
206 | ctx: &AssistContext, | 211 | ctx: &AssistContext, |
207 | rewriter: &mut SyntaxRewriter, | 212 | rewriter: &mut SyntaxRewriter, |
208 | reference: Reference, | 213 | reference: FileReference, |
209 | source_file: &SourceFile, | 214 | source_file: &SourceFile, |
210 | enum_module_def: &ModuleDef, | 215 | enum_module_def: &ModuleDef, |
211 | variant_hir_name: &Name, | 216 | variant_hir_name: &Name, |
212 | visited_modules_set: &mut FxHashSet<Module>, | 217 | visited_modules_set: &mut FxHashSet<Module>, |
213 | ) -> Option<()> { | 218 | ) -> Option<()> { |
214 | let offset = reference.file_range.range.start(); | 219 | let offset = reference.range.start(); |
215 | let (segment, expr) = if let Some(path_expr) = | 220 | let (segment, expr) = if let Some(path_expr) = |
216 | find_node_at_offset::<ast::PathExpr>(source_file.syntax(), offset) | 221 | find_node_at_offset::<ast::PathExpr>(source_file.syntax(), offset) |
217 | { | 222 | { |
diff --git a/crates/assists/src/handlers/inline_local_variable.rs b/crates/assists/src/handlers/inline_local_variable.rs index d559be9cb..dc798daaa 100644 --- a/crates/assists/src/handlers/inline_local_variable.rs +++ b/crates/assists/src/handlers/inline_local_variable.rs | |||
@@ -1,4 +1,7 @@ | |||
1 | use ide_db::{defs::Definition, search::ReferenceKind}; | 1 | use ide_db::{ |
2 | defs::Definition, | ||
3 | search::{FileReference, ReferenceKind}, | ||
4 | }; | ||
2 | use syntax::{ | 5 | use syntax::{ |
3 | ast::{self, AstNode, AstToken}, | 6 | ast::{self, AstNode, AstToken}, |
4 | TextRange, | 7 | TextRange, |
@@ -44,8 +47,8 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O | |||
44 | 47 | ||
45 | let def = ctx.sema.to_def(&bind_pat)?; | 48 | let def = ctx.sema.to_def(&bind_pat)?; |
46 | let def = Definition::Local(def); | 49 | let def = Definition::Local(def); |
47 | let refs = def.usages(&ctx.sema).all(); | 50 | let usages = def.usages(&ctx.sema).all(); |
48 | if refs.is_empty() { | 51 | if usages.is_empty() { |
49 | mark::hit!(test_not_applicable_if_variable_unused); | 52 | mark::hit!(test_not_applicable_if_variable_unused); |
50 | return None; | 53 | return None; |
51 | }; | 54 | }; |
@@ -63,48 +66,45 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O | |||
63 | let_stmt.syntax().text_range() | 66 | let_stmt.syntax().text_range() |
64 | }; | 67 | }; |
65 | 68 | ||
66 | let mut wrap_in_parens = vec![true; refs.len()]; | 69 | let wrap_in_parens = usages |
67 | 70 | .references | |
68 | for (i, desc) in refs.iter().enumerate() { | 71 | .values() |
69 | let usage_node = ctx | 72 | .flatten() |
70 | .covering_node_for_range(desc.file_range.range) | 73 | .map(|&FileReference { range, .. }| { |
71 | .ancestors() | 74 | let usage_node = |
72 | .find_map(ast::PathExpr::cast)?; | 75 | ctx.covering_node_for_range(range).ancestors().find_map(ast::PathExpr::cast)?; |
73 | let usage_parent_option = usage_node.syntax().parent().and_then(ast::Expr::cast); | 76 | let usage_parent_option = usage_node.syntax().parent().and_then(ast::Expr::cast); |
74 | let usage_parent = match usage_parent_option { | 77 | let usage_parent = match usage_parent_option { |
75 | Some(u) => u, | 78 | Some(u) => u, |
76 | None => { | 79 | None => return Ok(false), |
77 | wrap_in_parens[i] = false; | 80 | }; |
78 | continue; | 81 | |
79 | } | 82 | Ok(!matches!((&initializer_expr, usage_parent), |
80 | }; | 83 | (ast::Expr::CallExpr(_), _) |
81 | 84 | | (ast::Expr::IndexExpr(_), _) | |
82 | wrap_in_parens[i] = match (&initializer_expr, usage_parent) { | 85 | | (ast::Expr::MethodCallExpr(_), _) |
83 | (ast::Expr::CallExpr(_), _) | 86 | | (ast::Expr::FieldExpr(_), _) |
84 | | (ast::Expr::IndexExpr(_), _) | 87 | | (ast::Expr::TryExpr(_), _) |
85 | | (ast::Expr::MethodCallExpr(_), _) | 88 | | (ast::Expr::RefExpr(_), _) |
86 | | (ast::Expr::FieldExpr(_), _) | 89 | | (ast::Expr::Literal(_), _) |
87 | | (ast::Expr::TryExpr(_), _) | 90 | | (ast::Expr::TupleExpr(_), _) |
88 | | (ast::Expr::RefExpr(_), _) | 91 | | (ast::Expr::ArrayExpr(_), _) |
89 | | (ast::Expr::Literal(_), _) | 92 | | (ast::Expr::ParenExpr(_), _) |
90 | | (ast::Expr::TupleExpr(_), _) | 93 | | (ast::Expr::PathExpr(_), _) |
91 | | (ast::Expr::ArrayExpr(_), _) | 94 | | (ast::Expr::BlockExpr(_), _) |
92 | | (ast::Expr::ParenExpr(_), _) | 95 | | (ast::Expr::EffectExpr(_), _) |
93 | | (ast::Expr::PathExpr(_), _) | 96 | | (_, ast::Expr::CallExpr(_)) |
94 | | (ast::Expr::BlockExpr(_), _) | 97 | | (_, ast::Expr::TupleExpr(_)) |
95 | | (ast::Expr::EffectExpr(_), _) | 98 | | (_, ast::Expr::ArrayExpr(_)) |
96 | | (_, ast::Expr::CallExpr(_)) | 99 | | (_, ast::Expr::ParenExpr(_)) |
97 | | (_, ast::Expr::TupleExpr(_)) | 100 | | (_, ast::Expr::ForExpr(_)) |
98 | | (_, ast::Expr::ArrayExpr(_)) | 101 | | (_, ast::Expr::WhileExpr(_)) |
99 | | (_, ast::Expr::ParenExpr(_)) | 102 | | (_, ast::Expr::BreakExpr(_)) |
100 | | (_, ast::Expr::ForExpr(_)) | 103 | | (_, ast::Expr::ReturnExpr(_)) |
101 | | (_, ast::Expr::WhileExpr(_)) | 104 | | (_, ast::Expr::MatchExpr(_)) |
102 | | (_, ast::Expr::BreakExpr(_)) | 105 | )) |
103 | | (_, ast::Expr::ReturnExpr(_)) | 106 | }) |
104 | | (_, ast::Expr::MatchExpr(_)) => false, | 107 | .collect::<Result<Vec<_>, _>>()?; |
105 | _ => true, | ||
106 | }; | ||
107 | } | ||
108 | 108 | ||
109 | let init_str = initializer_expr.syntax().text().to_string(); | 109 | let init_str = initializer_expr.syntax().text().to_string(); |
110 | let init_in_paren = format!("({})", &init_str); | 110 | let init_in_paren = format!("({})", &init_str); |
@@ -116,15 +116,16 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O | |||
116 | target, | 116 | target, |
117 | move |builder| { | 117 | move |builder| { |
118 | builder.delete(delete_range); | 118 | builder.delete(delete_range); |
119 | for (desc, should_wrap) in refs.iter().zip(wrap_in_parens) { | 119 | for (reference, should_wrap) in usages.references.values().flatten().zip(wrap_in_parens) |
120 | { | ||
120 | let replacement = | 121 | let replacement = |
121 | if should_wrap { init_in_paren.clone() } else { init_str.clone() }; | 122 | if should_wrap { init_in_paren.clone() } else { init_str.clone() }; |
122 | match desc.kind { | 123 | match reference.kind { |
123 | ReferenceKind::FieldShorthandForLocal => { | 124 | ReferenceKind::FieldShorthandForLocal => { |
124 | mark::hit!(inline_field_shorthand); | 125 | mark::hit!(inline_field_shorthand); |
125 | builder.insert(desc.file_range.range.end(), format!(": {}", replacement)) | 126 | builder.insert(reference.range.end(), format!(": {}", replacement)) |
126 | } | 127 | } |
127 | _ => builder.replace(desc.file_range.range, replacement), | 128 | _ => builder.replace(reference.range, replacement), |
128 | } | 129 | } |
129 | } | 130 | } |
130 | }, | 131 | }, |
diff --git a/crates/assists/src/handlers/remove_dbg.rs b/crates/assists/src/handlers/remove_dbg.rs index 0320c2f12..6114091f2 100644 --- a/crates/assists/src/handlers/remove_dbg.rs +++ b/crates/assists/src/handlers/remove_dbg.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use syntax::{ | 1 | use syntax::{ |
2 | ast::{self, AstNode}, | 2 | ast::{self, AstNode}, |
3 | match_ast, SyntaxElement, SyntaxKind, TextRange, TextSize, T, | 3 | match_ast, SyntaxElement, TextRange, TextSize, T, |
4 | }; | 4 | }; |
5 | 5 | ||
6 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | 6 | use crate::{AssistContext, AssistId, AssistKind, Assists}; |
@@ -136,14 +136,14 @@ fn needs_parentheses_around_macro_contents(macro_contents: Vec<SyntaxElement>) - | |||
136 | symbol_kind => { | 136 | symbol_kind => { |
137 | let symbol_not_in_bracket = unpaired_brackets_in_contents.is_empty(); | 137 | let symbol_not_in_bracket = unpaired_brackets_in_contents.is_empty(); |
138 | if symbol_not_in_bracket | 138 | if symbol_not_in_bracket |
139 | && symbol_kind != SyntaxKind::COLON // paths | 139 | && symbol_kind != T![:] // paths |
140 | && (symbol_kind != SyntaxKind::DOT // field/method access | 140 | && (symbol_kind != T![.] // field/method access |
141 | || macro_contents // range expressions consist of two SyntaxKind::Dot in macro invocations | 141 | || macro_contents // range expressions consist of two SyntaxKind::Dot in macro invocations |
142 | .peek() | 142 | .peek() |
143 | .map(|element| element.kind() == SyntaxKind::DOT) | 143 | .map(|element| element.kind() == T![.]) |
144 | .unwrap_or(false)) | 144 | .unwrap_or(false)) |
145 | && symbol_kind != SyntaxKind::QUESTION // try operator | 145 | && symbol_kind != T![?] // try operator |
146 | && (symbol_kind.is_punct() || symbol_kind == SyntaxKind::AS_KW) | 146 | && (symbol_kind.is_punct() || symbol_kind == T![as]) |
147 | { | 147 | { |
148 | return true; | 148 | return true; |
149 | } | 149 | } |
diff --git a/crates/assists/src/handlers/remove_unused_param.rs b/crates/assists/src/handlers/remove_unused_param.rs index 56e8b5229..c961680e2 100644 --- a/crates/assists/src/handlers/remove_unused_param.rs +++ b/crates/assists/src/handlers/remove_unused_param.rs | |||
@@ -1,8 +1,8 @@ | |||
1 | use ide_db::{defs::Definition, search::Reference}; | 1 | use ide_db::{base_db::FileId, defs::Definition, search::FileReference}; |
2 | use syntax::{ | 2 | use syntax::{ |
3 | algo::find_node_at_range, | 3 | algo::find_node_at_range, |
4 | ast::{self, ArgListOwner}, | 4 | ast::{self, ArgListOwner}, |
5 | AstNode, SyntaxKind, SyntaxNode, TextRange, T, | 5 | AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange, T, |
6 | }; | 6 | }; |
7 | use test_utils::mark; | 7 | use test_utils::mark; |
8 | use SyntaxKind::WHITESPACE; | 8 | use SyntaxKind::WHITESPACE; |
@@ -58,32 +58,41 @@ pub(crate) fn remove_unused_param(acc: &mut Assists, ctx: &AssistContext) -> Opt | |||
58 | param.syntax().text_range(), | 58 | param.syntax().text_range(), |
59 | |builder| { | 59 | |builder| { |
60 | builder.delete(range_to_remove(param.syntax())); | 60 | builder.delete(range_to_remove(param.syntax())); |
61 | for usage in fn_def.usages(&ctx.sema).all() { | 61 | for (file_id, references) in fn_def.usages(&ctx.sema).all() { |
62 | process_usage(ctx, builder, usage, param_position); | 62 | process_usages(ctx, builder, file_id, references, param_position); |
63 | } | 63 | } |
64 | }, | 64 | }, |
65 | ) | 65 | ) |
66 | } | 66 | } |
67 | 67 | ||
68 | fn process_usage( | 68 | fn process_usages( |
69 | ctx: &AssistContext, | 69 | ctx: &AssistContext, |
70 | builder: &mut AssistBuilder, | 70 | builder: &mut AssistBuilder, |
71 | usage: Reference, | 71 | file_id: FileId, |
72 | references: Vec<FileReference>, | ||
72 | arg_to_remove: usize, | 73 | arg_to_remove: usize, |
73 | ) -> Option<()> { | 74 | ) { |
74 | let source_file = ctx.sema.parse(usage.file_range.file_id); | 75 | let source_file = ctx.sema.parse(file_id); |
75 | let call_expr: ast::CallExpr = | 76 | builder.edit_file(file_id); |
76 | find_node_at_range(source_file.syntax(), usage.file_range.range)?; | 77 | for usage in references { |
78 | if let Some(text_range) = process_usage(&source_file, usage, arg_to_remove) { | ||
79 | builder.delete(text_range); | ||
80 | } | ||
81 | } | ||
82 | } | ||
83 | |||
84 | fn process_usage( | ||
85 | source_file: &SourceFile, | ||
86 | FileReference { range, .. }: FileReference, | ||
87 | arg_to_remove: usize, | ||
88 | ) -> Option<TextRange> { | ||
89 | let call_expr: ast::CallExpr = find_node_at_range(source_file.syntax(), range)?; | ||
77 | let call_expr_range = call_expr.expr()?.syntax().text_range(); | 90 | let call_expr_range = call_expr.expr()?.syntax().text_range(); |
78 | if !call_expr_range.contains_range(usage.file_range.range) { | 91 | if !call_expr_range.contains_range(range) { |
79 | return None; | 92 | return None; |
80 | } | 93 | } |
81 | let arg = call_expr.arg_list()?.args().nth(arg_to_remove)?; | 94 | let arg = call_expr.arg_list()?.args().nth(arg_to_remove)?; |
82 | 95 | Some(range_to_remove(arg.syntax())) | |
83 | builder.edit_file(usage.file_range.file_id); | ||
84 | builder.delete(range_to_remove(arg.syntax())); | ||
85 | |||
86 | Some(()) | ||
87 | } | 96 | } |
88 | 97 | ||
89 | fn range_to_remove(node: &SyntaxNode) -> TextRange { | 98 | fn range_to_remove(node: &SyntaxNode) -> TextRange { |
diff --git a/crates/assists/src/handlers/reorder_impl.rs b/crates/assists/src/handlers/reorder_impl.rs new file mode 100644 index 000000000..309f291c8 --- /dev/null +++ b/crates/assists/src/handlers/reorder_impl.rs | |||
@@ -0,0 +1,201 @@ | |||
1 | use itertools::Itertools; | ||
2 | use rustc_hash::FxHashMap; | ||
3 | |||
4 | use hir::{PathResolution, Semantics}; | ||
5 | use ide_db::RootDatabase; | ||
6 | use syntax::{ | ||
7 | algo, | ||
8 | ast::{self, NameOwner}, | ||
9 | AstNode, | ||
10 | }; | ||
11 | use test_utils::mark; | ||
12 | |||
13 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | ||
14 | |||
15 | // Assist: reorder_impl | ||
16 | // | ||
17 | // Reorder the methods of an `impl Trait`. The methods will be ordered | ||
18 | // in the same order as in the trait definition. | ||
19 | // | ||
20 | // ``` | ||
21 | // trait Foo { | ||
22 | // fn a() {} | ||
23 | // fn b() {} | ||
24 | // fn c() {} | ||
25 | // } | ||
26 | // | ||
27 | // struct Bar; | ||
28 | // $0impl Foo for Bar { | ||
29 | // fn b() {} | ||
30 | // fn c() {} | ||
31 | // fn a() {} | ||
32 | // } | ||
33 | // ``` | ||
34 | // -> | ||
35 | // ``` | ||
36 | // trait Foo { | ||
37 | // fn a() {} | ||
38 | // fn b() {} | ||
39 | // fn c() {} | ||
40 | // } | ||
41 | // | ||
42 | // struct Bar; | ||
43 | // impl Foo for Bar { | ||
44 | // fn a() {} | ||
45 | // fn b() {} | ||
46 | // fn c() {} | ||
47 | // } | ||
48 | // ``` | ||
49 | // | ||
50 | pub(crate) fn reorder_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | ||
51 | let impl_ast = ctx.find_node_at_offset::<ast::Impl>()?; | ||
52 | let items = impl_ast.assoc_item_list()?; | ||
53 | let methods = get_methods(&items); | ||
54 | |||
55 | let path = impl_ast | ||
56 | .trait_() | ||
57 | .and_then(|t| match t { | ||
58 | ast::Type::PathType(path) => Some(path), | ||
59 | _ => None, | ||
60 | })? | ||
61 | .path()?; | ||
62 | |||
63 | let ranks = compute_method_ranks(&path, ctx)?; | ||
64 | let sorted: Vec<_> = methods | ||
65 | .iter() | ||
66 | .cloned() | ||
67 | .sorted_by_key(|f| { | ||
68 | f.name().and_then(|n| ranks.get(&n.to_string()).copied()).unwrap_or(usize::max_value()) | ||
69 | }) | ||
70 | .collect(); | ||
71 | |||
72 | // Don't edit already sorted methods: | ||
73 | if methods == sorted { | ||
74 | mark::hit!(not_applicable_if_sorted); | ||
75 | return None; | ||
76 | } | ||
77 | |||
78 | let target = items.syntax().text_range(); | ||
79 | acc.add(AssistId("reorder_impl", AssistKind::RefactorRewrite), "Sort methods", target, |edit| { | ||
80 | let mut rewriter = algo::SyntaxRewriter::default(); | ||
81 | for (old, new) in methods.iter().zip(&sorted) { | ||
82 | rewriter.replace(old.syntax(), new.syntax()); | ||
83 | } | ||
84 | edit.rewrite(rewriter); | ||
85 | }) | ||
86 | } | ||
87 | |||
88 | fn compute_method_ranks(path: &ast::Path, ctx: &AssistContext) -> Option<FxHashMap<String, usize>> { | ||
89 | let td = trait_definition(path, &ctx.sema)?; | ||
90 | |||
91 | Some( | ||
92 | td.items(ctx.db()) | ||
93 | .iter() | ||
94 | .flat_map(|i| match i { | ||
95 | hir::AssocItem::Function(f) => Some(f), | ||
96 | _ => None, | ||
97 | }) | ||
98 | .enumerate() | ||
99 | .map(|(idx, func)| ((func.name(ctx.db()).to_string(), idx))) | ||
100 | .collect(), | ||
101 | ) | ||
102 | } | ||
103 | |||
104 | fn trait_definition(path: &ast::Path, sema: &Semantics<RootDatabase>) -> Option<hir::Trait> { | ||
105 | match sema.resolve_path(path)? { | ||
106 | PathResolution::Def(hir::ModuleDef::Trait(trait_)) => Some(trait_), | ||
107 | _ => None, | ||
108 | } | ||
109 | } | ||
110 | |||
111 | fn get_methods(items: &ast::AssocItemList) -> Vec<ast::Fn> { | ||
112 | items | ||
113 | .assoc_items() | ||
114 | .flat_map(|i| match i { | ||
115 | ast::AssocItem::Fn(f) => Some(f), | ||
116 | _ => None, | ||
117 | }) | ||
118 | .filter(|f| f.name().is_some()) | ||
119 | .collect() | ||
120 | } | ||
121 | |||
122 | #[cfg(test)] | ||
123 | mod tests { | ||
124 | use test_utils::mark; | ||
125 | |||
126 | use crate::tests::{check_assist, check_assist_not_applicable}; | ||
127 | |||
128 | use super::*; | ||
129 | |||
130 | #[test] | ||
131 | fn not_applicable_if_sorted() { | ||
132 | mark::check!(not_applicable_if_sorted); | ||
133 | check_assist_not_applicable( | ||
134 | reorder_impl, | ||
135 | r#" | ||
136 | trait Bar { | ||
137 | fn a() {} | ||
138 | fn z() {} | ||
139 | fn b() {} | ||
140 | } | ||
141 | struct Foo; | ||
142 | $0impl Bar for Foo { | ||
143 | fn a() {} | ||
144 | fn z() {} | ||
145 | fn b() {} | ||
146 | } | ||
147 | "#, | ||
148 | ) | ||
149 | } | ||
150 | |||
151 | #[test] | ||
152 | fn not_applicable_if_empty() { | ||
153 | check_assist_not_applicable( | ||
154 | reorder_impl, | ||
155 | r#" | ||
156 | trait Bar {}; | ||
157 | struct Foo; | ||
158 | $0impl Bar for Foo {} | ||
159 | "#, | ||
160 | ) | ||
161 | } | ||
162 | |||
163 | #[test] | ||
164 | fn reorder_impl_trait_methods() { | ||
165 | check_assist( | ||
166 | reorder_impl, | ||
167 | r#" | ||
168 | trait Bar { | ||
169 | fn a() {} | ||
170 | fn c() {} | ||
171 | fn b() {} | ||
172 | fn d() {} | ||
173 | } | ||
174 | |||
175 | struct Foo; | ||
176 | $0impl Bar for Foo { | ||
177 | fn d() {} | ||
178 | fn b() {} | ||
179 | fn c() {} | ||
180 | fn a() {} | ||
181 | } | ||
182 | "#, | ||
183 | r#" | ||
184 | trait Bar { | ||
185 | fn a() {} | ||
186 | fn c() {} | ||
187 | fn b() {} | ||
188 | fn d() {} | ||
189 | } | ||
190 | |||
191 | struct Foo; | ||
192 | impl Bar for Foo { | ||
193 | fn a() {} | ||
194 | fn c() {} | ||
195 | fn b() {} | ||
196 | fn d() {} | ||
197 | } | ||
198 | "#, | ||
199 | ) | ||
200 | } | ||
201 | } | ||
diff --git a/crates/assists/src/lib.rs b/crates/assists/src/lib.rs index 90009c55a..1080294ab 100644 --- a/crates/assists/src/lib.rs +++ b/crates/assists/src/lib.rs | |||
@@ -146,6 +146,7 @@ mod handlers { | |||
146 | mod remove_mut; | 146 | mod remove_mut; |
147 | mod remove_unused_param; | 147 | mod remove_unused_param; |
148 | mod reorder_fields; | 148 | mod reorder_fields; |
149 | mod reorder_impl; | ||
149 | mod replace_derive_with_manual_impl; | 150 | mod replace_derive_with_manual_impl; |
150 | mod replace_if_let_with_match; | 151 | mod replace_if_let_with_match; |
151 | mod replace_impl_trait_with_generic; | 152 | mod replace_impl_trait_with_generic; |
@@ -202,6 +203,7 @@ mod handlers { | |||
202 | remove_mut::remove_mut, | 203 | remove_mut::remove_mut, |
203 | remove_unused_param::remove_unused_param, | 204 | remove_unused_param::remove_unused_param, |
204 | reorder_fields::reorder_fields, | 205 | reorder_fields::reorder_fields, |
206 | reorder_impl::reorder_impl, | ||
205 | replace_derive_with_manual_impl::replace_derive_with_manual_impl, | 207 | replace_derive_with_manual_impl::replace_derive_with_manual_impl, |
206 | replace_if_let_with_match::replace_if_let_with_match, | 208 | replace_if_let_with_match::replace_if_let_with_match, |
207 | replace_if_let_with_match::replace_match_with_if_let, | 209 | replace_if_let_with_match::replace_match_with_if_let, |
diff --git a/crates/assists/src/tests/generated.rs b/crates/assists/src/tests/generated.rs index e28837b53..217f577eb 100644 --- a/crates/assists/src/tests/generated.rs +++ b/crates/assists/src/tests/generated.rs | |||
@@ -896,6 +896,41 @@ const test: Foo = Foo {foo: 1, bar: 0} | |||
896 | } | 896 | } |
897 | 897 | ||
898 | #[test] | 898 | #[test] |
899 | fn doctest_reorder_impl() { | ||
900 | check_doc_test( | ||
901 | "reorder_impl", | ||
902 | r#####" | ||
903 | trait Foo { | ||
904 | fn a() {} | ||
905 | fn b() {} | ||
906 | fn c() {} | ||
907 | } | ||
908 | |||
909 | struct Bar; | ||
910 | $0impl Foo for Bar { | ||
911 | fn b() {} | ||
912 | fn c() {} | ||
913 | fn a() {} | ||
914 | } | ||
915 | "#####, | ||
916 | r#####" | ||
917 | trait Foo { | ||
918 | fn a() {} | ||
919 | fn b() {} | ||
920 | fn c() {} | ||
921 | } | ||
922 | |||
923 | struct Bar; | ||
924 | impl Foo for Bar { | ||
925 | fn a() {} | ||
926 | fn b() {} | ||
927 | fn c() {} | ||
928 | } | ||
929 | "#####, | ||
930 | ) | ||
931 | } | ||
932 | |||
933 | #[test] | ||
899 | fn doctest_replace_derive_with_manual_impl() { | 934 | fn doctest_replace_derive_with_manual_impl() { |
900 | check_doc_test( | 935 | check_doc_test( |
901 | "replace_derive_with_manual_impl", | 936 | "replace_derive_with_manual_impl", |