From 755077e3720bd97e1e506bf8fbe0a2534389f282 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 6 Feb 2020 18:10:25 +0100 Subject: [PATCH] Doctest autoimport --- crates/ra_assists/src/assists/auto_import.rs | 4 +- crates/ra_assists/src/doc_tests.rs | 10 +---- crates/ra_assists/src/doc_tests/generated.rs | 4 +- docs/user/assists.md | 2 +- xtask/src/codegen/gen_assists_docs.rs | 42 +++++++++++++++----- 5 files changed, 42 insertions(+), 20 deletions(-) diff --git a/crates/ra_assists/src/assists/auto_import.rs b/crates/ra_assists/src/assists/auto_import.rs index 2190510632..f16b08c43b 100644 --- a/crates/ra_assists/src/assists/auto_import.rs +++ b/crates/ra_assists/src/assists/auto_import.rs @@ -18,14 +18,16 @@ use ra_ide_db::imports_locator::ImportsLocatorIde; // fn main() { // let map = HashMap<|>::new(); // } +// # pub mod std { pub mod collections { pub struct HashMap { } } } // ``` // -> // ``` // use std::collections::HashMap; // // fn main() { -// let map = HashMap<|>::new(); +// let map = HashMap::new(); // } +// # pub mod std { pub mod collections { pub struct HashMap { } } } // ``` pub(crate) fn auto_import(ctx: AssistCtx) -> Option { let path_to_import: ast::Path = ctx.find_node_at_offset()?; diff --git a/crates/ra_assists/src/doc_tests.rs b/crates/ra_assists/src/doc_tests.rs index 370b642255..56020028cf 100644 --- a/crates/ra_assists/src/doc_tests.rs +++ b/crates/ra_assists/src/doc_tests.rs @@ -5,18 +5,12 @@ mod generated; -use ra_db::{fixture::WithFixture, FileRange}; +use ra_db::FileRange; use test_utils::{assert_eq_text, extract_range_or_offset}; -use ra_ide_db::RootDatabase; - fn check(assist_id: &str, before: &str, after: &str) { - // FIXME we cannot get the imports search functionality here yet, but still need to generate a test and a doc for an assist - if assist_id == "auto_import" { - return; - } let (selection, before) = extract_range_or_offset(before); - let (db, file_id) = RootDatabase::with_single_file(&before); + let (db, file_id) = crate::helpers::with_single_file(&before); let frange = FileRange { file_id, range: selection.into() }; let assist = crate::assists(&db, frange) diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs index ec4587ce7c..0d95b957ba 100644 --- a/crates/ra_assists/src/doc_tests/generated.rs +++ b/crates/ra_assists/src/doc_tests/generated.rs @@ -222,13 +222,15 @@ fn doctest_auto_import() { fn main() { let map = HashMap<|>::new(); } +pub mod std { pub mod collections { pub struct HashMap { } } } "#####, r#####" use std::collections::HashMap; fn main() { - let map = HashMap<|>::new(); + let map = HashMap::new(); } +pub mod std { pub mod collections { pub struct HashMap { } } } "#####, ) } diff --git a/docs/user/assists.md b/docs/user/assists.md index c36c5df6a2..1e2dd74858 100644 --- a/docs/user/assists.md +++ b/docs/user/assists.md @@ -223,7 +223,7 @@ fn main() { use std::collections::HashMap; fn main() { - let map = HashMap┃::new(); + let map = HashMap::new(); } ``` diff --git a/xtask/src/codegen/gen_assists_docs.rs b/xtask/src/codegen/gen_assists_docs.rs index 69f9b48728..697e830df4 100644 --- a/xtask/src/codegen/gen_assists_docs.rs +++ b/xtask/src/codegen/gen_assists_docs.rs @@ -20,6 +20,28 @@ struct Assist { after: String, } +fn hide_hash_comments(text: &str) -> String { + text.split('\n') // want final newline + .filter(|&it| !(it.starts_with("# ") || it == "#")) + .map(|it| format!("{}\n", it)) + .collect() +} + +fn reveal_hash_comments(text: &str) -> String { + text.split('\n') // want final newline + .map(|it| { + if it.starts_with("# ") { + &it[2..] + } else if it == "#" { + "" + } else { + it + } + }) + .map(|it| format!("{}\n", it)) + .collect() +} + fn collect_assists() -> Result> { let mut res = Vec::new(); for entry in fs::read_dir(project_root().join(codegen::ASSISTS_DIR))? { @@ -91,13 +113,14 @@ fn doctest_{}() {{ check( "{}", r#####" -{} -"#####, r#####" -{} -"#####) +{}"#####, r#####" +{}"#####) }} "######, - assist.id, assist.id, assist.before, assist.after + assist.id, + assist.id, + reveal_hash_comments(&assist.before), + reveal_hash_comments(&assist.after) ); buf.push_str(&test) @@ -123,12 +146,13 @@ fn generate_docs(assists: &[Assist], mode: Mode) -> Result<()> { ```rust // BEFORE {} - // AFTER -{} -``` +{}``` ", - assist.id, assist.doc, before, after + assist.id, + assist.doc, + hide_hash_comments(&before), + hide_hash_comments(&after) ); buf.push_str(&docs); }