From 0a97886cd2e942070ac478b2802f49dca8249f36 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 29 Mar 2025 09:43:24 -0500 Subject: [PATCH] fix --debug --- argv.go | 1 + example/Makefile | 2 +- example/fruit.proto | 3 +-- protoReformat.go | 35 +++++++++++++++++++++++++++-------- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/argv.go b/argv.go index 9b05d60..c977657 100644 --- a/argv.go +++ b/argv.go @@ -20,6 +20,7 @@ type args struct { Delete bool `arg:"--delete" help:"use delete with copy experiment"` DryRun bool `arg:"--dry-run" help:"check the .proto syntax, but don't do anything"` Format bool `arg:"--format" help:"format the .proto file and exit"` + Debug bool `arg:"--debug" help:"enable debugging information"` Comments bool `arg:"--format-comments" help:"enforce parseable comments in a .proto file"` NoFormat bool `arg:"--no-format" help:"do not auto-reformat the .proto file"` GoSrc string `arg:"--go-src" help:"default is ~/go/src. could be set to your go.work path"` diff --git a/example/Makefile b/example/Makefile index 06b3cdb..1ddb086 100644 --- a/example/Makefile +++ b/example/Makefile @@ -110,7 +110,7 @@ proto-reformat-comments: ../autogenpb --proto signal.proto --format-comments proto-reformat-full: - ../autogenpb --proto signal.proto --format + ../autogenpb --proto signal.proto --format --debug proto-reformat-fruit: ../autogenpb --proto fruit.proto --format diff --git a/example/fruit.proto b/example/fruit.proto index 362c59b..3924026 100644 --- a/example/fruit.proto +++ b/example/fruit.proto @@ -54,7 +54,6 @@ message Basket { // `autogenpb:nomutex` repeated Pear pears = 5; repeated Apple stacks = 6; } - // "Fruit" must exist. you can put anything in it message Fruit { string brand = 1; // `autogenpb:unique` `autogenpb:sort` @@ -67,7 +66,6 @@ message Fruit { repeated Basket gifts = 8; int64 price = 9; // `autogenpb:sort` } - // "Fruits" MUST EXIST and start exactly this way // It must be "Fruit" + 's' and must match the name of this file: "fruit.proto" message Fruits { // `autogenpb:marshal` `autogenpb:mutex` `autogenpb:gui` @@ -77,3 +75,4 @@ message Fruits { // `autogenpb:marshal` ` int64 cost = 4; // you can add additional things here but the three lines above must conform to the standard above map junk = 5; } +// footer was empty diff --git a/protoReformat.go b/protoReformat.go index 4248ed9..efd568b 100644 --- a/protoReformat.go +++ b/protoReformat.go @@ -18,6 +18,7 @@ import ( var allTheLines *LinesScanner +/* type EnumMessage struct { msgPB *FormatMsg all []Message @@ -44,6 +45,7 @@ type Message interface { load() addMsg(Message) } +*/ func protoReformatComments(filename string) error { // read in the .proto file @@ -137,6 +139,7 @@ func doParse(lines []string) *FormatMsg { if strings.HasPrefix(line, "oneof ") { newmsg := basemsg.newOneofMessage(line) + comments = strings.TrimSpace(comments) newmsg.Notes = strings.Split(comments, "\n") comments = "" newmsg.load() @@ -146,6 +149,7 @@ func doParse(lines []string) *FormatMsg { if strings.HasPrefix(line, "enum ") { newmsg := basemsg.newEnumMessage(line) + comments = strings.TrimSpace(comments) newmsg.Notes = strings.Split(comments, "\n") comments = "" newmsg.load() @@ -157,6 +161,7 @@ func doParse(lines []string) *FormatMsg { log.Info("got to message", line) newmsg := basemsg.newStdMessage(line) + comments = strings.TrimSpace(comments) newmsg.Notes = strings.Split(comments, "\n") comments = "" newmsg.load() @@ -227,9 +232,11 @@ func (msgPB *FormatMsg) newEnumMessage(header string) *FormatMsg { // 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 @@ -416,13 +423,16 @@ func (all *FormatMsg) format() []string { func formatMessage(curmsg *FormatMsg) []string { var newmsg []string - // print the Notes - for _, line := range curmsg.Notes { - newmsg = append(newmsg, line) - } + // add the notes & comments before the header + newmsg = append(newmsg, strings.TrimSpace(strings.Join(curmsg.Notes, "\n"))) if curmsg.Header != "" { - line := fmt.Sprintf("%s%s // msg depth=%d", curmsg.padBase(), curmsg.Header, curmsg.Depth) + 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) + } parts := strings.Fields(line) if len(parts) > 3 { // hack to actually indent comments on the message line itself. you're welcome @@ -430,10 +440,19 @@ func formatMessage(curmsg *FormatMsg) []string { 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 // depth=%d" - line = fmt.Sprintf(hmm, start, " ", end, curmsg.Depth) + if argv.Debug { + hmm := "%s %" + pad + "s %s // depth=%d" + line = fmt.Sprintf(hmm, start, " ", end, curmsg.Depth) + } else { + hmm := "%s %" + pad + "s %s" + line = fmt.Sprintf(hmm, start, " ", end) + } } else { - line = fmt.Sprintf("%s // len(parts)=%d depth=%d", line, len(parts), curmsg.Depth) + 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") } else {