Merge #1627
1627: Hide ignored lines in rustdoc r=kjeremy a=rizakrko This fixes #1620. Co-authored-by: Roman Stoliar <rizakrko@rambler.ru>
This commit is contained in:
commit
1af7738c28
@ -171,7 +171,7 @@ impl Conv for ra_ide_api::Documentation {
|
|||||||
fn conv(self) -> Documentation {
|
fn conv(self) -> Documentation {
|
||||||
Documentation::MarkupContent(MarkupContent {
|
Documentation::MarkupContent(MarkupContent {
|
||||||
kind: MarkupKind::Markdown,
|
kind: MarkupKind::Markdown,
|
||||||
value: crate::markdown::mark_fenced_blocks_as_rust(self.as_str()),
|
value: crate::markdown::format_docs(self.as_str()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -485,7 +485,7 @@ pub fn handle_hover(
|
|||||||
let res = Hover {
|
let res = Hover {
|
||||||
contents: HoverContents::Markup(MarkupContent {
|
contents: HoverContents::Markup(MarkupContent {
|
||||||
kind: MarkupKind::Markdown,
|
kind: MarkupKind::Markdown,
|
||||||
value: info.info.to_markup(),
|
value: crate::markdown::format_docs(&info.info.to_markup()),
|
||||||
}),
|
}),
|
||||||
range: Some(range),
|
range: Some(range),
|
||||||
};
|
};
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
pub(crate) fn mark_fenced_blocks_as_rust(src: &str) -> String {
|
pub(crate) fn format_docs(src: &str) -> String {
|
||||||
let mut processed_lines = Vec::new();
|
let mut processed_lines = Vec::new();
|
||||||
let mut in_code_block = false;
|
let mut in_code_block = false;
|
||||||
for line in src.lines() {
|
for line in src.lines() {
|
||||||
|
if in_code_block && code_line_ignored_by_rustdoc(line) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if line.starts_with("```") {
|
if line.starts_with("```") {
|
||||||
in_code_block ^= true
|
in_code_block ^= true
|
||||||
}
|
}
|
||||||
@ -17,13 +21,31 @@ pub(crate) fn mark_fenced_blocks_as_rust(src: &str) -> String {
|
|||||||
processed_lines.join("\n")
|
processed_lines.join("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn code_line_ignored_by_rustdoc(line: &str) -> bool {
|
||||||
|
let trimmed = line.trim();
|
||||||
|
trimmed == "#" || trimmed.starts_with("# ") || trimmed.starts_with("#\t")
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_codeblock_adds_rust() {
|
fn test_format_docs_adds_rust() {
|
||||||
let comment = "```\nfn some_rust() {}\n```";
|
let comment = "```\nfn some_rust() {}\n```";
|
||||||
assert_eq!(mark_fenced_blocks_as_rust(comment), "```rust\nfn some_rust() {}\n```");
|
assert_eq!(format_docs(comment), "```rust\nfn some_rust() {}\n```");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_format_docs_skips_comments_in_rust_block() {
|
||||||
|
let comment =
|
||||||
|
"```rust\n # skip1\n# skip2\n#stay1\nstay2\n#\n #\n # \n #\tskip3\n\t#\t\n```";
|
||||||
|
assert_eq!(format_docs(comment), "```rust\n#stay1\nstay2\n```");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_format_docs_keeps_comments_outside_of_rust_block() {
|
||||||
|
let comment = " # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t";
|
||||||
|
assert_eq!(format_docs(comment), comment);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user