Skip to content

Commit

Permalink
feat(fmt): add prefer_single_line option
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Aug 25, 2024
1 parent 89102f3 commit 651e854
Show file tree
Hide file tree
Showing 18 changed files with 422 additions and 104 deletions.
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [braceSpacing](./config/brace-spacing.md)
- [bracketSpacing](./config/bracket-spacing.md)
- [dashSpacing](./config/dash-spacing.md)
- [preferSingleLine](./config/prefer-single-line.md)
- [trimTrailingWhitespaces](./config/trim-trailing-whitespaces.md)
- [trimTrailingZero](./config/trim-trailing-zero.md)
- [ignoreCommentDirective](./config/ignore-comment-directive.md)
80 changes: 80 additions & 0 deletions docs/src/config/prefer-single-line.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# `preferSingleLine`

Control whether items should be placed on single line as possible, even they're originally on multiple lines.

Default option value is `false`.

This global option can be overridden by different syntax nodes:

- `flowSequence.preferSingleLine`
- `flowMap.preferSingleLine`

## Example for `false`

```yaml
- [1,
2,
3]

- [
1, 2, 3
]

- {k1: v1,
k2: v2,
k3: v3}

- {
k1: v1, k2: v2, k3: v3}
```
will be formatted as:
```yaml
- [1, 2, 3]

- [
1,
2,
3,
]

- { k1: v1, k2: v2, k3: v3 }

- {
k1: v1,
k2: v2,
k3: v3,
}
```

## Example for `true`

```yaml
- [1,
2,
3]

- [
1, 2, 3
]

- {k1: v1,
k2: v2,
k3: v3}

- {
k1: v1, k2: v2, k3: v3}
```
will be formatted as:
```yaml
- [1, 2, 3]

- [1, 2, 3]

- { k1: v1, k2: v2, k3: v3 }

- { k1: v1, k2: v2, k3: v3 }
```
16 changes: 16 additions & 0 deletions dprint_plugin/deployment/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
"title": "Config",
"description": "Configuration for dprint-plugin-yaml.",
"type": "object",
"definitions": {
"preferSingleLine": {
"description": "Control whether items should be placed on single line as possible, even they're originally on multiple lines.",
"type": "boolean",
"default": false
}
},
"properties": {
"printWidth": {
"description": "The line width limitation that Pretty YAML should *(but not must)* avoid exceeding. Pretty YAML will try its best to keep line width less than this value, but it may exceed for some cases, for example, a very very long single word.",
Expand Down Expand Up @@ -77,6 +84,15 @@
}
]
},
"preferSingleLine": {
"$ref": "#/definitions/preferSingleLine"
},
"flowSequence.preferSingleLine": {
"$ref": "#/definitions/preferSingleLine"
},
"flowMap.preferSingleLine": {
"$ref": "#/definitions/preferSingleLine"
},
"trimTrailingWhitespaces": {
"description": "Control whether trailing whitespaces should be trimmed or not.",
"type": "boolean",
Expand Down
15 changes: 13 additions & 2 deletions dprint_plugin/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dprint_core::configuration::{
get_unknown_property_diagnostics, get_value, ConfigKeyMap, ConfigurationDiagnostic,
GlobalConfiguration, NewLineKind, ResolveConfigurationResult,
get_nullable_value, get_unknown_property_diagnostics, get_value, ConfigKeyMap,
ConfigurationDiagnostic, GlobalConfiguration, NewLineKind, ResolveConfigurationResult,
};
use pretty_yaml::config::*;

Expand Down Expand Up @@ -88,6 +88,17 @@ pub(crate) fn resolve_config(
Default::default()
}
},
prefer_single_line: get_value(&mut config, "preferSingleLine", false, &mut diagnostics),
flow_sequence_prefer_single_line: get_nullable_value(
&mut config,
"flowSequence.preferSingleLine",
&mut diagnostics,
),
flow_map_prefer_single_line: get_nullable_value(
&mut config,
"flowMap.preferSingleLine",
&mut diagnostics,
),
trim_trailing_whitespaces: get_value(
&mut config,
"trimTrailingWhitespaces",
Expand Down
22 changes: 22 additions & 0 deletions pretty_yaml/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,25 @@ pub struct LanguageOptions {
#[cfg_attr(feature = "config_serde", serde(alias = "dashSpacing"))]
pub dash_spacing: DashSpacing,

#[cfg_attr(feature = "config_serde", serde(alias = "preferSingleLine"))]
pub prefer_single_line: bool,
#[cfg_attr(
feature = "config_serde",
serde(
rename = "flow_sequence.prefer_single_line",
alias = "flowSequence.preferSingleLine"
)
)]
pub flow_sequence_prefer_single_line: Option<bool>,
#[cfg_attr(
feature = "config_serde",
serde(
rename = "flow_map.prefer_single_line",
alias = "flowMap.preferSingleLine"
)
)]
pub flow_map_prefer_single_line: Option<bool>,

#[cfg_attr(feature = "config_serde", serde(alias = "trimTrailingWhitespaces"))]
pub trim_trailing_whitespaces: bool,

Expand All @@ -108,6 +127,9 @@ impl Default for LanguageOptions {
brace_spacing: true,
bracket_spacing: false,
dash_spacing: DashSpacing::default(),
prefer_single_line: false,
flow_sequence_prefer_single_line: None,
flow_map_prefer_single_line: None,
trim_trailing_whitespaces: true,
trim_trailing_zero: false,
ignore_comment_directive: "pretty-yaml-ignore".into(),
Expand Down
Loading

0 comments on commit 651e854

Please sign in to comment.