diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-02-09 08:52:09 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-02-09 08:52:09 +0000 |
commit | 3e8351fb0607f8711749b00d80f68bf25de01a76 (patch) | |
tree | 97388dafe71ececcbaf97249021b9c8d49786ccf /crates/ra_assists/src/change_visibility.rs | |
parent | 12e3b4c70b5ef23b2fdfc197296d483680e125f9 (diff) | |
parent | 4fdeb54bb5c7ba0704839a65996766d223c51fc1 (diff) |
Merge #768
768: Sort assists by the range of the affected element r=matklad a=robojumper
Closes #763.
https://github.com/rust-analyzer/rust-analyzer/blob/3be98f2ac93b278828e76eb813bdd8033f647b12/crates/ra_assists/src/lib.rs#L233-L236
This could be made more robust by a) adding a way to identify actions by things other than their label and b) allowing arbitrary actions to appear in the list as long as the tested actions are there in the correct order. Let me know if I should do any of that.
Co-authored-by: robojumper <[email protected]>
Diffstat (limited to 'crates/ra_assists/src/change_visibility.rs')
-rw-r--r-- | crates/ra_assists/src/change_visibility.rs | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/crates/ra_assists/src/change_visibility.rs b/crates/ra_assists/src/change_visibility.rs index fa5f231c8..6d9a4eec2 100644 --- a/crates/ra_assists/src/change_visibility.rs +++ b/crates/ra_assists/src/change_visibility.rs | |||
@@ -20,7 +20,7 @@ fn add_vis(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | |||
20 | _ => false, | 20 | _ => false, |
21 | }); | 21 | }); |
22 | 22 | ||
23 | let offset = if let Some(keyword) = item_keyword { | 23 | let (offset, target) = if let Some(keyword) = item_keyword { |
24 | let parent = keyword.parent()?; | 24 | let parent = keyword.parent()?; |
25 | let def_kws = vec![FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF]; | 25 | let def_kws = vec![FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF]; |
26 | // Parent is not a definition, can't add visibility | 26 | // Parent is not a definition, can't add visibility |
@@ -31,17 +31,18 @@ fn add_vis(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | |||
31 | if parent.children().any(|child| child.kind() == VISIBILITY) { | 31 | if parent.children().any(|child| child.kind() == VISIBILITY) { |
32 | return None; | 32 | return None; |
33 | } | 33 | } |
34 | vis_offset(parent) | 34 | (vis_offset(parent), keyword.range()) |
35 | } else { | 35 | } else { |
36 | let ident = ctx.leaf_at_offset().find(|leaf| leaf.kind() == IDENT)?; | 36 | let ident = ctx.leaf_at_offset().find(|leaf| leaf.kind() == IDENT)?; |
37 | let field = ident.ancestors().find_map(ast::NamedFieldDef::cast)?; | 37 | let field = ident.ancestors().find_map(ast::NamedFieldDef::cast)?; |
38 | if field.name()?.syntax().range() != ident.range() && field.visibility().is_some() { | 38 | if field.name()?.syntax().range() != ident.range() && field.visibility().is_some() { |
39 | return None; | 39 | return None; |
40 | } | 40 | } |
41 | vis_offset(field.syntax()) | 41 | (vis_offset(field.syntax()), ident.range()) |
42 | }; | 42 | }; |
43 | 43 | ||
44 | ctx.build("make pub(crate)", |edit| { | 44 | ctx.build("make pub(crate)", |edit| { |
45 | edit.target(target); | ||
45 | edit.insert(offset, "pub(crate) "); | 46 | edit.insert(offset, "pub(crate) "); |
46 | edit.set_cursor(offset); | 47 | edit.set_cursor(offset); |
47 | }) | 48 | }) |
@@ -60,13 +61,15 @@ fn vis_offset(node: &SyntaxNode) -> TextUnit { | |||
60 | 61 | ||
61 | fn change_vis(ctx: AssistCtx<impl HirDatabase>, vis: &ast::Visibility) -> Option<Assist> { | 62 | fn change_vis(ctx: AssistCtx<impl HirDatabase>, vis: &ast::Visibility) -> Option<Assist> { |
62 | if vis.syntax().text() == "pub" { | 63 | if vis.syntax().text() == "pub" { |
63 | return ctx.build("chage to pub(crate)", |edit| { | 64 | return ctx.build("change to pub(crate)", |edit| { |
65 | edit.target(vis.syntax().range()); | ||
64 | edit.replace(vis.syntax().range(), "pub(crate)"); | 66 | edit.replace(vis.syntax().range(), "pub(crate)"); |
65 | edit.set_cursor(vis.syntax().range().start()); | 67 | edit.set_cursor(vis.syntax().range().start()); |
66 | }); | 68 | }); |
67 | } | 69 | } |
68 | if vis.syntax().text() == "pub(crate)" { | 70 | if vis.syntax().text() == "pub(crate)" { |
69 | return ctx.build("chage to pub", |edit| { | 71 | return ctx.build("change to pub", |edit| { |
72 | edit.target(vis.syntax().range()); | ||
70 | edit.replace(vis.syntax().range(), "pub"); | 73 | edit.replace(vis.syntax().range(), "pub"); |
71 | edit.set_cursor(vis.syntax().range().start()); | 74 | edit.set_cursor(vis.syntax().range().start()); |
72 | }); | 75 | }); |
@@ -77,7 +80,7 @@ fn change_vis(ctx: AssistCtx<impl HirDatabase>, vis: &ast::Visibility) -> Option | |||
77 | #[cfg(test)] | 80 | #[cfg(test)] |
78 | mod tests { | 81 | mod tests { |
79 | use super::*; | 82 | use super::*; |
80 | use crate::helpers::check_assist; | 83 | use crate::helpers::{check_assist, check_assist_target}; |
81 | 84 | ||
82 | #[test] | 85 | #[test] |
83 | fn change_visibility_adds_pub_crate_to_items() { | 86 | fn change_visibility_adds_pub_crate_to_items() { |
@@ -135,4 +138,11 @@ mod tests { | |||
135 | ", | 138 | ", |
136 | ) | 139 | ) |
137 | } | 140 | } |
141 | |||
142 | #[test] | ||
143 | fn change_visibility_target() { | ||
144 | check_assist_target(change_visibility, "<|>fn foo() {}", "fn"); | ||
145 | check_assist_target(change_visibility, "pub(crate)<|> fn foo() {}", "pub(crate)"); | ||
146 | check_assist_target(change_visibility, "struct S { <|>field: u32 }", "field"); | ||
147 | } | ||
138 | } | 148 | } |