Skip to content

Commit

Permalink
feat(fmt): add dashSpacing option
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Jul 6, 2024
1 parent 0134882 commit 4b37527
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 10 deletions.
30 changes: 30 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,36 @@ Default option is `false`.
[ a, b ]
```

## `dashSpacing`

Control the whitespace behavior of block compact map in block sequence value.
This option is only effective when `indentWidth` is greater than 2.

Possible options:

- `"oneSpace"`: Insert only one space after `-`.
- `"indent"`: Insert spaces to align indentation, respecting `indentWidth` option.

Default option is `"oneSpace"`.

The examples below assume `indentWidth` is `4`.

### Example for `"oneSpace"`

```yaml
outer:
- key1: value1
key2: value2
```

### Example for `"indent"`

```yaml
outer:
- key1: value1
key2: value2
```

## `ignoreCommentDirective`

Text directive for ignoring formatting specific content.
Expand Down
14 changes: 14 additions & 0 deletions dprint_plugin/deployment/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@
"type": "boolean",
"default": false
},
"dashSpacing": {
"description": "Control the whitespace behavior of block compact map in block sequence value. This option is only effective when `indentWidth` is greater than 2.",
"type": "string",
"oneOf": [
{
"const": "oneSpace",
"description": "Insert only one space after `-`."
},
{
"const": "indent",
"description": "Insert spaces to align indentation, respecting `indentWidth` option."
}
]
},
"ignoreCommentDirective": {
"description": "Text directive for ignoring formatting specific content.",
"type": "string",
Expand Down
16 changes: 16 additions & 0 deletions dprint_plugin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ pub(crate) fn resolve_config(
),
brace_spacing: get_value(&mut config, "braceSpacing", true, &mut diagnostics),
bracket_spacing: get_value(&mut config, "bracketSpacing", false, &mut diagnostics),
dash_spacing: match &*get_value(
&mut config,
"dashSpacing",
"oneSpace".to_string(),
&mut diagnostics,
) {
"oneSpace" => DashSpacing::OneSpace,
"indent" => DashSpacing::Indent,
_ => {
diagnostics.push(ConfigurationDiagnostic {
property_name: "dashSpacing".into(),
message: "invalid value for config `dashSpacing`".into(),
});
Default::default()
}
},
ignore_comment_directive: get_value(
&mut config,
"ignoreCommentDirective",
Expand Down
15 changes: 15 additions & 0 deletions pretty_yaml/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ pub struct LanguageOptions {
/// See [`bracketSpacing`](https://github.com/g-plane/pretty_yaml/blob/main/docs/config.md#bracketspacing) on GitHub
pub bracket_spacing: bool,

#[cfg_attr(feature = "config_serde", serde(alias = "dashSpacing"))]
/// 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 = "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 @@ -112,6 +116,7 @@ impl Default for LanguageOptions {
indent_block_sequence_in_map: true,
brace_spacing: true,
bracket_spacing: false,
dash_spacing: DashSpacing::default(),
ignore_comment_directive: "pretty-yaml-ignore".into(),
}
}
Expand All @@ -130,3 +135,13 @@ pub enum Quotes {
/// Make string to single quoted unless it contains double quotes inside.
PreferSingle,
}

#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "config_serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "config_serde", serde(rename_all = "kebab-case"))]
pub enum DashSpacing {
#[default]
#[cfg_attr(feature = "config_serde", serde(alias = "oneSpace"))]
OneSpace,
Indent,
}
22 changes: 14 additions & 8 deletions pretty_yaml/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,25 +184,28 @@ impl DocGen for BlockSeq {

impl DocGen for BlockSeqEntry {
fn doc(&self, ctx: &Ctx) -> Doc<'static> {
use crate::config::DashSpacing;

let mut docs = Vec::with_capacity(3);

if let Some(token) = self.minus() {
docs.push(Doc::text("-"));
let spacing = match ctx.options.dash_spacing {
DashSpacing::OneSpace => Doc::space(),
DashSpacing::Indent => {
Doc::text(" ".repeat(ctx.indent_width.checked_sub(1).unwrap_or(1)))
}
};
if let Some(token) = token
.next_sibling_or_token()
.and_then(SyntaxElement::into_token)
.filter(|token| token.kind() == SyntaxKind::WHITESPACE)
{
let mut trivia_docs = format_trivias_after_token(&token, ctx);
docs.push(Doc::space());
docs.push(Doc::text(
" ".repeat(ctx.indent_width.checked_sub(2).unwrap_or_default()),
));
docs.push(spacing);
docs.append(&mut trivia_docs);
} else if self.block().is_some() || self.flow().is_some() {
docs.push(Doc::text(
" ".repeat(ctx.indent_width.checked_sub(1).unwrap_or(1)),
));
docs.push(spacing);
}
}

Expand All @@ -212,7 +215,10 @@ impl DocGen for BlockSeqEntry {
docs.push(flow.doc(ctx));
}

Doc::list(docs).nest(ctx.indent_width)
Doc::list(docs).nest(match ctx.options.dash_spacing {
DashSpacing::OneSpace => 2,
DashSpacing::Indent => ctx.indent_width,
})
}
}

Expand Down
7 changes: 7 additions & 0 deletions pretty_yaml/tests/fmt/dash-spacing/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[one-space]
indentWidth = 4
dashSpacing = "oneSpace"

[indent]
indentWidth = 4
dashSpacing = "indent"
6 changes: 6 additions & 0 deletions pretty_yaml/tests/fmt/dash-spacing/flow-map.indent.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: pretty_yaml/tests/fmt.rs
---
outer:
- key1: value1
key2: value2
6 changes: 6 additions & 0 deletions pretty_yaml/tests/fmt/dash-spacing/flow-map.one-space.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: pretty_yaml/tests/fmt.rs
---
outer:
- key1: value1
key2: value2
3 changes: 3 additions & 0 deletions pretty_yaml/tests/fmt/dash-spacing/flow-map.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
outer:
- key1: value1
key2: value2
4 changes: 2 additions & 2 deletions pretty_yaml/tests/fmt/indent/flow-sequence.4.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: pretty_yaml/tests/fmt.rs
---
- a: b
c: d
- a: b
c: d

0 comments on commit 4b37527

Please sign in to comment.