Skip to content

Commit

Permalink
feat: add preferSingleLine option
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Aug 4, 2024
1 parent 714f07d commit a07134e
Show file tree
Hide file tree
Showing 51 changed files with 1,103 additions and 60 deletions.
54 changes: 54 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,60 @@ Default option is `"always"`.
[key=value] {}
```

## `preferSingleLine`

Control whether items should be placed on single line as possible, even they're originally on multiple lines.

Default option is `false`.

This global option can be overridden by different syntax nodes:

- `selectors.preferSingleLine` (`blockSelectorLinebreak` must be `"consistent"`, otherwise this will be ignored)
- `functionArgs.preferSingleLine`
- `sassContentAtRule.preferSingleLine`
- `sassIncludeAtRule.preferSingleLine`
- `sassMap.preferSingleLine`
- `sassModuleConfig.preferSingleLine`
- `sassParams.preferSingleLine`
- `lessImportOptions.preferSingleLine`
- `lessMixinArgs.preferSingleLine`
- `lessMixinParams.preferSingleLine`

Given the following example CSS:

```css
a {
color: rgb(
0,
0, 0
);
}
```

### Example for `false`

[Playground](https://malva-play.vercel.app/?code=H4sIAAAAAAAAA0tUqOZSUEjOz8kvslIoSk%2FSAPIUFAx0oJSCAZChac1VywUAA9K0WikAAAA%3D&config=H4sIAAAAAAAAA6vmUlBQKihKTUstCs7MS89J9cnMS1WyUkhLzClO5aoFAEOBhgUfAAAA&syntax=css)

```css
a {
color: rgb(
0,
0,
0
);
}
```

### Example for `true`

[Playground](https://malva-play.vercel.app/?code=H4sIAAAAAAAAA0tUqOZSUEjOz8kvslIoSk%2FSAPIUFAx0oJSCAZChac1VywUAA9K0WikAAAA%3D&config=H4sIAAAAAAAAA6vmUlBQKihKTUstCs7MS89J9cnMS1WyUigpKk3lqgUAfjpc%2BR4AAAA%3D&syntax=css)

```css
a {
color: rgb(0, 0, 0);
}
```

## `selectorOverrideCommentDirective`

Text directive for overriding selector formatting.
Expand Down
55 changes: 55 additions & 0 deletions dprint_plugin/deployment/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,61 @@
],
"default": "always"
},
"preferSingleLine": {
"description": "Control whether items should be placed on single line as possible, even they're originally on multiple lines.",
"type": "boolean",
"default": false
},
"selectors.preferSingleLine": {
"description": "Control whether items should be placed on single line as possible, even they're originally on multiple lines.",
"type": ["boolean", "null"],
"default": null
},
"functionArgs.preferSingleLine": {
"description": "Control whether items should be placed on single line as possible, even they're originally on multiple lines.",
"type": ["boolean", "null"],
"default": null
},
"sassContentAtRule.preferSingleLine": {
"description": "Control whether items should be placed on single line as possible, even they're originally on multiple lines.",
"type": ["boolean", "null"],
"default": null
},
"sassIncludeAtRule.preferSingleLine": {
"description": "Control whether items should be placed on single line as possible, even they're originally on multiple lines.",
"type": ["boolean", "null"],
"default": null
},
"sassMap.preferSingleLine": {
"description": "Control whether items should be placed on single line as possible, even they're originally on multiple lines.",
"type": ["boolean", "null"],
"default": null
},
"sassModuleConfig.preferSingleLine": {
"description": "Control whether items should be placed on single line as possible, even they're originally on multiple lines.",
"type": ["boolean", "null"],
"default": null
},
"sassParams.preferSingleLine": {
"description": "Control whether items should be placed on single line as possible, even they're originally on multiple lines.",
"type": ["boolean", "null"],
"default": null
},
"lessImportOptions.preferSingleLine": {
"description": "Control whether items should be placed on single line as possible, even they're originally on multiple lines.",
"type": ["boolean", "null"],
"default": null
},
"lessMixinArgs.preferSingleLine": {
"description": "Control whether items should be placed on single line as possible, even they're originally on multiple lines.",
"type": ["boolean", "null"],
"default": null
},
"lessMixinParams.preferSingleLine": {
"description": "Control whether items should be placed on single line as possible, even they're originally on multiple lines.",
"type": ["boolean", "null"],
"default": null
},
"selectorOverrideCommentDirective": {
"description": "Text directive for overriding selector formatting.",
"type": "string",
Expand Down
51 changes: 51 additions & 0 deletions dprint_plugin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,57 @@ pub(crate) fn resolve_config(
Default::default()
}
},
prefer_single_line: get_value(&mut config, "preferSingleLine", false, &mut diagnostics),
selectors_prefer_single_line: get_nullable_value(
&mut config,
"selectorsPreferSingleLine",
&mut diagnostics,
),
function_args_prefer_single_line: get_nullable_value(
&mut config,
"functionArgsPreferSingleLine",
&mut diagnostics,
),
sass_content_at_rule_prefer_single_line: get_nullable_value(
&mut config,
"sassContentAtRulePreferSingleLine",
&mut diagnostics,
),
sass_include_at_rule_prefer_single_line: get_nullable_value(
&mut config,
"sassIncludeAtRulePreferSingleLine",
&mut diagnostics,
),
sass_map_prefer_single_line: get_nullable_value(
&mut config,
"sassMapPreferSingleLine",
&mut diagnostics,
),
sass_module_config_prefer_single_line: get_nullable_value(
&mut config,
"sassModuleConfigPreferSingleLine",
&mut diagnostics,
),
sass_params_prefer_single_line: get_nullable_value(
&mut config,
"sassParamsPreferSingleLine",
&mut diagnostics,
),
less_import_options_prefer_single_line: get_nullable_value(
&mut config,
"lessImportOptionsPreferSingleLine",
&mut diagnostics,
),
less_mixin_args_prefer_single_line: get_nullable_value(
&mut config,
"lessMixinArgsPreferSingleLine",
&mut diagnostics,
),
less_mixin_params_prefer_single_line: get_nullable_value(
&mut config,
"lessMixinParamsPreferSingleLine",
&mut diagnostics,
),
selector_override_comment_directive: get_value(
&mut config,
"selectorOverrideCommentDirective",
Expand Down
105 changes: 105 additions & 0 deletions malva/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,100 @@ pub struct LanguageOptions {
/// See [`attrValueQuotes`](https://github.com/g-plane/malva/blob/main/docs/config.md#attrvaluequotes) on GitHub
pub attr_value_quotes: AttrValueQuotes,

#[cfg_attr(feature = "config_serde", serde(alias = "preferSingleLine"))]
/// See [`preferSingleLine`](https://github.com/g-plane/malva/blob/main/docs/config.md#prefersingleline) on GitHub
pub prefer_single_line: bool,
#[cfg_attr(
feature = "config_serde",
serde(
rename = "selectors.prefer_single_line",
alias = "selectors.preferSingleLine"
)
)]
/// See [`preferSingleLine`](https://github.com/g-plane/malva/blob/main/docs/config.md#prefersingleline) on GitHub
pub selectors_prefer_single_line: Option<bool>,
#[cfg_attr(
feature = "config_serde",
serde(
rename = "function_args.prefer_single_line",
alias = "functionArgs.preferSingleLine"
)
)]
/// See [`preferSingleLine`](https://github.com/g-plane/malva/blob/main/docs/config.md#prefersingleline) on GitHub
pub function_args_prefer_single_line: Option<bool>,
#[cfg_attr(
feature = "config_serde",
serde(
rename = "sass_content_at_rule.prefer_single_line",
alias = "sassContentAtRule.preferSingleLine"
)
)]
/// See [`preferSingleLine`](https://github.com/g-plane/malva/blob/main/docs/config.md#prefersingleline) on GitHub
pub sass_content_at_rule_prefer_single_line: Option<bool>,
#[cfg_attr(
feature = "config_serde",
serde(
rename = "sass_include_at_rule.prefer_single_line",
alias = "sassIncludeAtRule.preferSingleLine"
)
)]
/// See [`preferSingleLine`](https://github.com/g-plane/malva/blob/main/docs/config.md#prefersingleline) on GitHub
pub sass_include_at_rule_prefer_single_line: Option<bool>,
#[cfg_attr(
feature = "config_serde",
serde(
rename = "sass_map.prefer_single_line",
alias = "sassMap.preferSingleLine"
)
)]
/// See [`preferSingleLine`](https://github.com/g-plane/malva/blob/main/docs/config.md#prefersingleline) on GitHub
pub sass_map_prefer_single_line: Option<bool>,
#[cfg_attr(
feature = "config_serde",
serde(
rename = "sass_module_config.prefer_single_line",
alias = "sassModuleConfig.preferSingleLine"
)
)]
/// See [`preferSingleLine`](https://github.com/g-plane/malva/blob/main/docs/config.md#prefersingleline) on GitHub
pub sass_module_config_prefer_single_line: Option<bool>,
#[cfg_attr(
feature = "config_serde",
serde(
rename = "sass_params.prefer_single_line",
alias = "sassParams.preferSingleLine"
)
)]
/// See [`preferSingleLine`](https://github.com/g-plane/malva/blob/main/docs/config.md#prefersingleline) on GitHub
pub sass_params_prefer_single_line: Option<bool>,
#[cfg_attr(
feature = "config_serde",
serde(
rename = "less_import_options.prefer_single_line",
alias = "lessImportOptions.preferSingleLine"
)
)]
/// See [`preferSingleLine`](https://github.com/g-plane/malva/blob/main/docs/config.md#prefersingleline) on GitHub
pub less_import_options_prefer_single_line: Option<bool>,
#[cfg_attr(
feature = "config_serde",
serde(
rename = "less_mixin_args.prefer_single_line",
alias = "lessMixinArgs.preferSingleLine"
)
)]
/// See [`preferSingleLine`](https://github.com/g-plane/malva/blob/main/docs/config.md#prefersingleline) on GitHub
pub less_mixin_args_prefer_single_line: Option<bool>,
#[cfg_attr(
feature = "config_serde",
serde(
rename = "less_mixin_params.prefer_single_line",
alias = "lessMixinParams.preferSingleLine"
)
)]
/// See [`preferSingleLine`](https://github.com/g-plane/malva/blob/main/docs/config.md#prefersingleline) on GitHub
pub less_mixin_params_prefer_single_line: Option<bool>,

#[cfg_attr(
feature = "config_serde",
serde(alias = "selectorOverrideCommentDirective")
Expand Down Expand Up @@ -163,6 +257,17 @@ impl Default for LanguageOptions {
single_line_block_threshold: None,
keyframe_selector_notation: None,
attr_value_quotes: AttrValueQuotes::default(),
prefer_single_line: false,
selectors_prefer_single_line: None,
function_args_prefer_single_line: None,
sass_content_at_rule_prefer_single_line: None,
sass_include_at_rule_prefer_single_line: None,
sass_map_prefer_single_line: None,
sass_module_config_prefer_single_line: None,
sass_params_prefer_single_line: None,
less_import_options_prefer_single_line: None,
less_mixin_args_prefer_single_line: None,
less_mixin_params_prefer_single_line: None,
selector_override_comment_directive: "malva-selector-override".into(),
ignore_comment_directive: "malva-ignore".into(),
}
Expand Down
7 changes: 6 additions & 1 deletion malva/src/doc_gen/at_rule/custom_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ impl<'s> DocGen<'s> for CustomSelectorArgs<'s> {
helpers::format_parenthesized(
helpers::SeparatedListFormatter::new(
",",
helpers::get_smart_linebreak(self.span.start, &self.args, ctx),
helpers::get_smart_linebreak(
self.span.start,
&self.args,
ctx.options.selectors_prefer_single_line,
ctx,
),
)
.format(&self.args, &self.comma_spans, self.span.start, ctx, state),
self.args
Expand Down
27 changes: 25 additions & 2 deletions malva/src/doc_gen/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,25 @@ where
",",
match linebreak {
BlockSelectorLineBreak::Always => Doc::hard_line(),
BlockSelectorLineBreak::Consistent => Doc::line_or_space(),
BlockSelectorLineBreak::Consistent => {
if ctx
.options
.selectors_prefer_single_line
.unwrap_or(ctx.options.prefer_single_line)
|| selectors
.first()
.zip(selectors.get(1))
.is_some_and(|(first, second)| {
ctx.line_bounds
.line_distance(first.span().end, second.span().start)
== 0
})
{
Doc::line_or_space()
} else {
Doc::hard_line()
}
}
BlockSelectorLineBreak::Wrap => Doc::soft_line(),
},
)
Expand Down Expand Up @@ -335,13 +353,18 @@ pub(super) fn ident_to_lowercase<'s>(
pub(super) fn get_smart_linebreak<N>(
start: usize,
elements: &[N],
prefer_single_line: Option<bool>,
ctx: &Ctx<'_, '_>,
) -> Doc<'static>
where
N: Spanned,
{
let prefer_single_line = prefer_single_line.unwrap_or(ctx.options.prefer_single_line);
match elements.first() {
Some(element) if ctx.line_bounds.line_distance(start, element.span().start) > 0 => {
Some(element)
if !prefer_single_line
&& ctx.line_bounds.line_distance(start, element.span().start) > 0 =>
{
Doc::hard_line()
}
_ => Doc::line_or_space(),
Expand Down
Loading

0 comments on commit a07134e

Please sign in to comment.