Skip to content

Commit

Permalink
support customizing ignore comment directive
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed May 17, 2024
1 parent 7ccf35b commit fbba5de
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 2 deletions.
17 changes: 17 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -618,3 +618,20 @@ Default option is `null`.
100% {}
}
```

## `ignoreCommentDirective`

Text directive for ignoring formatting specific statement.

Default is `"malva-ignore"`.

### Example

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

```css
/* malva-ignore */
a,b{
width : 0;
}
```
4 changes: 4 additions & 0 deletions dprint_plugin/deployment/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
"type": ["string", "null"],
"enum": ["keyword", "percentage"],
"default": null
},
"ignoreCommentDirective": {
"type": "string",
"default": "malva-ignore"
}
}
}
6 changes: 6 additions & 0 deletions dprint_plugin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ pub(crate) fn resolve_config(
None
}
}),
ignore_comment_directive: get_value(
&mut config,
"ignoreCommentDirective",
"malva-ignore".into(),
&mut diagnostics,
),
},
};

Expand Down
26 changes: 25 additions & 1 deletion malva/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl From<LineBreak> for tiny_pretty::LineBreak {
}
}

#[derive(Clone, Debug, Default)]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "config_serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "config_serde", serde(default))]
/// Configuration related to syntax.
Expand Down Expand Up @@ -130,6 +130,30 @@ pub struct LanguageOptions {
#[cfg_attr(feature = "config_serde", serde(alias = "keyframeSelectorNotation"))]
/// See [`keyframeSelectorNotation`](https://github.com/g-plane/malva/blob/main/docs/config.md#keyframeselectornotation) on GitHub
pub keyframe_selector_notation: Option<KeyframeSelectorNotation>,

#[cfg_attr(feature = "config_serde", serde(alias = "ignoreCommentDirective"))]
/// See [`ignoreCommentDirective`](https://github.com/g-plane/malva/blob/main/docs/config.md#ignorecommentdirective) on GitHub
pub ignore_comment_directive: String,
}

impl Default for LanguageOptions {
fn default() -> Self {
LanguageOptions {
hex_case: HexCase::default(),
hex_color_length: None,
quotes: Quotes::default(),
operator_linebreak: OperatorLineBreak::default(),
block_selector_linebreak: BlockSelectorLineBreak::default(),
omit_number_leading_zero: false,
trailing_comma: false,
pad_comments: false,
linebreak_in_pseudo_parens: false,
declaration_order: None,
single_line_block_threshold: None,
keyframe_selector_notation: None,
ignore_comment_directive: "malva-ignore".into(),
}
}
}

#[derive(Clone, Debug, Default)]
Expand Down
7 changes: 6 additions & 1 deletion malva/src/doc_gen/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,12 @@ fn format_single_stmt<'s>(
}
if comments
.last()
.and_then(|comment| comment.content.trim_start().strip_prefix("malva-ignore"))
.and_then(|comment| {
comment
.content
.trim_start()
.strip_prefix(&ctx.options.ignore_comment_directive)
})
.is_some_and(|rest| rest.is_empty() || rest.starts_with(|c: char| c.is_ascii_whitespace()))
{
if let Some(source) = ctx.source {
Expand Down
24 changes: 24 additions & 0 deletions malva/tests/fmt/css/ignore/custom-directive.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*cfg ignoreCommentDirective = "dprint-ignore" */

a,b{}

/* dprint-ignore*/
a,b{}

/* dprint-ignore for some reason */
a,b{}

a{
/* dprint-ignore */
width : 0;
height : 0;
}

/* do not dprint-ignore */
a,b{}

/* dprint-ignore-will-not-apply */
a,b{}

/* malva-ignore will not apply */
a,b{}
27 changes: 27 additions & 0 deletions malva/tests/fmt/css/ignore/custom-directive.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
source: malva/tests/fmt.rs
---
/*cfg ignoreCommentDirective = "dprint-ignore" */

a, b {}

/* dprint-ignore*/
a,b{}

/* dprint-ignore for some reason */
a,b{}

a {
/* dprint-ignore */
width : 0;
height: 0;
}

/* do not dprint-ignore */
a, b {}

/* dprint-ignore-will-not-apply */
a, b {}

/* malva-ignore will not apply */
a, b {}
45 changes: 45 additions & 0 deletions malva/tests/fmt/scss/ignore/custom-directive.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*cfg ignoreCommentDirective = "dprint-ignore" */

a,b{}

/* dprint-ignore*/
a,b{}

/* dprint-ignore for some reason */
a,b{}

a{
/* dprint-ignore */
width : 0;
height : 0;
}

/* do not dprint-ignore */
a,b{}

/* dprint-ignore-will-not-apply */
a,b{}

/* malva-ignore will not apply */
a,b{}

// dprint-ignore
a,b{}

// dprint-ignore for some reason
a,b{}

a{
// dprint-ignore
width : 0;
height : 0;
}

// do not dprint-ignore
a,b{}

// dprint-ignore-will-not-apply
a,b{}

// malva-ignore will not apply
a,b{}
48 changes: 48 additions & 0 deletions malva/tests/fmt/scss/ignore/custom-directive.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
source: malva/tests/fmt.rs
---
/*cfg ignoreCommentDirective = "dprint-ignore" */

a, b {}

/* dprint-ignore*/
a,b{}

/* dprint-ignore for some reason */
a,b{}

a {
/* dprint-ignore */
width : 0;
height: 0;
}

/* do not dprint-ignore */
a, b {}

/* dprint-ignore-will-not-apply */
a, b {}

/* malva-ignore will not apply */
a, b {}

// dprint-ignore
a,b{}

// dprint-ignore for some reason
a,b{}

a {
// dprint-ignore
width : 0;
height: 0;
}

// do not dprint-ignore
a, b {}

// dprint-ignore-will-not-apply
a, b {}

// malva-ignore will not apply
a, b {}

0 comments on commit fbba5de

Please sign in to comment.