mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-02 07:22:42 +00:00
Merge #11869
11869: fix: code blocks with tilde also works like code block r=Veykril a=moreal The `rustdoc` uses the `pulldown_cmark` package to parse *doc_comment* and the package also treat triple `~` characters also as code block fences. So when we run `cargo doc`, they will be placed also. <img width="965" alt="image" src="https://user-images.githubusercontent.com/26626194/161208072-5a09a209-57fc-4a52-b190-b0a9be9ffcd6.png"> But `rust-analyzer` doesn't support it so it doesn't have any injected code highlights and any `Run doctest` hint. This pull request tries to allow also them. 🙇🏻♂️ Before: <img width="224" alt="image" src="https://user-images.githubusercontent.com/26626194/161207405-b1d6cfda-82b1-4f60-8e42-c51d0ed98f38.png"> After: <img width="161" alt="image" src="https://user-images.githubusercontent.com/26626194/161207693-8e39997c-9ca6-4e69-8c65-e9b70899f7db.png"> Co-authored-by: Lee Dogeon <dev.moreal@gmail.com>
This commit is contained in:
commit
50225fe630
@ -474,7 +474,7 @@ impl 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
|
||||
|
@ -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;
|
||||
|
@ -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"><</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">></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>
|
||||
|
@ -644,6 +644,11 @@ impl 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) {
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user