From aa5fe93f311924d05061c7fa0192c25cc3849058 Mon Sep 17 00:00:00 2001 From: Dmitry Stoletov Date: Tue, 16 Feb 2021 10:48:09 +0300 Subject: [PATCH] add a newline Before declaration a function or if statement or return statement if previous statement is not assignment or newline then a newline will be added before it. Signed-off-by: Dmitry Stoletov --- formatter/body.go | 31 +++++++++++++++++++++++++++---- formatter/document.go | 1 + formatter/exp.go | 8 ++------ formatter/fieldlist.go | 6 +----- formatter/statement_if.go | 2 +- tests/default/0004.lua.fmt | 1 + tests/default/0005.lua.fmt | 1 + tests/default/0007.lua.fmt | 1 + 8 files changed, 35 insertions(+), 16 deletions(-) diff --git a/formatter/body.go b/formatter/body.go index dcf2760..944e2a6 100644 --- a/formatter/body.go +++ b/formatter/body.go @@ -168,10 +168,33 @@ func (b *body) Format(c *Config, p printer, w io.Writer) error { } func isSemanticNewline(prev statement, cur statement) bool { - if prev != nil { - if cur.TypeOf() == tsReturn && prev.TypeOf() != tsNewline && prev.TypeOf() != tsComment { - return true - } + if prev == nil { + return false + } + + if prev.TypeOf() == tsNewline || prev.TypeOf() == tsComment { + return false + } + + if cur.TypeOf() == tsReturn { + // always before tsAssignment + return true + } + + if prev.TypeOf() == tsAssignment { + return false + } + + if cur.TypeOf() == tsFunction { + return true + } + + if cur.TypeOf() == tsIfStatement { + return true + } + + if cur.TypeOf() == tsAssignment { + return true } return false diff --git a/formatter/document.go b/formatter/document.go index 9e7a031..3e559c0 100644 --- a/formatter/document.go +++ b/formatter/document.go @@ -48,6 +48,7 @@ const ( tsReturn tsNewline tsComment + tsIfStatement ) // chunk ::= block diff --git a/formatter/exp.go b/formatter/exp.go index 43aa872..79e343a 100644 --- a/formatter/exp.go +++ b/formatter/exp.go @@ -524,17 +524,13 @@ func (s *exp) Format(c *Config, p printer, w io.Writer) error { } if s.Binop.Token.Type == nConcat { - curpos, ok := w.(cursorPositioner) - if !ok { - return nil - } - buf := bytes.NewBuffer(nil) if err := s.Exp.Format(c, p, buf); err != nil { return err } - if curpos.Cursor().Col+uint64(buf.Len()) > uint64(c.MaxLineLength) { + curpos := getCursorPosition(w) + if curpos.Col+uint64(buf.Len()) > uint64(c.MaxLineLength) { if err := newLine(w); err != nil { return err } diff --git a/formatter/fieldlist.go b/formatter/fieldlist.go index f6562b6..c2d9fea 100644 --- a/formatter/fieldlist.go +++ b/formatter/fieldlist.go @@ -193,11 +193,7 @@ type fieldLength struct { } func (s *fieldlist) isInline(c *Config, p printer, w io.Writer) bool { - var curpos cursorPosition - - if v, ok := w.(cursorPositioner); ok { - curpos = v.Cursor() - } + curpos := getCursorPosition(w) buf := bytes.NewBuffer([]byte("{}")) diff --git a/formatter/statement_if.go b/formatter/statement_if.go index 28b45f4..0859f72 100644 --- a/formatter/statement_if.go +++ b/formatter/statement_if.go @@ -31,7 +31,7 @@ func (ifStatement) InnerStatement(prev, cur *element) (bool, statement) { } func (ifStatement) TypeOf() typeStatement { - return tsNone + return tsIfStatement } func (s *ifStatement) IsEnd(prev, cur *element) (bool, bool) { diff --git a/tests/default/0004.lua.fmt b/tests/default/0004.lua.fmt index c88eede..c80db0d 100644 --- a/tests/default/0004.lua.fmt +++ b/tests/default/0004.lua.fmt @@ -17,5 +17,6 @@ else return 4 end end + c = b() print(c) diff --git a/tests/default/0005.lua.fmt b/tests/default/0005.lua.fmt index d2eee8c..1dfa00d 100644 --- a/tests/default/0005.lua.fmt +++ b/tests/default/0005.lua.fmt @@ -97,6 +97,7 @@ local vehicles = load_vehicles_from_disk("vehicles.dat") if vehicles["Porsche"] then porsche_handler(vehicles["Porsche"]) + vehicles["Porsche"] = nil end diff --git a/tests/default/0007.lua.fmt b/tests/default/0007.lua.fmt index 224051d..1e48c23 100644 --- a/tests/default/0007.lua.fmt +++ b/tests/default/0007.lua.fmt @@ -34,6 +34,7 @@ end local function foo() -- code end + local function bar() -- code end