diff --git a/file.proto b/file.proto index e4e6a4f..a8313de 100644 --- a/file.proto +++ b/file.proto @@ -65,6 +65,7 @@ message FormatMsg { MESSAGE = 0; ENUM = 1; ONEOF = 2; + VAR = 3; } int64 depth = 1; // used to indent output @@ -75,7 +76,7 @@ message FormatMsg { repeated FormatMsg msgs = 6; // locally defined messages and enums repeated string lines = 7; // the variables string footer = 8; // the '}' line - Type type = 9; + Type type = 9; // yep. type. yep. that's what this is for } message Find { diff --git a/protoReformat.go b/protoReformat.go index c2296b0..4248ed9 100644 --- a/protoReformat.go +++ b/protoReformat.go @@ -76,58 +76,69 @@ func protoReformat(filename string) error { var newfile string - var bigName int64 - var bigType int64 + // var bigName int64 + // var bigType int64 - var fmtmsg *FormatMsg - fmtmsg = new(FormatMsg) + // var fmtmsg *FormatMsg + // fmtmsg = new(FormatMsg) - var inMessage bool - var allLinesIter iter.Seq[string] - allLinesIter = makeLineIter(data) - // gets the max vartype and varname - for line := range allLinesIter { - if strings.HasPrefix(line, "message ") { - inMessage = true - continue - } - - // find the end of the message - if strings.HasPrefix(line, "}") { - inMessage = false - setMaxSizes(fmtmsg) - if bigName < fmtmsg.MaxVarname { - bigName = fmtmsg.MaxVarname + /* + var allLinesIter iter.Seq[string] + allLinesIter = makeLineIter(data) + // gets the max vartype and varname + for line := range allLinesIter { + if strings.HasPrefix(line, "message ") { + inMessage = true + continue } - if bigType < fmtmsg.MaxVartype { - bigType = fmtmsg.MaxVartype - } - fmtmsg = new(FormatMsg) - continue - } - // don't format or change anything when not in a "message {" section - if !inMessage { - continue + // find the end of the message + if strings.HasPrefix(line, "}") { + inMessage = false + setMaxSizes(fmtmsg) + if bigName < fmtmsg.MaxVarname { + bigName = fmtmsg.MaxVarname + } + if bigType < fmtmsg.MaxVartype { + bigType = fmtmsg.MaxVartype + } + fmtmsg = new(FormatMsg) + continue + } + + // don't format or change anything when not in a "message {" section + if !inMessage { + continue + } } + */ + + basemsg := doParse(strings.Split(string(data), "\n")) + + for _, newline := range basemsg.format() { + newfile += fmt.Sprintln(newline) } + return saveFile(filename, newfile) +} + +func doParse(lines []string) *FormatMsg { + var comments string + var inMessage bool + var basemsg *FormatMsg basemsg = new(FormatMsg) - basemsg.MaxVarname = bigName - basemsg.MaxVartype = bigType inMessage = false - var comments string - // write out the messages - allTheLines = newLinesScanner(strings.Split(string(data), "\n")) + allTheLines = newLinesScanner(lines) for allTheLines.Scan() { line := allTheLines.NextRaw() if strings.HasPrefix(line, "oneof ") { newmsg := basemsg.newOneofMessage(line) newmsg.Notes = strings.Split(comments, "\n") + comments = "" newmsg.load() inMessage = true continue @@ -136,6 +147,7 @@ func protoReformat(filename string) error { if strings.HasPrefix(line, "enum ") { newmsg := basemsg.newEnumMessage(line) newmsg.Notes = strings.Split(comments, "\n") + comments = "" newmsg.load() inMessage = true continue @@ -146,6 +158,7 @@ func protoReformat(filename string) error { newmsg := basemsg.newStdMessage(line) newmsg.Notes = strings.Split(comments, "\n") + comments = "" newmsg.load() inMessage = true continue @@ -158,11 +171,7 @@ func protoReformat(filename string) error { } } - for _, newline := range basemsg.format() { - newfile += fmt.Sprintln(newline) - } - - return saveFile(filename, newfile) + return basemsg } func saveFile(filename string, data string) error { @@ -296,6 +305,7 @@ func makeLineIter(data []byte) iter.Seq[string] { } } +/* // func loadEnumDefinition(newMsg *EnumMessage) *EnumMessage { func (newMsg *EnumMessage) load() { curPB := newMsg.msgPB @@ -308,6 +318,7 @@ func (newMsg *EnumMessage) load() { curPB.Lines = append(curPB.Lines, line) } } +*/ // find the max length of varname and vartype func setMaxSizes(curmsg *FormatMsg) { @@ -361,14 +372,45 @@ func formatEnum(curmsg *FormatMsg) []string { return newmsg } -func (msg *FormatMsg) format() []string { - switch msg.Type { - case FormatMsg_ENUM: - return formatEnum(msg) - case FormatMsg_MESSAGE: - return formatMessage(msg) +func (all *FormatMsg) format() []string { + var bigType int64 + var bigName int64 + + // find the biggest var names and var types + for _, msg := range all.Msgs { + switch msg.Type { + case FormatMsg_ENUM: + case FormatMsg_MESSAGE: + // find the max length of varname and vartype + setMaxSizes(msg) + if bigType < msg.MaxVartype { + bigType = msg.MaxVartype + } + if bigName < msg.MaxVarname { + bigName = msg.MaxVarname + } + default: + } } - return formatMessage(msg) + + // set this size in each message + for _, msg := range all.Msgs { + switch msg.Type { + case FormatMsg_ENUM: + case FormatMsg_MESSAGE: + msg.MaxVartype = bigType + msg.MaxVarname = bigName + default: + } + } + + switch all.Type { + case FormatMsg_ENUM: + return formatEnum(all) + case FormatMsg_MESSAGE: + return formatMessage(all) + } + return formatMessage(all) } func formatMessage(curmsg *FormatMsg) []string { @@ -400,9 +442,6 @@ func formatMessage(curmsg *FormatMsg) []string { } } - // find the max length of varname and vartype - setMaxSizes(curmsg) - for _, msg := range curmsg.Msgs { switch msg.Type { case FormatMsg_ENUM: @@ -411,7 +450,7 @@ func formatMessage(curmsg *FormatMsg) []string { newmsg = append(newmsg, line) } case FormatMsg_MESSAGE: - for _, line := range formatMessage(msg) { + for _, line := range msg.format() { line = fmt.Sprintf("%s%s", curmsg.pad(), line) newmsg = append(newmsg, line) } @@ -445,7 +484,11 @@ func formatMessage(curmsg *FormatMsg) []string { newline = strings.TrimRight(newline, " ") newmsg = append(newmsg, newline) } - newmsg = append(newmsg, curmsg.Footer) // +" //footer") + if curmsg.Footer == "" { + newmsg = append(newmsg, "// footer was empty") + } else { + newmsg = append(newmsg, curmsg.Footer) // +" //footer") + } return newmsg }