Skip to content

Commit

Permalink
add a table formatting rule to the parameters of the function call st…
Browse files Browse the repository at this point in the history
…atement.

If the parameter is a table, and the table has only one field and
a one-line state, then the parameters will be written in one line.

setmetatable({one = "line"}, MT)

Signed-off-by: Dmitry Stoletov <[email protected]>
  • Loading branch information
irvis committed Feb 13, 2021
1 parent fabaa81 commit c34dd5c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
15 changes: 14 additions & 1 deletion formatter/explist.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,20 @@ func (s *explist) isInline(c *Config, p printer, w io.Writer) bool {
}

for _, item := range s.List {
if item.Table != nil || item.Func != nil {
if item.Func != nil {
return false
}

if item.Table != nil {
fl := item.Table.FieldList
if fl.List == nil {
return true
}

if len(fl.List) == 1 {
return fl.isInline(c, p, w)
}

return false
}
}
Expand Down
28 changes: 23 additions & 5 deletions formatter/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package formatter

import "fmt"

// Parse code.
func parse(code []byte) (*document, error) {
var (
Expand All @@ -37,6 +39,8 @@ func parse(code []byte) (*document, error) {
currentBody = b
currentStatement = b

logging := true

for s.Next() {
el, err := s.Scan()
if err != nil {
Expand All @@ -45,18 +49,23 @@ func parse(code []byte) (*document, error) {

curElement = &el

// if prevElement != nil {
// fmt.Printf("%s%s %s%s = ", mMagenta, TokenIDs[prevElement.Token.Type], prevElement.Token.Value, defaultStyle)
// }
if logging {
if prevElement != nil {
fmt.Printf("%s%s %s%s = ", mMagenta, TokenIDs[prevElement.Token.Type], prevElement.Token.Value, defaultStyle)
}

// fmt.Printf("%s%s %s%s\n", mMagenta, TokenIDs[el.Token.Type], el.Token.Value, defaultStyle)
fmt.Printf("%s%s %s%s\n", mMagenta, TokenIDs[el.Token.Type], el.Token.Value, defaultStyle)
}

for isBlockEnd, ok := currentStatement.IsEnd(prevElement, curElement); ok; isBlockEnd, ok = currentStatement.IsEnd(prevElement, curElement) {
if currentStatement.TypeOf() == tsUnknow && curElement.Token.Type == nComma {
break
}

cs := chainSt.ExtractPrev()
if logging {
fmt.Printf("-- ex %p %#v\n", cs, cs)
}
if cs == nil {
currentBody = doc.Body
chainSt.Append(currentBody)
Expand All @@ -82,6 +91,9 @@ func parse(code []byte) (*document, error) {
}

if st := currentStatement.GetStatement(prevElement, curElement); st != nil {
if logging {
fmt.Printf("-- st %p %#v\n", st, st)
}
var assignmentWithOneVar statement

isPrefixexpConvertAssignment := false
Expand Down Expand Up @@ -112,6 +124,10 @@ func parse(code []byte) (*document, error) {

st.AppendStatement(extracted)
chainSt.Prev().AppendStatement(st)
} else if currentStatement.TypeOf() == tsFuncCallStatement && st.TypeOf() == tsPrefixexpStatement {
extracted := chainSt.ExctractStatement(tsFuncCallStatement)
bodyRemoveStatement(chainSt.GetLastBody(), extracted)
chainSt.GetLastBody().AppendStatement(st)
} else if currentStatement.TypeOf() == tsUnknow && st.TypeOf() == tsFuncCallStatement {
st.AppendStatement(chainSt.ExctractStatement(tsUnknow))
chainSt.Prev().AppendStatement(st)
Expand All @@ -133,7 +149,9 @@ func parse(code []byte) (*document, error) {
st.AppendStatement(inner)
chainSt.Append(inner)
// }

if logging {
fmt.Printf("-- in %p %#v\n", inner, inner)
}
st = inner

if isBreak {
Expand Down

0 comments on commit c34dd5c

Please sign in to comment.