Skip to content

Commit

Permalink
Formatter: one more fix of formatting of 1SetOf Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpevzner committed Jun 22, 2024
1 parent d4a0e6c commit b36209c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
24 changes: 15 additions & 9 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,19 @@ func (f *Formatter) fmtAttributeOrMember(attr Attribute, member bool) {
}

tag := TagZero
for i, val := range attr.Values {
for _, val := range attr.Values {
if val.T != tag {
fmt.Fprintf(buf, " %s:", val.T)
tag = val.T
}

if collection, ok := val.V.(Collection); ok {
if i == 0 {
buf.WriteByte(' ')
if f.onNL() {
f.Printf("{")
} else {
buf.Write([]byte(" {\n"))
}
buf.Write([]byte("{\n"))

f.indent++

for _, attr2 := range collection {
Expand All @@ -200,12 +202,16 @@ func (f *Formatter) fmtAttributeOrMember(attr Attribute, member bool) {
f.forceNL()
}

// forceNL inserts newline character, text is not empty and the last
// character is not yet newline (i.e., if Formatter is not at the
// beginning of the new line).
func (f *Formatter) forceNL() {
// onNL returns true if formatter is at the beginning of new line
func (f *Formatter) onNL() bool {
b := f.buf.Bytes()
if len(b) > 0 && b[len(b)-1] != '\n' {
return len(b) == 0 || b[len(b)-1] == '\n'
}

// forceNL inserts newline character if formatter is not at the
// beginning of new line
func (f *Formatter) forceNL() {
if !f.onNL() {
f.buf.WriteByte('\n')
}
}
Expand Down
18 changes: 9 additions & 9 deletions formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@ func TestFmtAttribute(t *testing.T) {
TagInteger, Integer(20990)),
},
),

indent: 2,
out: []string{
`ATTR "media-size-supported" collection: {`,
` MEMBER "x-dimension" integer: 20990`,
` MEMBER "y-dimension" integer: 29704`,
`}`,
`{`,
` MEMBER "x-dimension" integer: 14852`,
` MEMBER "y-dimension" integer: 20990`,
`}`,
` ATTR "media-size-supported" collection: {`,
` MEMBER "x-dimension" integer: 20990`,
` MEMBER "y-dimension" integer: 29704`,
` }`,
` {`,
` MEMBER "x-dimension" integer: 14852`,
` MEMBER "y-dimension" integer: 20990`,
` }`,
},
},

Expand Down

0 comments on commit b36209c

Please sign in to comment.