Skip to content

Commit

Permalink
feat: get ready for formatting style attr in HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Aug 8, 2024
1 parent d3223cc commit bbeeda2
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/src/config/prefer-single-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This global option can be overridden by different syntax nodes:
- `lessImportOptions.preferSingleLine`
- `lessMixinArgs.preferSingleLine`
- `lessMixinParams.preferSingleLine`
- `topLevelDeclarations.preferSingleLine` (Only for CSS)

Given the following example CSS:

Expand Down
5 changes: 5 additions & 0 deletions dprint_plugin/deployment/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@
"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
},
"selectorOverrideCommentDirective": {
"description": "Text directive for overriding selector formatting.",
"type": "string",
Expand Down
5 changes: 5 additions & 0 deletions dprint_plugin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ pub(crate) fn resolve_config(
"lessMixinParamsPreferSingleLine",
&mut diagnostics,
),
top_level_declarations_prefer_single_line: get_nullable_value(
&mut config,
"topLevelDeclarationsPreferSingleLine",
&mut diagnostics,
),
selector_override_comment_directive: get_value(
&mut config,
"selectorOverrideCommentDirective",
Expand Down
10 changes: 10 additions & 0 deletions malva/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ 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"
)
)]
/// See [`preferSingleLine`](https://malva.netlify.app/config/prefer-single-line.html)
pub top_level_declarations_prefer_single_line: Option<bool>,

#[cfg_attr(
feature = "config_serde",
Expand Down Expand Up @@ -275,6 +284,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,
selector_override_comment_directive: "malva-selector-override".into(),
ignore_comment_directive: "malva-ignore".into(),
}
Expand Down
36 changes: 28 additions & 8 deletions malva/src/doc_gen/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,34 @@ impl<'s> DocGen<'s> for Statement<'s> {
impl<'s> DocGen<'s> for Stylesheet<'s> {
fn doc(&self, ctx: &Ctx<'_, 's>, state: &State) -> Doc<'s> {
let mut stmt_docs = vec![];
format_statements(
&mut stmt_docs,
&self.statements,
&self.span,
Doc::hard_line(),
ctx,
state,
);
if ctx.syntax == Syntax::Css
&& matches!(
ctx.options.top_level_declarations_prefer_single_line,
Some(true)
)
&& self.statements.iter().all(|stmt| stmt.is_declaration())
{
// Declarations can't be at the top level in CSS,
// but parser allows them and treat them as recoverable errors.
// This situation can happen when formatting declarations
// inside `style` attribute in HTML.
// All comments are ignored.
stmt_docs = itertools::intersperse(
self.statements.iter().map(|stmt| stmt.doc(ctx, state)),
Doc::text("; "),
)
.collect();
} else {
format_statements(
&mut stmt_docs,
&self.statements,
&self.span,
Doc::hard_line(),
ctx,
state,
);
}

if ctx.syntax != Syntax::Sass {
stmt_docs.push(Doc::empty_line());
}
Expand Down
2 changes: 2 additions & 0 deletions malva/tests/fmt/css/top-level-declarations/default.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
width : 1PX;
height : 1PX;
5 changes: 5 additions & 0 deletions malva/tests/fmt/css/top-level-declarations/default.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: malva/tests/fmt.rs
---
width: 1px;
height: 1px;
3 changes: 3 additions & 0 deletions malva/tests/fmt/css/top-level-declarations/single-line.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*cfg "topLevelDeclarations.preferSingleLine" = true */
width : 1PX;
height : 1PX;
4 changes: 4 additions & 0 deletions malva/tests/fmt/css/top-level-declarations/single-line.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
source: malva/tests/fmt.rs
---
width: 1px; height: 1px

0 comments on commit bbeeda2

Please sign in to comment.