Skip to content

Commit

Permalink
feat: support format options: indent and align
Browse files Browse the repository at this point in the history
  • Loading branch information
joyme123 committed Jan 31, 2024
1 parent bee9c15 commit 63550dc
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 11 additions & 3 deletions format/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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])
}
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 15 additions & 2 deletions format/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

var Indent = " "
var Align = "field"

type Options struct {
// Do not print reformatted sources to standard output.
Expand All @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions format/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions tests/e2e/fields/2spaces.assign.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/fields/2spaces.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/fields/4spaces.field.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/fields/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/fields/tab.assign.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 = "",
}
28 changes: 28 additions & 0 deletions tests/e2e/fields/test.sh
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions tests/e2e/run-e2e.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
for f in ./tests/e2e/*
do
if test -d "$f";then
bash "$f"/test.sh
fi
done

0 comments on commit 63550dc

Please sign in to comment.