1use rustc_span::{BytePos, Symbol};
23use crate::token::CommentKind;
45#[cfg(test)]
6mod tests;
78#[derive(Clone, Copy, PartialEq, Debug)]
9pub enum CommentStyle {
10/// No code on either side of each line of the comment
11Isolated,
12/// Code exists to the left of the comment
13Trailing,
14/// Code before /* foo */ and after the comment
15Mixed,
16/// Just a manual blank line "\n\n", for layout
17BlankLine,
18}
1920#[derive(Clone)]
21pub struct Comment {
22pub style: CommentStyle,
23pub lines: Vec<String>,
24pub pos: BytePos,
25}
2627/// A fast conservative estimate on whether the string can contain documentation links.
28/// A pair of square brackets `[]` must exist in the string, but we only search for the
29/// opening bracket because brackets always go in pairs in practice.
30#[inline]
31pub fn may_have_doc_links(s: &str) -> bool {
32s.contains('[')
33}
3435/// Makes a doc string more presentable to users.
36/// Used by rustdoc and perhaps other tools, but not by rustc.
37pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol {
38fn get_vertical_trim(lines: &[&str]) -> Option<(usize, usize)> {
39let mut i = 0;
40let mut j = lines.len();
41// first line of all-stars should be omitted
42if lines.first().is_some_and(|line| line.chars().all(|c| c == '*')) {
43i += 1;
44 }
4546// like the first, a last line of all stars should be omitted
47if j > i && !lines[j - 1].is_empty() && lines[j - 1].chars().all(|c| c == '*') {
48j -= 1;
49 }
5051if i != 0 || j != lines.len() { Some((i, j)) } else { None }
52 }
5354fn get_horizontal_trim(lines: &[&str], kind: CommentKind) -> Option<String> {
55let mut i = usize::MAX;
56let mut first = true;
5758// In case we have doc comments like `/**` or `/*!`, we want to remove stars if they are
59 // present. However, we first need to strip the empty lines so they don't get in the middle
60 // when we try to compute the "horizontal trim".
61let lines = match kind {
62CommentKind::Block => {
63// Whatever happens, we skip the first line.
64let mut i = lines65 .first()
66 .map(|l| if l.trim_start().starts_with('*') { 0 } else { 1 })
67 .unwrap_or(0);
68let mut j = lines.len();
6970while i < j && lines[i].trim().is_empty() {
71 i += 1;
72 }
73while j > i && lines[j - 1].trim().is_empty() {
74 j -= 1;
75 }
76&lines[i..j]
77 }
78CommentKind::Line => lines,
79 };
8081for line in lines {
82for (j, c) in line.chars().enumerate() {
83if j > i || !"* \t".contains(c) {
84return None;
85 }
86if c == '*' {
87if first {
88 i = j;
89 first = false;
90 } else if i != j {
91return None;
92 }
93break;
94 }
95 }
96if i >= line.len() {
97return None;
98 }
99 }
100Some(lines.first()?[..i].to_string())
101 }
102103let data_s = data.as_str();
104if data_s.contains('\n') {
105let mut lines = data_s.lines().collect::<Vec<&str>>();
106let mut changes = false;
107let lines = if let Some((i, j)) = get_vertical_trim(&lines) {
108changes = true;
109// remove whitespace-only lines from the start/end of lines
110&mut lines[i..j]
111 } else {
112&mut lines113 };
114if let Some(horizontal) = get_horizontal_trim(lines, kind) {
115changes = true;
116// remove a "[ \t]*\*" block from each line, if possible
117for line in lines.iter_mut() {
118if let Some(tmp) = line.strip_prefix(&horizontal) {
119*line = tmp;
120if kind == CommentKind::Block
121 && (*line == "*" || line.starts_with("* ") || line.starts_with("**"))
122 {
123*line = &line[1..];
124 }
125 }
126 }
127 }
128if changes {
129return Symbol::intern(&lines.join("\n"));
130 }
131 }
132data133}