Code blocks with tilde also works like code block

This commit is contained in:
Lee Dogeon 2022-04-01 15:22:32 +09:00
parent 259182b50b
commit e3f32d13e1
5 changed files with 21 additions and 7 deletions

View File

@ -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

View File

@ -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;

View File

@ -110,6 +110,11 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="comment documentation">///</span><span class="comment documentation"> </span><span class="keyword injected">let</span><span class="none injected"> </span><span class="variable declaration injected">foobar</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none injected"> </span><span class="struct injected">Foo</span><span class="operator injected">::</span><span class="function injected">new</span><span class="parenthesis injected">(</span><span class="parenthesis injected">)</span><span class="operator injected">.</span><span class="function injected">bar</span><span class="parenthesis injected">(</span><span class="parenthesis injected">)</span><span class="semicolon injected">;</span>
<span class="comment documentation">/// ```</span>
<span class="comment documentation">///</span>
<span class="comment documentation">/// ~~~rust,no_run</span>
<span class="comment documentation">///</span><span class="comment documentation"> </span><span class="comment injected">// code block with tilde.</span>
<span class="comment documentation">///</span><span class="comment documentation"> </span><span class="keyword injected">let</span><span class="none injected"> </span><span class="variable declaration injected">foobar</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none injected"> </span><span class="struct injected">Foo</span><span class="operator injected">::</span><span class="function injected">new</span><span class="parenthesis injected">(</span><span class="parenthesis injected">)</span><span class="operator injected">.</span><span class="function injected">bar</span><span class="parenthesis injected">(</span><span class="parenthesis injected">)</span><span class="semicolon injected">;</span>
<span class="comment documentation">/// ~~~</span>
<span class="comment documentation">///</span>
<span class="comment documentation">/// ```</span>
<span class="comment documentation">///</span><span class="comment documentation"> </span><span class="comment injected">// functions</span>
<span class="comment documentation">///</span><span class="comment documentation"> </span><span class="keyword injected">fn</span><span class="none injected"> </span><span class="function declaration injected">foo</span><span class="angle injected">&lt;</span><span class="type_param declaration injected">T</span><span class="comma injected">,</span><span class="none injected"> </span><span class="keyword injected">const</span><span class="none injected"> </span><span class="const_param declaration injected">X</span><span class="colon injected">:</span><span class="none injected"> </span><span class="builtin_type injected">usize</span><span class="angle injected">&gt;</span><span class="parenthesis injected">(</span><span class="value_param declaration injected">arg</span><span class="colon injected">:</span><span class="none injected"> </span><span class="builtin_type injected">i32</span><span class="parenthesis injected">)</span><span class="none injected"> </span><span class="brace injected">{</span>

View File

@ -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<T, const X: usize>(arg: i32) {

View File

@ -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 {