From 63caef372ad96a4cfc6aeafa52218c426daf7f2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Wed, 26 Aug 2020 03:02:55 +0200 Subject: Improve support for code block attributes --- crates/ide/src/runnables.rs | 108 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 99 insertions(+), 9 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs index dd59d9e70..989a63c09 100644 --- a/crates/ide/src/runnables.rs +++ b/crates/ide/src/runnables.rs @@ -211,12 +211,29 @@ fn has_test_related_attribute(fn_def: &ast::Fn) -> bool { .any(|attribute_text| attribute_text.contains("test")) } +const RUSTDOC_FENCE: &str = "```"; +const RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUNNABLE: &[&str] = + &["", "rust", "should_panic", "edition2015", "edition2018"]; + fn has_runnable_doc_test(fn_def: &ast::Fn) -> bool { fn_def.doc_comment_text().map_or(false, |comments_text| { - comments_text.contains("```") - && !comments_text.contains("```ignore") - && !comments_text.contains("```no_run") - && !comments_text.contains("```compile_fail") + let mut in_code_block = false; + + for line in comments_text.lines() { + if let Some(header) = line.strip_prefix(RUSTDOC_FENCE) { + in_code_block = !in_code_block; + + if in_code_block + && header + .split(',') + .all(|sub| RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUNNABLE.contains(&sub.trim())) + { + return true; + } + } + } + + false }) } @@ -421,7 +438,21 @@ fn main() {} /// ``` /// let x = 5; /// ``` -fn foo() {} +fn should_have_runnable() {} + +/// ```edition2018 +/// let x = 5; +/// ``` +fn should_have_runnable_1() {} + +/// ``` +/// let z = 55; +/// ``` +/// +/// ```ignore +/// let z = 56; +/// ``` +fn should_have_runnable_2() {} /// ```no_run /// let z = 55; @@ -437,8 +468,27 @@ fn should_have_no_runnable_2() {} /// let z = 55; /// ``` fn should_have_no_runnable_3() {} + +/// ```text +/// arbitrary plain text +/// ``` +fn should_have_no_runnable_4() {} + +/// ```text +/// arbitrary plain text +/// ``` +/// +/// ```sh +/// $ shell code +/// ``` +fn should_have_no_runnable_5() {} + +/// ```rust,no_run +/// let z = 55; +/// ``` +fn should_have_no_runnable_6() {} "#, - &[&BIN, &DOCTEST], + &[&BIN, &DOCTEST, &DOCTEST, &DOCTEST], expect![[r#" [ Runnable { @@ -464,9 +514,49 @@ fn should_have_no_runnable_3() {} file_id: FileId( 1, ), - full_range: 15..57, + full_range: 15..74, focus_range: None, - name: "foo", + name: "should_have_runnable", + kind: FN, + container_name: None, + description: None, + docs: None, + }, + kind: DocTest { + test_id: Path( + "should_have_runnable", + ), + }, + cfg_exprs: [], + }, + Runnable { + nav: NavigationTarget { + file_id: FileId( + 1, + ), + full_range: 76..148, + focus_range: None, + name: "should_have_runnable_1", + kind: FN, + container_name: None, + description: None, + docs: None, + }, + kind: DocTest { + test_id: Path( + "should_have_runnable_1", + ), + }, + cfg_exprs: [], + }, + Runnable { + nav: NavigationTarget { + file_id: FileId( + 1, + ), + full_range: 150..254, + focus_range: None, + name: "should_have_runnable_2", kind: FN, container_name: None, description: None, @@ -474,7 +564,7 @@ fn should_have_no_runnable_3() {} }, kind: DocTest { test_id: Path( - "foo", + "should_have_runnable_2", ), }, cfg_exprs: [], -- cgit v1.2.3