Skip to content

Commit

Permalink
feat: rename to singleLineTopLevelDeclarations
Browse files Browse the repository at this point in the history
from `topLevelDeclarations.preferSingleLine`
  • Loading branch information
g-plane committed Aug 12, 2024
1 parent 4e77747 commit 0cb1809
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 17 deletions.
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
- [keyframeSelectorNotation](./config/keyframe-selector-notation.md)
- [attrValueQuotes](./config/attr-value-quotes.md)
- [preferSingleLine](./config/prefer-single-line.md)
- [singleLineTopLevelDeclarations](./config/single-line-top-level-declarations.md)
- [selectorOverrideCommentDirective](./config/selector-override-comment-directive.md)
- [ignoreCommentDirective](./config/ignore-comment-directive.md)
27 changes: 27 additions & 0 deletions docs/src/config/single-line-top-level-declarations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# `singleLineTopLevelDeclarations`

Control whether to force to format all top-level declarations on a single line.

When this option is `true`, trailing semicolons are removed.

Most of the time, you don't need to set this option, because declarations at top level are invalid,
but if you're formatting HTML's `style` attribute, you may want to set this to `true`.

Default option is `false`.

## Example for `false`

[Playground](https://malva-play.vercel.app/?code=H4sIAAAAAAAAAyvPTCnJsFIwNDAoqLDmykjNTM8ogXEBfacJkBwAAAA%3D&config=H4sIAAAAAAAAA6vmUlBQKs7MS89J9cnMSw3JL%2FBJLUvNcUlNzkksSizJzM8rVrJSSEvMKU7lqgUASka%2FoS0AAAA%3D&syntax=css)

```css
width: 100px;
height: 100px;
```

## Example for `true`

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

```css
width: 100px; height: 100px
```
8 changes: 4 additions & 4 deletions dprint_plugin/deployment/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,10 @@
"type": ["boolean", "null"],
"default": null
},
"topLevelDeclarations.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
"singleLineTopLevelDeclarations": {
"description": "Control whether to force to format all top-level declarations on a single line.",
"type": "boolean",
"default": false
},
"selectorOverrideCommentDirective": {
"description": "Text directive for overriding selector formatting.",
Expand Down
5 changes: 3 additions & 2 deletions dprint_plugin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,10 @@ pub(crate) fn resolve_config(
"lessMixinParamsPreferSingleLine",
&mut diagnostics,
),
top_level_declarations_prefer_single_line: get_nullable_value(
single_line_top_level_declarations: get_value(
&mut config,
"topLevelDeclarationsPreferSingleLine",
"singleLineTopLevelDeclarations",
false,
&mut diagnostics,
),
selector_override_comment_directive: get_value(
Expand Down
12 changes: 5 additions & 7 deletions malva/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,13 @@ pub struct LanguageOptions {
)]
/// See [`preferSingleLine`](https://malva.netlify.app/config/prefer-single-line.html)
pub less_mixin_params_prefer_single_line: Option<bool>,

#[cfg_attr(
feature = "config_serde",
serde(
rename = "top_level_declarations.prefer_single_line",
alias = "topLevelDeclarations.preferSingleLine"
)
serde(alias = "singleLineTopLevelDeclarations")
)]
/// See [`preferSingleLine`](https://malva.netlify.app/config/prefer-single-line.html)
pub top_level_declarations_prefer_single_line: Option<bool>,
/// See [`singleLineTopLevelDeclarations`](https://malva.netlify.app/config/single-line-top-level-declarations.html)
pub single_line_top_level_declarations: bool,

#[cfg_attr(
feature = "config_serde",
Expand Down Expand Up @@ -284,7 +282,7 @@ impl Default for LanguageOptions {
less_import_options_prefer_single_line: None,
less_mixin_args_prefer_single_line: None,
less_mixin_params_prefer_single_line: None,
top_level_declarations_prefer_single_line: None,
single_line_top_level_declarations: false,
selector_override_comment_directive: "malva-selector-override".into(),
ignore_comment_directive: "malva-ignore".into(),
}
Expand Down
5 changes: 1 addition & 4 deletions malva/src/doc_gen/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,7 @@ impl<'s> DocGen<'s> for Stylesheet<'s> {
fn doc(&self, ctx: &Ctx<'_, 's>, state: &State) -> Doc<'s> {
let mut stmt_docs = vec![];
if ctx.syntax == Syntax::Css
&& matches!(
ctx.options.top_level_declarations_prefer_single_line,
Some(true)
)
&& ctx.options.single_line_top_level_declarations
&& self.statements.iter().all(|stmt| stmt.is_declaration())
{
// Declarations can't be at the top level in CSS,
Expand Down

0 comments on commit 0cb1809

Please sign in to comment.