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:
bors[bot] 2019-07-31 12:09:22 +00:00
commit 1af7738c28
3 changed files with 27 additions and 5 deletions

View file

@ -171,7 +171,7 @@ impl Conv for ra_ide_api::Documentation {
fn conv(self) -> Documentation {
Documentation::MarkupContent(MarkupContent {
kind: MarkupKind::Markdown,
value: crate::markdown::mark_fenced_blocks_as_rust(self.as_str()),
value: crate::markdown::format_docs(self.as_str()),
})
}
}

View file

@ -485,7 +485,7 @@ pub fn handle_hover(
let res = Hover {
contents: HoverContents::Markup(MarkupContent {
kind: MarkupKind::Markdown,
value: info.info.to_markup(),
value: crate::markdown::format_docs(&info.info.to_markup()),
}),
range: Some(range),
};

View file

@ -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 in_code_block = false;
for line in src.lines() {
if in_code_block && code_line_ignored_by_rustdoc(line) {
continue;
}
if line.starts_with("```") {
in_code_block ^= true
}
@ -17,13 +21,31 @@ pub(crate) fn mark_fenced_blocks_as_rust(src: &str) -> String {
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)]
mod tests {
use super::*;
#[test]
fn test_codeblock_adds_rust() {
fn test_format_docs_adds_rust() {
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);
}
}