diff --git a/Makefile b/Makefile index 45f36dd..31cd21b 100644 --- a/Makefile +++ b/Makefile @@ -8,3 +8,6 @@ install-for-mason: test: @go test -gcflags=all=-l -gcflags=all=-d=checkptr=0 -race -coverpkg=./... -coverprofile=coverage.out $(shell go list ./...) @go tool cover -func coverage.out | tail -n 1 | awk '{ print "Total coverage: " $$3 }' + +e2e-test: + @bash tests/e2e/run-e2e.sh diff --git a/format/field.go b/format/field.go index fb7fab2..394d514 100644 --- a/format/field.go +++ b/format/field.go @@ -23,7 +23,11 @@ func MustFormatFields(fields []*parser.Field, indent string) string { fieldGroups = append(fieldGroups, fg) fg = make(fieldGroup, 0) } - fg = append(fg, MustFormatField(field, "\t", indent)) + space := " " + if Align == AlignTypeField { + space = "\t" + } + fg = append(fg, MustFormatField(field, space, indent)) fmtCtx.preNode = field } @@ -33,7 +37,7 @@ func MustFormatFields(fields []*parser.Field, indent string) string { for i, fg := range fieldGroups { w := new(tabwriter.Writer) - w.Init(buf, 1, 0, 1, ' ', 0) + w.Init(buf, 1, 8, 1, ' ', tabwriter.TabIndent) for j := range fg { fmt.Fprintln(w, fg[j]) } @@ -73,7 +77,11 @@ func MustFormatField(field *parser.Field, space string, indent string) string { value := "" if field.ConstValue != nil { - value = fmt.Sprintf("%s%s%s%s", space, MustFormatKeyword(field.EqualKeyword.Keyword), space, MustFormatConstValue(field.ConstValue, indent, false)) + equalSpace := space + if Align == AlignTypeAssign { + equalSpace = "\t" + } + value = fmt.Sprintf("%s%s%s%s", equalSpace, MustFormatKeyword(field.EqualKeyword.Keyword), equalSpace, MustFormatConstValue(field.ConstValue, indent, false)) } 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) diff --git a/format/options.go b/format/options.go index 2dd7377..ebad5ae 100644 --- a/format/options.go +++ b/format/options.go @@ -7,6 +7,7 @@ import ( ) var Indent = " " +var Align = "field" type Options struct { // Do not print reformatted sources to standard output. @@ -21,17 +22,29 @@ type Options struct { // Do not print reformatted sources to standard output. // If a file's formatting is different than gofmt's, print diffs // to standard output. - Diff bool `yaml:"Diff"` + Diff bool `yaml:"diff"` + + // Align enables align option for struct/enum/exception/union fields + // Options: "field", "assign", "disable" + // Default is "field" if not set + Align string `yaml:"alignByAssign"` } 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.`) } -func (o *Options) InitDefaultIndent() { +func (o *Options) InitDefault() { Indent = o.GetIndent() + + if o.Align == "" || (o.Align != AlignTypeField && o.Align != AlignTypeAssign && o.Align != AlignTypeDisable) { + o.Align = "field" + } + + Align = o.Align } func (o *Options) GetIndent() string { diff --git a/format/utils.go b/format/utils.go index b830b85..7df2a0e 100644 --- a/format/utils.go +++ b/format/utils.go @@ -9,6 +9,12 @@ import ( "github.com/joyme123/thrift-ls/parser" ) +const ( + AlignTypeAssign = "assign" + AlignTypeField = "field" + AlignTypeDisable = "disable" +) + func MustFormat(tplText string, formatter any) string { tpl, err := template.New("default").Parse(tplText) if err != nil { diff --git a/main.go b/main.go index d4c2897..37adc17 100644 --- a/main.go +++ b/main.go @@ -27,8 +27,6 @@ type Options struct { } func main_format(opt format.Options, file string) error { - opt.InitDefaultIndent() - if file == "" { err := errors.New("must specified a thrift file to format") fmt.Println(err) @@ -87,6 +85,8 @@ func main() { flag.StringVar(&formatFile, "f", "", "file path to format") formatOpts := format.Options{} formatOpts.SetFlags() + flag.Parse() + formatOpts.InitDefault() opts := configInit() tlog.Init(opts.LogLevel) diff --git a/tests/e2e/fields/2spaces.assign.expect b/tests/e2e/fields/2spaces.assign.expect new file mode 100644 index 0000000..25d226e --- /dev/null +++ b/tests/e2e/fields/2spaces.assign.expect @@ -0,0 +1,5 @@ +struct User { + 1: required string Name, + 2: required i32 Age = 15, + 3: optional string longlonglonglonglonglonglong_info = "", +} diff --git a/tests/e2e/fields/2spaces.disable.expect b/tests/e2e/fields/2spaces.disable.expect new file mode 100644 index 0000000..66ef7f6 --- /dev/null +++ b/tests/e2e/fields/2spaces.disable.expect @@ -0,0 +1,5 @@ +struct User { + 1: required string Name, + 2: required i32 Age = 15, + 3: optional string longlonglonglonglonglonglong_info = "", +} diff --git a/tests/e2e/fields/4spaces.field.expect b/tests/e2e/fields/4spaces.field.expect new file mode 100644 index 0000000..0adaab8 --- /dev/null +++ b/tests/e2e/fields/4spaces.field.expect @@ -0,0 +1,5 @@ +struct User { + 1: required string Name, + 2: required i32 Age = 15, + 3: optional string longlonglonglonglonglonglong_info = "", +} diff --git a/tests/e2e/fields/fields.thrift b/tests/e2e/fields/fields.thrift new file mode 100644 index 0000000..b90f2c9 --- /dev/null +++ b/tests/e2e/fields/fields.thrift @@ -0,0 +1,5 @@ +struct User { + 1: required string Name, + 2: required i32 Age = 15, + 3: optional string longlonglonglonglonglonglong_info = "", +} diff --git a/tests/e2e/fields/tab.assign.expect b/tests/e2e/fields/tab.assign.expect new file mode 100644 index 0000000..c6daa44 --- /dev/null +++ b/tests/e2e/fields/tab.assign.expect @@ -0,0 +1,5 @@ +struct User { + 1: required string Name, + 2: required i32 Age = 15, + 3: optional string longlonglonglonglonglonglong_info = "", +} diff --git a/tests/e2e/fields/test.sh b/tests/e2e/fields/test.sh new file mode 100644 index 0000000..b4fd200 --- /dev/null +++ b/tests/e2e/fields/test.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +echo "run e2e test case: fields" + +IFS="." +for f in ./tests/e2e/fields/*.expect +do + if test -f "$f"; then + echo "======================================================" + substr=${f##*/} + read -ra options <<<"$substr" + indent=${options[0]} + align=${options[1]} + echo "indent: ${indent}, align: ${align}" + got=$(./bin/thriftls -format -indent "${indent}" -align "${align}" -f tests/e2e/fields/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 diff --git a/tests/e2e/run-e2e.sh b/tests/e2e/run-e2e.sh new file mode 100644 index 0000000..dee808a --- /dev/null +++ b/tests/e2e/run-e2e.sh @@ -0,0 +1,6 @@ +for f in ./tests/e2e/* +do + if test -d "$f";then + bash "$f"/test.sh + fi +done