Skip to content

Commit

Permalink
fix: fix map format
Browse files Browse the repository at this point in the history
  • Loading branch information
joyme123 committed Nov 24, 2023
1 parent 920ca9d commit 0cb9bc3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
14 changes: 12 additions & 2 deletions format/constvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,19 @@ func MustFormatConstValue(cv *parser.ConstValue, indent string) string {
}
}

buf.WriteString(MustFormatKeyword(cv.LCurKeyword.Keyword) + "\n")
var preNode parser.Node
buf.WriteString(MustFormatKeyword(cv.LCurKeyword.Keyword))
preNode = cv.LCurKeyword
for i := range values {
distance := lineDistance(preNode, values[i])
if distance >= 1 {
buf.WriteString("\n")
}
buf.WriteString(MustFormatConstValue(values[i], ""))
preNode = values[i]
}
if lineDistance(cv.RCurKeyword, preNode) >= 1 {
buf.WriteString("\n")
}
buf.WriteString(MustFormatKeyword(cv.RCurKeyword.Keyword))
case "pair":
Expand All @@ -59,7 +69,7 @@ func MustFormatConstValue(cv *parser.ConstValue, indent string) string {
if cv.ListSeparatorKeyword != nil {
sep = MustFormatKeyword(cv.ListSeparatorKeyword.Keyword)
}
buf.WriteString(fmt.Sprintf("%s%s %s%s\n", MustFormatConstValue(key, indent+Indent), MustFormatKeyword(cv.ColonKeyword.Keyword), MustFormatConstValue(value, ""), sep))
buf.WriteString(fmt.Sprintf("%s%s %s%s", MustFormatConstValue(key, indent+Indent), MustFormatKeyword(cv.ColonKeyword.Keyword), MustFormatConstValue(value, ""), sep))
case "identifier":
if len(cv.Comments) > 0 {
// special case for iline distance
Expand Down
4 changes: 2 additions & 2 deletions format/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ 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 to report bug at https://github.com/joyme123/thrift-ls/issues")
return "", fmt.Errorf("format error: format result failed to parse. Please report bug to author at https://github.com/joyme123/thrift-ls/issues")
}

if !doc.Equals(formattedAst) {
return "", fmt.Errorf("format error: format result failed to pass self validation. Please to report bug at https://github.com/joyme123/thrift-ls/issues")
return "", fmt.Errorf("format error: format result failed to pass self validation. Please report bug to author at https://github.com/joyme123/thrift-ls/issues")
}
}

Expand Down
25 changes: 21 additions & 4 deletions format/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ var expectedFormated = `/*
* details.
*/
include "a.thrift"
include "b.thrift"
include "a.thrift"
include "b.thrift"
cpp_include "aaaaa"
cpp_include "bbbbb"
cpp_include "aaaaa"
cpp_include "bbbbb"
namespace c_glib TTest
namespace cpp thrift.test
Expand Down Expand Up @@ -456,6 +456,14 @@ struct OptionalBinary {
2: optional map<binary,i32> bin_map = {}
}
struct OptionalMap {
1: optional map<string, i32> str_map = {/* comment 1 */ "text": 1, "text2": 2}
2: optional map<string, string> str_map2 = {
// comments
"text": "text",
"text2": "text2", "text3": "text3"
}
}
// comments at end of doc`

Expand Down Expand Up @@ -912,5 +920,14 @@ struct OptionalBinary {
2: optional map<binary,i32> bin_map = {}
}
struct OptionalMap {
1: optional map<string, i32> str_map = {/* comment 1 */ "text": 1, "text2": 2}
2: optional map<string, string> str_map2 = {
// comments
"text": "text",
"text2": "text2", "text3": "text3"
}
}
// comments at end of doc
`

0 comments on commit 0cb9bc3

Please sign in to comment.