mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 22:24:14 +00:00
Fix: only shows one # when we encounter ##
This commit is contained in:
parent
5051717856
commit
b7b9cd5e67
1 changed files with 23 additions and 3 deletions
|
@ -1,4 +1,6 @@
|
||||||
//! Transforms markdown
|
//! Transforms markdown
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use ide_db::helpers::rust_doc::is_rust_fence;
|
use ide_db::helpers::rust_doc::is_rust_fence;
|
||||||
|
|
||||||
const RUSTDOC_FENCE: &str = "```";
|
const RUSTDOC_FENCE: &str = "```";
|
||||||
|
@ -8,8 +10,9 @@ pub(crate) fn format_docs(src: &str) -> String {
|
||||||
let mut in_code_block = false;
|
let mut in_code_block = false;
|
||||||
let mut is_rust = false;
|
let mut is_rust = false;
|
||||||
|
|
||||||
for mut line in src.lines() {
|
for line in src.lines() {
|
||||||
if in_code_block && is_rust && code_line_ignored_by_rustdoc(line) {
|
let mut line = Cow::from(line);
|
||||||
|
if in_code_block && is_rust && code_line_ignored_by_rustdoc(&line) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,11 +23,18 @@ pub(crate) fn format_docs(src: &str) -> String {
|
||||||
is_rust = is_rust_fence(header);
|
is_rust = is_rust_fence(header);
|
||||||
|
|
||||||
if is_rust {
|
if is_rust {
|
||||||
line = "```rust";
|
line = Cow::Borrowed("```rust");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if in_code_block {
|
||||||
|
let trimmed = line.trim();
|
||||||
|
if trimmed.starts_with("##") {
|
||||||
|
line = Cow::Owned(line.replacen("##", "#", 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
processed_lines.push(line);
|
processed_lines.push(line);
|
||||||
}
|
}
|
||||||
processed_lines.join("\n")
|
processed_lines.join("\n")
|
||||||
|
@ -136,4 +146,14 @@ let a = 1;
|
||||||
"```text\nfiller\ntext\n```\nSome comment.\n```rust\nlet a = 1;\n```"
|
"```text\nfiller\ntext\n```\nSome comment.\n```rust\nlet a = 1;\n```"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_format_docs_handles_escape_double_hashes() {
|
||||||
|
let comment = r#"```rust
|
||||||
|
let s = "foo
|
||||||
|
## bar # baz";
|
||||||
|
```"#;
|
||||||
|
|
||||||
|
assert_eq!(format_docs(comment), "```rust\nlet s = \"foo\n# bar # baz\";\n```");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue