Skip to content

Commit

Permalink
feat: format for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
joyme123 committed Feb 6, 2024
1 parent 3520231 commit 3639f6a
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 14 deletions.
3 changes: 1 addition & 2 deletions format/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const (
enumOneLineTpl = `{{.Comments}}{{.Enum}} {{.Identifier}} {{.LCUR}}{{.RCUR}}{{.Annotations}}{{.EndLineComments}}`

enumMultiLineTpl = `{{.Comments}}{{.Enum}} {{.Identifier}} {{.LCUR}}
{{.EnumValues}}
{{.RCUR}}{{.Annotations}}{{.EndLineComments}}
{{.EnumValues}}{{.RCUR}}{{.Annotations}}{{.EndLineComments}}
`
)

Expand Down
50 changes: 41 additions & 9 deletions format/enumvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,55 @@ package format
import (
"bytes"
"fmt"
"text/tabwriter"

"github.com/joyme123/thrift-ls/parser"
)

type enumValueGroup []string

func MustFormatEnumValues(values []*parser.EnumValue, indent string) string {
buf := bytes.NewBuffer(nil)

fmtCtx := &fmtContext{}

var enumValueGroups []enumValueGroup
var eg enumValueGroup

for i, v := range values {
if needAddtionalLineForEnumValues(fmtCtx.preNode, values[i]) {
buf.WriteString("\n")
enumValueGroups = append(enumValueGroups, eg)
eg = make(enumValueGroup, 0)
}
buf.WriteString(MustFormatEnumValue(v, indent))
if i < len(values)-1 {
buf.WriteString("\n")
space := " "
if Align == AlignTypeField {
space = "\t"
}
eg = append(eg, MustFormatEnumValue(v, space, indent))
fmtCtx.preNode = values[i]
}

if len(eg) > 0 {
enumValueGroups = append(enumValueGroups, eg)
}

for i, eg := range enumValueGroups {
w := new(tabwriter.Writer)
w.Init(buf, 1, 8, 1, ' ', tabwriter.TabIndent)
for j := range eg {
fmt.Fprintln(w, eg[j])
}
w.Flush()

if i < len(enumValueGroups)-1 {
buf.WriteString("\n")
}
}

return buf.String()
}

// TODO(jpf): comments
func MustFormatEnumValue(enumValue *parser.EnumValue, indent string) string {
func MustFormatEnumValue(enumValue *parser.EnumValue, space, indent string) string {
comments, annos := formatCommentsAndAnnos(enumValue.Comments, enumValue.Annotations, indent)

if len(comments) > 0 && lineDistance(enumValue.Comments[len(enumValue.Comments)-1], enumValue.Name) > 1 {
Expand All @@ -37,13 +61,21 @@ func MustFormatEnumValue(enumValue *parser.EnumValue, indent string) string {
buf := bytes.NewBufferString(comments)
buf.WriteString(indent + MustFormatIdentifier(enumValue.Name))
if enumValue.ValueNode != nil {
buf.WriteString(fmt.Sprintf(" %s %s", MustFormatKeyword(enumValue.EqualKeyword.Keyword), MustFormatConstValue(enumValue.ValueNode, indent, false)))
equalSpace := space
if Align == AlignTypeAssign {
equalSpace = "\t"
}
buf.WriteString(fmt.Sprintf("%s%s%s%s", equalSpace, MustFormatKeyword(enumValue.EqualKeyword.Keyword), equalSpace, MustFormatConstValue(enumValue.ValueNode, indent, false)))
}

buf.WriteString(annos)

if enumValue.ListSeparatorKeyword != nil {
buf.WriteString(MustFormatKeyword(enumValue.ListSeparatorKeyword.Keyword))
if FieldLineComma == FieldLineCommaAdd {
buf.WriteString(",")
} else if FieldLineComma == FieldLineCommaDisable {
if enumValue.ListSeparatorKeyword != nil {
buf.WriteString(MustFormatKeyword(enumValue.ListSeparatorKeyword.Keyword))
}
}

buf.WriteString(MustFormatEndLineComments(enumValue.EndLineComments, ""))
Expand Down
7 changes: 4 additions & 3 deletions parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1752,9 +1752,10 @@ func (e *EnumValue) Equals(node Node) bool {
return true
}

if !e.ListSeparatorKeyword.Equals(en.ListSeparatorKeyword) {
return false
}
// comma 不影响语义
// if !e.ListSeparatorKeyword.Equals(en.ListSeparatorKeyword) {
// return false
// }

if !e.EqualKeyword.Equals(en.EqualKeyword) {
return false
Expand Down
8 changes: 8 additions & 0 deletions tests/e2e/enums/2spaces.assign.add.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
enum UserType {
PHONE = 1,
EMMAIL = 211,
INVITATION = 300000,

OTHER1 = 2,
ANOTHER2 = 33,
}
8 changes: 8 additions & 0 deletions tests/e2e/enums/2spaces.disable.remove.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
enum UserType {
PHONE = 1
EMMAIL = 211
INVITATION = 300000

OTHER1 = 2
ANOTHER2 = 33
}
8 changes: 8 additions & 0 deletions tests/e2e/enums/4spaces.field.disable.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
enum UserType {
PHONE = 1
EMMAIL = 211,
INVITATION = 300000

OTHER1 = 2
ANOTHER2 = 33
}
8 changes: 8 additions & 0 deletions tests/e2e/enums/enums.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
enum UserType {
PHONE = 1
EMMAIL = 211,
INVITATION = 300000

OTHER1 = 2
ANOTHER2 = 33
}
8 changes: 8 additions & 0 deletions tests/e2e/enums/tab.assign.disable.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
enum UserType {
PHONE = 1
EMMAIL = 211,
INVITATION = 300000

OTHER1 = 2
ANOTHER2 = 33
}
29 changes: 29 additions & 0 deletions tests/e2e/enums/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

echo "run e2e test case: enums"

IFS="."
for f in ./tests/e2e/enums/*.expect
do
if test -f "$f"; then
echo "======================================================"
substr=${f##*/}
read -ra options <<<"$substr"
indent=${options[0]}
align=${options[1]}
field_line_comma=${options[2]}
echo "indent: ${indent}, align: ${align}, field_line_comma: ${field_line_comma}"
got=$(./bin/thriftls -format -indent "${indent}" -align "${align}" -fieldLineComma "${field_line_comma}" -f tests/e2e/enums/enums.thrift)
expected=$(cat "$f")
if [ "$got" == "$expected" ];then
echo "pass"
else :
echo "failed"
printf 'got: \n%s\n' "${got}"
printf 'expected: \n%s\n' "${expected}"
diff <(echo "$got") <(echo "$expected")
fi
echo "======================================================"
fi

done

0 comments on commit 3639f6a

Please sign in to comment.