diff --git a/format/document.go b/format/document.go index 59fff28..276121c 100644 --- a/format/document.go +++ b/format/document.go @@ -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) { diff --git a/format/document_test.go b/format/document_test.go index 5be9c99..28ab061 100644 --- a/format/document_test.go +++ b/format/document_test.go @@ -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) diff --git a/format/function.go b/format/function.go index 7920a80..2005bd0 100644 --- a/format/function.go +++ b/format/function.go @@ -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 { diff --git a/parser/ast.go b/parser/ast.go index 9b34417..1f640e8 100644 --- a/parser/ast.go +++ b/parser/ast.go @@ -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