From e3f32d13e11f8b0aee13656a1dde6b89e90051df Mon Sep 17 00:00:00 2001 From: Lee Dogeon Date: Fri, 1 Apr 2022 15:22:32 +0900 Subject: [PATCH] Code blocks with tilde also works like code block --- crates/ide/src/runnables.rs | 6 ++++-- crates/ide/src/syntax_highlighting/inject.rs | 7 ++++--- .../syntax_highlighting/test_data/highlight_doctest.html | 5 +++++ crates/ide/src/syntax_highlighting/tests.rs | 5 +++++ crates/rust-analyzer/src/markdown.rs | 5 +++-- 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs index bcb2c8797d0..1c455c5f30c 100644 --- a/crates/ide/src/runnables.rs +++ b/crates/ide/src/runnables.rs @@ -474,7 +474,7 @@ fn from_fn(fn_def: &ast::Fn) -> TestAttr { } } -const RUSTDOC_FENCE: &str = "```"; +const RUSTDOC_FENCES: [&str; 2] = ["```", "~~~"]; const RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUNNABLE: &[&str] = &["", "rust", "should_panic", "edition2015", "edition2018", "edition2021"]; @@ -483,7 +483,9 @@ fn has_runnable_doc_test(attrs: &hir::Attrs) -> bool { let mut in_code_block = false; for line in String::from(doc).lines() { - if let Some(header) = line.strip_prefix(RUSTDOC_FENCE) { + if let Some(header) = + RUSTDOC_FENCES.into_iter().find_map(|fence| line.strip_prefix(fence)) + { in_code_block = !in_code_block; if in_code_block diff --git a/crates/ide/src/syntax_highlighting/inject.rs b/crates/ide/src/syntax_highlighting/inject.rs index 8af0d8007d3..8ac3c2da50b 100644 --- a/crates/ide/src/syntax_highlighting/inject.rs +++ b/crates/ide/src/syntax_highlighting/inject.rs @@ -78,7 +78,8 @@ pub(super) fn ra_fixture( Some(()) } -const RUSTDOC_FENCE: &str = "```"; +const RUSTDOC_FENCE_LENGTH: usize = 3; +const RUSTDOC_FENCES: [&str; 2] = ["```", "~~~"]; /// Injection of syntax highlighting of doctests. pub(super) fn doc_comment( @@ -166,11 +167,11 @@ pub(super) fn doc_comment( }; let mut pos = TextSize::from(0); - match line.find(RUSTDOC_FENCE) { + match RUSTDOC_FENCES.into_iter().find_map(|fence| line.find(fence)) { Some(idx) => { is_codeblock = !is_codeblock; // Check whether code is rust by inspecting fence guards - let guards = &line[idx + RUSTDOC_FENCE.len()..]; + let guards = &line[idx + RUSTDOC_FENCE_LENGTH..]; let is_rust = is_rust_fence(guards); is_doctest = is_codeblock && is_rust; continue; diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html index d9126421cda..36e9ec6333b 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html @@ -110,6 +110,11 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd /// let foobar = Foo::new().bar(); /// ``` /// + /// ~~~rust,no_run + /// // code block with tilde. + /// let foobar = Foo::new().bar(); + /// ~~~ + /// /// ``` /// // functions /// fn foo<T, const X: usize>(arg: i32) { diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index 2cd4fa809a4..fdfe347a328 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -644,6 +644,11 @@ pub const fn new() -> Foo { /// let foobar = Foo::new().bar(); /// ``` /// + /// ~~~rust,no_run + /// // code block with tilde. + /// let foobar = Foo::new().bar(); + /// ~~~ + /// /// ``` /// // functions /// fn foo(arg: i32) { diff --git a/crates/rust-analyzer/src/markdown.rs b/crates/rust-analyzer/src/markdown.rs index e1ff0f0178b..912ed1e7642 100644 --- a/crates/rust-analyzer/src/markdown.rs +++ b/crates/rust-analyzer/src/markdown.rs @@ -1,7 +1,7 @@ //! Transforms markdown use ide_db::rust_doc::is_rust_fence; -const RUSTDOC_FENCE: &str = "```"; +const RUSTDOC_FENCES: [&str; 2] = ["```", "~~~"]; pub(crate) fn format_docs(src: &str) -> String { let mut processed_lines = Vec::new(); @@ -13,7 +13,8 @@ pub(crate) fn format_docs(src: &str) -> String { continue; } - if let Some(header) = line.strip_prefix(RUSTDOC_FENCE) { + if let Some(header) = RUSTDOC_FENCES.into_iter().find_map(|fence| line.strip_prefix(fence)) + { in_code_block ^= true; if in_code_block {