2069: auto-generate assists docs and tests r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-10-25 11:50:06 +00:00 committed by GitHub
commit c48b467eff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 269 additions and 52 deletions

View file

@ -1,26 +1,3 @@
//! Assist: `convert_to_guarded_return`
//!
//! Replace a large conditional with a guarded return.
//!
//! ```text
//! fn <|>main() {
//! if cond {
//! foo();
//! bar();
//! }
//! }
//! ```
//! ->
//! ```text
//! fn main() {
//! if !cond {
//! return;
//! }
//! foo();
//! bar();
//! }
//! ```
use std::ops::RangeInclusive;
use hir::db::HirDatabase;
@ -36,6 +13,26 @@ use crate::{
AssistId,
};
// Assist: convert_to_guarded_return
// Replace a large conditional with a guarded return.
// ```
// fn main() {
// <|>if cond {
// foo();
// bar();
// }
// }
// ```
// ->
// ```
// fn main() {
// if !cond {
// return;
// }
// foo();
// bar();
// }
// ```
pub(crate) fn convert_to_guarded_return(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let if_expr: ast::IfExpr = ctx.node_at_offset()?;
let expr = if_expr.condition()?.expr()?;

View file

@ -0,0 +1,23 @@
//! Each assist definition has a special comment, which specifies docs and
//! example.
//!
//! We collect all the example and write the as tests in this module.
mod generated;
use hir::mock::MockDatabase;
use ra_db::FileRange;
use ra_syntax::TextRange;
use test_utils::{assert_eq_text, extract_offset};
fn check(assist_id: &str, before: &str, after: &str) {
let (before_cursor_pos, before) = extract_offset(before);
let (db, _source_root, file_id) = MockDatabase::with_single_file(&before);
let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
let (_assist_id, action) =
crate::assists(&db, frange).into_iter().find(|(id, _)| id.id.0 == assist_id).unwrap();
let actual = action.edit.apply(&before);
assert_eq_text!(after, &actual);
}

View file

@ -0,0 +1,27 @@
//! Generated file, do not edit by hand, see `crate/ra_tools/src/codegen`
use super::check;
#[test]
fn doctest_convert_to_guarded_return() {
check(
"convert_to_guarded_return",
r#####"
fn main() {
<|>if cond {
foo();
bar();
}
}
"#####,
r#####"
fn main() {
if !cond {
return;
}
foo();
bar();
}
"#####,
)
}

View file

@ -7,6 +7,8 @@
mod assist_ctx;
mod marks;
#[cfg(test)]
mod doc_tests;
use hir::db::HirDatabase;
use itertools::Itertools;
@ -36,7 +38,7 @@ pub struct AssistAction {
pub target: Option<TextRange>,
}
/// Return all the assists eapplicable at the given position.
/// Return all the assists applicable at the given position.
///
/// Assists are returned in the "unresolved" state, that is only labels are
/// returned, without actual edits.