2023-11-27 19:23:20 +11:00
|
|
|
use rustc_span::{BytePos, Symbol};
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2022-01-18 16:56:16 +01:00
|
|
|
use crate::token::CommentKind;
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2019-08-02 00:26:40 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests;
|
|
|
|
|
|
2016-10-12 20:54:41 +03:00
|
|
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub enum CommentStyle {
|
2014-06-09 13:12:30 -07:00
|
|
|
/// No code on either side of each line of the comment
|
|
|
|
|
Isolated,
|
|
|
|
|
/// Code exists to the left of the comment
|
|
|
|
|
Trailing,
|
|
|
|
|
/// Code before /* foo */ and after the comment
|
|
|
|
|
Mixed,
|
|
|
|
|
/// Just a manual blank line "\n\n", for layout
|
|
|
|
|
BlankLine,
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub struct Comment {
|
2014-03-27 15:39:48 -07:00
|
|
|
pub style: CommentStyle,
|
2014-05-22 16:57:53 -07:00
|
|
|
pub lines: Vec<String>,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub pos: BytePos,
|
2013-02-21 00:16:31 -08:00
|
|
|
}
|
2012-04-15 03:27:24 -07:00
|
|
|
|
2022-04-16 23:49:37 +03:00
|
|
|
/// A fast conservative estimate on whether the string can contain documentation links.
|
|
|
|
|
/// A pair of square brackets `[]` must exist in the string, but we only search for the
|
|
|
|
|
/// opening bracket because brackets always go in pairs in practice.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn may_have_doc_links(s: &str) -> bool {
|
|
|
|
|
s.contains('[')
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-22 12:28:17 +03:00
|
|
|
/// Makes a doc string more presentable to users.
|
|
|
|
|
/// Used by rustdoc and perhaps other tools, but not by rustc.
|
2022-01-18 16:56:16 +01:00
|
|
|
pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol {
|
2020-12-20 23:37:24 +01:00
|
|
|
fn get_vertical_trim(lines: &[&str]) -> Option<(usize, usize)> {
|
2015-01-28 01:01:48 +00:00
|
|
|
let mut i = 0;
|
2013-06-04 21:43:41 -07:00
|
|
|
let mut j = lines.len();
|
2013-09-23 14:18:26 -07:00
|
|
|
// first line of all-stars should be omitted
|
2016-01-03 11:14:09 +02:00
|
|
|
if !lines.is_empty() && lines[0].chars().all(|c| c == '*') {
|
2013-09-23 14:18:26 -07:00
|
|
|
i += 1;
|
|
|
|
|
}
|
2018-08-12 15:43:51 +02:00
|
|
|
|
2013-09-23 14:18:26 -07:00
|
|
|
// like the first, a last line of all stars should be omitted
|
2021-11-06 19:41:33 +01:00
|
|
|
if j > i && !lines[j - 1].is_empty() && lines[j - 1].chars().all(|c| c == '*') {
|
2013-09-23 14:18:26 -07:00
|
|
|
j -= 1;
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
2018-08-12 15:43:51 +02:00
|
|
|
|
2020-12-20 23:37:24 +01:00
|
|
|
if i != 0 || j != lines.len() { Some((i, j)) } else { None }
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-20 22:10:40 +01:00
|
|
|
fn get_horizontal_trim(lines: &[&str], kind: CommentKind) -> Option<String> {
|
2015-01-17 23:33:05 +00:00
|
|
|
let mut i = usize::MAX;
|
2013-06-14 18:37:29 -04:00
|
|
|
let mut first = true;
|
2018-08-12 15:43:51 +02:00
|
|
|
|
2022-01-18 16:56:16 +01:00
|
|
|
// 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".
|
2023-01-30 11:03:32 +00:00
|
|
|
let lines = match kind {
|
|
|
|
|
CommentKind::Block => {
|
|
|
|
|
// Whatever happens, we skip the first line.
|
|
|
|
|
let mut i = lines
|
2023-07-23 13:11:20 +02:00
|
|
|
.first()
|
2023-01-30 11:03:32 +00:00
|
|
|
.map(|l| if l.trim_start().starts_with('*') { 0 } else { 1 })
|
|
|
|
|
.unwrap_or(0);
|
|
|
|
|
let mut j = lines.len();
|
2022-01-18 16:56:16 +01:00
|
|
|
|
2023-01-30 11:03:32 +00:00
|
|
|
while i < j && lines[i].trim().is_empty() {
|
|
|
|
|
i += 1;
|
|
|
|
|
}
|
|
|
|
|
while j > i && lines[j - 1].trim().is_empty() {
|
|
|
|
|
j -= 1;
|
|
|
|
|
}
|
|
|
|
|
&lines[i..j]
|
2022-01-18 16:56:16 +01:00
|
|
|
}
|
2023-01-30 11:03:32 +00:00
|
|
|
CommentKind::Line => lines,
|
2022-01-18 16:56:16 +01:00
|
|
|
};
|
|
|
|
|
|
2020-12-20 23:37:24 +01:00
|
|
|
for line in lines {
|
2014-11-27 15:00:50 -05:00
|
|
|
for (j, c) in line.chars().enumerate() {
|
2015-02-19 14:36:58 +01:00
|
|
|
if j > i || !"* \t".contains(c) {
|
2020-12-20 23:37:24 +01:00
|
|
|
return None;
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
2013-06-14 18:37:29 -04:00
|
|
|
if c == '*' {
|
|
|
|
|
if first {
|
|
|
|
|
i = j;
|
|
|
|
|
first = false;
|
|
|
|
|
} else if i != j {
|
2020-12-20 23:37:24 +01:00
|
|
|
return None;
|
2013-06-14 18:37:29 -04:00
|
|
|
}
|
2012-06-30 11:54:54 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-04 23:09:02 -08:00
|
|
|
if i >= line.len() {
|
2020-12-20 23:37:24 +01:00
|
|
|
return None;
|
2013-06-14 18:37:29 -04:00
|
|
|
}
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
2022-02-05 14:45:29 +01:00
|
|
|
if lines.is_empty() { None } else { Some(lines[0][..i].into()) }
|
2020-12-20 23:37:24 +01:00
|
|
|
}
|
2012-06-30 11:54:54 +01:00
|
|
|
|
2020-12-20 23:37:24 +01:00
|
|
|
let data_s = data.as_str();
|
|
|
|
|
if data_s.contains('\n') {
|
|
|
|
|
let mut lines = data_s.lines().collect::<Vec<&str>>();
|
|
|
|
|
let mut changes = false;
|
|
|
|
|
let lines = if let Some((i, j)) = get_vertical_trim(&lines) {
|
|
|
|
|
changes = true;
|
|
|
|
|
// remove whitespace-only lines from the start/end of lines
|
|
|
|
|
&mut lines[i..j]
|
2013-06-14 18:37:29 -04:00
|
|
|
} else {
|
2020-12-20 23:37:24 +01:00
|
|
|
&mut lines
|
|
|
|
|
};
|
2022-11-29 11:01:17 +00:00
|
|
|
if let Some(horizontal) = get_horizontal_trim(lines, kind) {
|
2020-12-20 23:37:24 +01:00
|
|
|
changes = true;
|
|
|
|
|
// remove a "[ \t]*\*" block from each line, if possible
|
|
|
|
|
for line in lines.iter_mut() {
|
2022-02-05 14:45:29 +01:00
|
|
|
if let Some(tmp) = line.strip_prefix(&horizontal) {
|
|
|
|
|
*line = tmp;
|
|
|
|
|
if kind == CommentKind::Block
|
|
|
|
|
&& (*line == "*" || line.starts_with("* ") || line.starts_with("**"))
|
|
|
|
|
{
|
|
|
|
|
*line = &line[1..];
|
|
|
|
|
}
|
2022-01-18 16:56:16 +01:00
|
|
|
}
|
2020-12-20 23:37:24 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if changes {
|
|
|
|
|
return Symbol::intern(&lines.join("\n"));
|
2013-06-14 18:37:29 -04:00
|
|
|
}
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
2020-12-20 23:37:24 +01:00
|
|
|
data
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|