diff options
author | Aleksey Kladov <[email protected]> | 2019-01-08 19:17:36 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-08 19:17:36 +0000 |
commit | 1967884d6836219ee78a754ca5c66ac781351559 (patch) | |
tree | 7594f37cd0a5200eb097b9d472c61f0223d01d05 /crates/ra_editor/src/assists/change_visibility.rs | |
parent | 4f4f7933b1b7ff34f8633b1686b18b2d1b994c47 (diff) |
rename ra_editor -> ra_ide_api_light
Diffstat (limited to 'crates/ra_editor/src/assists/change_visibility.rs')
-rw-r--r-- | crates/ra_editor/src/assists/change_visibility.rs | 116 |
1 files changed, 0 insertions, 116 deletions
diff --git a/crates/ra_editor/src/assists/change_visibility.rs b/crates/ra_editor/src/assists/change_visibility.rs deleted file mode 100644 index 89729e2c2..000000000 --- a/crates/ra_editor/src/assists/change_visibility.rs +++ /dev/null | |||
@@ -1,116 +0,0 @@ | |||
1 | use ra_syntax::{ | ||
2 | AstNode, | ||
3 | ast::{self, VisibilityOwner, NameOwner}, | ||
4 | SyntaxKind::{VISIBILITY, FN_KW, MOD_KW, STRUCT_KW, ENUM_KW, TRAIT_KW, FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF, IDENT}, | ||
5 | }; | ||
6 | |||
7 | use crate::assists::{AssistCtx, Assist}; | ||
8 | |||
9 | pub fn change_visibility(ctx: AssistCtx) -> Option<Assist> { | ||
10 | if let Some(vis) = ctx.node_at_offset::<ast::Visibility>() { | ||
11 | return change_vis(ctx, vis); | ||
12 | } | ||
13 | add_vis(ctx) | ||
14 | } | ||
15 | |||
16 | fn add_vis(ctx: AssistCtx) -> Option<Assist> { | ||
17 | let item_keyword = ctx.leaf_at_offset().find(|leaf| match leaf.kind() { | ||
18 | FN_KW | MOD_KW | STRUCT_KW | ENUM_KW | TRAIT_KW => true, | ||
19 | _ => false, | ||
20 | }); | ||
21 | |||
22 | let offset = if let Some(keyword) = item_keyword { | ||
23 | let parent = keyword.parent()?; | ||
24 | let def_kws = vec![FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF]; | ||
25 | // Parent is not a definition, can't add visibility | ||
26 | if !def_kws.iter().any(|&def_kw| def_kw == parent.kind()) { | ||
27 | return None; | ||
28 | } | ||
29 | // Already have visibility, do nothing | ||
30 | if parent.children().any(|child| child.kind() == VISIBILITY) { | ||
31 | return None; | ||
32 | } | ||
33 | parent.range().start() | ||
34 | } else { | ||
35 | let ident = ctx.leaf_at_offset().find(|leaf| leaf.kind() == IDENT)?; | ||
36 | let field = ident.ancestors().find_map(ast::NamedFieldDef::cast)?; | ||
37 | if field.name()?.syntax().range() != ident.range() && field.visibility().is_some() { | ||
38 | return None; | ||
39 | } | ||
40 | field.syntax().range().start() | ||
41 | }; | ||
42 | |||
43 | ctx.build("make pub(crate)", |edit| { | ||
44 | edit.insert(offset, "pub(crate) "); | ||
45 | edit.set_cursor(offset); | ||
46 | }) | ||
47 | } | ||
48 | |||
49 | fn change_vis(ctx: AssistCtx, vis: &ast::Visibility) -> Option<Assist> { | ||
50 | if vis.syntax().text() != "pub" { | ||
51 | return None; | ||
52 | } | ||
53 | ctx.build("chage to pub(crate)", |edit| { | ||
54 | edit.replace(vis.syntax().range(), "pub(crate)"); | ||
55 | edit.set_cursor(vis.syntax().range().start()); | ||
56 | }) | ||
57 | } | ||
58 | |||
59 | #[cfg(test)] | ||
60 | mod tests { | ||
61 | use super::*; | ||
62 | use crate::assists::check_assist; | ||
63 | |||
64 | #[test] | ||
65 | fn change_visibility_adds_pub_crate_to_items() { | ||
66 | check_assist( | ||
67 | change_visibility, | ||
68 | "<|>fn foo() {}", | ||
69 | "<|>pub(crate) fn foo() {}", | ||
70 | ); | ||
71 | check_assist( | ||
72 | change_visibility, | ||
73 | "f<|>n foo() {}", | ||
74 | "<|>pub(crate) fn foo() {}", | ||
75 | ); | ||
76 | check_assist( | ||
77 | change_visibility, | ||
78 | "<|>struct Foo {}", | ||
79 | "<|>pub(crate) struct Foo {}", | ||
80 | ); | ||
81 | check_assist( | ||
82 | change_visibility, | ||
83 | "<|>mod foo {}", | ||
84 | "<|>pub(crate) mod foo {}", | ||
85 | ); | ||
86 | check_assist( | ||
87 | change_visibility, | ||
88 | "<|>trait Foo {}", | ||
89 | "<|>pub(crate) trait Foo {}", | ||
90 | ); | ||
91 | check_assist(change_visibility, "m<|>od {}", "<|>pub(crate) mod {}"); | ||
92 | check_assist( | ||
93 | change_visibility, | ||
94 | "unsafe f<|>n foo() {}", | ||
95 | "<|>pub(crate) unsafe fn foo() {}", | ||
96 | ); | ||
97 | } | ||
98 | |||
99 | #[test] | ||
100 | fn change_visibility_works_with_struct_fields() { | ||
101 | check_assist( | ||
102 | change_visibility, | ||
103 | "struct S { <|>field: u32 }", | ||
104 | "struct S { <|>pub(crate) field: u32 }", | ||
105 | ) | ||
106 | } | ||
107 | |||
108 | #[test] | ||
109 | fn change_visibility_pub_to_pub_crate() { | ||
110 | check_assist( | ||
111 | change_visibility, | ||
112 | "<|>pub fn foo() {}", | ||
113 | "<|>pub(crate) fn foo() {}", | ||
114 | ) | ||
115 | } | ||
116 | } | ||