From 4e3e05684b642a2a70a8f6c1f00a54874b9827c1 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 30 Mar 2025 04:26:58 -0500 Subject: [PATCH] working again on fruit.proto --- protoReformat.go | 116 +++++++++++++++++++---------------------------- 1 file changed, 46 insertions(+), 70 deletions(-) diff --git a/protoReformat.go b/protoReformat.go index 2732522..d38b61e 100644 --- a/protoReformat.go +++ b/protoReformat.go @@ -32,7 +32,6 @@ type StdMessage struct { type Message interface { name() string - load() addMsg(Message) } */ @@ -91,13 +90,14 @@ func doParse(lines []string) *FormatMsg { line := allTheLines.NextRaw() if strings.HasPrefix(line, "oneof ") { - newmsg := basemsg.newMessage(line, comments, FormatMsg_ONEOF) - comments = "" inMessage = true + comments = "" if strings.Contains(line, "}") { + newmsg := basemsg.newMessage(line, comments, FormatMsg_ONEOF) newmsg.Footer = "} // blah" continue } + newmsg := basemsg.newMessage(line, comments, FormatMsg_ONEOF) newmsg.load() continue } @@ -161,20 +161,6 @@ func newDepth(fmtmsg *FormatMsg, header string) *FormatMsg { return newmsg } -/* -// func newStdMessage(fmtmsg *FormatMsg, header string) *StdMessage { -func (msgPB *FormatMsg) newStdMessage(header string, comments string) *FormatMsg { - newmsg := newDepth(msgPB, header) - newmsg.Type = FormatMsg_MESSAGE - msgPB.Msgs = append(msgPB.Msgs, newmsg) - - comments = strings.TrimSpace(comments) - newmsg.Notes = strings.Split(comments, "\n") - - return newmsg -} -*/ - func (msgPB *FormatMsg) newMessage(header string, comments string, msgType FormatMsg_Type) *FormatMsg { newmsg := newDepth(msgPB, header) newmsg.Type = msgType @@ -186,29 +172,6 @@ func (msgPB *FormatMsg) newMessage(header string, comments string, msgType Forma return newmsg } -/* -func (msgPB *FormatMsg) newEnumMessage(header string, comments string) *FormatMsg { - newmsg := newDepth(msgPB, header) - newmsg.Type = FormatMsg_ENUM - msgPB.Msgs = append(msgPB.Msgs, newmsg) - - comments = strings.TrimSpace(comments) - newmsg.Notes = strings.Split(comments, "\n") - - return newmsg -} -*/ - -// proto files can be defined as trees -// func loadMsgDefinition(msg *StdMessage) { -// func (newMsg *EnumMessage) load() { -// func (msg *StdMessage) loadMsgDefinition(msg *StdMessage) { -/* -func (msg *StdMessage) load() { - msg.msgPB.load() -} -*/ - func (msg *FormatMsg) load() { // fmtmsg := msg.msgPB for allTheLines.Scan() { @@ -409,28 +372,39 @@ func (all *FormatMsg) format() []string { return formatMessage(all) } +func (msg *FormatMsg) formatLineBase(line string, dbg string) string { + line = strings.TrimSpace(line) + if argv.Debug { + return fmt.Sprintf("%s%s // %s depth=%d", msg.padBase(), line, dbg, msg.Depth) + } + return fmt.Sprintf("%s%s", msg.padBase(), line) +} + +func (msg *FormatMsg) formatLine(line string, dbg string) string { + line = strings.TrimSpace(line) + if argv.Debug { + return fmt.Sprintf("%s%s // %s depth=%d", msg.pad(), line, dbg, msg.Depth) + } + return fmt.Sprintf("%s%s", msg.padBase(), line) +} + +func trimLines(lines []string) []string { + return strings.Split(strings.TrimSuffix(strings.Join(lines, "\n"), "\n"), "\n") +} + func formatMessage(curmsg *FormatMsg) []string { var newmsg []string // add the notes & comments before the header // newmsg = append(newmsg, strings.TrimSpace(strings.Join(curmsg.Notes, "\n"))) - for _, line := range strings.Split(strings.TrimSpace(strings.Join(curmsg.Notes, "\n")), "\n") { - if argv.Debug { - footer := fmt.Sprintf("%s%s // msg notes footer depth=%d", curmsg.padBase(), line, curmsg.Depth) - newmsg = append(newmsg, footer) - } else { - footer := fmt.Sprintf("%s%s", curmsg.padBase(), line) - newmsg = append(newmsg, footer) - } + for _, line := range trimLines(curmsg.Notes) { + newmsg = append(newmsg, curmsg.formatLineBase(line, "notes")) } if curmsg.Header != "" { var line string - if argv.Debug { - line = fmt.Sprintf("%s%s // msg depth=%d", curmsg.padBase(), curmsg.Header, curmsg.Depth) - } else { - line = fmt.Sprintf("%s%s", curmsg.padBase(), curmsg.Header) - } + line = curmsg.formatLineBase(curmsg.Header, "header") + parts := strings.Fields(line) if len(parts) > 3 { // hack to actually indent comments on the message line itself. you're welcome @@ -445,17 +419,13 @@ func formatMessage(curmsg *FormatMsg) []string { hmm := "%s %" + pad + "s %s" line = fmt.Sprintf(hmm, start, " ", end) } + newmsg = append(newmsg, line) // " //header") } else { - if argv.Debug { - line = fmt.Sprintf("%s // len(parts)=%d depth=%d", line, len(parts), curmsg.Depth) - } else { - // line = fmt.Sprintf("%s test", line) - } + newmsg = append(newmsg, line) // " //header") } - newmsg = append(newmsg, line) // " //header") } else { if curmsg.Depth != 0 { - newmsg = append(newmsg, "// ERROR: header was blank") // +" //header") + newmsg = append(newmsg, "// ERROR: header was blank") } } @@ -463,13 +433,16 @@ func formatMessage(curmsg *FormatMsg) []string { switch msg.Type { case FormatMsg_ENUM: for _, line := range formatEnum(msg) { - // 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) - } + 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_MESSAGE: for _, line := range msg.format() { @@ -483,7 +456,8 @@ func formatMessage(curmsg *FormatMsg) []string { for _, line := range curmsg.Lines { line = strings.TrimSpace(line) if line == "" { - newmsg = append(newmsg, line) + // newmsg = append(newmsg, line) + newmsg = append(newmsg, curmsg.formatLine(line, "lines")) continue } if strings.HasPrefix(line, "//") { @@ -511,11 +485,13 @@ func formatMessage(curmsg *FormatMsg) []string { newline = strings.TrimRight(newline, " ") newmsg = append(newmsg, newline) } + if curmsg.Footer == "" { newmsg = append(newmsg, "// footer was empty") } else { - newline := fmt.Sprintf("%s%s", curmsg.padding(1), curmsg.Footer) // +" //footer") - newmsg = append(newmsg, newline) + // newline := fmt.Sprintf("%s%s", curmsg.padding(1), curmsg.Footer) // +" //footer") + // newmsg = append(newmsg, newline) + newmsg = append(newmsg, curmsg.formatLineBase(curmsg.Footer, "msg depth")) } return newmsg }