Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ytt formatting issue on colons in multiline strings #856

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions pkg/yamlmeta/internal/yaml.v2/emitterc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,7 @@ func yamlEmitterAnalyzeScalar(emitter *yamlEmitterT, value []byte) bool {
followedByWhitespace = false
previousSpace = false
previousBreak = false
previousColon = false
)

emitter.scalarData.value = value
Expand Down Expand Up @@ -1061,7 +1062,11 @@ func yamlEmitterAnalyzeScalar(emitter *yamlEmitterT, value []byte) bool {
if !isPrintable(value, i) || !isASCII(value, i) && !emitter.unicode {
specialCharacters = true
}
if isSpace(value, i) {
if isColon(value, i) {
previousColon = true
previousSpace = false
previousBreak = false
} else if isSpace(value, i) {
if i == 0 {
leadingSpace = true
}
Expand All @@ -1081,14 +1086,16 @@ func yamlEmitterAnalyzeScalar(emitter *yamlEmitterT, value []byte) bool {
if i+width(value[i]) == len(value) {
trailingBreak = true
}
if previousSpace {
if previousSpace && !previousColon {
spaceBreak = true
}
previousSpace = false
previousBreak = true
previousColon = false
} else {
previousSpace = false
previousBreak = false
previousColon = false
}

// [Go]: Why 'z'? Couldn't be the end of the string as that's the loop condition.
Expand Down
12 changes: 12 additions & 0 deletions pkg/yamlmeta/internal/yaml.v2/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,18 @@ var marshalTests = []struct {
&marshalIntTest,
"123\n",
},
{
"a:\n b",
"|-\n a:\n b\n",
},
{
"a: \n b",
"|-\n a: \n b\n",
},
{
"a: \n b",
"|-\n a: \n b\n",
},

// Structures
{
Expand Down
5 changes: 5 additions & 0 deletions pkg/yamlmeta/internal/yaml.v2/yamlprivateh.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ func isSpace(b []byte, i int) bool {
return b[i] == ' '
}

// Check if the character at the specified position is colon.
func isColon(b []byte, i int) bool {
return b[i] == ':'
}

// Check if the character at the specified position is tab.
func isTab(b []byte, i int) bool {
return b[i] == '\t'
Expand Down