Skip to content

Commit

Permalink
formatter: add pad_line_comments option (#107)
Browse files Browse the repository at this point in the history
Signed-off-by: Ayaz Badouraly <ayaz.badouraly@datadoghq.com>
  • Loading branch information
badouralix committed Apr 2, 2023
1 parent f81fb82 commit cdca3ce
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 11 deletions.
1 change: 1 addition & 0 deletions docs/config-file.md
Expand Up @@ -57,6 +57,7 @@ The basic formatter is a barebones formatter that simply takes the data provided
| `scan_folded_as_literal` | bool | false | Option that will preserve newlines in folded block scalars (blocks that start with `>`). |
| `indentless_arrays` | bool | false | Render `-` array items (block sequence items) without an increased indent. |
| `drop_merge_tag` | bool | false | Assume that any well formed merge using just a `<<` token will be a merge, and drop the `!!merge` tag from the formatted result. |
| `pad_line_comments` | int | 1 | The number of padding spaces to insert before line comments. |

### Note on `max_line_length`

Expand Down
6 changes: 4 additions & 2 deletions formatters/basic/config.go
Expand Up @@ -30,6 +30,7 @@ type Config struct {
ScanFoldedAsLiteral bool `mapstructure:"scan_folded_as_literal"`
IndentlessArrays bool `mapstructure:"indentless_arrays"`
DropMergeTag bool `mapstructure:"drop_merge_tag"`
PadLineComments int `mapstructure:"pad_line_comments"`
}

func DefaultConfig() *Config {
Expand All @@ -38,7 +39,8 @@ func DefaultConfig() *Config {
lineBreakStyle = yamlfmt.LineBreakStyleCRLF
}
return &Config{
Indent: 2,
LineEnding: lineBreakStyle,
Indent: 2,
LineEnding: lineBreakStyle,
PadLineComments: 1,
}
}
17 changes: 17 additions & 0 deletions formatters/basic/factory_test.go
Expand Up @@ -36,6 +36,7 @@ func TestNewWithConfigRetainsDefaultValues(t *testing.T) {
Indent: 4,
IncludeDocumentStart: false,
LineEnding: yamlfmt.LineBreakStyleLF,
PadLineComments: 1,
},
},
{
Expand All @@ -47,6 +48,7 @@ func TestNewWithConfigRetainsDefaultValues(t *testing.T) {
Indent: 2,
IncludeDocumentStart: true,
LineEnding: yamlfmt.LineBreakStyleLF,
PadLineComments: 1,
},
},
{
Expand All @@ -58,6 +60,19 @@ func TestNewWithConfigRetainsDefaultValues(t *testing.T) {
Indent: 2,
IncludeDocumentStart: false,
LineEnding: yamlfmt.LineBreakStyleCRLF,
PadLineComments: 1,
},
},
{
name: "only pad_line_comments specified",
configMap: map[string]interface{}{
"pad_line_comments": 2,
},
expectedConfig: basic.Config{
Indent: 2,
IncludeDocumentStart: false,
LineEnding: yamlfmt.LineBreakStyleLF,
PadLineComments: 2,
},
},
{
Expand All @@ -66,11 +81,13 @@ func TestNewWithConfigRetainsDefaultValues(t *testing.T) {
"indent": 4,
"line_ending": "crlf",
"include_document_start": true,
"pad_line_comments": 2,
},
expectedConfig: basic.Config{
Indent: 4,
IncludeDocumentStart: true,
LineEnding: yamlfmt.LineBreakStyleCRLF,
PadLineComments: 2,
},
},
}
Expand Down
1 change: 1 addition & 0 deletions formatters/basic/formatter.go
Expand Up @@ -109,6 +109,7 @@ func (f *BasicFormatter) getNewEncoder(buf *bytes.Buffer) *yaml.Encoder {
e.SetAssumeBlockAsLiteral(f.Config.ScanFoldedAsLiteral)
e.SetIndentlessBlockSequence(f.Config.IndentlessArrays)
e.SetDropMergeTag(f.Config.DropMergeTag)
e.SetPadLineComments(f.Config.PadLineComments)

return e
}
18 changes: 18 additions & 0 deletions formatters/basic/formatter_test.go
Expand Up @@ -269,3 +269,21 @@ b:
t.Fatalf("expected formatted result to drop merge tag, was found:\n%s", resultStr)
}
}

func TestPadLineComments(t *testing.T) {
config := basic.DefaultConfig()
config.PadLineComments = 2
f := newFormatter(config)

yml := "a: 1 # line comment"
expectedStr := "a: 1 # line comment"

result, err := f.Format([]byte(yml))
if err != nil {
t.Fatalf("expected formatting to pass, returned error: %v", err)
}
resultStr := strings.TrimSuffix(string(result), "\n")
if resultStr != expectedStr {
t.Fatalf("expected: '%s', got: '%s'", expectedStr, resultStr)
}
}
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
github.com/RageCage64/multilinediff v0.2.0
github.com/bmatcuk/doublestar/v4 v4.6.0
github.com/braydonk/yaml v0.6.0
github.com/braydonk/yaml v0.7.0
github.com/mitchellh/mapstructure v1.5.0
)

Expand Down
10 changes: 2 additions & 8 deletions go.sum
@@ -1,15 +1,9 @@
github.com/RageCage64/multilinediff v0.2.0 h1:yNSpSF5NXIrmo6bRIgO4Q0g7SXqFD4j/WEcBE+BdCFY=
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/braydonk/yaml v0.6.0 h1:nHt2AzmV4yMhXSNROksvkTgAxzollgy7uVOKfCVXhMo=
github.com/braydonk/yaml v0.6.0/go.mod h1:hcm3h581tudlirk8XEUPDBAimBPbmnL0Y45hCRl47N4=
github.com/braydonk/yaml v0.7.0 h1:ySkqO7r0MGoCNhiRJqE0Xe9yhINMyvOAB3nFjgyJn2k=
github.com/braydonk/yaml v0.7.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=
Expand Down

0 comments on commit cdca3ce

Please sign in to comment.