From 1efe935ac3cf8a2c419f58d8623df68a4eb6e009 Mon Sep 17 00:00:00 2001 From: Braydon Kains <93549768+braydonk@users.noreply.github.com> Date: Sat, 18 Mar 2023 15:45:55 -0400 Subject: [PATCH] basic: add indentless_array option (#93) Updates the yaml library and adds a new option for indentless arrays. --- docs/config.md | 3 ++- formatters/basic/config.go | 1 + formatters/basic/formatter.go | 13 +++++++------ go.mod | 4 ++-- go.sum | 4 ++++ 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/docs/config.md b/docs/config.md index 8c2ef8b..29bc574 100644 --- a/docs/config.md +++ b/docs/config.md @@ -63,8 +63,9 @@ The basic formatter is a barebones formatter that simply takes the data provided | `line_ending` | `lf` or `crlf` | `crlf` on Windows, `lf` otherwise | Parse and write the file with "lf" or "crlf" line endings. This setting will be overwritten by the global `line_ending`. | | `retain_line_breaks` | bool | false | Retain line breaks in formatted yaml | | `disallow_anchors` | bool | false | If true, reject any YAML anchors or aliases found in the document. | -| `max_line_length` | int | 0 | Set the maximum line length (see notes below). if not set, defaults to 0 which means no limit. | +| `max_line_length` | int | 0 | Set the maximum line length (see notes below). if not set, defaults to 0 which means no limit. | | `scan_folded_as_literal` | bool | false | Option that will preserve newlines in folded block scalars (blocks that start with `>`). | +| `indentless_array` | bool | false | Render `-` array items (block sequence items) without an increased indent. | ### Note on `max_line_length` diff --git a/formatters/basic/config.go b/formatters/basic/config.go index fc2a725..63f682a 100644 --- a/formatters/basic/config.go +++ b/formatters/basic/config.go @@ -28,6 +28,7 @@ type Config struct { RetainLineBreaks bool `mapstructure:"retain_line_breaks"` DisallowAnchors bool `mapstructure:"disallow_anchors"` ScanFoldedAsLiteral bool `mapstructure:"scan_folded_as_literal"` + IndentlessArrays bool `mapstructure:"indentless_arrays"` } func DefaultConfig() *Config { diff --git a/formatters/basic/formatter.go b/formatters/basic/formatter.go index feb59d5..de4d1a0 100644 --- a/formatters/basic/formatter.go +++ b/formatters/basic/formatter.go @@ -96,17 +96,18 @@ func (f *BasicFormatter) getNewDecoder(reader io.Reader) *yaml.Decoder { func (f *BasicFormatter) getNewEncoder(buf *bytes.Buffer) *yaml.Encoder { e := yaml.NewEncoder(buf) e.SetIndent(f.Config.Indent) + if f.Config.LineLength > 0 { e.SetWidth(f.Config.LineLength) } + if f.Config.LineEnding == yamlfmt.LineBreakStyleCRLF { e.SetLineBreakStyle(yaml.LineBreakStyleCRLF) } - if f.Config.IncludeDocumentStart { - e.SetExplicitDocumentStart() - } - if f.Config.ScanFoldedAsLiteral { - e.SetAssumeBlockAsLiteral(true) - } + + e.SetExplicitDocumentStart(f.Config.IncludeDocumentStart) + e.SetAssumeBlockAsLiteral(f.Config.ScanFoldedAsLiteral) + e.SetIndentlessBlockSequence(f.Config.IndentlessArrays) + return e } diff --git a/go.mod b/go.mod index d4cd36b..509f23f 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,8 @@ go 1.20 require ( github.com/RageCage64/multilinediff v0.2.0 - github.com/bmatcuk/doublestar/v4 v4.2.0 - github.com/braydonk/yaml v0.4.0 + github.com/bmatcuk/doublestar/v4 v4.6.0 + github.com/braydonk/yaml v0.5.0 github.com/mitchellh/mapstructure v1.5.0 ) diff --git a/go.sum b/go.sum index c5275e7..985a8ea 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,12 @@ github.com/RageCage64/multilinediff v0.2.0 h1:yNSpSF5NXIrmo6bRIgO4Q0g7SXqFD4j/WE github.com/RageCage64/multilinediff v0.2.0/go.mod h1:pKr+KLgP0gvRzA+yv0/IUaYQuBYN1ucWysvsL58aMP0= github.com/bmatcuk/doublestar/v4 v4.2.0 h1:Qu+u9wR3Vd89LnlLMHvnZ5coJMWKQamqdz9/p5GNthA= github.com/bmatcuk/doublestar/v4 v4.2.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= +github.com/bmatcuk/doublestar/v4 v4.6.0 h1:HTuxyug8GyFbRkrffIpzNCSK4luc0TY3wzXvzIZhEXc= +github.com/bmatcuk/doublestar/v4 v4.6.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/braydonk/yaml v0.4.0 h1:MNjdriecuspytC31J7Tzx6O8b1yAbMSTGvRG6vLSXZc= github.com/braydonk/yaml v0.4.0/go.mod h1:hcm3h581tudlirk8XEUPDBAimBPbmnL0Y45hCRl47N4= +github.com/braydonk/yaml v0.5.0 h1:j1SlSSD9JLRKgkmFb66hsDH4D0YFlLOLXSXDwNH9/LU= +github.com/braydonk/yaml v0.5.0/go.mod h1:hcm3h581tudlirk8XEUPDBAimBPbmnL0Y45hCRl47N4= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=