From 3639f6ad9236d294f7a79e364ee1a8a9f273c960 Mon Sep 17 00:00:00 2001 From: jiangpengfei Date: Tue, 6 Feb 2024 10:01:16 +0800 Subject: [PATCH] feat: format for enums --- format/enum.go | 3 +- format/enumvalue.go | 50 +++++++++++++++---- parser/ast.go | 7 +-- tests/e2e/enums/2spaces.assign.add.expect | 8 +++ tests/e2e/enums/2spaces.disable.remove.expect | 8 +++ tests/e2e/enums/4spaces.field.disable.expect | 8 +++ tests/e2e/enums/enums.thrift | 8 +++ tests/e2e/enums/tab.assign.disable.expect | 8 +++ tests/e2e/enums/test.sh | 29 +++++++++++ 9 files changed, 115 insertions(+), 14 deletions(-) create mode 100644 tests/e2e/enums/2spaces.assign.add.expect create mode 100644 tests/e2e/enums/2spaces.disable.remove.expect create mode 100644 tests/e2e/enums/4spaces.field.disable.expect create mode 100644 tests/e2e/enums/enums.thrift create mode 100644 tests/e2e/enums/tab.assign.disable.expect create mode 100755 tests/e2e/enums/test.sh diff --git a/format/enum.go b/format/enum.go index 82e9eeb..f9f324c 100644 --- a/format/enum.go +++ b/format/enum.go @@ -8,8 +8,7 @@ const ( enumOneLineTpl = `{{.Comments}}{{.Enum}} {{.Identifier}} {{.LCUR}}{{.RCUR}}{{.Annotations}}{{.EndLineComments}}` enumMultiLineTpl = `{{.Comments}}{{.Enum}} {{.Identifier}} {{.LCUR}} -{{.EnumValues}} -{{.RCUR}}{{.Annotations}}{{.EndLineComments}} +{{.EnumValues}}{{.RCUR}}{{.Annotations}}{{.EndLineComments}} ` ) diff --git a/format/enumvalue.go b/format/enumvalue.go index e7cf83e..a39caeb 100644 --- a/format/enumvalue.go +++ b/format/enumvalue.go @@ -3,31 +3,55 @@ package format import ( "bytes" "fmt" + "text/tabwriter" "github.com/joyme123/thrift-ls/parser" ) +type enumValueGroup []string + func MustFormatEnumValues(values []*parser.EnumValue, indent string) string { buf := bytes.NewBuffer(nil) fmtCtx := &fmtContext{} + var enumValueGroups []enumValueGroup + var eg enumValueGroup + for i, v := range values { if needAddtionalLineForEnumValues(fmtCtx.preNode, values[i]) { - buf.WriteString("\n") + enumValueGroups = append(enumValueGroups, eg) + eg = make(enumValueGroup, 0) } - buf.WriteString(MustFormatEnumValue(v, indent)) - if i < len(values)-1 { - buf.WriteString("\n") + space := " " + if Align == AlignTypeField { + space = "\t" } + eg = append(eg, MustFormatEnumValue(v, space, indent)) fmtCtx.preNode = values[i] } + if len(eg) > 0 { + enumValueGroups = append(enumValueGroups, eg) + } + + for i, eg := range enumValueGroups { + w := new(tabwriter.Writer) + w.Init(buf, 1, 8, 1, ' ', tabwriter.TabIndent) + for j := range eg { + fmt.Fprintln(w, eg[j]) + } + w.Flush() + + if i < len(enumValueGroups)-1 { + buf.WriteString("\n") + } + } + return buf.String() } -// TODO(jpf): comments -func MustFormatEnumValue(enumValue *parser.EnumValue, indent string) string { +func MustFormatEnumValue(enumValue *parser.EnumValue, space, indent string) string { comments, annos := formatCommentsAndAnnos(enumValue.Comments, enumValue.Annotations, indent) if len(comments) > 0 && lineDistance(enumValue.Comments[len(enumValue.Comments)-1], enumValue.Name) > 1 { @@ -37,13 +61,21 @@ func MustFormatEnumValue(enumValue *parser.EnumValue, indent string) string { buf := bytes.NewBufferString(comments) buf.WriteString(indent + MustFormatIdentifier(enumValue.Name)) if enumValue.ValueNode != nil { - buf.WriteString(fmt.Sprintf(" %s %s", MustFormatKeyword(enumValue.EqualKeyword.Keyword), MustFormatConstValue(enumValue.ValueNode, indent, false))) + equalSpace := space + if Align == AlignTypeAssign { + equalSpace = "\t" + } + buf.WriteString(fmt.Sprintf("%s%s%s%s", equalSpace, MustFormatKeyword(enumValue.EqualKeyword.Keyword), equalSpace, MustFormatConstValue(enumValue.ValueNode, indent, false))) } buf.WriteString(annos) - if enumValue.ListSeparatorKeyword != nil { - buf.WriteString(MustFormatKeyword(enumValue.ListSeparatorKeyword.Keyword)) + if FieldLineComma == FieldLineCommaAdd { + buf.WriteString(",") + } else if FieldLineComma == FieldLineCommaDisable { + if enumValue.ListSeparatorKeyword != nil { + buf.WriteString(MustFormatKeyword(enumValue.ListSeparatorKeyword.Keyword)) + } } buf.WriteString(MustFormatEndLineComments(enumValue.EndLineComments, "")) diff --git a/parser/ast.go b/parser/ast.go index 131fb5f..76cbcae 100644 --- a/parser/ast.go +++ b/parser/ast.go @@ -1752,9 +1752,10 @@ func (e *EnumValue) Equals(node Node) bool { return true } - if !e.ListSeparatorKeyword.Equals(en.ListSeparatorKeyword) { - return false - } + // comma 不影响语义 + // if !e.ListSeparatorKeyword.Equals(en.ListSeparatorKeyword) { + // return false + // } if !e.EqualKeyword.Equals(en.EqualKeyword) { return false diff --git a/tests/e2e/enums/2spaces.assign.add.expect b/tests/e2e/enums/2spaces.assign.add.expect new file mode 100644 index 0000000..e253d5a --- /dev/null +++ b/tests/e2e/enums/2spaces.assign.add.expect @@ -0,0 +1,8 @@ +enum UserType { + PHONE = 1, + EMMAIL = 211, + INVITATION = 300000, + + OTHER1 = 2, + ANOTHER2 = 33, +} diff --git a/tests/e2e/enums/2spaces.disable.remove.expect b/tests/e2e/enums/2spaces.disable.remove.expect new file mode 100644 index 0000000..14f02f5 --- /dev/null +++ b/tests/e2e/enums/2spaces.disable.remove.expect @@ -0,0 +1,8 @@ +enum UserType { + PHONE = 1 + EMMAIL = 211 + INVITATION = 300000 + + OTHER1 = 2 + ANOTHER2 = 33 +} diff --git a/tests/e2e/enums/4spaces.field.disable.expect b/tests/e2e/enums/4spaces.field.disable.expect new file mode 100644 index 0000000..4214f69 --- /dev/null +++ b/tests/e2e/enums/4spaces.field.disable.expect @@ -0,0 +1,8 @@ +enum UserType { + PHONE = 1 + EMMAIL = 211, + INVITATION = 300000 + + OTHER1 = 2 + ANOTHER2 = 33 +} diff --git a/tests/e2e/enums/enums.thrift b/tests/e2e/enums/enums.thrift new file mode 100644 index 0000000..2884a89 --- /dev/null +++ b/tests/e2e/enums/enums.thrift @@ -0,0 +1,8 @@ +enum UserType { + PHONE = 1 + EMMAIL = 211, + INVITATION = 300000 + + OTHER1 = 2 + ANOTHER2 = 33 +} diff --git a/tests/e2e/enums/tab.assign.disable.expect b/tests/e2e/enums/tab.assign.disable.expect new file mode 100644 index 0000000..6e3ac7f --- /dev/null +++ b/tests/e2e/enums/tab.assign.disable.expect @@ -0,0 +1,8 @@ +enum UserType { + PHONE = 1 + EMMAIL = 211, + INVITATION = 300000 + + OTHER1 = 2 + ANOTHER2 = 33 +} diff --git a/tests/e2e/enums/test.sh b/tests/e2e/enums/test.sh new file mode 100755 index 0000000..b3a244d --- /dev/null +++ b/tests/e2e/enums/test.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +echo "run e2e test case: enums" + +IFS="." +for f in ./tests/e2e/enums/*.expect +do + if test -f "$f"; then + echo "======================================================" + substr=${f##*/} + read -ra options <<<"$substr" + indent=${options[0]} + align=${options[1]} + field_line_comma=${options[2]} + echo "indent: ${indent}, align: ${align}, field_line_comma: ${field_line_comma}" + got=$(./bin/thriftls -format -indent "${indent}" -align "${align}" -fieldLineComma "${field_line_comma}" -f tests/e2e/enums/enums.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