mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 22:32:33 +00:00
refactor(derive): Shift responsibility for long help
This commit is contained in:
parent
abcee38466
commit
a71b2eb8b8
4 changed files with 30 additions and 33 deletions
|
@ -299,7 +299,7 @@ pub fn gen_augment(
|
|||
};
|
||||
|
||||
let id = item.id();
|
||||
let explicit_methods = item.field_methods(true);
|
||||
let explicit_methods = item.field_methods();
|
||||
let deprecations = if !override_required {
|
||||
item.deprecations()
|
||||
} else {
|
||||
|
|
|
@ -84,7 +84,7 @@ fn lits(variants: &[(&Variant, Item)]) -> Vec<(TokenStream, Ident)> {
|
|||
if !matches!(variant.fields, Fields::Unit) {
|
||||
abort!(variant.span(), "`#[derive(ValueEnum)]` only supports unit variants. Non-unit variants must be skipped");
|
||||
}
|
||||
let fields = item.field_methods(false);
|
||||
let fields = item.field_methods();
|
||||
let deprecations = item.deprecations();
|
||||
let name = item.cased_name();
|
||||
Some((
|
||||
|
|
|
@ -64,7 +64,7 @@ impl Item {
|
|||
let parsed_attrs = ClapAttr::parse_all(attrs);
|
||||
res.infer_kind(&parsed_attrs);
|
||||
res.push_attrs(&parsed_attrs);
|
||||
res.push_doc_comment(attrs, "about");
|
||||
res.push_doc_comment(attrs, "about", true);
|
||||
|
||||
res
|
||||
}
|
||||
|
@ -80,7 +80,7 @@ impl Item {
|
|||
let parsed_attrs = ClapAttr::parse_all(attrs);
|
||||
res.infer_kind(&parsed_attrs);
|
||||
res.push_attrs(&parsed_attrs);
|
||||
res.push_doc_comment(attrs, "about");
|
||||
res.push_doc_comment(attrs, "about", true);
|
||||
|
||||
res
|
||||
}
|
||||
|
@ -131,7 +131,7 @@ impl Item {
|
|||
res.infer_kind(&parsed_attrs);
|
||||
res.push_attrs(&parsed_attrs);
|
||||
if matches!(&*res.kind, Kind::Command(_)) {
|
||||
res.push_doc_comment(&variant.attrs, "about");
|
||||
res.push_doc_comment(&variant.attrs, "about", true);
|
||||
}
|
||||
|
||||
match &*res.kind {
|
||||
|
@ -174,7 +174,7 @@ impl Item {
|
|||
res.infer_kind(&parsed_attrs);
|
||||
res.push_attrs(&parsed_attrs);
|
||||
if matches!(&*res.kind, Kind::Value) {
|
||||
res.push_doc_comment(&variant.attrs, "help");
|
||||
res.push_doc_comment(&variant.attrs, "help", false);
|
||||
}
|
||||
|
||||
res
|
||||
|
@ -200,7 +200,7 @@ impl Item {
|
|||
res.infer_kind(&parsed_attrs);
|
||||
res.push_attrs(&parsed_attrs);
|
||||
if matches!(&*res.kind, Kind::Arg(_)) {
|
||||
res.push_doc_comment(&field.attrs, "help");
|
||||
res.push_doc_comment(&field.attrs, "help", true);
|
||||
}
|
||||
|
||||
match &*res.kind {
|
||||
|
@ -798,7 +798,7 @@ impl Item {
|
|||
}
|
||||
}
|
||||
|
||||
fn push_doc_comment(&mut self, attrs: &[Attribute], name: &str) {
|
||||
fn push_doc_comment(&mut self, attrs: &[Attribute], name: &str, supports_long_help: bool) {
|
||||
use syn::Lit::*;
|
||||
use syn::Meta::*;
|
||||
|
||||
|
@ -816,7 +816,11 @@ impl Item {
|
|||
})
|
||||
.collect();
|
||||
|
||||
self.doc_comment = process_doc_comment(comment_parts, name, !self.verbatim_doc_comment);
|
||||
let (short, long) = process_doc_comment(comment_parts, name, !self.verbatim_doc_comment);
|
||||
self.doc_comment.extend(short);
|
||||
if supports_long_help {
|
||||
self.doc_comment.extend(long);
|
||||
}
|
||||
}
|
||||
|
||||
fn set_kind(&mut self, kind: Sp<Kind>) {
|
||||
|
@ -865,21 +869,10 @@ impl Item {
|
|||
}
|
||||
|
||||
/// generate methods on top of a field
|
||||
pub fn field_methods(&self, supports_long_help: bool) -> proc_macro2::TokenStream {
|
||||
pub fn field_methods(&self) -> proc_macro2::TokenStream {
|
||||
let methods = &self.methods;
|
||||
match supports_long_help {
|
||||
true => {
|
||||
let doc_comment = &self.doc_comment;
|
||||
quote!( #(#doc_comment)* #(#methods)* )
|
||||
}
|
||||
false => {
|
||||
let doc_comment = self
|
||||
.doc_comment
|
||||
.iter()
|
||||
.filter(|mth| mth.name != "long_help");
|
||||
quote!( #(#doc_comment)* #(#methods)* )
|
||||
}
|
||||
}
|
||||
let doc_comment = &self.doc_comment;
|
||||
quote!( #(#doc_comment)* #(#methods)* )
|
||||
}
|
||||
|
||||
pub fn deprecations(&self) -> proc_macro2::TokenStream {
|
||||
|
|
|
@ -8,7 +8,11 @@ use crate::item::Method;
|
|||
use quote::{format_ident, quote};
|
||||
use std::iter;
|
||||
|
||||
pub fn process_doc_comment(lines: Vec<String>, name: &str, preprocess: bool) -> Vec<Method> {
|
||||
pub fn process_doc_comment(
|
||||
lines: Vec<String>,
|
||||
name: &str,
|
||||
preprocess: bool,
|
||||
) -> (Option<Method>, Option<Method>) {
|
||||
// multiline comments (`/** ... */`) may have LFs (`\n`) in them,
|
||||
// we need to split so we could handle the lines correctly
|
||||
//
|
||||
|
@ -31,7 +35,7 @@ pub fn process_doc_comment(lines: Vec<String>, name: &str, preprocess: bool) ->
|
|||
}
|
||||
|
||||
if lines.is_empty() {
|
||||
return vec![];
|
||||
return (None, None);
|
||||
}
|
||||
|
||||
let short_name = format_ident!("{}", name);
|
||||
|
@ -49,10 +53,10 @@ pub fn process_doc_comment(lines: Vec<String>, name: &str, preprocess: bool) ->
|
|||
(short, long)
|
||||
};
|
||||
|
||||
vec![
|
||||
Method::new(short_name, quote!(#short)),
|
||||
Method::new(long_name, quote!(#long)),
|
||||
]
|
||||
(
|
||||
Some(Method::new(short_name, quote!(#short))),
|
||||
Some(Method::new(long_name, quote!(#long))),
|
||||
)
|
||||
} else {
|
||||
let short = if preprocess {
|
||||
let s = merge_lines(&lines);
|
||||
|
@ -61,10 +65,10 @@ pub fn process_doc_comment(lines: Vec<String>, name: &str, preprocess: bool) ->
|
|||
lines.join("\n")
|
||||
};
|
||||
|
||||
vec![
|
||||
Method::new(short_name, quote!(#short)),
|
||||
Method::new(long_name, quote!(None)),
|
||||
]
|
||||
(
|
||||
Some(Method::new(short_name, quote!(#short))),
|
||||
Some(Method::new(long_name, quote!(None))),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue