diff --git a/docs/config.md b/docs/config.md index 0a347fd..82358e4 100644 --- a/docs/config.md +++ b/docs/config.md @@ -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; +} +``` diff --git a/dprint_plugin/deployment/schema.json b/dprint_plugin/deployment/schema.json index 293c44b..74b1149 100644 --- a/dprint_plugin/deployment/schema.json +++ b/dprint_plugin/deployment/schema.json @@ -79,6 +79,10 @@ "type": ["string", "null"], "enum": ["keyword", "percentage"], "default": null + }, + "ignoreCommentDirective": { + "type": "string", + "default": "malva-ignore" } } } diff --git a/dprint_plugin/src/config.rs b/dprint_plugin/src/config.rs index 666fca9..3083018 100644 --- a/dprint_plugin/src/config.rs +++ b/dprint_plugin/src/config.rs @@ -191,6 +191,12 @@ pub(crate) fn resolve_config( None } }), + ignore_comment_directive: get_value( + &mut config, + "ignoreCommentDirective", + "malva-ignore".into(), + &mut diagnostics, + ), }, }; diff --git a/malva/src/config.rs b/malva/src/config.rs index cfa303f..7b96e04 100644 --- a/malva/src/config.rs +++ b/malva/src/config.rs @@ -70,7 +70,7 @@ impl From 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. @@ -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, + + #[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)] diff --git a/malva/src/doc_gen/stmt.rs b/malva/src/doc_gen/stmt.rs index 00ed1bc..8d2c6eb 100644 --- a/malva/src/doc_gen/stmt.rs +++ b/malva/src/doc_gen/stmt.rs @@ -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 { diff --git a/malva/tests/fmt/css/ignore/custom-directive.css b/malva/tests/fmt/css/ignore/custom-directive.css new file mode 100644 index 0000000..0b6bb46 --- /dev/null +++ b/malva/tests/fmt/css/ignore/custom-directive.css @@ -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{} diff --git a/malva/tests/fmt/css/ignore/custom-directive.snap b/malva/tests/fmt/css/ignore/custom-directive.snap new file mode 100644 index 0000000..41b90d0 --- /dev/null +++ b/malva/tests/fmt/css/ignore/custom-directive.snap @@ -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 {} diff --git a/malva/tests/fmt/scss/ignore/custom-directive.scss b/malva/tests/fmt/scss/ignore/custom-directive.scss new file mode 100644 index 0000000..b1c166b --- /dev/null +++ b/malva/tests/fmt/scss/ignore/custom-directive.scss @@ -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{} diff --git a/malva/tests/fmt/scss/ignore/custom-directive.snap b/malva/tests/fmt/scss/ignore/custom-directive.snap new file mode 100644 index 0000000..d68c20b --- /dev/null +++ b/malva/tests/fmt/scss/ignore/custom-directive.snap @@ -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 {}