From 76cc7327f5e51fb138285eb2b2db61e905dc3d90 Mon Sep 17 00:00:00 2001 From: Dmitry Stoletov Date: Mon, 15 Mar 2021 17:32:50 +0300 Subject: [PATCH] exp lenght Signed-off-by: Dmitry Stoletov --- formatter/exp.go | 8 ++++++- formatter/explist.go | 36 +++++++++++++++++++++++++++++--- formatter/formatter.go | 30 ++++++++++++++++++++++++++ formatter/statement_assigment.go | 2 ++ 4 files changed, 72 insertions(+), 4 deletions(-) diff --git a/formatter/exp.go b/formatter/exp.go index 43aa872..4c8dca6 100644 --- a/formatter/exp.go +++ b/formatter/exp.go @@ -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 } @@ -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 } diff --git a/formatter/explist.go b/formatter/explist.go index 4180c5b..8cc0fba 100644 --- a/formatter/explist.go +++ b/formatter/explist.go @@ -14,7 +14,10 @@ package formatter -import "io" +import ( + "bytes" + "io" +) type explist struct { List []*exp // separator , @@ -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 } @@ -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 } @@ -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 } @@ -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 diff --git a/formatter/formatter.go b/formatter/formatter.go index 5854a9a..9c23cae 100644 --- a/formatter/formatter.go +++ b/formatter/formatter.go @@ -16,7 +16,9 @@ package formatter import ( "bytes" + "fmt" "io" + "reflect" ) const ( @@ -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 +} diff --git a/formatter/statement_assigment.go b/formatter/statement_assigment.go index 0c3a9a0..8cc3b84 100644 --- a/formatter/statement_assigment.go +++ b/formatter/statement_assigment.go @@ -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