diff --git a/file.proto b/file.proto index 62033f3..5b8aeff 100644 --- a/file.proto +++ b/file.proto @@ -59,7 +59,7 @@ message Sort { bool needAll = 5; // } -// used to format protobuf files +// used to auto-format protobuf files message FormatMsg { repeated string lines = 1; // keys to sort on int64 maxVarname = 2; // max string length of var names @@ -67,6 +67,9 @@ message FormatMsg { repeated FormatMsg inceptionMsgs = 4; // messages inside messages repeated FormatMsg enums = 5; // locally defined enums repeated FormatMsg oneofs = 6; // locally defined oneofs + string header = 7; // the 'message {','enum {', etc line + string footer = 8; // the '}' line + repeated string notes = 9; // unknown lines or comments } message Find { diff --git a/protoReformat.go b/protoReformat.go index 46f93f4..ee3d2fa 100644 --- a/protoReformat.go +++ b/protoReformat.go @@ -67,84 +67,115 @@ func protoReformat(filename string) error { fmtmsg.MaxVarname = bigName fmtmsg.MaxVartype = bigType + // getMessage(fmtmsg) + // write out the messages allTheLines = newLinesScanner(strings.Split(string(data), "\n")) for allTheLines.Scan() { line := allTheLines.Next() if strings.HasPrefix(line, "oneof ") { - if inMessage { - // message inception. search for the architect. don't forget your totem - newmsg := new(FormatMsg) - newmsg.MaxVarname = bigName - newmsg.MaxVartype = bigType - newmsg.Lines = append(newmsg.Lines, line) - getInceptionMsg(newmsg) - newmsg.Enums = append(newmsg.Oneofs, newmsg) - continue - } + /* + if inMessage { + // message inception. search for the architect. don't forget your totem + newmsg := new(FormatMsg) + newmsg.MaxVarname = bigName + newmsg.MaxVartype = bigType + newmsg.Lines = append(newmsg.Lines, line) + getInceptionEnum(newmsg) + fmtmsg.Enums = append(fmtmsg.Oneofs, newmsg) + continue + } + */ + newmsg := new(FormatMsg) + newmsg.MaxVarname = bigName + newmsg.MaxVartype = bigType + newmsg.Lines = append(newmsg.Lines, line) + getInceptionEnum(newmsg) + continue } if strings.HasPrefix(line, "enum ") { - if inMessage { - // message inception. search for the architect. don't forget your totem - newmsg := new(FormatMsg) - newmsg.MaxVarname = bigName - newmsg.MaxVartype = bigType - newmsg.Lines = append(newmsg.Lines, line) - getInceptionMsg(newmsg) - newmsg.Enums = append(newmsg.Enums, newmsg) - continue - } + /* + if inMessage { + // message inception. search for the architect. don't forget your totem + newmsg := new(FormatMsg) + newmsg.MaxVarname = bigName + newmsg.MaxVartype = bigType + newmsg.Lines = append(newmsg.Lines, line) + getInceptionEnum(newmsg) + fmtmsg.Enums = append(fmtmsg.Enums, newmsg) + continue + } + */ + newmsg := new(FormatMsg) + newmsg.MaxVarname = bigName + newmsg.MaxVartype = bigType + newmsg.Header = line + getInceptionEnum(newmsg) + continue } if strings.HasPrefix(line, "message ") { - if inMessage { - // message inception. search for the architect. don't forget your totem - newmsg := new(FormatMsg) - newmsg.MaxVarname = bigName - newmsg.MaxVartype = bigType - newmsg.Lines = append(newmsg.Lines, line) - getInceptionMsg(newmsg) - newmsg.InceptionMsgs = append(newmsg.InceptionMsgs, newmsg) - continue - + newmsg := FormatMsg{ + MaxVarname: bigName, + MaxVartype: bigType, + Header: line, } - inMessage = true - parts := strings.Fields(line) - if len(parts) > 3 { - // hack to actually indent comments on the message line itself. you're welcome - start := parts[0] + " " + parts[1] + " " + parts[2] - end := strings.Join(parts[3:], " ") - offset := int(bigName) + int(bigType) + 16 - len(start) - pad := fmt.Sprintf("%d", offset) - hmm := "%s %" + pad + "s %s" - line = fmt.Sprintf(hmm, start, " ", end) - } - newfile += fmt.Sprintln(line) - 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) { + getInceptionMsg(&newmsg) + for _, newline := range formatMessage2(&newmsg) { newfile += fmt.Sprintln(newline) } - newfile += fmt.Sprintln(line) - fmtmsg = new(FormatMsg) - fmtmsg.MaxVarname = bigName - fmtmsg.MaxVartype = bigType + /* + if inMessage { + // message inception. search for the architect. don't forget your totem + newmsg := new(FormatMsg) + newmsg.MaxVarname = bigName + newmsg.MaxVartype = bigType + newmsg.Lines = append(newmsg.Lines, line) + getInceptionMsg(newmsg) + fmtmsg.InceptionMsgs = append(fmtmsg.InceptionMsgs, newmsg) + continue + + } + inMessage = true + parts := strings.Fields(line) + if len(parts) > 3 { + // hack to actually indent comments on the message line itself. you're welcome + start := parts[0] + " " + parts[1] + " " + parts[2] + end := strings.Join(parts[3:], " ") + offset := int(bigName) + int(bigType) + 16 - len(start) + pad := fmt.Sprintf("%d", offset) + hmm := "%s %" + pad + "s %s" + line = fmt.Sprintf(hmm, start, " ", end) + } + newfile += fmt.Sprintln(line) + */ 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) + // fmtmsg.Lines = append(fmtmsg.Lines, line) } pf, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) @@ -160,6 +191,45 @@ func protoReformat(filename string) error { return nil } +func getInceptionMsg(fmtmsg *FormatMsg) { + for allTheLines.Scan() { + line := allTheLines.Next() + if strings.HasPrefix(line, "oneof ") { + // message inception. search for the architect. don't forget your totem + newmsg := new(FormatMsg) + newmsg.MaxVarname = fmtmsg.MaxVarname + newmsg.MaxVartype = fmtmsg.MaxVartype + newmsg.Header = line + getInceptionEnum(newmsg) + fmtmsg.Enums = append(fmtmsg.Oneofs, newmsg) + continue + } + if strings.HasPrefix(line, "enum ") { + // message inception. search for the architect. don't forget your totem + newmsg := new(FormatMsg) + newmsg.MaxVarname = fmtmsg.MaxVarname + newmsg.MaxVartype = fmtmsg.MaxVartype + newmsg.Header = line + fmtmsg.Enums = append(fmtmsg.Enums, newmsg) + continue + } + if strings.HasPrefix(line, "message ") { + // message inception. search for the architect. don't forget your totem + newmsg := new(FormatMsg) + newmsg.MaxVarname = fmtmsg.MaxVarname + newmsg.MaxVartype = fmtmsg.MaxVartype + newmsg.Header = line + fmtmsg.InceptionMsgs = append(fmtmsg.InceptionMsgs, newmsg) + continue + } + if strings.HasPrefix(line, "}") { + fmtmsg.Footer = line + return + } + fmtmsg.Lines = append(fmtmsg.Lines, line) + } +} + // returns vartype, varname, id, end func tokenMsgVar(line string) (string, string, string, string) { parts := strings.Split(line, ";") @@ -204,7 +274,7 @@ func makeLineIter(data []byte) iter.Seq[string] { } } -func getInceptionMsg(curmsg *FormatMsg) { +func getInceptionEnum(curmsg *FormatMsg) { for allTheLines.Scan() { line := allTheLines.Next() if strings.HasPrefix(line, "}") { @@ -218,6 +288,7 @@ func getInceptionMsg(curmsg *FormatMsg) { func formatMessage2(curmsg *FormatMsg) []string { var newmsg []string + newmsg = append(newmsg, curmsg.Header) // +" //header") // find the max length of varname and vartype for _, line := range curmsg.Lines { parts := strings.Split(line, ";") @@ -261,6 +332,7 @@ func formatMessage2(curmsg *FormatMsg) []string { newline = strings.TrimRight(newline, " ") newmsg = append(newmsg, newline) } + newmsg = append(newmsg, curmsg.Footer) // +" //footer") return newmsg }