Correctly handle starts in block doc comments
This commit is contained in:
parent
7531d2fdd4
commit
2938be612d
5 changed files with 47 additions and 15 deletions
|
@ -1,3 +1,4 @@
|
|||
use crate::token::CommentKind;
|
||||
use rustc_span::source_map::SourceMap;
|
||||
use rustc_span::{BytePos, CharPos, FileName, Pos, Symbol};
|
||||
|
||||
|
@ -25,7 +26,7 @@ pub struct Comment {
|
|||
|
||||
/// Makes a doc string more presentable to users.
|
||||
/// Used by rustdoc and perhaps other tools, but not by rustc.
|
||||
pub fn beautify_doc_string(data: Symbol) -> Symbol {
|
||||
pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol {
|
||||
fn get_vertical_trim(lines: &[&str]) -> Option<(usize, usize)> {
|
||||
let mut i = 0;
|
||||
let mut j = lines.len();
|
||||
|
@ -42,10 +43,28 @@ pub fn beautify_doc_string(data: Symbol) -> Symbol {
|
|||
if i != 0 || j != lines.len() { Some((i, j)) } else { None }
|
||||
}
|
||||
|
||||
fn get_horizontal_trim(lines: &[&str]) -> Option<usize> {
|
||||
fn get_horizontal_trim(lines: &[&str], kind: CommentKind) -> Option<usize> {
|
||||
let mut i = usize::MAX;
|
||||
let mut first = true;
|
||||
|
||||
// In case we have doc comments like `/**` or `/*!`, we want to remove stars if they are
|
||||
// present. However, we first need to strip the empty lines so they don't get in the middle
|
||||
// when we try to compute the "horizontal trim".
|
||||
let lines = if kind == CommentKind::Block {
|
||||
let mut i = 0;
|
||||
let mut j = lines.len();
|
||||
|
||||
while i < j && lines[i].trim().is_empty() {
|
||||
i += 1;
|
||||
}
|
||||
while j > i && lines[j - 1].trim().is_empty() {
|
||||
j -= 1;
|
||||
}
|
||||
&lines[i..j]
|
||||
} else {
|
||||
lines
|
||||
};
|
||||
|
||||
for line in lines {
|
||||
for (j, c) in line.chars().enumerate() {
|
||||
if j > i || !"* \t".contains(c) {
|
||||
|
@ -79,11 +98,13 @@ pub fn beautify_doc_string(data: Symbol) -> Symbol {
|
|||
} else {
|
||||
&mut lines
|
||||
};
|
||||
if let Some(horizontal) = get_horizontal_trim(&lines) {
|
||||
if let Some(horizontal) = get_horizontal_trim(&lines, kind) {
|
||||
changes = true;
|
||||
// remove a "[ \t]*\*" block from each line, if possible
|
||||
for line in lines.iter_mut() {
|
||||
*line = &line[horizontal + 1..];
|
||||
if horizontal + 1 < line.len() {
|
||||
*line = &line[horizontal + 1..];
|
||||
}
|
||||
}
|
||||
}
|
||||
if changes {
|
||||
|
|
|
@ -5,7 +5,7 @@ use rustc_span::create_default_session_globals_then;
|
|||
fn test_block_doc_comment_1() {
|
||||
create_default_session_globals_then(|| {
|
||||
let comment = "\n * Test \n ** Test\n * Test\n";
|
||||
let stripped = beautify_doc_string(Symbol::intern(comment));
|
||||
let stripped = beautify_doc_string(Symbol::intern(comment), CommentKind::Block);
|
||||
assert_eq!(stripped.as_str(), " Test \n* Test\n Test");
|
||||
})
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ fn test_block_doc_comment_1() {
|
|||
fn test_block_doc_comment_2() {
|
||||
create_default_session_globals_then(|| {
|
||||
let comment = "\n * Test\n * Test\n";
|
||||
let stripped = beautify_doc_string(Symbol::intern(comment));
|
||||
let stripped = beautify_doc_string(Symbol::intern(comment), CommentKind::Block);
|
||||
assert_eq!(stripped.as_str(), " Test\n Test");
|
||||
})
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ fn test_block_doc_comment_2() {
|
|||
fn test_block_doc_comment_3() {
|
||||
create_default_session_globals_then(|| {
|
||||
let comment = "\n let a: *i32;\n *a = 5;\n";
|
||||
let stripped = beautify_doc_string(Symbol::intern(comment));
|
||||
let stripped = beautify_doc_string(Symbol::intern(comment), CommentKind::Block);
|
||||
assert_eq!(stripped.as_str(), " let a: *i32;\n *a = 5;");
|
||||
})
|
||||
}
|
||||
|
@ -31,13 +31,13 @@ fn test_block_doc_comment_3() {
|
|||
#[test]
|
||||
fn test_line_doc_comment() {
|
||||
create_default_session_globals_then(|| {
|
||||
let stripped = beautify_doc_string(Symbol::intern(" test"));
|
||||
let stripped = beautify_doc_string(Symbol::intern(" test"), CommentKind::Line);
|
||||
assert_eq!(stripped.as_str(), " test");
|
||||
let stripped = beautify_doc_string(Symbol::intern("! test"));
|
||||
let stripped = beautify_doc_string(Symbol::intern("! test"), CommentKind::Line);
|
||||
assert_eq!(stripped.as_str(), "! test");
|
||||
let stripped = beautify_doc_string(Symbol::intern("test"));
|
||||
let stripped = beautify_doc_string(Symbol::intern("test"), CommentKind::Line);
|
||||
assert_eq!(stripped.as_str(), "test");
|
||||
let stripped = beautify_doc_string(Symbol::intern("!test"));
|
||||
let stripped = beautify_doc_string(Symbol::intern("!test"), CommentKind::Line);
|
||||
assert_eq!(stripped.as_str(), "!test");
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue