Do not collapse newlines in doc comments (#310)

This commit is contained in:
Graham Esau 2024-08-04 16:43:22 +01:00 committed by GitHub
parent 91ee3f915c
commit ce15380863
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 14 additions and 48 deletions

View file

@ -1,7 +1,7 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "My Amazing Struct",
"description": "This struct shows off generating a schema with a custom title and description.",
"description": "This struct shows off generating a schema with\na custom title and description.",
"type": "object",
"properties": {
"my_bool": {
@ -48,7 +48,7 @@
]
},
{
"description": "A struct-like enum variant which contains some floats",
"description": "A struct-like enum variant which contains\nsome floats",
"type": "object",
"properties": {
"StructVariant": {

View file

@ -1,7 +1,7 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "My Amazing Struct",
"description": "This struct shows off generating a schema with a custom title and description.",
"description": "This struct shows off generating a schema with\na custom title and description.",
"type": "object",
"properties": {
"my_bool": {
@ -48,7 +48,7 @@
]
},
{
"description": "A struct-like enum variant which contains some floats",
"description": "A struct-like enum variant which contains\nsome floats",
"type": "object",
"properties": {
"StructVariant": {

View file

@ -5,12 +5,10 @@ use util::*;
#[allow(dead_code)]
#[derive(JsonSchema)]
/**
*
* # This is the struct's title
*
* This is the struct's description.
*
*/
# This is the struct's title
This is the struct's description.
*/
struct MyStruct {
/// # An integer
my_int: i32,

View file

@ -1,7 +1,7 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "This is the enum's title",
"description": "This is the enum's description.",
"description": "This is \n the enum's description.",
"oneOf": [
{
"type": "string",
@ -25,7 +25,7 @@
"properties": {
"my_nullable_string": {
"title": "A nullable string",
"description": "This field is a nullable string.\n\nThis is the second line!\n\nAnd this is the third!",
"description": "This field is a nullable string.\n\n This\nis\n the second\n line!\n\n\n\n\n And this is the third!",
"type": [
"string",
"null"

View file

@ -14,25 +14,15 @@ pub fn get_title_and_desc_from_doc(attrs: &[Attribute]) -> (Option<String>, Opti
.trim_start_matches('#')
.trim()
.to_owned();
let maybe_desc = split.next().and_then(merge_description_lines);
let maybe_desc = split.next().map(|s| s.trim().to_owned());
(none_if_empty(title), maybe_desc)
} else {
(None, merge_description_lines(&doc))
(None, Some(doc))
}
}
fn merge_description_lines(doc: &str) -> Option<String> {
let desc = doc
.trim()
.split("\n\n")
.filter_map(|line| none_if_empty(line.trim().replace('\n', " ")))
.collect::<Vec<_>>()
.join("\n\n");
none_if_empty(desc)
}
fn get_doc(attrs: &[Attribute]) -> Option<String> {
let attrs = attrs
let lines = attrs
.iter()
.filter_map(|attr| {
if !attr.path().is_ident("doc") {
@ -52,29 +42,7 @@ fn get_doc(attrs: &[Attribute]) -> Option<String> {
})
.collect::<Vec<_>>();
let mut lines = attrs
.iter()
.flat_map(|a| a.split('\n'))
.map(str::trim)
.skip_while(|s| s.is_empty())
.collect::<Vec<_>>();
if let Some(&"") = lines.last() {
lines.pop();
}
// Added for backward-compatibility, but perhaps we shouldn't do this
// https://github.com/rust-lang/rust/issues/32088
if lines.iter().all(|l| l.starts_with('*')) {
for line in lines.iter_mut() {
*line = line[1..].trim()
}
while let Some(&"") = lines.first() {
lines.remove(0);
}
};
none_if_empty(lines.join("\n"))
none_if_empty(lines.join("\n").trim().to_owned())
}
fn none_if_empty(s: String) -> Option<String> {