diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-10-25 12:50:06 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2019-10-25 12:50:06 +0100 |
commit | c48b467eff0e18f3b8fa1b65a21abb19f800f56a (patch) | |
tree | 47d48171346d25e357df7b15c012447756cf9b0b /crates/ra_assists | |
parent | 5f779f6c46f29c63483c0e2be732377b1b87e685 (diff) | |
parent | 0dd35ff2b2ceffdb926953fdacc7d30e1968047d (diff) |
Merge #2069
2069: auto-generate assists docs and tests r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_assists')
-rw-r--r-- | crates/ra_assists/src/assists/early_return.rs | 43 | ||||
-rw-r--r-- | crates/ra_assists/src/doc_tests.rs | 23 | ||||
-rw-r--r-- | crates/ra_assists/src/doc_tests/generated.rs | 27 | ||||
-rw-r--r-- | crates/ra_assists/src/lib.rs | 4 |
4 files changed, 73 insertions, 24 deletions
diff --git a/crates/ra_assists/src/assists/early_return.rs b/crates/ra_assists/src/assists/early_return.rs index f7d7e12e7..b3d025340 100644 --- a/crates/ra_assists/src/assists/early_return.rs +++ b/crates/ra_assists/src/assists/early_return.rs | |||
@@ -1,26 +1,3 @@ | |||
1 | //! Assist: `convert_to_guarded_return` | ||
2 | //! | ||
3 | //! Replace a large conditional with a guarded return. | ||
4 | //! | ||
5 | //! ```text | ||
6 | //! fn <|>main() { | ||
7 | //! if cond { | ||
8 | //! foo(); | ||
9 | //! bar(); | ||
10 | //! } | ||
11 | //! } | ||
12 | //! ``` | ||
13 | //! -> | ||
14 | //! ```text | ||
15 | //! fn main() { | ||
16 | //! if !cond { | ||
17 | //! return; | ||
18 | //! } | ||
19 | //! foo(); | ||
20 | //! bar(); | ||
21 | //! } | ||
22 | //! ``` | ||
23 | |||
24 | use std::ops::RangeInclusive; | 1 | use std::ops::RangeInclusive; |
25 | 2 | ||
26 | use hir::db::HirDatabase; | 3 | use hir::db::HirDatabase; |
@@ -36,6 +13,26 @@ use crate::{ | |||
36 | AssistId, | 13 | AssistId, |
37 | }; | 14 | }; |
38 | 15 | ||
16 | // Assist: convert_to_guarded_return | ||
17 | // Replace a large conditional with a guarded return. | ||
18 | // ``` | ||
19 | // fn main() { | ||
20 | // <|>if cond { | ||
21 | // foo(); | ||
22 | // bar(); | ||
23 | // } | ||
24 | // } | ||
25 | // ``` | ||
26 | // -> | ||
27 | // ``` | ||
28 | // fn main() { | ||
29 | // if !cond { | ||
30 | // return; | ||
31 | // } | ||
32 | // foo(); | ||
33 | // bar(); | ||
34 | // } | ||
35 | // ``` | ||
39 | pub(crate) fn convert_to_guarded_return(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 36 | pub(crate) fn convert_to_guarded_return(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
40 | let if_expr: ast::IfExpr = ctx.node_at_offset()?; | 37 | let if_expr: ast::IfExpr = ctx.node_at_offset()?; |
41 | let expr = if_expr.condition()?.expr()?; | 38 | let expr = if_expr.condition()?.expr()?; |
diff --git a/crates/ra_assists/src/doc_tests.rs b/crates/ra_assists/src/doc_tests.rs new file mode 100644 index 000000000..88e901517 --- /dev/null +++ b/crates/ra_assists/src/doc_tests.rs | |||
@@ -0,0 +1,23 @@ | |||
1 | //! Each assist definition has a special comment, which specifies docs and | ||
2 | //! example. | ||
3 | //! | ||
4 | //! We collect all the example and write the as tests in this module. | ||
5 | |||
6 | mod generated; | ||
7 | |||
8 | use hir::mock::MockDatabase; | ||
9 | use ra_db::FileRange; | ||
10 | use ra_syntax::TextRange; | ||
11 | use test_utils::{assert_eq_text, extract_offset}; | ||
12 | |||
13 | fn check(assist_id: &str, before: &str, after: &str) { | ||
14 | let (before_cursor_pos, before) = extract_offset(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()) }; | ||
17 | |||
18 | let (_assist_id, action) = | ||
19 | crate::assists(&db, frange).into_iter().find(|(id, _)| id.id.0 == assist_id).unwrap(); | ||
20 | |||
21 | let actual = action.edit.apply(&before); | ||
22 | assert_eq_text!(after, &actual); | ||
23 | } | ||
diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs new file mode 100644 index 000000000..e5f6910f1 --- /dev/null +++ b/crates/ra_assists/src/doc_tests/generated.rs | |||
@@ -0,0 +1,27 @@ | |||
1 | //! Generated file, do not edit by hand, see `crate/ra_tools/src/codegen` | ||
2 | |||
3 | use super::check; | ||
4 | |||
5 | #[test] | ||
6 | fn doctest_convert_to_guarded_return() { | ||
7 | check( | ||
8 | "convert_to_guarded_return", | ||
9 | r#####" | ||
10 | fn main() { | ||
11 | <|>if cond { | ||
12 | foo(); | ||
13 | bar(); | ||
14 | } | ||
15 | } | ||
16 | "#####, | ||
17 | r#####" | ||
18 | fn main() { | ||
19 | if !cond { | ||
20 | return; | ||
21 | } | ||
22 | foo(); | ||
23 | bar(); | ||
24 | } | ||
25 | "#####, | ||
26 | ) | ||
27 | } | ||
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index ab77b46a9..de576324f 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -7,6 +7,8 @@ | |||
7 | 7 | ||
8 | mod assist_ctx; | 8 | mod assist_ctx; |
9 | mod marks; | 9 | mod marks; |
10 | #[cfg(test)] | ||
11 | mod doc_tests; | ||
10 | 12 | ||
11 | use hir::db::HirDatabase; | 13 | use hir::db::HirDatabase; |
12 | use itertools::Itertools; | 14 | use itertools::Itertools; |
@@ -36,7 +38,7 @@ pub struct AssistAction { | |||
36 | pub target: Option<TextRange>, | 38 | pub target: Option<TextRange>, |
37 | } | 39 | } |
38 | 40 | ||
39 | /// Return all the assists eapplicable at the given position. | 41 | /// Return all the assists applicable at the given position. |
40 | /// | 42 | /// |
41 | /// Assists are returned in the "unresolved" state, that is only labels are | 43 | /// Assists are returned in the "unresolved" state, that is only labels are |
42 | /// returned, without actual edits. | 44 | /// returned, without actual edits. |