Skip to content

Commit

Permalink
feat(fmt): add forceDouble/forceSingle to quotes option
Browse files Browse the repository at this point in the history
(close #3)
  • Loading branch information
g-plane committed Aug 25, 2024
1 parent 651e854 commit ea3def9
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 15 deletions.
20 changes: 18 additions & 2 deletions docs/src/config/quotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ Control the quotes.

Possible options:

- `"preferDouble"`: Use double quotes as possible. However if there're escaped characters in strings, quotes will be kept as-is.
- `"preferSingle"`: Use single quotes as possible. However if there're `\` char or `"` char in strings, quotes will be kept as-is.
- `"preferDouble"`: Use double quotes as possible. However if there're quotes or escaped characters in strings, quotes will be kept as-is.
- `"preferSingle"`: Use single quotes as possible. However if there're quotes or `\` characters in strings, quotes will be kept as-is.
- `"forceDouble"`: Use double quotes. However if there're escaped characters in strings, quotes will be kept as-is.
- `"forceSingle"`: Use single quotes. However if there're `\` char or `"` char in strings, quotes will be kept as-is.

Default option is `"preferDouble"`.
We recommend to use double quotes because behavior in single quoted scalars is counter-intuitive.
Expand All @@ -14,10 +16,24 @@ We recommend to use double quotes because behavior in single quoted scalars is c

```yaml
- "text"
- '"'
```
## Example for `"preferSingle"`
```yaml
- 'text'
- "'"
```
## Example for `"forceDouble"`
```yaml
- "text"
```
## Example for `"forceSingle"`
```yaml
- 'text'
```
10 changes: 9 additions & 1 deletion dprint_plugin/deployment/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,18 @@
"oneOf": [
{
"const": "preferDouble",
"description": "Use double quotes as possible. However if there're escaped characters in strings, quotes will be kept as-is. (Recommended)"
"description": "Use double quotes as possible. However if there're quotes or escaped characters in strings, quotes will be kept as-is."
},
{
"const": "preferSingle",
"description": "Use single quotes as possible. However if there're quotes or `\\` characters in strings, quotes will be kept as-is."
},
{
"const": "forceDouble",
"description": "Use double quotes as possible. However if there're escaped characters in strings, quotes will be kept as-is. (Recommended)"
},
{
"const": "forceSingle",
"description": "Use single quotes as possible. However if there're `\\` char or `\"` char in strings, quotes will be kept as-is."
}
],
Expand Down
2 changes: 2 additions & 0 deletions dprint_plugin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub(crate) fn resolve_config(
) {
"preferDouble" => Quotes::PreferDouble,
"preferSingle" => Quotes::PreferSingle,
"forceDouble" => Quotes::ForceDouble,
"forceSingle" => Quotes::ForceSingle,
_ => {
diagnostics.push(ConfigurationDiagnostic {
property_name: "quotes".into(),
Expand Down
6 changes: 6 additions & 0 deletions pretty_yaml/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ pub enum Quotes {
#[cfg_attr(feature = "config_serde", serde(alias = "preferSingle"))]
/// Make string to single quoted unless it contains double quotes inside.
PreferSingle,

#[cfg_attr(feature = "config_serde", serde(alias = "forceDouble"))]
ForceDouble,

#[cfg_attr(feature = "config_serde", serde(alias = "forceSingle"))]
ForceSingle,
}

#[derive(Clone, Debug, Default)]
Expand Down
36 changes: 27 additions & 9 deletions pretty_yaml/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,20 @@ impl DocGen for Flow {
let text = text
.get(1..text.len() - 1)
.expect("expected double quoted scalar");
let is_double_preferred = matches!(ctx.options.quotes, Quotes::PreferDouble);
let (quotes_option, quote) = if is_double_preferred || text.contains('\\') {
let (quotes_option, quote) = if text.contains('\\') {
(None, "\"")
} else {
(Some(&ctx.options.quotes), "'")
match &ctx.options.quotes {
Quotes::PreferSingle => {
if text.contains(['\'', '"']) {
(None, "\"")
} else {
(Some(&ctx.options.quotes), "'")
}
}
Quotes::PreferDouble | Quotes::ForceDouble => (None, "\""),
Quotes::ForceSingle => (Some(&ctx.options.quotes), "'"),
}
};
docs.push(Doc::text(quote));
format_quoted_scalar(text, quotes_option, &mut docs, ctx);
Expand All @@ -351,11 +360,20 @@ impl DocGen for Flow {
let text = text
.get(1..text.len() - 1)
.expect("expected single quoted scalar");
let is_single_preferred = matches!(ctx.options.quotes, Quotes::PreferSingle);
let (quotes_option, quote) = if is_single_preferred || text.contains(['\\', '"']) {
let (quotes_option, quote) = if text.contains(['\\', '"']) {
(None, "'")
} else {
(Some(&ctx.options.quotes), "\"")
match &ctx.options.quotes {
Quotes::PreferDouble => {
if text.contains(['\'', '"']) {
(None, "'")
} else {
(Some(&ctx.options.quotes), "\"")
}
}
Quotes::PreferSingle | Quotes::ForceSingle => (None, "'"),
Quotes::ForceDouble => (Some(&ctx.options.quotes), "\""),
}
};
docs.push(Doc::text(quote));
format_quoted_scalar(text, quotes_option, &mut docs, ctx);
Expand Down Expand Up @@ -1207,9 +1225,9 @@ fn format_quoted_scalar(
}
fn format_quoted_scalar_line(s: &str, quotes_option: Option<&Quotes>) -> String {
match quotes_option {
Some(Quotes::PreferDouble) => s.replace("''", "'"),
Some(Quotes::PreferSingle) => s.replace('\'', "''"),
None => s.to_owned(),
Some(Quotes::ForceDouble) => s.replace("''", "'"),
Some(Quotes::ForceSingle) => s.replace('\'', "''"),
Some(Quotes::PreferDouble | Quotes::PreferSingle) | None => s.to_owned(),
}
}

Expand Down
6 changes: 6 additions & 0 deletions pretty_yaml/tests/fmt/quote/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ quotes = "preferDouble"

[prefer-single]
quotes = "preferSingle"

[force-double]
quotes = "forceDouble"

[force-single]
quotes = "forceSingle"
41 changes: 41 additions & 0 deletions pretty_yaml/tests/fmt/quote/multiline.force-double.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
source: pretty_yaml/tests/fmt.rs
---
a: "
123123123123123123123123123
123123123123123123123123123
123123123123123123123123123
123123123123123123123123123
123123123123123123123123123
123123123123123123123123123
123123123123123123123123123

123123123123123123123123123


123123123123123123123123123




123123123123123123123123123
"
b: "
123123123123123123123123123
123123123123123123123123123
123123123123123123123123123
123123123123123123123123123
123123123123123123123123123
123123123123123123123123123
123123123123123123123123123

123123123123123123123123123


123123123123123123123123123




123123123123123123123123123
"
41 changes: 41 additions & 0 deletions pretty_yaml/tests/fmt/quote/multiline.force-single.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
source: pretty_yaml/tests/fmt.rs
---
a: '
123123123123123123123123123
123123123123123123123123123
123123123123123123123123123
123123123123123123123123123
123123123123123123123123123
123123123123123123123123123
123123123123123123123123123

123123123123123123123123123


123123123123123123123123123




123123123123123123123123123
'
b: '
123123123123123123123123123
123123123123123123123123123
123123123123123123123123123
123123123123123123123123123
123123123123123123123123123
123123123123123123123123123
123123123123123123123123123

123123123123123123123123123


123123123123123123123123123




123123123123123123123123123
'
12 changes: 12 additions & 0 deletions pretty_yaml/tests/fmt/quote/quote.force-double.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: pretty_yaml/tests/fmt.rs
---
- "123"
- "123"
- "''"
- '""'
- "'"
- "\"\""
- '\n123'
- "\n123"
- "'a\"b"
12 changes: 12 additions & 0 deletions pretty_yaml/tests/fmt/quote/quote.force-single.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: pretty_yaml/tests/fmt.rs
---
- '123'
- '123'
- ''''''
- '""'
- ''''
- "\"\""
- '\n123'
- "\n123"
- "'a\"b"
2 changes: 1 addition & 1 deletion pretty_yaml/tests/fmt/quote/quote.prefer-double.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ source: pretty_yaml/tests/fmt.rs
- "123"
- "''"
- '""'
- "'"
- ''''
- "\"\""
- '\n123'
- "\n123"
Expand Down
2 changes: 1 addition & 1 deletion pretty_yaml/tests/fmt/quote/quote.prefer-single.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source: pretty_yaml/tests/fmt.rs
---
- '123'
- '123'
- ''''''
- "''"
- '""'
- ''''
- "\"\""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ control: "\b1998\t1999\t2000\n"
hex esc: "\x0d\x0a is \r\n"

single: '"Howdy!" he cried.'
quoted: " # Not a 'comment'."
quoted: ' # Not a ''comment''.'
tie-fighter: '|\-*-/|'

0 comments on commit ea3def9

Please sign in to comment.