mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 14:21:44 +00:00
make Docs handing more ideomatic
This commit is contained in:
parent
5dc2789895
commit
33026c654e
4 changed files with 13 additions and 23 deletions
|
@ -171,7 +171,7 @@ impl Conv for ra_ide_api::Documentation {
|
|||
fn conv(self) -> Documentation {
|
||||
Documentation::MarkupContent(MarkupContent {
|
||||
kind: MarkupKind::Markdown,
|
||||
value: crate::markdown::sanitize_markdown(self).into(),
|
||||
value: crate::markdown::mark_fenced_blocks_as_rust(self.as_str()).into(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,26 +1,20 @@
|
|||
use ra_ide_api::Documentation;
|
||||
|
||||
pub(crate) fn sanitize_markdown(docs: Documentation) -> Documentation {
|
||||
let docs: String = docs.into();
|
||||
|
||||
// Massage markdown
|
||||
pub(crate) fn mark_fenced_blocks_as_rust(src: &str) -> String {
|
||||
let mut processed_lines = Vec::new();
|
||||
let mut in_code_block = false;
|
||||
for line in docs.lines() {
|
||||
for line in src.lines() {
|
||||
if line.starts_with("```") {
|
||||
in_code_block = !in_code_block;
|
||||
in_code_block ^= true
|
||||
}
|
||||
|
||||
let line = if in_code_block && line.starts_with("```") && !line.contains("rust") {
|
||||
"```rust".into()
|
||||
"```rust"
|
||||
} else {
|
||||
line.to_string()
|
||||
line
|
||||
};
|
||||
|
||||
processed_lines.push(line);
|
||||
}
|
||||
|
||||
Documentation::new(&processed_lines.join("\n"))
|
||||
processed_lines.join("\n")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -30,9 +24,6 @@ mod tests {
|
|||
#[test]
|
||||
fn test_codeblock_adds_rust() {
|
||||
let comment = "```\nfn some_rust() {}\n```";
|
||||
assert_eq!(
|
||||
sanitize_markdown(Documentation::new(comment)).contents(),
|
||||
"```rust\nfn some_rust() {}\n```"
|
||||
);
|
||||
assert_eq!(mark_fenced_blocks_as_rust(comment), "```rust\nfn some_rust() {}\n```");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue