Add a prefer_single_line_descriptions option on SDLExportOptions (#955)
* Add a prefer_single_line_descriptions option on SDLExportOptions The default export uses three lines for each description. Single-line descriptions improve the readability of the exported SDL when there are many short descriptions. * export_sdl: escape " in single-line descriptions I confirmed that graphql-code-generator properly handles SDL with this escaping.
This commit is contained in:
parent
25261e2166
commit
5b7303d3fe
|
@ -12,6 +12,7 @@ pub struct SDLExportOptions {
|
|||
sorted_arguments: bool,
|
||||
sorted_enum_values: bool,
|
||||
federation: bool,
|
||||
prefer_single_line_descriptions: bool,
|
||||
}
|
||||
|
||||
impl SDLExportOptions {
|
||||
|
@ -60,6 +61,16 @@ impl SDLExportOptions {
|
|||
..self
|
||||
}
|
||||
}
|
||||
|
||||
/// When possible, write one-line instead of three-line descriptions
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn prefer_single_line_descriptions(self) -> Self {
|
||||
Self {
|
||||
prefer_single_line_descriptions: true,
|
||||
..self
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Registry {
|
||||
|
@ -124,13 +135,8 @@ impl Registry {
|
|||
continue;
|
||||
}
|
||||
|
||||
if field.description.is_some() {
|
||||
writeln!(
|
||||
sdl,
|
||||
"\t\"\"\"\n\t{}\n\t\"\"\"",
|
||||
field.description.unwrap().replace('\n', "\n\t")
|
||||
)
|
||||
.ok();
|
||||
if let Some(description) = field.description {
|
||||
export_description(sdl, options, false, description);
|
||||
}
|
||||
|
||||
if !field.args.is_empty() {
|
||||
|
@ -180,8 +186,8 @@ impl Registry {
|
|||
export_scalar = false;
|
||||
}
|
||||
if export_scalar {
|
||||
if description.is_some() {
|
||||
writeln!(sdl, "\"\"\"\n{}\n\"\"\"", description.unwrap()).ok();
|
||||
if let Some(description) = description {
|
||||
export_description(sdl, options, true, description);
|
||||
}
|
||||
writeln!(sdl, "scalar {}", name).ok();
|
||||
}
|
||||
|
@ -218,8 +224,8 @@ impl Registry {
|
|||
}
|
||||
}
|
||||
|
||||
if description.is_some() {
|
||||
writeln!(sdl, "\"\"\"\n{}\n\"\"\"", description.unwrap()).ok();
|
||||
if let Some(description) = description {
|
||||
export_description(sdl, options, true, description);
|
||||
}
|
||||
|
||||
if options.federation && *extends {
|
||||
|
@ -249,8 +255,8 @@ impl Registry {
|
|||
description,
|
||||
..
|
||||
} => {
|
||||
if description.is_some() {
|
||||
writeln!(sdl, "\"\"\"\n{}\n\"\"\"", description.unwrap()).ok();
|
||||
if let Some(description) = description {
|
||||
export_description(sdl, options, true, description);
|
||||
}
|
||||
|
||||
if options.federation && *extends {
|
||||
|
@ -277,8 +283,8 @@ impl Registry {
|
|||
description,
|
||||
..
|
||||
} => {
|
||||
if description.is_some() {
|
||||
writeln!(sdl, "\"\"\"\n{}\n\"\"\"", description.unwrap()).ok();
|
||||
if let Some(description) = description {
|
||||
export_description(sdl, options, true, description);
|
||||
}
|
||||
|
||||
write!(sdl, "enum {} ", name).ok();
|
||||
|
@ -304,8 +310,8 @@ impl Registry {
|
|||
oneof,
|
||||
..
|
||||
} => {
|
||||
if description.is_some() {
|
||||
writeln!(sdl, "\"\"\"\n{}\n\"\"\"", description.unwrap()).ok();
|
||||
if let Some(description) = description {
|
||||
export_description(sdl, options, true, description);
|
||||
}
|
||||
|
||||
write!(sdl, "input {} ", name).ok();
|
||||
|
@ -322,7 +328,7 @@ impl Registry {
|
|||
|
||||
for field in fields {
|
||||
if let Some(description) = field.description {
|
||||
writeln!(sdl, "\t\"\"\"\n\t{}\n\t\"\"\"", description).ok();
|
||||
export_description(sdl, options, false, description);
|
||||
}
|
||||
writeln!(sdl, "\t{}", export_input_value(&field)).ok();
|
||||
}
|
||||
|
@ -335,8 +341,8 @@ impl Registry {
|
|||
description,
|
||||
..
|
||||
} => {
|
||||
if description.is_some() {
|
||||
writeln!(sdl, "\"\"\"\n{}\n\"\"\"", description.unwrap()).ok();
|
||||
if let Some(description) = description {
|
||||
export_description(sdl, options, true, description);
|
||||
}
|
||||
|
||||
write!(sdl, "union {} =", name).ok();
|
||||
|
@ -370,6 +376,26 @@ impl Registry {
|
|||
}
|
||||
}
|
||||
|
||||
fn export_description(
|
||||
sdl: &mut String,
|
||||
options: &SDLExportOptions,
|
||||
top_level: bool,
|
||||
description: &str,
|
||||
) {
|
||||
if options.prefer_single_line_descriptions && !description.contains('\n') {
|
||||
let tab = if top_level { "" } else { "\t" };
|
||||
let description = description.replace('"', r#"\""#);
|
||||
writeln!(sdl, "{}\"{}\"", tab, description).ok();
|
||||
} else {
|
||||
if top_level {
|
||||
writeln!(sdl, "\"\"\"\n{}\n\"\"\"", description).ok();
|
||||
} else {
|
||||
let description = description.replace('\n', "\n\t");
|
||||
writeln!(sdl, "\t\"\"\"\n\t{}\n\t\"\"\"", description).ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn export_input_value(input_value: &MetaInputValue) -> String {
|
||||
if let Some(default_value) = &input_value.default_value {
|
||||
format!(
|
||||
|
|
Loading…
Reference in New Issue