Skip to content

Commit

Permalink
up formatter fields
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Stoletov <[email protected]>
  • Loading branch information
irvis committed Jan 11, 2021
1 parent c4db4b8 commit 7770e89
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 8 deletions.
40 changes: 33 additions & 7 deletions formatter/fieldlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,47 @@ func (s *fieldlist) Format(c *Config, p printer, w io.Writer) error {
}
}

for i, v := range s.List {
for i := 0; i < len(s.List); i++ {
v := s.List[i]

if v.Key.Comments != nil {
for _, com := range v.Key.Comments {
for n := 0; n < len(v.Key.Comments); n++ {
com := v.Key.Comments[uint64(n)]
if com.Token.Type != nComment {
break
}

if i == 0 {
if i == 0 && n == 0 {
if err := newLine(w); err != nil {
return err
}

if err := p.WritePad(w); err != nil {
return err
}
}

if i > 0 {
if err := p.WriteSpaces(w, int(fl[uint64(i-1)].Val+1)); err != nil {
pad := 1
if n > 0 {
pad = 0

if err := newLine(w); err != nil {
return err
}

if err := p.WritePad(w); err != nil {
return err
}
}

if i > 0 {
if n == 0 {
if err := p.WriteSpaces(w, int(fl[uint64(i-1)].Val)+pad); err != nil {
return err
}
}
}

if _, err := w.Write([]byte("-- ")); err != nil {
return err
}
Expand Down Expand Up @@ -177,7 +196,8 @@ func (s *fieldlist) Align(c *Config, p printer) map[uint64]fieldLength {
for i := 0; i < len(s.List); i++ {
item := s.List[i]

if isEndedAlignedBlock(item) {
isEnded := isEndedAlignedBlock(item)
if isEnded || isStartAlignedBlock(item) {
for b, v := range alignBlock {
res[b] = fieldLength{
Key: MaxKeyLength - v.Key,
Expand All @@ -188,7 +208,9 @@ func (s *fieldlist) Align(c *Config, p printer) map[uint64]fieldLength {
alignBlock = make(map[uint64]fieldLength)
MaxKeyLength = 0
MaxValueLength = 0
}

if isEnded {
continue
}

Expand Down Expand Up @@ -233,7 +255,7 @@ func (s *fieldlist) Align(c *Config, p printer) map[uint64]fieldLength {
return res
}

func isEndedAlignedBlock(f *field) bool {
func isStartAlignedBlock(f *field) bool {
if f.Key.Comments != nil && len(f.Key.Comments) > 1 {
for i := 0; i < len(f.Key.Comments); i++ {
if i > 0 && f.Key.Comments[uint64(i)].Token.Type == nComment {
Expand All @@ -242,5 +264,9 @@ func isEndedAlignedBlock(f *field) bool {
}
}

return false
}

func isEndedAlignedBlock(f *field) bool {
return f.Val != nil && f.Val.Func != nil
}
65 changes: 64 additions & 1 deletion formatter/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,16 +626,79 @@ table = {
},
wantW: `
table = {
["a()"] = false, -- comm 1 -- comm 0
["a()"] = false, -- comm 1
-- comm 0
[1 + 1] = true, -- comm 2
bb = function()
return 1
end, -- comm 3
["1394-E"] = val1, -- comm 4
["UTF-8"] = val2, -- comm 5
["and"] = val3, -- comm 6
[true] = 1, -- comm 7
aa = nil, -- comm 8
}
`,
wantErr: false,
},
{
name: "config for table alignment",
args: args{
c: Config{
IndentSize: 4,
MaxLineLength: 80,
Alignment: Alignment{
Table: AlignmentTable{
KeyValuePairs: true,
Comments: true,
},
},
},
b: []byte(`
table = {
-- comm a
-- comm b
["a()"] = false, -- comm 1
-- comm c
-- comm d
[1+1] = true, -- comm 2
-- comm e
-- comm f
bb = function () return 1 end, -- comm 3
-- comm g
-- comm h
["1394-E"] = val1, -- comm 4
["UTF-8"] = val2, -- comm 5
["and"] = val3, -- comm 6
[true] = 1, -- comm 7
aa = nil, -- comm 8
-- comm i
-- comm j
}
`),
},
wantW: `
table = {
-- comm a
-- comm b
["a()"] = false, -- comm 1
-- comm c
-- comm d
[1 + 1] = true, -- comm 2
-- comm e
-- comm f
bb = function()
return 1
end, -- comm 3
-- comm g
-- comm h
["1394-E"] = val1, -- comm 4
["UTF-8"] = val2, -- comm 5
["and"] = val3, -- comm 6
[true] = 1, -- comm 7
aa = nil, -- comm 8
-- comm i
-- comm j
}
`,
wantErr: false,
Expand Down

0 comments on commit 7770e89

Please sign in to comment.