diff --git a/Makefile b/Makefile index 71d34ba..3d007b6 100644 --- a/Makefile +++ b/Makefile @@ -91,3 +91,6 @@ clean: clean-more: ls -l autogenpb autogenpb.last -rm -f autogenpb.2* + +Signal-Desktop: + autogenpb --proto SignalService.proto --format diff --git a/protoReformat.go b/protoReformat.go index 00d9352..818153e 100644 --- a/protoReformat.go +++ b/protoReformat.go @@ -18,6 +18,12 @@ import ( var allTheLines *LinesScanner +type Messages interface { + format() []string +} + +var allMessages []Messages + func protoReformat(filename string) error { // read in the .proto file data, err := os.ReadFile(filename) @@ -78,34 +84,17 @@ func protoReformat(filename string) error { fmtmsg.Lines = append(fmtmsg.Lines, line) } - fmtmsg = new(FormatMsg) - 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) - 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) + newmsg.Header = line + loadEnumDefinition(newmsg) + allMessages = append(allMessages, newmsg) for _, newline := range formatMessage(newmsg) { newfile += fmt.Sprintln(newline) } @@ -113,23 +102,11 @@ func protoReformat(filename string) error { } 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) - getInceptionEnum(newmsg) - fmtmsg.Enums = append(fmtmsg.Enums, newmsg) - continue - } - */ newmsg := new(FormatMsg) newmsg.MaxVarname = bigName newmsg.MaxVartype = bigType newmsg.Header = line - getInceptionEnum(newmsg) + loadEnumDefinition(newmsg) for _, newline := range formatEnum(newmsg) { newfile += fmt.Sprintln(newline) } @@ -142,23 +119,11 @@ func protoReformat(filename string) error { newmsg.MaxVartype = bigType newmsg.Header = line - getInceptionMsg(newmsg) + loadMsgDefinition(newmsg) for _, newline := range formatMessage(newmsg) { newfile += fmt.Sprintln(newline) } /* - 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 @@ -194,26 +159,24 @@ func saveFile(filename string, data string) error { return nil } -func getInceptionMsg(fmtmsg *FormatMsg) { +func loadMsgDefinition(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) + loadEnumDefinition(newmsg) fmtmsg.Oneofs = 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 - getInceptionEnum(newmsg) + loadEnumDefinition(newmsg) fmtmsg.Enums = append(fmtmsg.Enums, newmsg) // log.Info("got here:", line) // os.Exit(-1) @@ -225,7 +188,7 @@ func getInceptionMsg(fmtmsg *FormatMsg) { newmsg.MaxVarname = fmtmsg.MaxVarname newmsg.MaxVartype = fmtmsg.MaxVartype newmsg.Header = line - getInceptionMsg(newmsg) + loadMsgDefinition(newmsg) fmtmsg.InceptionMsgs = append(fmtmsg.InceptionMsgs, newmsg) continue } @@ -281,11 +244,11 @@ func makeLineIter(data []byte) iter.Seq[string] { } } -func getInceptionEnum(curmsg *FormatMsg) { +func loadEnumDefinition(curmsg *FormatMsg) { for allTheLines.Scan() { line := allTheLines.Next() if strings.HasPrefix(line, "}") { - curmsg.Lines = append(curmsg.Lines, line) + curmsg.Footer = line return } curmsg.Lines = append(curmsg.Lines, line) @@ -311,12 +274,16 @@ func setMaxSizes(curmsg *FormatMsg) { } } +func (curmsg *FormatMsg) format() []string { + return formatEnum(curmsg) +} + func formatEnum(curmsg *FormatMsg) []string { var newmsg []string newmsg = append(newmsg, curmsg.Header) // +" //header") for _, line := range curmsg.Lines { - line = strings.TrimSpace(line) + line = " " + strings.TrimSpace(line) newmsg = append(newmsg, line) } @@ -326,7 +293,21 @@ func formatEnum(curmsg *FormatMsg) []string { func formatMessage(curmsg *FormatMsg) []string { var newmsg []string - newmsg = append(newmsg, curmsg.Header) // +" //header") + + if curmsg.Header != "" { + line := curmsg.Header + 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(curmsg.MaxVarname) + int(curmsg.MaxVartype) + 16 - len(start) + pad := fmt.Sprintf("%d", offset) + hmm := "%s %" + pad + "s %s" + line = fmt.Sprintf(hmm, start, " ", end) + } + newmsg = append(newmsg, line) // +" //header") + } // find the max length of varname and vartype setMaxSizes(curmsg)