Skip to content

Commit

Permalink
feat: add ignoreFileCommentDirective option
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Oct 21, 2024
1 parent 4aa773b commit 1f006e1
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
- [singleLineTopLevelDeclarations](./config/single-line-top-level-declarations.md)
- [selectorOverrideCommentDirective](./config/selector-override-comment-directive.md)
- [ignoreCommentDirective](./config/ignore-comment-directive.md)
- [ignoreFileCommentDirective](./config/ignore-file-comment-directive.md)
17 changes: 17 additions & 0 deletions docs/src/config/ignore-file-comment-directive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# `ignoreFileCommentDirective`

Text directive for ignoring formatting a whole file.

Default is `"malva-ignore-file"`,
but if you're using as a dprint plugin, it will be `"dprint-ignore-file"`.

## Example

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

```css
/* malva-ignore-file */
a,b{
width : 0;
}
```
5 changes: 5 additions & 0 deletions dprint_plugin/deployment/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@
"description": "Text directive for ignoring formatting specific statement.",
"type": "string",
"default": "malva-ignore"
},
"ignoreFileCommentDirective": {
"description": "Text directive for ignoring formatting a whole file.",
"type": "string",
"default": "dprint-ignore-file"
}
}
}
6 changes: 6 additions & 0 deletions dprint_plugin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ pub(crate) fn resolve_config(
"malva-ignore".into(),
&mut diagnostics,
),
ignore_file_comment_directive: get_value(
&mut config,
"ignoreFileCommentDirective",
"dprint-ignore-file".into(),
&mut diagnostics,
),
},
};

Expand Down
5 changes: 5 additions & 0 deletions malva/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ pub struct LanguageOptions {
#[cfg_attr(feature = "config_serde", serde(alias = "ignoreCommentDirective"))]
/// See [`ignoreCommentDirective`](https://malva.netlify.app/config/ignore-comment-directive.html)
pub ignore_comment_directive: String,

#[cfg_attr(feature = "config_serde", serde(alias = "ignoreFileCommentDirective"))]
/// See [`ignoreFileCommentDirective`](https://malva.netlify.app/config/ignore-file-comment-directive.html)
pub ignore_file_comment_directive: String,
}

impl Default for LanguageOptions {
Expand Down Expand Up @@ -289,6 +293,7 @@ impl Default for LanguageOptions {
single_line_top_level_declarations: false,
selector_override_comment_directive: "malva-selector-override".into(),
ignore_comment_directive: "malva-ignore".into(),
ignore_file_comment_directive: "malva-ignore-file".into(),
}
}
}
Expand Down
29 changes: 21 additions & 8 deletions malva/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,27 @@ pub fn format_text(input: &str, syntax: Syntax, options: &FormatOptions) -> Resu
}
};

Ok(print_stylesheet(
&stylesheet,
&comments,
Some(input),
line_bounds,
syntax,
options,
))
if comments.first().is_some_and(|comment| {
comment.span.start == 0
&& comment
.content
.trim_start()
.strip_prefix(&options.language.ignore_file_comment_directive)
.is_some_and(|rest| {
rest.is_empty() || rest.starts_with(|c: char| c.is_ascii_whitespace())
})
}) {
Ok(input.to_owned())
} else {
Ok(print_stylesheet(
&stylesheet,
&comments,
Some(input),
line_bounds,
syntax,
options,
))
}
}

/// Print the given stylesheet AST.
Expand Down
3 changes: 3 additions & 0 deletions malva/tests/fmt/css/ignore/ignore-file.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* malva-ignore-file */
a,b{}
a,b{}
6 changes: 6 additions & 0 deletions malva/tests/fmt/css/ignore/ignore-file.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: malva/tests/fmt.rs
---
/* malva-ignore-file */
a,b{}
a,b{}

0 comments on commit 1f006e1

Please sign in to comment.