aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-10-25 12:16:46 +0100
committerAleksey Kladov <[email protected]>2019-10-25 12:47:48 +0100
commit0dd35ff2b2ceffdb926953fdacc7d30e1968047d (patch)
treea8bcab00ef1c434e56845034f22a5595d119dea7 /crates/ra_assists
parent518f99e16b993e3414a81181c8bad7a89e590ece (diff)
auto-generate assists docs and tests
Diffstat (limited to 'crates/ra_assists')
-rw-r--r--crates/ra_assists/src/assists/early_return.rs43
-rw-r--r--crates/ra_assists/src/doc_tests.rs23
-rw-r--r--crates/ra_assists/src/doc_tests/generated.rs27
-rw-r--r--crates/ra_assists/src/lib.rs4
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
24use std::ops::RangeInclusive; 1use std::ops::RangeInclusive;
25 2
26use hir::db::HirDatabase; 3use 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// ```
39pub(crate) fn convert_to_guarded_return(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 36pub(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
6mod generated;
7
8use hir::mock::MockDatabase;
9use ra_db::FileRange;
10use ra_syntax::TextRange;
11use test_utils::{assert_eq_text, extract_offset};
12
13fn 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
3use super::check;
4
5#[test]
6fn doctest_convert_to_guarded_return() {
7 check(
8 "convert_to_guarded_return",
9 r#####"
10fn main() {
11 <|>if cond {
12 foo();
13 bar();
14 }
15}
16"#####,
17 r#####"
18fn 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
8mod assist_ctx; 8mod assist_ctx;
9mod marks; 9mod marks;
10#[cfg(test)]
11mod doc_tests;
10 12
11use hir::db::HirDatabase; 13use hir::db::HirDatabase;
12use itertools::Itertools; 14use 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.