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
31 changes: 27 additions & 4 deletions formatter/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions formatter/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const (
tsReturn
tsNewline
tsComment
tsIfStatement
)

// chunk ::= block
Expand Down
8 changes: 2 additions & 6 deletions formatter/exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 1 addition & 5 deletions formatter/fieldlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("{}"))

Expand Down
2 changes: 1 addition & 1 deletion formatter/statement_if.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions tests/default/0004.lua.fmt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ else
return 4
end
end

c = b()
print(c)
1 change: 1 addition & 0 deletions tests/default/0005.lua.fmt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ local vehicles = load_vehicles_from_disk("vehicles.dat")

if vehicles["Porsche"] then
porsche_handler(vehicles["Porsche"])

vehicles["Porsche"] = nil
end

Expand Down
1 change: 1 addition & 0 deletions tests/default/0007.lua.fmt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ end
local function foo()
-- code
end

local function bar()
-- code
end
Expand Down