Skip to content

Commit

Permalink
Merge pull request #47 from joyme123/fix-service-comma
Browse files Browse the repository at this point in the history
fix(format): fix format service comma settings
  • Loading branch information
joyme123 authored Oct 22, 2024
2 parents 54432fd + 59af872 commit d227317
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
2 changes: 1 addition & 1 deletion format/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func FormatDocumentWithValidation(doc *parser.Document, selfValidation bool) (st
psr := parser.PEGParser{}
formattedAst, err := psr.Parse("formated.thrift", []byte(res))
if err != nil {
return "", fmt.Errorf("format error: format result failed to parse. Please report bug to author at https://github.com/joyme123/thrift-ls/issues")
return "", fmt.Errorf("format error: format result failed to parse, error msg: %v. Please report bug to author at https://github.com/joyme123/thrift-ls/issues", err)
}

if !doc.Equals(formattedAst) {
Expand Down
64 changes: 64 additions & 0 deletions format/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,70 @@ import (
"github.com/stretchr/testify/assert"
)

func Test_FormatDocumentServiceCommaOptions(t *testing.T) {
doc := `
service A {
bool func1()
bool func2();
}`

// add comma
expectedDoc := `service A {
bool func1(),
bool func2(),
}`

FieldLineComma = FieldLineCommaAdd

ast, err := parser.Parse("test.thrift", []byte(doc))
assert.NoError(t, err)
assert.NotNil(t, ast)

formated, err := FormatDocument(ast.(*parser.Document))
assert.Equal(t, expectedDoc, formated)

_, err = FormatDocumentWithValidation(ast.(*parser.Document), true)
assert.NoError(t, err)

// remove comma
expectedDoc = `service A {
bool func1()
bool func2()
}`
FieldLineComma = FieldLineCommaRemove

ast, err = parser.Parse("test.thrift", []byte(doc))
assert.NoError(t, err)
assert.NotNil(t, ast)

formated, err = FormatDocument(ast.(*parser.Document))
assert.Equal(t, expectedDoc, formated)

_, err = FormatDocumentWithValidation(ast.(*parser.Document), true)
assert.NoError(t, err)

// disable
expectedDoc = `service A {
bool func1()
bool func2();
}`
FieldLineComma = FieldLineCommaDisable

ast, err = parser.Parse("test.thrift", []byte(doc))
assert.NoError(t, err)
assert.NotNil(t, ast)

formated, err = FormatDocument(ast.(*parser.Document))
assert.Equal(t, expectedDoc, formated)

_, err = FormatDocumentWithValidation(ast.(*parser.Document), true)
assert.NoError(t, err)
}

func Test_FormatDocument(t *testing.T) {
ast, err := parser.Parse("test.thrift", []byte(ThriftTestContent))
assert.NoError(t, err)
Expand Down
11 changes: 8 additions & 3 deletions format/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,14 @@ func MustFormatFunction(fn *parser.Function, indent string) string {
}

sep := ""
if fn.ListSeparatorKeyword != nil {
sep = MustFormatKeyword(fn.ListSeparatorKeyword.Keyword)
}

if FieldLineComma == FieldLineCommaAdd { // add comma always
sep = ","
} else if FieldLineComma == FieldLineCommaDisable { // add list separator
if fn.ListSeparatorKeyword != nil {
sep = MustFormatKeyword(fn.ListSeparatorKeyword.Keyword)
}
} // otherwise, sep will be removed

throws := MustFormatThrows(fn.Throws)
if fn.Throws != nil {
Expand Down
7 changes: 4 additions & 3 deletions parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2362,9 +2362,10 @@ func (f *Function) Equals(node Node) bool {
return false
}

if !f.ListSeparatorKeyword.Equals(fn.ListSeparatorKeyword) {
return false
}
// 格式化场景,会变更默认的分隔符,所以在 equals 时不需要比较
// if !f.ListSeparatorKeyword.Equals(fn.ListSeparatorKeyword) {
// return false
// }

if !f.Name.Equals(fn.Name) {
return false
Expand Down

0 comments on commit d227317

Please sign in to comment.