From 8471b3e683a46acdd038a529656d71093963b315 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 2 Apr 2025 23:26:49 -0500 Subject: [PATCH] more complete format() functions --- Makefile | 4 ++ example/Makefile | 3 ++ protoReformat.go | 135 ++++++++++++++++++++++++++++++----------------- 3 files changed, 95 insertions(+), 47 deletions(-) diff --git a/Makefile b/Makefile index 0a8a695..d7b93d1 100644 --- a/Makefile +++ b/Makefile @@ -118,3 +118,7 @@ reformat-signal.proto-full-debug: goimports vet build reformat-signal.proto-fruit: goimports vet build git checkout example/*.proto make -C example proto-reformat-fruit + +reformat-signal.proto-fruit-debug: goimports vet build + git checkout example/*.proto + make -C example proto-reformat-fruit-debug diff --git a/example/Makefile b/example/Makefile index 0f4afa5..95bb274 100644 --- a/example/Makefile +++ b/example/Makefile @@ -117,3 +117,6 @@ proto-reformat-full-debug: proto-reformat-fruit: ../autogenpb --proto fruit.proto --format + +proto-reformat-fruit-debug: + ../autogenpb --proto fruit.proto --format --debug diff --git a/protoReformat.go b/protoReformat.go index bafdea6..ed69d20 100644 --- a/protoReformat.go +++ b/protoReformat.go @@ -312,51 +312,59 @@ func formatEnum(curmsg *FormatMsg) []string { return newmsg } -func (all *FormatMsg) format() []string { +// set all children to have the same max sizes +func (parent *FormatMsg) formatStandardSizes() { var bigType int64 var bigName int64 // find the biggest var names and var types - for _, msg := range all.Msgs { - switch msg.Type { + for _, child := range parent.Msgs { + switch child.Type { case FormatMsg_ENUM: case FormatMsg_MESSAGE: // find the max length of varname and vartype - setMaxSizes(msg) - if bigType < msg.MaxVartype { - bigType = msg.MaxVartype + setMaxSizes(child) + if bigType < child.MaxVartype { + bigType = child.MaxVartype } - if bigName < msg.MaxVarname { - bigName = msg.MaxVarname + if bigName < child.MaxVarname { + bigName = child.MaxVarname } default: } } // set this size in each message - for _, msg := range all.Msgs { - switch msg.Type { + for _, child := range parent.Msgs { + switch child.Type { case FormatMsg_ENUM: case FormatMsg_MESSAGE: - msg.MaxVartype = bigType - msg.MaxVarname = bigName + child.MaxVartype = bigType + child.MaxVarname = bigName default: } } +} - switch all.Type { +func (parent *FormatMsg) format() []string { + parent.formatStandardSizes() + + switch parent.Type { case FormatMsg_ENUM: - return formatEnum(all) + return formatEnum(parent) + case FormatMsg_ONEOF: + return formatEnum(parent) case FormatMsg_MESSAGE: - return formatMessage(all) + return formatMessage(parent) + default: + return formatMessage(parent) } - return formatMessage(all) } func (msg *FormatMsg) formatLineBase(line string, dbg string) string { line = strings.TrimSpace(line) if argv.Debug { - return fmt.Sprintf("/*a*/ %s%s // %s depth=%d", msg.padBase(), line, dbg, msg.Depth) + return fmt.Sprintf("/*a*/%s/*b*/%s // %s depth=%d", msg.padBase(), line, dbg, msg.Depth) } return fmt.Sprintf("%s%s", msg.padBase(), line) } @@ -364,9 +372,40 @@ func (msg *FormatMsg) formatLineBase(line string, dbg string) string { func (msg *FormatMsg) formatLine(line string, dbg string) string { line = strings.TrimSpace(line) if argv.Debug { - return fmt.Sprintf("/*a*/ %s%s // %s depth=%d", msg.pad(), line, dbg, msg.Depth) + return fmt.Sprintf("/*a*/%s/*b*/%s // %s depth=%d", msg.pad(), line, dbg, msg.Depth) } - return fmt.Sprintf("%s%s", msg.padBase(), line) + return fmt.Sprintf("%s%s", msg.pad(), line) +} + +func (msg *FormatMsg) formatComment(line string, dbg string) string { + line = strings.TrimSpace(line) + pad := fmt.Sprintf("%d", msg.MaxVartype+msg.MaxVarname+13) // 21 is correct? + hmm := "%" + pad + "s %s" + comment := fmt.Sprintf(hmm, " ", line) // todo: compute 50 + if argv.Debug { + return fmt.Sprintf("/*a*/%s/*b*/%s // %s depth=%d", msg.pad(), comment, dbg, msg.Depth) + } + return fmt.Sprintf("%s%s", msg.pad(), comment) +} + +func (msg *FormatMsg) formatVarLine(line string, dbg string) string { + line = strings.TrimSpace(line) + mt := fmt.Sprintf("%d", msg.MaxVartype) + mv := fmt.Sprintf("%d", msg.MaxVarname) + + hmm := "%-" + mt + "s %-" + mv + "s = %-3s %s" + + vartype, varname, id, end := tokenMsgVar(line) + end = strings.TrimSpace(end) + id = id + ";" + + newline := fmt.Sprintf(hmm, vartype, varname, id, end) + newline = strings.TrimRight(newline, " ") + + if argv.Debug { + return fmt.Sprintf("/*a*/%s/*b*/%s // %s depth=%d", msg.pad(), newline, dbg, msg.Depth) + } + return fmt.Sprintf("%s%s", msg.pad(), newline) } func trimLines(lines []string) []string { @@ -415,15 +454,10 @@ func formatMessage(curmsg *FormatMsg) []string { case FormatMsg_ENUM: for _, line := range formatEnum(msg) { newmsg = append(newmsg, line) - /* - // line = fmt.Sprintf("%s%s", curmsg.pad(), line) - line = strings.TrimSpace(line) - if argv.Debug { - line = fmt.Sprintf("%s%s // msg depth=%d", msg.padBase(), line, msg.Depth) - } else { - line = fmt.Sprintf("%s%s", msg.padBase(), line) - } - */ + } + case FormatMsg_ONEOF: + for _, line := range formatEnum(msg) { + newmsg = append(newmsg, line) } case FormatMsg_MESSAGE: for _, line := range msg.format() { @@ -438,33 +472,40 @@ func formatMessage(curmsg *FormatMsg) []string { line = strings.TrimSpace(line) if line == "" { // newmsg = append(newmsg, line) - newmsg = append(newmsg, curmsg.formatLine(line, "lines")) + newmsg = append(newmsg, "\n") continue } if strings.HasPrefix(line, "//") { - pad := fmt.Sprintf("%d", curmsg.MaxVartype+curmsg.MaxVarname+21) - hmm := "%" + pad + "s %s" - line = fmt.Sprintf(hmm, " ", line) // todo: compute 50 - newmsg = append(newmsg, line) + /* + pad := fmt.Sprintf("%d", curmsg.MaxVartype+curmsg.MaxVarname+21) + hmm := "%" + pad + "s %s" + line = fmt.Sprintf(hmm, " ", line) // todo: compute 50 + newmsg = append(newmsg, line) + */ + newmsg = append(newmsg, curmsg.formatComment(line, "comment")) continue } - mt := fmt.Sprintf("%d", curmsg.MaxVartype) - mv := fmt.Sprintf("%d", curmsg.MaxVarname) + newmsg = append(newmsg, curmsg.formatVarLine(line, "var")) + continue + /* + mt := fmt.Sprintf("%d", curmsg.MaxVartype) + mv := fmt.Sprintf("%d", curmsg.MaxVarname) - hmm := "%s%-" + mt + "s %-" + mv + "s = %-3s %s" + hmm := "%s%-" + mt + "s %-" + mv + "s = %-3s %s" - vartype, varname, id, end := tokenMsgVar(line) - end = strings.TrimSpace(end) - id = id + ";" + vartype, varname, id, end := tokenMsgVar(line) + end = strings.TrimSpace(end) + id = id + ";" - var newline string - if argv.Debug { - newline = fmt.Sprintf(hmm+" //depth=%d", curmsg.padding(0), vartype, varname, id, end, curmsg.Depth) - } else { - newline = fmt.Sprintf(hmm, curmsg.padding(0), vartype, varname, id, end) - } - newline = strings.TrimRight(newline, " ") - newmsg = append(newmsg, newline) + var newline string + if argv.Debug { + newline = fmt.Sprintf(hmm+" //depth=%d", curmsg.padding(0), vartype, varname, id, end, curmsg.Depth) + } else { + newline = fmt.Sprintf(hmm, curmsg.padding(0), vartype, varname, id, end) + } + newline = strings.TrimRight(newline, " ") + newmsg = append(newmsg, newline) + */ } if curmsg.Footer == "" {