Improve prop docs (#1565)

* Do the stuff

* More sanitizing + fix warnings and format
This commit is contained in:
Leonard authored and GitHub committed 2023-10-23 08:41:26 -05:00
1 parent 74952179db
commit cea9563e25
4 files changed
+177 -34

No files matched your search

@@ -153,9 +153,20 @@ fn get_props_docs(fn_ident: &Ident, inputs: Vec<&FnArg>) -> Vec<Attribute> {
});
Some((
pt.pat.to_token_stream().to_string(),
pt.ty.to_token_stream().to_string(),
pt.attrs.iter().filter(|a| !is_attr_doc(a)),
&pt.pat,
&pt.ty,
pt.attrs.iter().find_map(|attr| {
if attr.path() != &parse_quote!(deprecated) {
return None;
}
let res = crate::utils::DeprecatedAttribute::from_meta(&attr.meta);
match res {
Err(e) => panic!("{}", e.to_string()),
Ok(v) => Some(v),
}
}),
arg_doc,
))
}
@@ -171,40 +182,37 @@ fn get_props_docs(fn_ident: &Ident, inputs: Vec<&FnArg>) -> Vec<Attribute> {
#[doc = #header]
});
for (arg_name, arg_type, attrs, input_arg_doc) in arg_docs {
let input_arg_doc =
keep_up_to_two_consecutive_chars(input_arg_doc.trim_matches('\n'), '\n')
.replace('\n', "<br>");
let prop_def_link = format!("{props_def_link}::{arg_name}");
let mut attribute_list = String::new();
let mut is_deprecated = false;
for (arg_name, arg_type, deprecation, input_arg_doc) in arg_docs {
let arg_name = arg_name.into_token_stream().to_string();
let arg_type = crate::utils::format_type_string(arg_type);
for attr in attrs {
if attr.path() == &parse_quote!(deprecated) {
is_deprecated = true;
continue;
let input_arg_doc =
keep_up_to_n_consecutive_chars(input_arg_doc.trim(), 2, '\n').replace('\n', "<br/>");
let prop_def_link = format!("{props_def_link}::{arg_name}");
let mut arg_doc = format!("- [`{arg_name}`]({prop_def_link}) : `{arg_type}`");
if let Some(deprecation) = deprecation {
arg_doc.push_str("<br/>👎 Deprecated");
if let Some(since) = deprecation.since {
arg_doc.push_str(&format!(" since {since}"));
}
attribute_list.push('`');
attribute_list.push_str(&quote!(#attr).into_token_stream().to_string());
attribute_list.push_str("`, ");
if let Some(note) = deprecation.note {
let note = keep_up_to_n_consecutive_chars(&note, 1, '\n').replace('\n', " ");
let note = keep_up_to_n_consecutive_chars(&note, 1, '\t').replace('\t', " ");
arg_doc.push_str(&format!(": {note}"));
}
if !input_arg_doc.is_empty() {
arg_doc.push_str("<hr/>");
}
} else {
arg_doc.push_str("<br/>");
}
let mut arg_doc = format!("- [`{arg_name}`]({prop_def_link})");
if is_deprecated {
arg_doc.push_str(" 👎 Deprecated");
}
arg_doc.push_str(&format!("<br>Type: `{arg_type}`"));
if !attribute_list.is_empty() {
// Truncate the last `, `
attribute_list.truncate(attribute_list.len() - 2);
arg_doc.push_str(&format!("<br>Attributes: {attribute_list}"));
}
arg_doc.push_str(&format!("<hr>{input_arg_doc}"));
arg_doc.push_str(&input_arg_doc);
props_docs.push(parse_quote! {
#[doc = #arg_doc]
@@ -302,7 +310,11 @@ fn is_attr_doc(attr: &Attribute) -> bool {
attr.path() == &parse_quote!(doc)
}
fn keep_up_to_two_consecutive_chars(input: &str, target_char: char) -> String {
fn keep_up_to_n_consecutive_chars(
input: &str,
n_of_consecutive_chars_allowed: usize,
target_char: char,
) -> String {
let mut output = String::new();
let mut prev_char: Option<char> = None;
let mut consecutive_count = 0;
@@ -310,7 +322,7 @@ fn keep_up_to_two_consecutive_chars(input: &str, target_char: char) -> String {
for c in input.chars() {
match prev_char {
Some(prev) if c == target_char && prev == target_char => {
if consecutive_count < 2 {
if consecutive_count < n_of_consecutive_chars_allowed {
output.push(c);
consecutive_count += 1;
}