1 pub use CommentStyle::*;
4 use crate::source_map::SourceMap;
5 use crate::sess::ParseSess;
7 use syntax_pos::{BytePos, CharPos, Pos, FileName};
16 #[derive(Clone, Copy, PartialEq, Debug)]
17 pub enum CommentStyle {
18 /// No code on either side of each line of the comment
20 /// Code exists to the left of the comment
22 /// Code before /* foo */ and after the comment
24 /// Just a manual blank line "\n\n", for layout
30 pub style: CommentStyle,
31 pub lines: Vec<String>,
35 pub fn is_line_doc_comment(s: &str) -> bool {
36 let res = (s.starts_with("///") && *s.as_bytes().get(3).unwrap_or(&b' ') != b'/') ||
38 debug!("is {:?} a doc comment? {}", s, res);
42 pub fn is_block_doc_comment(s: &str) -> bool {
43 // Prevent `/**/` from being parsed as a doc comment
44 let res = ((s.starts_with("/**") && *s.as_bytes().get(3).unwrap_or(&b' ') != b'*') ||
45 s.starts_with("/*!")) && s.len() >= 5;
46 debug!("is {:?} a doc comment? {}", s, res);
50 // FIXME(#64197): Try to privatize this again.
51 pub fn is_doc_comment(s: &str) -> bool {
52 (s.starts_with("///") && is_line_doc_comment(s)) || s.starts_with("//!") ||
53 (s.starts_with("/**") && is_block_doc_comment(s)) || s.starts_with("/*!")
56 pub fn doc_comment_style(comment: &str) -> ast::AttrStyle {
57 assert!(is_doc_comment(comment));
58 if comment.starts_with("//!") || comment.starts_with("/*!") {
65 pub fn strip_doc_comment_decoration(comment: &str) -> String {
66 /// remove whitespace-only lines from the start/end of lines
67 fn vertical_trim(lines: Vec<String>) -> Vec<String> {
69 let mut j = lines.len();
70 // first line of all-stars should be omitted
71 if !lines.is_empty() && lines[0].chars().all(|c| c == '*') {
75 while i < j && lines[i].trim().is_empty() {
78 // like the first, a last line of all stars should be omitted
87 while j > i && lines[j - 1].trim().is_empty() {
94 /// remove a "[ \t]*\*" block from each line, if possible
95 fn horizontal_trim(lines: Vec<String>) -> Vec<String> {
96 let mut i = usize::MAX;
97 let mut can_trim = true;
101 for (j, c) in line.chars().enumerate() {
102 if j > i || !"* \t".contains(c) {
126 .map(|line| (&line[i + 1..line.len()]).to_string())
133 // one-line comments lose their prefix
134 const ONELINERS: &[&str] = &["///!", "///", "//!", "//"];
136 for prefix in ONELINERS {
137 if comment.starts_with(*prefix) {
138 return (&comment[prefix.len()..]).to_string();
142 if comment.starts_with("/*") {
143 let lines = comment[3..comment.len() - 2]
145 .map(|s| s.to_string())
146 .collect::<Vec<String>>();
148 let lines = vertical_trim(lines);
149 let lines = horizontal_trim(lines);
151 return lines.join("\n");
154 panic!("not a doc-comment: {}", comment);
157 /// Returns `None` if the first `col` chars of `s` contain a non-whitespace char.
158 /// Otherwise returns `Some(k)` where `k` is first char offset after that leading
159 /// whitespace. Note that `k` may be outside bounds of `s`.
160 fn all_whitespace(s: &str, col: CharPos) -> Option<usize> {
162 for (i, ch) in s.char_indices().take(col.to_usize()) {
163 if !ch.is_whitespace() {
166 idx = i + ch.len_utf8();
171 fn trim_whitespace_prefix(s: &str, col: CharPos) -> &str {
173 match all_whitespace(&s, col) {
174 Some(col) => if col < len { &s[col..] } else { "" },
179 fn split_block_comment_into_lines(
183 let mut res: Vec<String> = vec![];
184 let mut lines = text.lines();
185 // just push the first line
186 res.extend(lines.next().map(|it| it.to_string()));
187 // for other lines, strip common whitespace prefix
189 res.push(trim_whitespace_prefix(line, col).to_string())
194 // it appears this function is called only from pprust... that's
195 // probably not a good thing.
196 crate fn gather_comments(sess: &ParseSess, path: FileName, src: String) -> Vec<Comment> {
197 let cm = SourceMap::new(sess.source_map().path_mapping().clone());
198 let source_file = cm.new_source_file(path, src);
199 let text = (*source_file.src.as_ref().unwrap()).clone();
201 let text: &str = text.as_str();
202 let start_bpos = source_file.start_pos;
204 let mut comments: Vec<Comment> = Vec::new();
205 let mut code_to_the_left = false;
207 if let Some(shebang_len) = rustc_lexer::strip_shebang(text) {
208 comments.push(Comment {
210 lines: vec![text[..shebang_len].to_string()],
216 for token in rustc_lexer::tokenize(&text[pos..]) {
217 let token_text = &text[pos..pos + token.len];
219 rustc_lexer::TokenKind::Whitespace => {
220 if let Some(mut idx) = token_text.find('\n') {
221 code_to_the_left = false;
222 while let Some(next_newline) = &token_text[idx + 1..].find('\n') {
223 idx = idx + 1 + next_newline;
224 comments.push(Comment {
227 pos: start_bpos + BytePos((pos + idx) as u32),
232 rustc_lexer::TokenKind::BlockComment { terminated: _ } => {
233 if !is_block_doc_comment(token_text) {
234 let code_to_the_right = match text[pos + token.len..].chars().next() {
235 Some('\r') | Some('\n') => false,
238 let style = match (code_to_the_left, code_to_the_right) {
239 (true, true) | (false, true) => Mixed,
240 (false, false) => Isolated,
241 (true, false) => Trailing,
244 // Count the number of chars since the start of the line by rescanning.
245 let pos_in_file = start_bpos + BytePos(pos as u32);
246 let line_begin_in_file = source_file.line_begin_pos(pos_in_file);
247 let line_begin_pos = (line_begin_in_file - start_bpos).to_usize();
248 let col = CharPos(text[line_begin_pos..pos].chars().count());
250 let lines = split_block_comment_into_lines(token_text, col);
251 comments.push(Comment { style, lines, pos: pos_in_file })
254 rustc_lexer::TokenKind::LineComment => {
255 if !is_doc_comment(token_text) {
256 comments.push(Comment {
257 style: if code_to_the_left { Trailing } else { Isolated },
258 lines: vec![token_text.to_string()],
259 pos: start_bpos + BytePos(pos as u32),
264 code_to_the_left = true;