Skip to content

Commit

Permalink
feat(fmt): add trim_trailing_whitespaces option
Browse files Browse the repository at this point in the history
(close #1)
  • Loading branch information
g-plane committed Jul 7, 2024
1 parent c91f57f commit 0b43695
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 10 deletions.
6 changes: 6 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ outer:
key2: value2
```

## `trimTrailingWhitespaces`

Control whether trailing whitespaces should be trimmed or not.

Default option is `true`.

## `ignoreCommentDirective`

Text directive for ignoring formatting specific content.
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 @@ -82,6 +82,11 @@
}
]
},
"trimTrailingWhitespaces": {
"description": "Control whether trailing whitespaces should be trimmed or not.",
"type": "boolean",
"default": true
},
"ignoreCommentDirective": {
"description": "Text directive for ignoring formatting specific content.",
"type": "string",
Expand Down
6 changes: 6 additions & 0 deletions dprint_plugin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ pub(crate) fn resolve_config(
Default::default()
}
},
trim_trailing_whitespaces: get_value(
&mut config,
"trimTrailingWhitespaces",
true,
&mut diagnostics,
),
ignore_comment_directive: get_value(
&mut config,
"ignoreCommentDirective",
Expand Down
5 changes: 5 additions & 0 deletions pretty_yaml/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ pub struct LanguageOptions {
/// See [`dashSpacing`](https://github.com/g-plane/pretty_yaml/blob/main/docs/config.md#dashspacing) on GitHub
pub dash_spacing: DashSpacing,

#[cfg_attr(feature = "config_serde", serde(alias = "trimTrailingWhitespaces"))]
/// See [`trimTrailingWhitespaces`](https://github.com/g-plane/pretty_yaml/blob/main/docs/config.md#trimtrailingwhitespaces) on GitHub
pub trim_trailing_whitespaces: bool,

#[cfg_attr(feature = "config_serde", serde(alias = "ignoreCommentDirective"))]
/// See [`ignoreCommentDirective`](https://github.com/g-plane/pretty_yaml/blob/main/docs/config.md#ignorecommentdirective) on GitHub
pub ignore_comment_directive: String,
Expand All @@ -117,6 +121,7 @@ impl Default for LanguageOptions {
brace_spacing: true,
bracket_spacing: false,
dash_spacing: DashSpacing::default(),
trim_trailing_whitespaces: true,
ignore_comment_directive: "pretty-yaml-ignore".into(),
}
}
Expand Down
17 changes: 12 additions & 5 deletions pretty_yaml/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,10 @@ impl DocGen for BlockScalar {
let lines = text.lines().map(|s| {
if s.trim().is_empty() {
String::new()
} else {
} else if ctx.options.trim_trailing_whitespaces {
s[space_len..].trim_end().to_owned()
} else {
s[space_len..].to_owned()
}
});
let mut docs = vec![];
Expand Down Expand Up @@ -341,7 +343,7 @@ impl DocGen for Flow {
(Some(&ctx.options.quotes), "'")
};
docs.push(Doc::text(quote));
format_quoted_scalar(text, quotes_option, &mut docs);
format_quoted_scalar(text, quotes_option, &mut docs, ctx);
docs.push(Doc::text(quote));
} else if let Some(single_quoted) = self.single_quoted_scalar() {
let text = single_quoted.text();
Expand All @@ -355,7 +357,7 @@ impl DocGen for Flow {
(Some(&ctx.options.quotes), "\"")
};
docs.push(Doc::text(quote));
format_quoted_scalar(text, quotes_option, &mut docs);
format_quoted_scalar(text, quotes_option, &mut docs, ctx);
docs.push(Doc::text(quote));
} else if let Some(plain) = self.plain_scalar() {
let lines = plain.text().lines().map(|s| s.trim().to_owned());
Expand Down Expand Up @@ -1118,7 +1120,12 @@ fn format_comment(token: &SyntaxToken, ctx: &Ctx) -> Doc<'static> {
}
}

fn format_quoted_scalar(text: &str, quotes_option: Option<&Quotes>, docs: &mut Vec<Doc<'static>>) {
fn format_quoted_scalar(
text: &str,
quotes_option: Option<&Quotes>,
docs: &mut Vec<Doc<'static>>,
ctx: &Ctx,
) {
if text.is_empty() {
return;
}
Expand All @@ -1128,7 +1135,7 @@ fn format_quoted_scalar(text: &str, quotes_option: Option<&Quotes>, docs: &mut V
if i > 0 {
line = line.trim_start();
}
if i < last_index {
if i < last_index && ctx.options.trim_trailing_whitespaces {
line = line.trim_end();
}
if i == 0 {
Expand Down
12 changes: 7 additions & 5 deletions pretty_yaml/tests/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ fn run_format_test(path: &Path, input: &str, options: &FormatOptions) -> String
let output = format_text(&input, &options)
.map_err(|err| format!("failed to format '{}': {:?}", path.display(), err))
.unwrap();
assert!(
!output.contains(" \n"),
"'{}' has trailing whitespace",
path.display()
);
if options.language.trim_trailing_whitespaces {
assert!(
!output.contains(" \n"),
"'{}' has trailing whitespaces",
path.display()
);
}
let regression_format = format_text(&output, &options)
.map_err(|err| {
format!(
Expand Down
2 changes: 2 additions & 0 deletions pretty_yaml/tests/fmt/trailing-whitespaces/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[disabled]
trimTrailingWhitespaces = false
13 changes: 13 additions & 0 deletions pretty_yaml/tests/fmt/trailing-whitespaces/trailing.disabled.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
source: pretty_yaml/tests/fmt.rs
---
- example: |
foo
bar

baz

- "
text

"
10 changes: 10 additions & 0 deletions pretty_yaml/tests/fmt/trailing-whitespaces/trailing.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- example: |
foo
bar
baz
- "
text
"

0 comments on commit 0b43695

Please sign in to comment.