diff options
Diffstat (limited to 'crates/ra_assists')
-rw-r--r-- | crates/ra_assists/src/assists/add_derive.rs | 18 | ||||
-rw-r--r-- | crates/ra_assists/src/assists/add_explicit_type.rs | 16 | ||||
-rw-r--r-- | crates/ra_assists/src/assists/add_impl.rs | 19 | ||||
-rw-r--r-- | crates/ra_assists/src/assists/add_missing_impl_members.rs | 58 | ||||
-rw-r--r-- | crates/ra_assists/src/assists/apply_demorgan.rs | 26 | ||||
-rw-r--r-- | crates/ra_assists/src/doc_tests.rs | 6 | ||||
-rw-r--r-- | crates/ra_assists/src/doc_tests/generated.rs | 139 |
7 files changed, 262 insertions, 20 deletions
diff --git a/crates/ra_assists/src/assists/add_derive.rs b/crates/ra_assists/src/assists/add_derive.rs index 77ecc33c9..d3ba634c4 100644 --- a/crates/ra_assists/src/assists/add_derive.rs +++ b/crates/ra_assists/src/assists/add_derive.rs | |||
@@ -1,5 +1,3 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use hir::db::HirDatabase; | 1 | use hir::db::HirDatabase; |
4 | use ra_syntax::{ | 2 | use ra_syntax::{ |
5 | ast::{self, AstNode, AttrsOwner}, | 3 | ast::{self, AstNode, AttrsOwner}, |
@@ -9,6 +7,22 @@ use ra_syntax::{ | |||
9 | 7 | ||
10 | use crate::{Assist, AssistCtx, AssistId}; | 8 | use crate::{Assist, AssistCtx, AssistId}; |
11 | 9 | ||
10 | // Assist: add_derive | ||
11 | // Adds a new `#[derive()]` clause to a struct or enum. | ||
12 | // ``` | ||
13 | // struct Point { | ||
14 | // x: u32, | ||
15 | // y: u32,<|> | ||
16 | // } | ||
17 | // ``` | ||
18 | // -> | ||
19 | // ``` | ||
20 | // #[derive()] | ||
21 | // struct Point { | ||
22 | // x: u32, | ||
23 | // y: u32, | ||
24 | // } | ||
25 | // ``` | ||
12 | pub(crate) fn add_derive(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 26 | pub(crate) fn add_derive(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
13 | let nominal = ctx.node_at_offset::<ast::NominalDef>()?; | 27 | let nominal = ctx.node_at_offset::<ast::NominalDef>()?; |
14 | let node_start = derive_insertion_offset(&nominal)?; | 28 | let node_start = derive_insertion_offset(&nominal)?; |
diff --git a/crates/ra_assists/src/assists/add_explicit_type.rs b/crates/ra_assists/src/assists/add_explicit_type.rs index 8c83dc987..33b7bea7f 100644 --- a/crates/ra_assists/src/assists/add_explicit_type.rs +++ b/crates/ra_assists/src/assists/add_explicit_type.rs | |||
@@ -1,5 +1,3 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use hir::{db::HirDatabase, HirDisplay, Ty}; | 1 | use hir::{db::HirDatabase, HirDisplay, Ty}; |
4 | use ra_syntax::{ | 2 | use ra_syntax::{ |
5 | ast::{self, AstNode, LetStmt, NameOwner}, | 3 | ast::{self, AstNode, LetStmt, NameOwner}, |
@@ -8,7 +6,19 @@ use ra_syntax::{ | |||
8 | 6 | ||
9 | use crate::{Assist, AssistCtx, AssistId}; | 7 | use crate::{Assist, AssistCtx, AssistId}; |
10 | 8 | ||
11 | /// Add explicit type assist. | 9 | // Assist: add_explicit_type |
10 | // Specify type for a let binding | ||
11 | // ``` | ||
12 | // fn main() { | ||
13 | // let x<|> = 92; | ||
14 | // } | ||
15 | // ``` | ||
16 | // -> | ||
17 | // ``` | ||
18 | // fn main() { | ||
19 | // let x: i32 = 92; | ||
20 | // } | ||
21 | // ``` | ||
12 | pub(crate) fn add_explicit_type(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 22 | pub(crate) fn add_explicit_type(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
13 | let stmt = ctx.node_at_offset::<LetStmt>()?; | 23 | let stmt = ctx.node_at_offset::<LetStmt>()?; |
14 | let expr = stmt.initializer()?; | 24 | let expr = stmt.initializer()?; |
diff --git a/crates/ra_assists/src/assists/add_impl.rs b/crates/ra_assists/src/assists/add_impl.rs index 94801fbc9..40bc5c464 100644 --- a/crates/ra_assists/src/assists/add_impl.rs +++ b/crates/ra_assists/src/assists/add_impl.rs | |||
@@ -1,5 +1,3 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use format_buf::format; | 1 | use format_buf::format; |
4 | use hir::db::HirDatabase; | 2 | use hir::db::HirDatabase; |
5 | use join_to_string::join; | 3 | use join_to_string::join; |
@@ -10,6 +8,23 @@ use ra_syntax::{ | |||
10 | 8 | ||
11 | use crate::{Assist, AssistCtx, AssistId}; | 9 | use crate::{Assist, AssistCtx, AssistId}; |
12 | 10 | ||
11 | // Assist: add_impl | ||
12 | // Adds a new inherent impl for a type | ||
13 | // ``` | ||
14 | // struct Ctx<T: Clone> { | ||
15 | // data: T,<|> | ||
16 | // } | ||
17 | // ``` | ||
18 | // -> | ||
19 | // ``` | ||
20 | // struct Ctx<T: Clone> { | ||
21 | // data: T, | ||
22 | // } | ||
23 | // | ||
24 | // impl<T: Clone> Ctx<T> { | ||
25 | // | ||
26 | // } | ||
27 | // ``` | ||
13 | pub(crate) fn add_impl(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 28 | pub(crate) fn add_impl(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
14 | let nominal = ctx.node_at_offset::<ast::NominalDef>()?; | 29 | let nominal = ctx.node_at_offset::<ast::NominalDef>()?; |
15 | let name = nominal.name()?; | 30 | let name = nominal.name()?; |
diff --git a/crates/ra_assists/src/assists/add_missing_impl_members.rs b/crates/ra_assists/src/assists/add_missing_impl_members.rs index 565b96fb5..36fa6f9ea 100644 --- a/crates/ra_assists/src/assists/add_missing_impl_members.rs +++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs | |||
@@ -1,5 +1,3 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use hir::{db::HirDatabase, HasSource}; | 1 | use hir::{db::HirDatabase, HasSource}; |
4 | use ra_syntax::{ | 2 | use ra_syntax::{ |
5 | ast::{self, edit, make, AstNode, NameOwner}, | 3 | ast::{self, edit, make, AstNode, NameOwner}, |
@@ -14,6 +12,32 @@ enum AddMissingImplMembersMode { | |||
14 | NoDefaultMethods, | 12 | NoDefaultMethods, |
15 | } | 13 | } |
16 | 14 | ||
15 | // Assist: add_impl_missing_members | ||
16 | // Adds scaffold for required impl members | ||
17 | // ``` | ||
18 | // trait T { | ||
19 | // Type X; | ||
20 | // fn foo(&self); | ||
21 | // fn bar(&self) {} | ||
22 | // } | ||
23 | // | ||
24 | // impl T for () {<|> | ||
25 | // | ||
26 | // } | ||
27 | // ``` | ||
28 | // -> | ||
29 | // ``` | ||
30 | // trait T { | ||
31 | // Type X; | ||
32 | // fn foo(&self); | ||
33 | // fn bar(&self) {} | ||
34 | // } | ||
35 | // | ||
36 | // impl T for () { | ||
37 | // fn foo(&self) { unimplemented!() } | ||
38 | // | ||
39 | // } | ||
40 | // ``` | ||
17 | pub(crate) fn add_missing_impl_members(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 41 | pub(crate) fn add_missing_impl_members(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
18 | add_missing_impl_members_inner( | 42 | add_missing_impl_members_inner( |
19 | ctx, | 43 | ctx, |
@@ -23,6 +47,36 @@ pub(crate) fn add_missing_impl_members(ctx: AssistCtx<impl HirDatabase>) -> Opti | |||
23 | ) | 47 | ) |
24 | } | 48 | } |
25 | 49 | ||
50 | // Assist: add_impl_default_members | ||
51 | // Adds scaffold for overriding default impl members | ||
52 | // ``` | ||
53 | // trait T { | ||
54 | // Type X; | ||
55 | // fn foo(&self); | ||
56 | // fn bar(&self) {} | ||
57 | // } | ||
58 | // | ||
59 | // impl T for () { | ||
60 | // Type X = (); | ||
61 | // fn foo(&self) {}<|> | ||
62 | // | ||
63 | // } | ||
64 | // ``` | ||
65 | // -> | ||
66 | // ``` | ||
67 | // trait T { | ||
68 | // Type X; | ||
69 | // fn foo(&self); | ||
70 | // fn bar(&self) {} | ||
71 | // } | ||
72 | // | ||
73 | // impl T for () { | ||
74 | // Type X = (); | ||
75 | // fn foo(&self) {} | ||
76 | // fn bar(&self) {} | ||
77 | // | ||
78 | // } | ||
79 | // ``` | ||
26 | pub(crate) fn add_missing_default_members(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 80 | pub(crate) fn add_missing_default_members(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
27 | add_missing_impl_members_inner( | 81 | add_missing_impl_members_inner( |
28 | ctx, | 82 | ctx, |
diff --git a/crates/ra_assists/src/assists/apply_demorgan.rs b/crates/ra_assists/src/assists/apply_demorgan.rs index 5f2b0dd18..a072f63e7 100644 --- a/crates/ra_assists/src/assists/apply_demorgan.rs +++ b/crates/ra_assists/src/assists/apply_demorgan.rs | |||
@@ -1,18 +1,26 @@ | |||
1 | //! This contains the functions associated with the demorgan assist. | ||
2 | //! This assist transforms boolean expressions of the form `!a || !b` into | ||
3 | //! `!(a && b)`. | ||
4 | use hir::db::HirDatabase; | 1 | use hir::db::HirDatabase; |
5 | use ra_syntax::ast::{self, AstNode}; | 2 | use ra_syntax::ast::{self, AstNode}; |
6 | use ra_syntax::SyntaxNode; | 3 | use ra_syntax::SyntaxNode; |
7 | 4 | ||
8 | use crate::{Assist, AssistCtx, AssistId}; | 5 | use crate::{Assist, AssistCtx, AssistId}; |
9 | 6 | ||
10 | /// Assist for applying demorgan's law | 7 | // Assist: apply_demorgan |
11 | /// | 8 | // Apply [De Morgan's law](https://en.wikipedia.org/wiki/De_Morgan%27s_laws). |
12 | /// This transforms expressions of the form `!l || !r` into `!(l && r)`. | 9 | // This transforms expressions of the form `!l || !r` into `!(l && r)`. |
13 | /// This also works with `&&`. This assist can only be applied with the cursor | 10 | // This also works with `&&`. This assist can only be applied with the cursor |
14 | /// on either `||` or `&&`, with both operands being a negation of some kind. | 11 | // on either `||` or `&&`, with both operands being a negation of some kind. |
15 | /// This means something of the form `!x` or `x != y`. | 12 | // This means something of the form `!x` or `x != y`. |
13 | // ``` | ||
14 | // fn main() { | ||
15 | // if x != 4 ||<|> !y {} | ||
16 | // } | ||
17 | // ``` | ||
18 | // -> | ||
19 | // ``` | ||
20 | // fn main() { | ||
21 | // if !(x == 4 && y) {} | ||
22 | // } | ||
23 | // ``` | ||
16 | pub(crate) fn apply_demorgan(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 24 | pub(crate) fn apply_demorgan(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
17 | let expr = ctx.node_at_offset::<ast::BinExpr>()?; | 25 | let expr = ctx.node_at_offset::<ast::BinExpr>()?; |
18 | let op = expr.op_kind()?; | 26 | let op = expr.op_kind()?; |
diff --git a/crates/ra_assists/src/doc_tests.rs b/crates/ra_assists/src/doc_tests.rs index 88e901517..872bbdf17 100644 --- a/crates/ra_assists/src/doc_tests.rs +++ b/crates/ra_assists/src/doc_tests.rs | |||
@@ -15,8 +15,10 @@ fn check(assist_id: &str, before: &str, after: &str) { | |||
15 | let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); | 15 | let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); |
16 | let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; | 16 | let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; |
17 | 17 | ||
18 | let (_assist_id, action) = | 18 | let (_assist_id, action) = crate::assists(&db, frange) |
19 | crate::assists(&db, frange).into_iter().find(|(id, _)| id.id.0 == assist_id).unwrap(); | 19 | .into_iter() |
20 | .find(|(id, _)| id.id.0 == assist_id) | ||
21 | .unwrap_or_else(|| panic!("Assist {:?} is not applicable", assist_id)); | ||
20 | 22 | ||
21 | let actual = action.edit.apply(&before); | 23 | let actual = action.edit.apply(&before); |
22 | assert_eq_text!(after, &actual); | 24 | assert_eq_text!(after, &actual); |
diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs index e5f6910f1..76d86b93d 100644 --- a/crates/ra_assists/src/doc_tests/generated.rs +++ b/crates/ra_assists/src/doc_tests/generated.rs | |||
@@ -3,6 +3,145 @@ | |||
3 | use super::check; | 3 | use super::check; |
4 | 4 | ||
5 | #[test] | 5 | #[test] |
6 | fn doctest_add_derive() { | ||
7 | check( | ||
8 | "add_derive", | ||
9 | r#####" | ||
10 | struct Point { | ||
11 | x: u32, | ||
12 | y: u32,<|> | ||
13 | } | ||
14 | "#####, | ||
15 | r#####" | ||
16 | #[derive()] | ||
17 | struct Point { | ||
18 | x: u32, | ||
19 | y: u32, | ||
20 | } | ||
21 | "#####, | ||
22 | ) | ||
23 | } | ||
24 | |||
25 | #[test] | ||
26 | fn doctest_add_explicit_type() { | ||
27 | check( | ||
28 | "add_explicit_type", | ||
29 | r#####" | ||
30 | fn main() { | ||
31 | let x<|> = 92; | ||
32 | } | ||
33 | "#####, | ||
34 | r#####" | ||
35 | fn main() { | ||
36 | let x: i32 = 92; | ||
37 | } | ||
38 | "#####, | ||
39 | ) | ||
40 | } | ||
41 | |||
42 | #[test] | ||
43 | fn doctest_add_impl() { | ||
44 | check( | ||
45 | "add_impl", | ||
46 | r#####" | ||
47 | struct Ctx<T: Clone> { | ||
48 | data: T,<|> | ||
49 | } | ||
50 | "#####, | ||
51 | r#####" | ||
52 | struct Ctx<T: Clone> { | ||
53 | data: T, | ||
54 | } | ||
55 | |||
56 | impl<T: Clone> Ctx<T> { | ||
57 | |||
58 | } | ||
59 | "#####, | ||
60 | ) | ||
61 | } | ||
62 | |||
63 | #[test] | ||
64 | fn doctest_add_impl_default_members() { | ||
65 | check( | ||
66 | "add_impl_default_members", | ||
67 | r#####" | ||
68 | trait T { | ||
69 | Type X; | ||
70 | fn foo(&self); | ||
71 | fn bar(&self) {} | ||
72 | } | ||
73 | |||
74 | impl T for () { | ||
75 | Type X = (); | ||
76 | fn foo(&self) {}<|> | ||
77 | |||
78 | } | ||
79 | "#####, | ||
80 | r#####" | ||
81 | trait T { | ||
82 | Type X; | ||
83 | fn foo(&self); | ||
84 | fn bar(&self) {} | ||
85 | } | ||
86 | |||
87 | impl T for () { | ||
88 | Type X = (); | ||
89 | fn foo(&self) {} | ||
90 | fn bar(&self) {} | ||
91 | |||
92 | } | ||
93 | "#####, | ||
94 | ) | ||
95 | } | ||
96 | |||
97 | #[test] | ||
98 | fn doctest_add_impl_missing_members() { | ||
99 | check( | ||
100 | "add_impl_missing_members", | ||
101 | r#####" | ||
102 | trait T { | ||
103 | Type X; | ||
104 | fn foo(&self); | ||
105 | fn bar(&self) {} | ||
106 | } | ||
107 | |||
108 | impl T for () {<|> | ||
109 | |||
110 | } | ||
111 | "#####, | ||
112 | r#####" | ||
113 | trait T { | ||
114 | Type X; | ||
115 | fn foo(&self); | ||
116 | fn bar(&self) {} | ||
117 | } | ||
118 | |||
119 | impl T for () { | ||
120 | fn foo(&self) { unimplemented!() } | ||
121 | |||
122 | } | ||
123 | "#####, | ||
124 | ) | ||
125 | } | ||
126 | |||
127 | #[test] | ||
128 | fn doctest_apply_demorgan() { | ||
129 | check( | ||
130 | "apply_demorgan", | ||
131 | r#####" | ||
132 | fn main() { | ||
133 | if x != 4 ||<|> !y {} | ||
134 | } | ||
135 | "#####, | ||
136 | r#####" | ||
137 | fn main() { | ||
138 | if !(x == 4 && y) {} | ||
139 | } | ||
140 | "#####, | ||
141 | ) | ||
142 | } | ||
143 | |||
144 | #[test] | ||
6 | fn doctest_convert_to_guarded_return() { | 145 | fn doctest_convert_to_guarded_return() { |
7 | check( | 146 | check( |
8 | "convert_to_guarded_return", | 147 | "convert_to_guarded_return", |