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_arguments: bool,
|
||||||
sorted_enum_values: bool,
|
sorted_enum_values: bool,
|
||||||
federation: bool,
|
federation: bool,
|
||||||
|
prefer_single_line_descriptions: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SDLExportOptions {
|
impl SDLExportOptions {
|
||||||
|
@ -60,6 +61,16 @@ impl SDLExportOptions {
|
||||||
..self
|
..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 {
|
impl Registry {
|
||||||
|
@ -124,13 +135,8 @@ impl Registry {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if field.description.is_some() {
|
if let Some(description) = field.description {
|
||||||
writeln!(
|
export_description(sdl, options, false, description);
|
||||||
sdl,
|
|
||||||
"\t\"\"\"\n\t{}\n\t\"\"\"",
|
|
||||||
field.description.unwrap().replace('\n', "\n\t")
|
|
||||||
)
|
|
||||||
.ok();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !field.args.is_empty() {
|
if !field.args.is_empty() {
|
||||||
|
@ -180,8 +186,8 @@ impl Registry {
|
||||||
export_scalar = false;
|
export_scalar = false;
|
||||||
}
|
}
|
||||||
if export_scalar {
|
if export_scalar {
|
||||||
if description.is_some() {
|
if let Some(description) = description {
|
||||||
writeln!(sdl, "\"\"\"\n{}\n\"\"\"", description.unwrap()).ok();
|
export_description(sdl, options, true, description);
|
||||||
}
|
}
|
||||||
writeln!(sdl, "scalar {}", name).ok();
|
writeln!(sdl, "scalar {}", name).ok();
|
||||||
}
|
}
|
||||||
|
@ -218,8 +224,8 @@ impl Registry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if description.is_some() {
|
if let Some(description) = description {
|
||||||
writeln!(sdl, "\"\"\"\n{}\n\"\"\"", description.unwrap()).ok();
|
export_description(sdl, options, true, description);
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.federation && *extends {
|
if options.federation && *extends {
|
||||||
|
@ -249,8 +255,8 @@ impl Registry {
|
||||||
description,
|
description,
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
if description.is_some() {
|
if let Some(description) = description {
|
||||||
writeln!(sdl, "\"\"\"\n{}\n\"\"\"", description.unwrap()).ok();
|
export_description(sdl, options, true, description);
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.federation && *extends {
|
if options.federation && *extends {
|
||||||
|
@ -277,8 +283,8 @@ impl Registry {
|
||||||
description,
|
description,
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
if description.is_some() {
|
if let Some(description) = description {
|
||||||
writeln!(sdl, "\"\"\"\n{}\n\"\"\"", description.unwrap()).ok();
|
export_description(sdl, options, true, description);
|
||||||
}
|
}
|
||||||
|
|
||||||
write!(sdl, "enum {} ", name).ok();
|
write!(sdl, "enum {} ", name).ok();
|
||||||
|
@ -304,8 +310,8 @@ impl Registry {
|
||||||
oneof,
|
oneof,
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
if description.is_some() {
|
if let Some(description) = description {
|
||||||
writeln!(sdl, "\"\"\"\n{}\n\"\"\"", description.unwrap()).ok();
|
export_description(sdl, options, true, description);
|
||||||
}
|
}
|
||||||
|
|
||||||
write!(sdl, "input {} ", name).ok();
|
write!(sdl, "input {} ", name).ok();
|
||||||
|
@ -322,7 +328,7 @@ impl Registry {
|
||||||
|
|
||||||
for field in fields {
|
for field in fields {
|
||||||
if let Some(description) = field.description {
|
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();
|
writeln!(sdl, "\t{}", export_input_value(&field)).ok();
|
||||||
}
|
}
|
||||||
|
@ -335,8 +341,8 @@ impl Registry {
|
||||||
description,
|
description,
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
if description.is_some() {
|
if let Some(description) = description {
|
||||||
writeln!(sdl, "\"\"\"\n{}\n\"\"\"", description.unwrap()).ok();
|
export_description(sdl, options, true, description);
|
||||||
}
|
}
|
||||||
|
|
||||||
write!(sdl, "union {} =", name).ok();
|
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 {
|
fn export_input_value(input_value: &MetaInputValue) -> String {
|
||||||
if let Some(default_value) = &input_value.default_value {
|
if let Some(default_value) = &input_value.default_value {
|
||||||
format!(
|
format!(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user