diff --git a/docs/src/config/comma.md b/docs/src/config/comma.md index b4e1e18..6259b5c 100644 --- a/docs/src/config/comma.md +++ b/docs/src/config/comma.md @@ -9,11 +9,12 @@ Possible option values: - `"never"`: Do not insert commas inside a list of items. All existed commas will be removed. - `"noTrailing"`: Insert commas inside a list of items without trailing comma. - `"onlySingleLine"`: Insert commas inside a list of items only for single line list. For multiple lines list, there won't be commas. +- `"inherit"`: Inherit from the base `comma` option. Default option value is `"onlySingleLine"`. This global option can be overridden by different syntax nodes. -Some syntax-node-specific options will override by default: +Some syntax-node-specific options will override by default instead of `"inherit"`: - `arguments.comma` - `argumentsDefinition.comma` diff --git a/docs/src/config/single-line.md b/docs/src/config/single-line.md index 7a97387..fee5968 100644 --- a/docs/src/config/single-line.md +++ b/docs/src/config/single-line.md @@ -8,11 +8,12 @@ Possible option values: - `"prefer"`: Place items on single line as possible. - `"smart"`: Whether items should be placed on single line will be determined by original layout. - `"never"`: Force items to be on multiple lines. +- `"inherit"`: Inherit from the base `singleLine` option. Default option value is `"smart"`. This global option can be overridden by different syntax nodes. -Some syntax-node-specific options will override by default: +Some syntax-node-specific options will override by default instead of `"inherit"`: - `arguments.singleLine` - `argumentsDefinition.singleLine` diff --git a/dprint_plugin/deployment/schema.json b/dprint_plugin/deployment/schema.json index e59580b..0582699 100644 --- a/dprint_plugin/deployment/schema.json +++ b/dprint_plugin/deployment/schema.json @@ -7,7 +7,7 @@ "definitions": { "comma": { "description": "Control whether commas should be inserted inside a list of items.", - "type": ["string", "null"], + "type": "string", "oneOf": [ { "const": "always", @@ -24,12 +24,16 @@ { "const": "onlySingleLine", "description": "Insert commas inside a list of items only for single line list. For multiple lines list, there won't be commas." + }, + { + "const": "inherit", + "description": "Inherit from the base `comma` option." } ] }, "singleLine": { "description": "Control whether items should be placed on single line as possible, even they're originally on multiple lines, or force them to be on multiple lines.", - "type": ["string", "null"], + "type": "string", "oneOf": [ { "const": "prefer", @@ -42,6 +46,10 @@ { "const": "never", "description": "Force items to be on multiple lines." + }, + { + "const": "inherit", + "description": "Inherit from the base `singleLine` option." } ] }, @@ -82,16 +90,15 @@ }, "comma": { "$ref": "#/definitions/comma", - "type": "string", "default": "onlySingleLine" }, "arguments.comma": { "$ref": "#/definitions/comma", - "default": null + "default": "inherit" }, "argumentsDefinition.comma": { "$ref": "#/definitions/comma", - "default": null + "default": "inherit" }, "directives.comma": { "$ref": "#/definitions/comma", @@ -111,11 +118,11 @@ }, "listValue.comma": { "$ref": "#/definitions/comma", - "default": null + "default": "inherit" }, "objectValue.comma": { "$ref": "#/definitions/comma", - "default": null + "default": "inherit" }, "schemaDefinition.comma": { "$ref": "#/definitions/comma", @@ -131,28 +138,27 @@ }, "variableDefinitions.comma": { "$ref": "#/definitions/comma", - "default": null + "default": "inherit" }, "singleLine": { "$ref": "#/definitions/singleLine", - "type": "string", "default": "smart" }, "arguments.singleLine": { "$ref": "#/definitions/singleLine", - "default": null + "default": "inherit" }, "argumentsDefinition.singleLine": { "$ref": "#/definitions/singleLine", - "default": null + "default": "inherit" }, "directiveLocations.singleLine": { "$ref": "#/definitions/singleLine", - "default": null + "default": "inherit" }, "directives.singleLine": { "$ref": "#/definitions/singleLine", - "default": null + "default": "inherit" }, "enumValuesDefinition.singleLine": { "$ref": "#/definitions/singleLine", @@ -164,7 +170,7 @@ }, "implementsInterfaces.singleLine": { "$ref": "#/definitions/singleLine", - "default": null + "default": "inherit" }, "inputFieldsDefinition.singleLine": { "$ref": "#/definitions/singleLine", @@ -172,11 +178,11 @@ }, "listValue.singleLine": { "$ref": "#/definitions/singleLine", - "default": null + "default": "inherit" }, "objectValue.singleLine": { "$ref": "#/definitions/singleLine", - "default": null + "default": "inherit" }, "schemaDefinition.singleLine": { "$ref": "#/definitions/singleLine", @@ -192,11 +198,11 @@ }, "unionMemberTypes.singleLine": { "$ref": "#/definitions/singleLine", - "default": null + "default": "inherit" }, "variableDefinitions.singleLine": { "$ref": "#/definitions/singleLine", - "default": null + "default": "inherit" }, "parenSpacing": { "$ref": "#/definitions/parenSpacing", diff --git a/dprint_plugin/src/config.rs b/dprint_plugin/src/config.rs index cd24846..d0cbffd 100644 --- a/dprint_plugin/src/config.rs +++ b/dprint_plugin/src/config.rs @@ -62,6 +62,7 @@ pub(crate) fn resolve_config( "never" => Comma::Never, "noTrailing" => Comma::NoTrailing, "onlySingleLine" => Comma::OnlySingleLine, + "inherit" => Comma::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "comma".into(), @@ -70,55 +71,55 @@ pub(crate) fn resolve_config( Comma::OnlySingleLine } }, - arguments_comma: get_nullable_value::( + arguments_comma: match &*get_value( &mut config, "arguments.comma", + "inherit".to_string(), &mut diagnostics, - ) - .as_deref() - .and_then(|option_value| match option_value { - "always" => Some(Comma::Always), - "never" => Some(Comma::Never), - "noTrailing" => Some(Comma::NoTrailing), - "onlySingleLine" => Some(Comma::OnlySingleLine), + ) { + "always" => Comma::Always, + "never" => Comma::Never, + "noTrailing" => Comma::NoTrailing, + "onlySingleLine" => Comma::OnlySingleLine, + "inherit" => Comma::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "arguments.comma".into(), message: "invalid value for config `arguments.comma`".into(), }); - None + Comma::Inherit } - }), - arguments_definition_comma: get_nullable_value::( + }, + arguments_definition_comma: match &*get_value( &mut config, "argumentsDefinition.comma", + "inherit".to_string(), &mut diagnostics, - ) - .as_deref() - .and_then(|option_value| match option_value { - "always" => Some(Comma::Always), - "never" => Some(Comma::Never), - "noTrailing" => Some(Comma::NoTrailing), - "onlySingleLine" => Some(Comma::OnlySingleLine), + ) { + "always" => Comma::Always, + "never" => Comma::Never, + "noTrailing" => Comma::NoTrailing, + "onlySingleLine" => Comma::OnlySingleLine, + "inherit" => Comma::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "argumentsDefinition.comma".into(), message: "invalid value for config `argumentsDefinition.comma`".into(), }); - None + Comma::Inherit } - }), - directives_comma: get_nullable_value::( + }, + directives_comma: match &*get_value( &mut config, "directives.comma", + "never".to_string(), &mut diagnostics, - ) - .as_deref() - .map(|option_value| match option_value { + ) { "always" => Comma::Always, "never" => Comma::Never, "noTrailing" => Comma::NoTrailing, "onlySingleLine" => Comma::OnlySingleLine, + "inherit" => Comma::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "directives.comma".into(), @@ -126,19 +127,18 @@ pub(crate) fn resolve_config( }); Comma::Never } - }) - .or(Some(Comma::Never)), - enum_values_definition_comma: get_nullable_value::( + }, + enum_values_definition_comma: match &*get_value( &mut config, "enumValuesDefinition.comma", + "never".to_string(), &mut diagnostics, - ) - .as_deref() - .map(|option_value| match option_value { + ) { "always" => Comma::Always, "never" => Comma::Never, "noTrailing" => Comma::NoTrailing, "onlySingleLine" => Comma::OnlySingleLine, + "inherit" => Comma::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "enumValuesDefinition.comma".into(), @@ -146,19 +146,18 @@ pub(crate) fn resolve_config( }); Comma::Never } - }) - .or(Some(Comma::Never)), - fields_definition_comma: get_nullable_value::( + }, + fields_definition_comma: match &*get_value( &mut config, "fieldsDefinition.comma", + "never".to_string(), &mut diagnostics, - ) - .as_deref() - .map(|option_value| match option_value { + ) { "always" => Comma::Always, "never" => Comma::Never, "noTrailing" => Comma::NoTrailing, "onlySingleLine" => Comma::OnlySingleLine, + "inherit" => Comma::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "fieldsDefinition.comma".into(), @@ -166,19 +165,18 @@ pub(crate) fn resolve_config( }); Comma::Never } - }) - .or(Some(Comma::Never)), - input_fields_definition_comma: get_nullable_value::( + }, + input_fields_definition_comma: match &*get_value( &mut config, "inputFieldsDefinition.comma", + "never".to_string(), &mut diagnostics, - ) - .as_deref() - .map(|option_value| match option_value { + ) { "always" => Comma::Always, "never" => Comma::Never, "noTrailing" => Comma::NoTrailing, "onlySingleLine" => Comma::OnlySingleLine, + "inherit" => Comma::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "inputFieldsDefinition.comma".into(), @@ -186,57 +184,56 @@ pub(crate) fn resolve_config( }); Comma::Never } - }) - .or(Some(Comma::Never)), - list_value_comma: get_nullable_value::( + }, + list_value_comma: match &*get_value( &mut config, "listValue.comma", + "inherit".to_string(), &mut diagnostics, - ) - .as_deref() - .and_then(|option_value| match option_value { - "always" => Some(Comma::Always), - "never" => Some(Comma::Never), - "noTrailing" => Some(Comma::NoTrailing), - "onlySingleLine" => Some(Comma::OnlySingleLine), + ) { + "always" => Comma::Always, + "never" => Comma::Never, + "noTrailing" => Comma::NoTrailing, + "onlySingleLine" => Comma::OnlySingleLine, + "inherit" => Comma::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "listValue.comma".into(), message: "invalid value for config `listValue.comma`".into(), }); - None + Comma::Inherit } - }), - object_value_comma: get_nullable_value::( + }, + object_value_comma: match &*get_value( &mut config, "objectValue.comma", + "inherit".to_string(), &mut diagnostics, - ) - .as_deref() - .and_then(|option_value| match option_value { - "always" => Some(Comma::Always), - "never" => Some(Comma::Never), - "noTrailing" => Some(Comma::NoTrailing), - "onlySingleLine" => Some(Comma::OnlySingleLine), + ) { + "always" => Comma::Always, + "never" => Comma::Never, + "noTrailing" => Comma::NoTrailing, + "onlySingleLine" => Comma::OnlySingleLine, + "inherit" => Comma::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "objectValue.comma".into(), message: "invalid value for config `objectValue.comma`".into(), }); - None + Comma::Inherit } - }), - schema_definition_comma: get_nullable_value::( + }, + schema_definition_comma: match &*get_value( &mut config, "schemaDefinition.comma", + "never".to_string(), &mut diagnostics, - ) - .as_deref() - .map(|option_value| match option_value { + ) { "always" => Comma::Always, "never" => Comma::Never, "noTrailing" => Comma::NoTrailing, "onlySingleLine" => Comma::OnlySingleLine, + "inherit" => Comma::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "schemaDefinition.comma".into(), @@ -244,19 +241,18 @@ pub(crate) fn resolve_config( }); Comma::Never } - }) - .or(Some(Comma::Never)), - schema_extension_comma: get_nullable_value::( + }, + schema_extension_comma: match &*get_value( &mut config, "schemaExtension.comma", + "never".to_string(), &mut diagnostics, - ) - .as_deref() - .map(|option_value| match option_value { + ) { "always" => Comma::Always, "never" => Comma::Never, "noTrailing" => Comma::NoTrailing, "onlySingleLine" => Comma::OnlySingleLine, + "inherit" => Comma::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "schemaExtension.comma".into(), @@ -264,19 +260,18 @@ pub(crate) fn resolve_config( }); Comma::Never } - }) - .or(Some(Comma::Never)), - selection_set_comma: get_nullable_value::( + }, + selection_set_comma: match &*get_value( &mut config, "selectionSet.comma", + "never".to_string(), &mut diagnostics, - ) - .as_deref() - .map(|option_value| match option_value { + ) { "always" => Comma::Always, "never" => Comma::Never, "noTrailing" => Comma::NoTrailing, "onlySingleLine" => Comma::OnlySingleLine, + "inherit" => Comma::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "selectionSet.comma".into(), @@ -284,27 +279,26 @@ pub(crate) fn resolve_config( }); Comma::Never } - }) - .or(Some(Comma::Never)), - variable_definitions_comma: get_nullable_value::( + }, + variable_definitions_comma: match &*get_value( &mut config, "variableDefinitions.comma", + "inherit".to_string(), &mut diagnostics, - ) - .as_deref() - .and_then(|option_value| match option_value { - "always" => Some(Comma::Always), - "never" => Some(Comma::Never), - "noTrailing" => Some(Comma::NoTrailing), - "onlySingleLine" => Some(Comma::OnlySingleLine), + ) { + "always" => Comma::Always, + "never" => Comma::Never, + "noTrailing" => Comma::NoTrailing, + "onlySingleLine" => Comma::OnlySingleLine, + "inherit" => Comma::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "variableDefinitions.comma".into(), message: "invalid value for config `variableDefinitions.comma`".into(), }); - None + Comma::Inherit } - }), + }, single_line: match &*get_value( &mut config, "singleLine", @@ -314,6 +308,7 @@ pub(crate) fn resolve_config( "prefer" => SingleLine::Prefer, "smart" => SingleLine::Smart, "never" => SingleLine::Never, + "inherit" => SingleLine::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "singleLine".into(), @@ -322,88 +317,88 @@ pub(crate) fn resolve_config( SingleLine::Smart } }, - arguments_single_line: get_nullable_value::( + arguments_single_line: match &*get_value( &mut config, "arguments.singleLine", + "inherit".to_string(), &mut diagnostics, - ) - .as_deref() - .and_then(|option_value| match option_value { - "prefer" => Some(SingleLine::Prefer), - "smart" => Some(SingleLine::Smart), - "never" => Some(SingleLine::Never), + ) { + "prefer" => SingleLine::Prefer, + "smart" => SingleLine::Smart, + "never" => SingleLine::Never, + "inherit" => SingleLine::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "arguments.singleLine".into(), message: "invalid value for config `arguments.singleLine`".into(), }); - None + SingleLine::Inherit } - }), - arguments_definition_single_line: get_nullable_value::( + }, + arguments_definition_single_line: match &*get_value( &mut config, "argumentsDefinition.singleLine", + "inherit".to_string(), &mut diagnostics, - ) - .as_deref() - .and_then(|option_value| match option_value { - "prefer" => Some(SingleLine::Prefer), - "smart" => Some(SingleLine::Smart), - "never" => Some(SingleLine::Never), + ) { + "prefer" => SingleLine::Prefer, + "smart" => SingleLine::Smart, + "never" => SingleLine::Never, + "inherit" => SingleLine::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "argumentsDefinition.singleLine".into(), message: "invalid value for config `argumentsDefinition.singleLine`".into(), }); - None + SingleLine::Inherit } - }), - directive_locations_single_line: get_nullable_value::( + }, + directive_locations_single_line: match &*get_value( &mut config, "directiveLocations.singleLine", + "inherit".to_string(), &mut diagnostics, - ) - .as_deref() - .and_then(|option_value| match option_value { - "prefer" => Some(SingleLine::Prefer), - "smart" => Some(SingleLine::Smart), - "never" => Some(SingleLine::Never), + ) { + "prefer" => SingleLine::Prefer, + "smart" => SingleLine::Smart, + "never" => SingleLine::Never, + "inherit" => SingleLine::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "directiveLocations.singleLine".into(), message: "invalid value for config `directiveLocations.singleLine`".into(), }); - None + SingleLine::Inherit } - }), - directives_single_line: get_nullable_value::( + }, + directives_single_line: match &*get_value( &mut config, "directives.singleLine", + "inherit".to_string(), &mut diagnostics, - ) - .as_deref() - .and_then(|option_value| match option_value { - "prefer" => Some(SingleLine::Prefer), - "smart" => Some(SingleLine::Smart), - "never" => Some(SingleLine::Never), + ) { + "prefer" => SingleLine::Prefer, + "smart" => SingleLine::Smart, + "never" => SingleLine::Never, + "inherit" => SingleLine::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "directives.singleLine".into(), message: "invalid value for config `directives.singleLine`".into(), }); - None + SingleLine::Inherit } - }), - enum_values_definition_single_line: get_nullable_value::( + }, + enum_values_definition_single_line: match &*get_value( &mut config, "enumValuesDefinition.singleLine", + "never".to_string(), &mut diagnostics, - ) - .as_deref() - .map(|option_value| match option_value { + ) { "prefer" => SingleLine::Prefer, "smart" => SingleLine::Smart, "never" => SingleLine::Never, + "inherit" => SingleLine::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "enumValuesDefinition.singleLine".into(), @@ -412,18 +407,17 @@ pub(crate) fn resolve_config( }); SingleLine::Never } - }) - .or(Some(SingleLine::Never)), - fields_definition_single_line: get_nullable_value::( + }, + fields_definition_single_line: match &*get_value( &mut config, "fieldsDefinition.singleLine", + "never".to_string(), &mut diagnostics, - ) - .as_deref() - .map(|option_value| match option_value { + ) { "prefer" => SingleLine::Prefer, "smart" => SingleLine::Smart, "never" => SingleLine::Never, + "inherit" => SingleLine::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "fieldsDefinition.singleLine".into(), @@ -431,37 +425,36 @@ pub(crate) fn resolve_config( }); SingleLine::Never } - }) - .or(Some(SingleLine::Never)), - implements_interfaces_single_line: get_nullable_value::( + }, + implements_interfaces_single_line: match &*get_value( &mut config, "implementsInterfaces.singleLine", + "inherit".to_string(), &mut diagnostics, - ) - .as_deref() - .and_then(|option_value| match option_value { - "prefer" => Some(SingleLine::Prefer), - "smart" => Some(SingleLine::Smart), - "never" => Some(SingleLine::Never), + ) { + "prefer" => SingleLine::Prefer, + "smart" => SingleLine::Smart, + "never" => SingleLine::Never, + "inherit" => SingleLine::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "implementsInterfaces.singleLine".into(), message: "invalid value for config `implementsInterfaces.singleLine`" .into(), }); - None + SingleLine::Inherit } - }), - input_fields_definition_single_line: get_nullable_value::( + }, + input_fields_definition_single_line: match &*get_value( &mut config, "inputFieldsDefinition.singleLine", + "never".to_string(), &mut diagnostics, - ) - .as_deref() - .map(|option_value| match option_value { + ) { "prefer" => SingleLine::Prefer, "smart" => SingleLine::Smart, "never" => SingleLine::Never, + "inherit" => SingleLine::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "inputFieldsDefinition.singleLine".into(), @@ -470,54 +463,53 @@ pub(crate) fn resolve_config( }); SingleLine::Never } - }) - .or(Some(SingleLine::Never)), - list_value_single_line: get_nullable_value::( + }, + list_value_single_line: match &*get_value( &mut config, "listValue.singleLine", + "inherit".to_string(), &mut diagnostics, - ) - .as_deref() - .and_then(|option_value| match option_value { - "prefer" => Some(SingleLine::Prefer), - "smart" => Some(SingleLine::Smart), - "never" => Some(SingleLine::Never), + ) { + "prefer" => SingleLine::Prefer, + "smart" => SingleLine::Smart, + "never" => SingleLine::Never, + "inherit" => SingleLine::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "listValue.singleLine".into(), message: "invalid value for config `listValue.singleLine`".into(), }); - None + SingleLine::Inherit } - }), - object_value_single_line: get_nullable_value::( + }, + object_value_single_line: match &*get_value( &mut config, "objectValue.singleLine", + "inherit".to_string(), &mut diagnostics, - ) - .as_deref() - .and_then(|option_value| match option_value { - "prefer" => Some(SingleLine::Prefer), - "smart" => Some(SingleLine::Smart), - "never" => Some(SingleLine::Never), + ) { + "prefer" => SingleLine::Prefer, + "smart" => SingleLine::Smart, + "never" => SingleLine::Never, + "inherit" => SingleLine::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "objectValue.singleLine".into(), message: "invalid value for config `objectValue.singleLine`".into(), }); - None + SingleLine::Inherit } - }), - schema_definition_single_line: get_nullable_value::( + }, + schema_definition_single_line: match &*get_value( &mut config, "schemaDefinition.singleLine", + "never".to_string(), &mut diagnostics, - ) - .as_deref() - .map(|option_value| match option_value { + ) { "prefer" => SingleLine::Prefer, "smart" => SingleLine::Smart, "never" => SingleLine::Never, + "inherit" => SingleLine::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "schemaDefinition.singleLine".into(), @@ -525,18 +517,17 @@ pub(crate) fn resolve_config( }); SingleLine::Never } - }) - .or(Some(SingleLine::Never)), - schema_extension_single_line: get_nullable_value::( + }, + schema_extension_single_line: match &*get_value( &mut config, "schemaExtension.singleLine", + "never".to_string(), &mut diagnostics, - ) - .as_deref() - .map(|option_value| match option_value { + ) { "prefer" => SingleLine::Prefer, "smart" => SingleLine::Smart, "never" => SingleLine::Never, + "inherit" => SingleLine::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "schemaExtension.singleLine".into(), @@ -544,18 +535,17 @@ pub(crate) fn resolve_config( }); SingleLine::Never } - }) - .or(Some(SingleLine::Never)), - selection_set_single_line: get_nullable_value::( + }, + selection_set_single_line: match &*get_value( &mut config, "selectionSet.singleLine", + "never".to_string(), &mut diagnostics, - ) - .as_deref() - .map(|option_value| match option_value { + ) { "prefer" => SingleLine::Prefer, "smart" => SingleLine::Smart, "never" => SingleLine::Never, + "inherit" => SingleLine::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "selectionSet.singleLine".into(), @@ -563,44 +553,43 @@ pub(crate) fn resolve_config( }); SingleLine::Never } - }) - .or(Some(SingleLine::Never)), - union_member_types_single_line: get_nullable_value::( + }, + union_member_types_single_line: match &*get_value( &mut config, "unionMemberTypes.singleLine", + "inherit".to_string(), &mut diagnostics, - ) - .as_deref() - .and_then(|option_value| match option_value { - "prefer" => Some(SingleLine::Prefer), - "smart" => Some(SingleLine::Smart), - "never" => Some(SingleLine::Never), + ) { + "prefer" => SingleLine::Prefer, + "smart" => SingleLine::Smart, + "never" => SingleLine::Never, + "inherit" => SingleLine::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "unionMemberTypes.singleLine".into(), message: "invalid value for config `unionMemberTypes.singleLine`".into(), }); - None + SingleLine::Inherit } - }), - variable_definitions_single_line: get_nullable_value::( + }, + variable_definitions_single_line: match &*get_value( &mut config, "variableDefinitions.singleLine", + "inherit".to_string(), &mut diagnostics, - ) - .as_deref() - .and_then(|option_value| match option_value { - "prefer" => Some(SingleLine::Prefer), - "smart" => Some(SingleLine::Smart), - "never" => Some(SingleLine::Never), + ) { + "prefer" => SingleLine::Prefer, + "smart" => SingleLine::Smart, + "never" => SingleLine::Never, + "inherit" => SingleLine::Inherit, _ => { diagnostics.push(ConfigurationDiagnostic { property_name: "variableDefinitions.singleLine".into(), message: "invalid value for config `variableDefinitions.singleLine`".into(), }); - None + SingleLine::Inherit } - }), + }, paren_spacing: get_value(&mut config, "parenSpacing", false, &mut diagnostics), arguments_paren_spacing: get_nullable_value( &mut config, diff --git a/pretty_graphql/src/config.rs b/pretty_graphql/src/config.rs index 5971a5b..491638a 100644 --- a/pretty_graphql/src/config.rs +++ b/pretty_graphql/src/config.rs @@ -74,7 +74,7 @@ impl From for tiny_pretty::LineBreak { pub struct LanguageOptions { pub comma: Comma, #[cfg_attr(feature = "config_serde", serde(alias = "arguments.comma"))] - pub arguments_comma: Option, + pub arguments_comma: Comma, #[cfg_attr( feature = "config_serde", serde( @@ -82,9 +82,9 @@ pub struct LanguageOptions { alias = "argumentsDefinition.comma" ) )] - pub arguments_definition_comma: Option, + pub arguments_definition_comma: Comma, #[cfg_attr(feature = "config_serde", serde(alias = "directives.comma"))] - pub directives_comma: Option, + pub directives_comma: Comma, #[cfg_attr( feature = "config_serde", serde( @@ -92,12 +92,12 @@ pub struct LanguageOptions { alias = "enumValuesDefinition.comma" ) )] - pub enum_values_definition_comma: Option, + pub enum_values_definition_comma: Comma, #[cfg_attr( feature = "config_serde", serde(rename = "fields_definition.comma", alias = "fieldsDefinition.comma") )] - pub fields_definition_comma: Option, + pub fields_definition_comma: Comma, #[cfg_attr( feature = "config_serde", serde( @@ -105,32 +105,32 @@ pub struct LanguageOptions { alias = "inputFieldsDefinition.comma" ) )] - pub input_fields_definition_comma: Option, + pub input_fields_definition_comma: Comma, #[cfg_attr( feature = "config_serde", serde(rename = "list_value.comma", alias = "listValue.comma") )] - pub list_value_comma: Option, + pub list_value_comma: Comma, #[cfg_attr( feature = "config_serde", serde(rename = "object_value.comma", alias = "objectValue.comma") )] - pub object_value_comma: Option, + pub object_value_comma: Comma, #[cfg_attr( feature = "config_serde", serde(rename = "schema_definition.comma", alias = "schemaDefinition.comma") )] - pub schema_definition_comma: Option, + pub schema_definition_comma: Comma, #[cfg_attr( feature = "config_serde", serde(rename = "schema_extension.comma", alias = "schemaExtension.comma") )] - pub schema_extension_comma: Option, + pub schema_extension_comma: Comma, #[cfg_attr( feature = "config_serde", serde(rename = "selection_set.comma", alias = "selectionSet.comma") )] - pub selection_set_comma: Option, + pub selection_set_comma: Comma, #[cfg_attr( feature = "config_serde", serde( @@ -138,7 +138,7 @@ pub struct LanguageOptions { alias = "variableDefinitions.comma" ) )] - pub variable_definitions_comma: Option, + pub variable_definitions_comma: Comma, #[cfg_attr(feature = "config_serde", serde(alias = "singleLine"))] pub single_line: SingleLine, @@ -146,7 +146,7 @@ pub struct LanguageOptions { feature = "config_serde", serde(rename = "arguments.single_line", alias = "arguments.singleLine") )] - pub arguments_single_line: Option, + pub arguments_single_line: SingleLine, #[cfg_attr( feature = "config_serde", serde( @@ -154,7 +154,7 @@ pub struct LanguageOptions { alias = "argumentsDefinition.singleLine" ) )] - pub arguments_definition_single_line: Option, + pub arguments_definition_single_line: SingleLine, #[cfg_attr( feature = "config_serde", serde( @@ -162,7 +162,7 @@ pub struct LanguageOptions { alias = "enumValuesDefinition.singleLine" ) )] - pub enum_values_definition_single_line: Option, + pub enum_values_definition_single_line: SingleLine, #[cfg_attr( feature = "config_serde", serde( @@ -170,12 +170,12 @@ pub struct LanguageOptions { alias = "directiveLocations.singleLine" ) )] - pub directive_locations_single_line: Option, + pub directive_locations_single_line: SingleLine, #[cfg_attr( feature = "config_serde", serde(rename = "directives.single_line", alias = "directives.singleLine") )] - pub directives_single_line: Option, + pub directives_single_line: SingleLine, #[cfg_attr( feature = "config_serde", serde( @@ -183,7 +183,7 @@ pub struct LanguageOptions { alias = "fieldsDefinition.singleLine" ) )] - pub fields_definition_single_line: Option, + pub fields_definition_single_line: SingleLine, #[cfg_attr( feature = "config_serde", serde( @@ -191,7 +191,7 @@ pub struct LanguageOptions { alias = "implementsInterfaces.singleLine" ) )] - pub implements_interfaces_single_line: Option, + pub implements_interfaces_single_line: SingleLine, #[cfg_attr( feature = "config_serde", serde( @@ -199,17 +199,17 @@ pub struct LanguageOptions { alias = "inputFieldsDefinition.singleLine" ) )] - pub input_fields_definition_single_line: Option, + pub input_fields_definition_single_line: SingleLine, #[cfg_attr( feature = "config_serde", serde(rename = "list_value.single_line", alias = "listValue.singleLine") )] - pub list_value_single_line: Option, + pub list_value_single_line: SingleLine, #[cfg_attr( feature = "config_serde", serde(rename = "object_value.single_line", alias = "objectValue.singleLine") )] - pub object_value_single_line: Option, + pub object_value_single_line: SingleLine, #[cfg_attr( feature = "config_serde", serde( @@ -217,7 +217,7 @@ pub struct LanguageOptions { alias = "schemaDefinition.singleLine" ) )] - pub schema_definition_single_line: Option, + pub schema_definition_single_line: SingleLine, #[cfg_attr( feature = "config_serde", serde( @@ -225,7 +225,7 @@ pub struct LanguageOptions { alias = "schemaExtension.singleLine" ) )] - pub schema_extension_single_line: Option, + pub schema_extension_single_line: SingleLine, #[cfg_attr( feature = "config_serde", serde( @@ -233,7 +233,7 @@ pub struct LanguageOptions { alias = "selectionSet.singleLine" ) )] - pub selection_set_single_line: Option, + pub selection_set_single_line: SingleLine, #[cfg_attr( feature = "config_serde", serde( @@ -241,7 +241,7 @@ pub struct LanguageOptions { alias = "unionMemberTypes.singleLine" ) )] - pub union_member_types_single_line: Option, + pub union_member_types_single_line: SingleLine, #[cfg_attr( feature = "config_serde", serde( @@ -249,7 +249,7 @@ pub struct LanguageOptions { alias = "variableDefinitions.singleLine" ) )] - pub variable_definitions_single_line: Option, + pub variable_definitions_single_line: SingleLine, #[cfg_attr(feature = "config_serde", serde(alias = "parenSpacing"))] pub paren_spacing: bool, @@ -348,34 +348,34 @@ impl Default for LanguageOptions { fn default() -> Self { LanguageOptions { comma: Comma::OnlySingleLine, - arguments_comma: None, - arguments_definition_comma: None, - directives_comma: Some(Comma::Never), - enum_values_definition_comma: Some(Comma::Never), - fields_definition_comma: Some(Comma::Never), - input_fields_definition_comma: Some(Comma::Never), - list_value_comma: None, - object_value_comma: None, - schema_definition_comma: Some(Comma::Never), - schema_extension_comma: Some(Comma::Never), - selection_set_comma: Some(Comma::Never), - variable_definitions_comma: None, + arguments_comma: Comma::Inherit, + arguments_definition_comma: Comma::Inherit, + directives_comma: Comma::Never, + enum_values_definition_comma: Comma::Never, + fields_definition_comma: Comma::Never, + input_fields_definition_comma: Comma::Never, + list_value_comma: Comma::Inherit, + object_value_comma: Comma::Inherit, + schema_definition_comma: Comma::Never, + schema_extension_comma: Comma::Never, + selection_set_comma: Comma::Never, + variable_definitions_comma: Comma::Inherit, single_line: SingleLine::Smart, - arguments_single_line: None, - arguments_definition_single_line: None, - directive_locations_single_line: None, - directives_single_line: None, - enum_values_definition_single_line: Some(SingleLine::Never), - fields_definition_single_line: Some(SingleLine::Never), - implements_interfaces_single_line: None, - input_fields_definition_single_line: Some(SingleLine::Never), - list_value_single_line: None, - object_value_single_line: None, - schema_definition_single_line: Some(SingleLine::Never), - schema_extension_single_line: Some(SingleLine::Never), - selection_set_single_line: Some(SingleLine::Never), - union_member_types_single_line: None, - variable_definitions_single_line: None, + arguments_single_line: SingleLine::Inherit, + arguments_definition_single_line: SingleLine::Inherit, + directive_locations_single_line: SingleLine::Inherit, + directives_single_line: SingleLine::Inherit, + enum_values_definition_single_line: SingleLine::Never, + fields_definition_single_line: SingleLine::Never, + implements_interfaces_single_line: SingleLine::Inherit, + input_fields_definition_single_line: SingleLine::Never, + list_value_single_line: SingleLine::Inherit, + object_value_single_line: SingleLine::Inherit, + schema_definition_single_line: SingleLine::Never, + schema_extension_single_line: SingleLine::Never, + selection_set_single_line: SingleLine::Never, + union_member_types_single_line: SingleLine::Inherit, + variable_definitions_single_line: SingleLine::Inherit, paren_spacing: false, arguments_paren_spacing: None, arguments_definition_paren_spacing: None, @@ -405,6 +405,7 @@ pub enum Comma { NoTrailing, #[cfg_attr(feature = "config_serde", serde(alias = "onlySingleLine"))] OnlySingleLine, + Inherit, } #[derive(Clone, Debug)] @@ -414,4 +415,5 @@ pub enum SingleLine { Prefer, Smart, Never, + Inherit, } diff --git a/pretty_graphql/src/printer.rs b/pretty_graphql/src/printer.rs index afc1b16..f2e7b88 100644 --- a/pretty_graphql/src/printer.rs +++ b/pretty_graphql/src/printer.rs @@ -61,12 +61,12 @@ impl DocGen for Arguments { ctx.options.arguments_paren_spacing, ctx, ) - .with_single_line(ctx.options.arguments_single_line.as_ref()) + .with_single_line(&ctx.options.arguments_single_line) .format(format_optional_comma_separated_list( self, self.arguments(), - ctx.options.arguments_single_line.as_ref(), - ctx.options.arguments_comma.as_ref(), + &ctx.options.arguments_single_line, + &ctx.options.arguments_comma, ctx, )) } @@ -84,12 +84,12 @@ impl DocGen for ArgumentsDefinition { ctx.options.arguments_definition_paren_spacing, ctx, ) - .with_single_line(ctx.options.arguments_definition_single_line.as_ref()) + .with_single_line(&ctx.options.arguments_definition_single_line) .format(format_optional_comma_separated_list( self, self.input_value_definitions(), - ctx.options.arguments_definition_single_line.as_ref(), - ctx.options.arguments_definition_comma.as_ref(), + &ctx.options.arguments_definition_single_line, + &ctx.options.arguments_definition_comma, ctx, )) .group() @@ -257,7 +257,7 @@ impl DocGen for DirectiveLocations { self.directive_locations(), S![|], "|", - ctx.options.directive_locations_single_line.as_ref(), + &ctx.options.directive_locations_single_line, ctx, ) .group() @@ -269,8 +269,8 @@ impl DocGen for Directives { format_optional_comma_separated_list( self, self.directives(), - ctx.options.directives_single_line.as_ref(), - ctx.options.directives_comma.as_ref(), + &ctx.options.directives_single_line, + &ctx.options.directives_comma, ctx, ) } @@ -475,12 +475,12 @@ impl DocGen for EnumValuesDefinition { ctx.options.enum_values_definition_brace_spacing, ctx, ) - .with_single_line(ctx.options.enum_values_definition_single_line.as_ref()) + .with_single_line(&ctx.options.enum_values_definition_single_line) .format(format_optional_comma_separated_list( self, self.enum_value_definitions(), - ctx.options.enum_values_definition_single_line.as_ref(), - ctx.options.enum_values_definition_comma.as_ref(), + &ctx.options.enum_values_definition_single_line, + &ctx.options.enum_values_definition_comma, ctx, )) } @@ -597,12 +597,12 @@ impl DocGen for FieldsDefinition { ctx.options.fields_definition_brace_spacing, ctx, ) - .with_single_line(ctx.options.fields_definition_single_line.as_ref()) + .with_single_line(&ctx.options.fields_definition_single_line) .format(format_optional_comma_separated_list( self, self.field_definitions(), - ctx.options.fields_definition_single_line.as_ref(), - ctx.options.fields_definition_comma.as_ref(), + &ctx.options.fields_definition_single_line, + &ctx.options.fields_definition_comma, ctx, )) } @@ -708,7 +708,7 @@ impl DocGen for ImplementsInterfaces { self.named_types(), S![&], "&", - ctx.options.implements_interfaces_single_line.as_ref(), + &ctx.options.implements_interfaces_single_line, ctx, ); if trivias.is_empty() { @@ -777,12 +777,12 @@ impl DocGen for InputFieldsDefinition { ctx.options.input_fields_definition_brace_spacing, ctx, ) - .with_single_line(ctx.options.input_fields_definition_single_line.as_ref()) + .with_single_line(&ctx.options.input_fields_definition_single_line) .format(format_optional_comma_separated_list( self, self.input_value_definitions(), - ctx.options.input_fields_definition_single_line.as_ref(), - ctx.options.input_fields_definition_comma.as_ref(), + &ctx.options.input_fields_definition_single_line, + &ctx.options.input_fields_definition_comma, ctx, )) } @@ -1048,7 +1048,7 @@ impl DocGen for IntValue { impl DocGen for ListType { fn doc(&self, ctx: &Ctx) -> Doc<'static> { DelimitersFormatter::bracket(self.l_brack_token(), self.r_brack_token(), Some(true), ctx) - .with_single_line(Some(&SingleLine::Prefer)) + .with_single_line(&SingleLine::Prefer) .with_space(Doc::nil()) .format(self.ty().map(|ty| ty.doc(ctx)).unwrap_or_else(Doc::nil)) } @@ -1065,12 +1065,12 @@ impl DocGen for ListValue { Some(ctx.options.bracket_spacing), ctx, ) - .with_single_line(ctx.options.list_value_single_line.as_ref()) + .with_single_line(&ctx.options.list_value_single_line) .format(format_optional_comma_separated_list( self, self.values(), - ctx.options.list_value_single_line.as_ref(), - ctx.options.list_value_comma.as_ref(), + &ctx.options.list_value_single_line, + &ctx.options.list_value_comma, ctx, )) } @@ -1268,12 +1268,12 @@ impl DocGen for ObjectValue { ctx.options.object_value_brace_spacing, ctx, ) - .with_single_line(ctx.options.object_value_single_line.as_ref()) + .with_single_line(&ctx.options.object_value_single_line) .format(format_optional_comma_separated_list( self, self.object_fields(), - ctx.options.object_value_single_line.as_ref(), - ctx.options.object_value_comma.as_ref(), + &ctx.options.object_value_single_line, + &ctx.options.object_value_comma, ctx, )) } @@ -1487,12 +1487,12 @@ impl DocGen for SchemaDefinition { ctx.options.schema_definition_brace_spacing, ctx, ) - .with_single_line(ctx.options.schema_definition_single_line.as_ref()) + .with_single_line(&ctx.options.schema_definition_single_line) .format(format_optional_comma_separated_list( self, self.root_operation_type_definitions(), - ctx.options.schema_definition_single_line.as_ref(), - ctx.options.schema_definition_comma.as_ref(), + &ctx.options.schema_definition_single_line, + &ctx.options.schema_definition_comma, ctx, )) }); @@ -1545,12 +1545,12 @@ impl DocGen for SchemaExtension { ctx.options.schema_extension_brace_spacing, ctx, ) - .with_single_line(ctx.options.schema_extension_single_line.as_ref()) + .with_single_line(&ctx.options.schema_extension_single_line) .format(format_optional_comma_separated_list( self, self.root_operation_type_definitions(), - ctx.options.schema_extension_single_line.as_ref(), - ctx.options.schema_extension_comma.as_ref(), + &ctx.options.schema_extension_single_line, + &ctx.options.schema_extension_comma, ctx, )) }); @@ -1578,12 +1578,12 @@ impl DocGen for SelectionSet { ctx.options.selection_set_brace_spacing, ctx, ) - .with_single_line(ctx.options.selection_set_single_line.as_ref()) + .with_single_line(&ctx.options.selection_set_single_line) .format(format_optional_comma_separated_list( self, self.selections(), - ctx.options.selection_set_single_line.as_ref(), - ctx.options.selection_set_comma.as_ref(), + &ctx.options.selection_set_single_line, + &ctx.options.selection_set_comma, ctx, )) } @@ -1648,7 +1648,7 @@ impl DocGen for UnionMemberTypes { self.named_types(), S![|], "|", - ctx.options.union_member_types_single_line.as_ref(), + &ctx.options.union_member_types_single_line, ctx, ); if trivias.is_empty() { @@ -1832,12 +1832,12 @@ impl DocGen for VariableDefinitions { ctx.options.variable_definitions_paren_spacing, ctx, ) - .with_single_line(ctx.options.variable_definitions_single_line.as_ref()) + .with_single_line(&ctx.options.variable_definitions_single_line) .format(format_optional_comma_separated_list( self, self.variable_definitions(), - ctx.options.variable_definitions_single_line.as_ref(), - ctx.options.variable_definitions_comma.as_ref(), + &ctx.options.variable_definitions_single_line, + &ctx.options.variable_definitions_comma, ctx, )) } @@ -1847,8 +1847,8 @@ impl DocGen for VariableDefinitions { fn format_optional_comma_separated_list( node: &N, entries: CstChildren, - single_line: Option<&SingleLine>, - comma: Option<&Comma>, + single_line: &SingleLine, + comma: &Comma, ctx: &Ctx, ) -> Doc<'static> where @@ -1858,9 +1858,14 @@ where let node = node.syntax(); let mut docs = vec![]; let mut entries = entries.peekable(); - let separator_space = match single_line.unwrap_or(&ctx.options.single_line) { + let single_line = if let SingleLine::Inherit = single_line { + &ctx.options.single_line + } else { + single_line + }; + let separator_space = match single_line { SingleLine::Prefer => Doc::line_or_space(), - SingleLine::Smart => { + SingleLine::Smart | SingleLine::Inherit => { if let Some(token) = node.first_token() { if token .siblings_with_tokens(Direction::Next) @@ -1899,7 +1904,11 @@ where SyntaxElement::Token(token) if token.kind() == S![,] => Some(token), _ => None, }); - let comma = comma.unwrap_or(&ctx.options.comma); + let comma = if let Comma::Inherit = comma { + &ctx.options.comma + } else { + comma + }; while let Some(entry) = entries.next() { let entry_node = entry.syntax(); if should_ignore(entry_node, ctx) { @@ -1921,7 +1930,7 @@ where docs.push(Doc::text(",")); } } - Comma::OnlySingleLine => { + Comma::OnlySingleLine | Comma::Inherit => { if entries.peek().is_some() { docs.push(Doc::flat_or_break(Doc::text(","), Doc::nil())); } @@ -2022,7 +2031,7 @@ fn format_union_like( entries: CstChildren, sep_token_kind: SyntaxKind, sep_text: &'static str, - single_line: Option<&SingleLine>, + single_line: &SingleLine, ctx: &Ctx, ) -> Doc<'static> where @@ -2082,9 +2091,14 @@ where }); } - let space = match single_line.unwrap_or(&ctx.options.single_line) { + let single_line = if let SingleLine::Inherit = single_line { + &ctx.options.single_line + } else { + single_line + }; + let space = match single_line { SingleLine::Prefer => Doc::line_or_space(), - SingleLine::Smart => { + SingleLine::Smart | SingleLine::Inherit => { if has_line_break_after_first { Doc::hard_line() } else { @@ -2127,7 +2141,7 @@ struct DelimitersFormatter<'a> { space: Doc<'static>, open_token: Option, close_token: Option, - single_line: Option<&'a SingleLine>, + single_line: &'a SingleLine, ctx: &'a Ctx<'a>, } impl<'a> DelimitersFormatter<'a> { @@ -2147,7 +2161,7 @@ impl<'a> DelimitersFormatter<'a> { }, open_token: open, close_token: close, - single_line: None, + single_line: &SingleLine::Inherit, ctx, } } @@ -2167,7 +2181,7 @@ impl<'a> DelimitersFormatter<'a> { }, open_token: open, close_token: close, - single_line: None, + single_line: &SingleLine::Inherit, ctx, } } @@ -2187,7 +2201,7 @@ impl<'a> DelimitersFormatter<'a> { }, open_token: open, close_token: close, - single_line: None, + single_line: &SingleLine::Inherit, ctx, } } @@ -2195,7 +2209,7 @@ impl<'a> DelimitersFormatter<'a> { self.space = space; self } - fn with_single_line(mut self, single_line: Option<&'a SingleLine>) -> Self { + fn with_single_line(mut self, single_line: &'a SingleLine) -> Self { self.single_line = single_line; self } @@ -2206,13 +2220,18 @@ impl<'a> DelimitersFormatter<'a> { docs.push(Doc::text(self.open_text)); if let Some(open) = self.open_token { + let single_line = if let SingleLine::Inherit = &self.single_line { + &ctx.options.single_line + } else { + self.single_line + }; if let Some(token) = open .next_token() .filter(|token| token.kind() == SyntaxKind::WHITESPACE) { - match self.single_line.unwrap_or(&ctx.options.single_line) { + match single_line { SingleLine::Prefer => docs.push(self.space.clone()), - SingleLine::Smart => { + SingleLine::Smart | SingleLine::Inherit => { if token.text().contains(['\n', '\r']) { docs.push(Doc::hard_line()); } else { @@ -2224,8 +2243,10 @@ impl<'a> DelimitersFormatter<'a> { let mut trivia_docs = format_trivias_after_token(&token, ctx); docs.append(&mut trivia_docs); } else { - match self.single_line.unwrap_or(&ctx.options.single_line) { - SingleLine::Prefer | SingleLine::Smart => docs.push(self.space.clone()), + match single_line { + SingleLine::Prefer | SingleLine::Smart | SingleLine::Inherit => { + docs.push(self.space.clone()) + } SingleLine::Never => docs.push(Doc::hard_line()), } let mut trivia_docs = format_trivias_after_token(&open, ctx);