Skip to content

Commit 87af88e

Browse files
authored
add array indent functionality (#239)
* add array indent functionality * update integrationtest golden files
1 parent 3b54c67 commit 87af88e

File tree

7 files changed

+53
-0
lines changed

7 files changed

+53
-0
lines changed

docs/config-file.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ The basic formatter is a barebones formatter that simply takes the data provided
8686
| `trim_trailing_whitespace` | bool | false | Trim trailing whitespace from lines. |
8787
| `eof_newline` | bool | false | Always add a newline at end of file. Useful in the scenario where `retain_line_breaks` is disabled but the trailing newline is still needed. |
8888
| `strip_directives` | bool | false | [YAML Directives](https://yaml.org/spec/1.2.2/#3234-directives) are not supported by this formatter. This feature will attempt to strip the directives before formatting and put them back. [Use this feature at your own risk.](#strip_directives) |
89+
| `array_indent` | int | = indent | Set a different indentation level for block sequences specifically. |
90+
| `indent_root_array` | int | false | Tells the formatter to indent an array that is at the lowest indentation level of the document. |
8991

9092
## Additional Notes
9193

formatters/basic/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type Config struct {
3535
TrimTrailingWhitespace bool `mapstructure:"trim_trailing_whitespace"`
3636
EOFNewline bool `mapstructure:"eof_newline"`
3737
StripDirectives bool `mapstructure:"strip_directives"`
38+
ArrayIndent int `mapstructure:"array_indent"`
39+
IndentRootArray bool `mapstructure:"indent_root_array"`
3840
}
3941

4042
func DefaultConfig() *Config {

formatters/basic/formatter.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ func (f *BasicFormatter) getNewEncoder(buf *bytes.Buffer) *yaml.Encoder {
114114
e.SetDropMergeTag(f.Config.DropMergeTag)
115115
e.SetPadLineComments(f.Config.PadLineComments)
116116

117+
if f.Config.ArrayIndent > 0 {
118+
e.SetArrayIndent(f.Config.ArrayIndent)
119+
}
120+
e.SetIndentRootArray(f.Config.IndentRootArray)
121+
117122
return e
118123
}
119124

formatters/basic/formatter_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,41 @@ func TestStripDirectives(t *testing.T) {
363363
_, err := f.Format([]byte(yml))
364364
assert.NilErr(t, err)
365365
}
366+
367+
func TestArrayIndent(t *testing.T) {
368+
config := basic.DefaultConfig()
369+
config.ArrayIndent = 1
370+
f := newFormatter(config)
371+
372+
yml := `a:
373+
- 1
374+
- 2
375+
`
376+
expectedYml := `a:
377+
- 1
378+
- 2
379+
`
380+
381+
result, err := f.Format([]byte(yml))
382+
assert.NilErr(t, err)
383+
resultStr := string(result)
384+
if resultStr != expectedYml {
385+
t.Fatalf("expected: '%s', got: '%s'", expectedYml, result)
386+
}
387+
}
388+
389+
func TestIndentRootArray(t *testing.T) {
390+
config := basic.DefaultConfig()
391+
config.IndentRootArray = true
392+
f := newFormatter(config)
393+
394+
yml := "- 1\n"
395+
expectedYml := " - 1\n"
396+
397+
result, err := f.Format([]byte(yml))
398+
assert.NilErr(t, err)
399+
resultStr := string(result)
400+
if resultStr != expectedYml {
401+
t.Fatalf("expected: '%s', got: '%s'", expectedYml, result)
402+
}
403+
}

integrationtest/command/testdata/print_conf_file/stdout/stdout.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ match_type: doublestar
1313
output_format: default
1414
regex_exclude: []
1515
formatter:
16+
array_indent: 0
1617
disallow_anchors: false
1718
drop_merge_tag: false
1819
eof_newline: false
1920
include_document_start: true
2021
indent: 2
22+
indent_root_array: false
2123
indentless_arrays: false
2224
line_ending: crlf
2325
max_line_length: 0

integrationtest/command/testdata/print_conf_flags/stdout/stdout.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ match_type: standard
1212
output_format: default
1313
regex_exclude: []
1414
formatter:
15+
array_indent: 0
1516
disallow_anchors: false
1617
drop_merge_tag: false
1718
eof_newline: false
1819
include_document_start: false
1920
indent: 2
21+
indent_root_array: false
2022
indentless_arrays: false
2123
line_ending: lf
2224
max_line_length: 0

integrationtest/command/testdata/print_conf_flags_and_file/stdout/stdout.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ match_type: doublestar
1313
output_format: default
1414
regex_exclude: []
1515
formatter:
16+
array_indent: 0
1617
disallow_anchors: false
1718
drop_merge_tag: false
1819
eof_newline: false
1920
include_document_start: true
2021
indent: 2
22+
indent_root_array: false
2123
indentless_arrays: false
2224
line_ending: crlf
2325
max_line_length: 0

0 commit comments

Comments
 (0)