From 255f08f81f35750f6ab5975175b5ee1fda8d027b Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 26 Mar 2025 10:07:29 -0500 Subject: [PATCH] still not right --- protoReformat.go | 93 +++++++++++++++++++++++++++++------------------- 1 file changed, 56 insertions(+), 37 deletions(-) diff --git a/protoReformat.go b/protoReformat.go index ee3d2fa..87eaf1a 100644 --- a/protoReformat.go +++ b/protoReformat.go @@ -25,7 +25,6 @@ func protoReformat(filename string) error { return err } - var inMessage bool var newfile string var fmtmsg *FormatMsg fmtmsg = new(FormatMsg) @@ -33,6 +32,7 @@ func protoReformat(filename string) error { var bigName int64 var bigType int64 + var inMessage bool var allLinesIter iter.Seq[string] allLinesIter = makeLineIter(data) // gets the max vartype and varname @@ -45,7 +45,7 @@ func protoReformat(filename string) error { // find the end of the message if strings.HasPrefix(line, "}") { inMessage = false - formatMessage2(fmtmsg) + setMaxSizes(fmtmsg) if bigName < fmtmsg.MaxVarname { bigName = fmtmsg.MaxVarname } @@ -91,6 +91,9 @@ func protoReformat(filename string) error { newmsg.MaxVartype = bigType newmsg.Lines = append(newmsg.Lines, line) getInceptionEnum(newmsg) + for _, newline := range formatMessage(newmsg) { + newfile += fmt.Sprintln(newline) + } continue } @@ -112,17 +115,20 @@ func protoReformat(filename string) error { newmsg.MaxVartype = bigType newmsg.Header = line getInceptionEnum(newmsg) + for _, newline := range formatEnum(newmsg) { + newfile += fmt.Sprintln(newline) + } continue } if strings.HasPrefix(line, "message ") { - newmsg := FormatMsg{ - MaxVarname: bigName, - MaxVartype: bigType, - Header: line, - } - getInceptionMsg(&newmsg) - for _, newline := range formatMessage2(&newmsg) { + newmsg := new(FormatMsg) + newmsg.MaxVarname = bigName + newmsg.MaxVartype = bigType + newmsg.Header = line + + getInceptionMsg(newmsg) + for _, newline := range formatMessage(newmsg) { newfile += fmt.Sprintln(newline) } /* @@ -153,29 +159,7 @@ func protoReformat(filename string) error { continue } - /* - // find the end of the message - if strings.HasPrefix(line, "}") { - inMessage = false - // format and write the last message to the file - for _, newline := range formatMessage2(fmtmsg) { - newfile += fmt.Sprintln(newline) - } - newfile += fmt.Sprintln(line) - fmtmsg = new(FormatMsg) - fmtmsg.MaxVarname = bigName - fmtmsg.MaxVartype = bigType - continue - } - */ - - // don't format or change anything when not in a "message {" section - if !inMessage { - newfile += fmt.Sprintln(line) - continue - } - - // fmtmsg.Lines = append(fmtmsg.Lines, line) + newfile += fmt.Sprintln(line) } pf, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) @@ -285,11 +269,8 @@ func getInceptionEnum(curmsg *FormatMsg) { } } -func formatMessage2(curmsg *FormatMsg) []string { - var newmsg []string - - newmsg = append(newmsg, curmsg.Header) // +" //header") - // find the max length of varname and vartype +// find the max length of varname and vartype +func setMaxSizes(curmsg *FormatMsg) { for _, line := range curmsg.Lines { parts := strings.Split(line, ";") if len(parts) < 2 { @@ -305,6 +286,44 @@ func formatMessage2(curmsg *FormatMsg) []string { curmsg.MaxVarname = int64(len(varname)) } } +} + +func formatEnum(curmsg *FormatMsg) []string { + var newmsg []string + newmsg = append(newmsg, curmsg.Header) // +" //header") + + for _, line := range curmsg.Lines { + line = strings.TrimSpace(line) + newmsg = append(newmsg, line) + } + + newmsg = append(newmsg, curmsg.Footer) // +" //footer") + return newmsg +} + +func formatMessage(curmsg *FormatMsg) []string { + var newmsg []string + newmsg = append(newmsg, curmsg.Header) // +" //header") + + // find the max length of varname and vartype + setMaxSizes(curmsg) + /* + for _, line := range curmsg.Lines { + parts := strings.Split(line, ";") + if len(parts) < 2 { + // line is blank or just a comment + continue + } + + vartype, varname, _, _ := tokenMsgVar(line) + if len(vartype) > int(curmsg.MaxVartype) { + curmsg.MaxVartype = int64(len(vartype)) + } + if len(varname) > int(curmsg.MaxVarname) { + curmsg.MaxVarname = int64(len(varname)) + } + } + */ for _, line := range curmsg.Lines { line = strings.TrimSpace(line)