]>
git.proxmox.com Git - rustc.git/blob - vendor/clap_derive-3.2.18/src/utils/doc_comments.rs
1 //! The preprocessing we apply to doc comments.
3 //! #[derive(Parser)] works in terms of "paragraphs". Paragraph is a sequence of
4 //! non-empty adjacent lines, delimited by sequences of blank (whitespace only) lines.
6 use crate::attrs
::Method
;
8 use quote
::{format_ident, quote}
;
11 pub fn process_doc_comment(lines
: Vec
<String
>, name
: &str, preprocess
: bool
) -> Vec
<Method
> {
12 // multiline comments (`/** ... */`) may have LFs (`\n`) in them,
13 // we need to split so we could handle the lines correctly
15 // we also need to remove leading and trailing blank lines
16 let mut lines
: Vec
<&str> = lines
18 .skip_while(|s
| is_blank(s
))
19 .flat_map(|s
| s
.split('
\n'
))
22 while let Some(true) = lines
.last().map(|s
| is_blank(s
)) {
26 // remove one leading space no matter what
27 for line
in lines
.iter_mut() {
28 if line
.starts_with(' '
) {
37 let short_name
= format_ident
!("{}", name
);
38 let long_name
= format_ident
!("long_{}", name
);
40 if let Some(first_blank
) = lines
.iter().position(|s
| is_blank(s
)) {
41 let (short
, long
) = if preprocess
{
42 let paragraphs
= split_paragraphs(&lines
);
43 let short
= paragraphs
[0].clone();
44 let long
= paragraphs
.join("\n\n");
45 (remove_period(short
), long
)
47 let short
= lines
[..first_blank
].join("\n");
48 let long
= lines
.join("\n");
53 Method
::new(short_name
, quote
!(#short)),
54 Method
::new(long_name
, quote
!(#long)),
57 let short
= if preprocess
{
58 let s
= merge_lines(&lines
);
65 Method
::new(short_name
, quote
!(#short)),
66 Method
::new(long_name
, quote
!(None
)),
71 fn split_paragraphs(lines
: &[&str]) -> Vec
<String
> {
72 let mut last_line
= 0;
74 let slice
= &lines
[last_line
..];
75 let start
= slice
.iter().position(|s
| !is_blank(s
)).unwrap_or(0);
77 let slice
= &slice
[start
..];
80 .position(|s
| is_blank(s
))
81 .unwrap_or(slice
.len());
83 last_line
+= start
+ len
;
86 Some(merge_lines(&slice
[..len
]))
94 fn remove_period(mut s
: String
) -> String
{
95 if s
.ends_with('
.'
) && !s
.ends_with("..") {
101 fn is_blank(s
: &str) -> bool
{
105 fn merge_lines(lines
: &[&str]) -> String
{
106 lines
.iter().map(|s
| s
.trim()).collect
::<Vec
<_
>>().join(" ")