Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion formatter/exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ func (s *exp) Format(c *Config, p printer, w io.Writer) error {
}

if st := s.Prefixexp; st != nil {
// isIncludedInMaxLineSize(c, p, w, s.Prefixexp)
if err := st.Format(c, p, w); err != nil {
return err
}
Expand All @@ -502,7 +503,12 @@ func (s *exp) Format(c *Config, p printer, w io.Writer) error {
}

if s.Binop.Token.Type == nAnd || s.Binop.Token.Type == nOr {
if p.IfStatementExpLong {
isIncluded, err := isIncludedInMaxLineSize(c, p, w, s.Prefixexp)
if err != nil {
return err
}

if !isIncluded {
if err := newLine(w); err != nil {
return err
}
Expand Down
36 changes: 33 additions & 3 deletions formatter/explist.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

package formatter

import "io"
import (
"bytes"
"io"
)

type explist struct {
List []*exp // separator ,
Expand Down Expand Up @@ -69,7 +72,7 @@ func (s *explist) Format(c *Config, p printer, w io.Writer) error {
}

for i := 0; i < len(s.List); i++ {
if !isInLine {
if !isInLine && i > 0 {
if err := newLine(w); err != nil {
return err
}
Expand All @@ -79,6 +82,12 @@ func (s *explist) Format(c *Config, p printer, w io.Writer) error {
}
}

isIncluded, err := isIncludedInMaxLineSize(c, p, w, s.List[i])
if err != nil {
return err
}

p.IfStatementExpLong = !isIncluded
if err := s.List[i].Format(c, p, w); err != nil {
return err
}
Expand All @@ -105,11 +114,18 @@ func (s *explist) Format(c *Config, p printer, w io.Writer) error {
}

func (s *explist) isInline(c *Config, p printer, w io.Writer) bool {
if len(s.List) == 1 {
// if len(s.List) == 1 {
// return true
// }

if p.ParentStatement == tsAssignment {
p.ParentStatement = tsNone
return true
}

buf := bytes.NewBuffer(nil)
for _, item := range s.List {
// if item.Func != nil && len(s.List) > 1 {
if item.Func != nil {
return false
}
Expand All @@ -126,6 +142,20 @@ func (s *explist) isInline(c *Config, p printer, w io.Writer) bool {

return false
}

if err := item.Format(c, p, buf); err != nil {
return false
}
}

curpos := getCursorPosition(w)
//fmt.Printf("--%s %d>%d\n", buf.String(), curpos.Col+uint64(buf.Len()), uint64(c.MaxLineLength))
if curpos.Col+uint64(buf.Len()) > uint64(c.MaxLineLength) {
// if len(s.List) == 1 {
// return true
// }

return false
}

return true
Expand Down
30 changes: 30 additions & 0 deletions formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package formatter

import (
"bytes"
"fmt"
"io"
"reflect"
)

const (
Expand Down Expand Up @@ -171,3 +173,31 @@ func spaceAroundOption(c *Config, tokenType int, w io.Writer) error {

return err
}

func isIncludedInMaxLineSize(
c *Config,
p printer,
w io.Writer,
sts ...statement,
) (bool, error) {
buf := bytes.NewBuffer(nil)
for _, st := range sts {
if reflect.ValueOf(st).IsNil() {
continue
}

if st == nil {
continue
}

if err := st.Format(c, p, buf); err != nil {
return false, err
}
}

curpos := getCursorPosition(w)

fmt.Printf("%s %d", buf.String(), buf.Len())

return curpos.Col+uint64(buf.Len()) <= uint64(c.MaxLineLength), nil
}
2 changes: 2 additions & 0 deletions formatter/statement_assigment.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ func (s *assignmentStatement) GetStatement(prev, cur *element) statement {
}

func (s *assignmentStatement) Format(c *Config, p printer, w io.Writer) error {
p.ParentStatement = s.TypeOf()

if s.IsLocal {
if _, err := w.Write([]byte("local ")); err != nil {
return err
Expand Down