Skip to content

Commit

Permalink
Merge pull request #34 from joyme123/feat-format-comma
Browse files Browse the repository at this point in the history
  • Loading branch information
Pengfei Jiang authored Feb 5, 2024
2 parents 96cd1bc + 7d17b59 commit 769f0a9
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 4 deletions.
7 changes: 6 additions & 1 deletion format/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ func MustFormatField(field *parser.Field, space string, indent string) string {
str := fmt.Sprintf("%s%d:%s%s%s%s%s%s", indent, field.Index.Value, space, required, MustFormatFieldType(field.FieldType), space, field.Identifier.Name.Text, value)
buf.WriteString(str)
buf.WriteString(annos)
buf.WriteString(formatListSeparator(field.ListSeparatorKeyword))
if FieldLineComma == FieldLineCommaAdd {
buf.WriteString(",")
} else if FieldLineComma == FieldLineCommaDisable {
buf.WriteString(formatListSeparator(field.ListSeparatorKeyword))
}

if len(field.EndLineComments) > 0 {
buf.WriteString(MustFormatEndLineComments(field.EndLineComments, ""))
}
Expand Down
17 changes: 17 additions & 0 deletions format/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

var Indent = " "
var Align = "field"
var FieldLineComma = "disable"

type Options struct {
// Do not print reformatted sources to standard output.
Expand All @@ -28,13 +29,20 @@ type Options struct {
// Options: "field", "assign", "disable"
// Default is "field" if not set
Align string `yaml:"alignByAssign"`

// FieldLineComma represents whether to add or remove comma at the end of field line.
// Options: "add", "remove", "disable"
// if choose disable, user input will be retained without modification
// Default is "disable" if not set
FieldLineComma string `yaml:"fieldLineComma"`
}

func (o *Options) SetFlags() {
flag.BoolVar(&o.Write, "w", false, "Do not print reformatted sources to standard output. If a file's formatting is different from thriftls's, overwrite it with thrfitls's version.")
flag.BoolVar(&o.Diff, "d", false, "Do not print reformatted sources to standard output. If a file's formatting is different than gofmt's, print diffs to standard output.")
flag.StringVar(&o.Indent, "indent", "4spaces", "Indent to use. Support: num*space, num*tab. example: 4spaces, 1tab, tab")
flag.StringVar(&o.Align, "align", "field", `Align enables align option for struct/enum/exception/union fields, Options: "field", "assign", "disable", Default is "field" if not set.`)
flag.StringVar(&o.FieldLineComma, "fieldLineComma", "disable", `FieldLineComma enables whether to add or remove comma at end of field line. Options: "add", "remove", "disable". If choose disable, user input will be retained without modification. Default is "disable" if not set`)
}

func (o *Options) InitDefault() {
Expand All @@ -45,6 +53,15 @@ func (o *Options) InitDefault() {
}

Align = o.Align

if o.FieldLineComma == "" ||
(o.FieldLineComma != FieldLineCommaAdd &&
o.FieldLineComma != FieldLineCommaRemove &&
o.FieldLineComma != FieldLineCommaDisable) {
o.FieldLineComma = "disable"
}

FieldLineComma = o.FieldLineComma
}

func (o *Options) GetIndent() string {
Expand Down
6 changes: 6 additions & 0 deletions format/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ const (
AlignTypeDisable = "disable"
)

const (
FieldLineCommaAdd = "add"
FieldLineCommaRemove = "remove"
FieldLineCommaDisable = "disable"
)

func MustFormat(tplText string, formatter any) string {
tpl, err := template.New("default").Parse(tplText)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func main_format(opt format.Options, file string) error {
return err
}
formated, err := format.FormatDocumentWithValidation(ast.(*parser.Document), true)
if err != nil {
fmt.Println(err)
return err
}

if opt.Write {
var perms os.FileMode
Expand Down
7 changes: 4 additions & 3 deletions parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -3094,9 +3094,10 @@ func (f *Field) Equals(node Node) bool {
return false
}

if !f.ListSeparatorKeyword.Equals(fn.ListSeparatorKeyword) {
return false
}
// 末尾的 , 不影响语义,暂时注释掉
// if !f.ListSeparatorKeyword.Equals(fn.ListSeparatorKeyword) {
// return false
// }

if len(f.Comments) != len(fn.Comments) {
return false
Expand Down
5 changes: 5 additions & 0 deletions tests/e2e/field_line_comma/add.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
struct User {
1: required string Name,
2: required i32 Age = 15,
3: optional string longlonglonglonglonglonglong_info = "",
}
5 changes: 5 additions & 0 deletions tests/e2e/field_line_comma/disable.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
struct User {
1: required string Name,
2: required i32 Age = 15
3: optional string longlonglonglonglonglonglong_info = ""
}
5 changes: 5 additions & 0 deletions tests/e2e/field_line_comma/fields.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
struct User {
1: required string Name,
2: required i32 Age = 15
3: optional string longlonglonglonglonglonglong_info = ""
}
5 changes: 5 additions & 0 deletions tests/e2e/field_line_comma/remove.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
struct User {
1: required string Name
2: required i32 Age = 15
3: optional string longlonglonglonglonglonglong_info = ""
}
27 changes: 27 additions & 0 deletions tests/e2e/field_line_comma/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

echo "run e2e test case: field line comma"

IFS="."
for f in ./tests/e2e/field_line_comma/*.expect
do
if test -f "$f"; then
echo "======================================================"
substr=${f##*/}
read -ra options <<<"$substr"
field_line_comma=${options[0]}
echo "fieldLineComma: ${field_line_comma}"
got=$(./bin/thriftls -format -fieldLineComma "${field_line_comma}" -f tests/e2e/field_line_comma/fields.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 769f0a9

Please sign in to comment.