From 40fbd387126d6d5be4db0a8bd9d644e3b0b518e9 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 27 Mar 2025 17:55:56 -0500 Subject: [PATCH] clean comment reparser --- Makefile | 8 +++++- example/Makefile | 4 ++- example/signal.proto | 16 ++++++------ main.go | 3 +++ protoReformat.go | 58 ++++++++++++++++++++++++++++++++++++++------ 5 files changed, 72 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index f2efd87..9093572 100644 --- a/Makefile +++ b/Makefile @@ -92,6 +92,12 @@ clean-more: ls -l autogenpb autogenpb.last -rm -f autogenpb.2* -reformat-signal.proto: +reformat-signal.proto-comments: + git checkout example/fruit.proto make -C example proto-reformat-restore make -C example proto-reformat-comments + +reformat-signal.proto-full: + git checkout example/fruit.proto + make -C example proto-reformat-restore + make -C example proto-reformat-full diff --git a/example/Makefile b/example/Makefile index 4f8171d..52ee5c6 100644 --- a/example/Makefile +++ b/example/Makefile @@ -108,4 +108,6 @@ proto-reformat-restore: proto-reformat-comments: ../autogenpb --proto signal.proto --format-comments - # autogenpb --proto SignalService.proto --format + +proto-reformat-full: + autogenpb --proto signal.proto --format diff --git a/example/signal.proto b/example/signal.proto index c939e53..b7d3bc7 100644 --- a/example/signal.proto +++ b/example/signal.proto @@ -321,14 +321,14 @@ message Chat { uint32 expireTimerVersion = 10; } -/** - * Call Links have some associated data including a call, but unlike other recipients - * are not tied to threads because they do not have messages associated with them. - * - * note: - * - room id can be derived from the root key - * - the presence of an admin key means this user is a call admin - */ +// +// Call Links have some associated data including a call, but unlike other recipients +// are not tied to threads because they do not have messages associated with them. +// +// note: +// - room id can be derived from the root key +// - the presence of an admin key means this user is a call admin +// message CallLink { enum Restrictions { UNKNOWN = 0; // Interpret as "Admin Approval" diff --git a/main.go b/main.go index e0702f9..b57c924 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ import ( "os" "path/filepath" "strings" + "time" "github.com/alexflint/go-arg" "github.com/go-cmd/cmd" @@ -63,6 +64,8 @@ func main() { } if argv.Format { + protoReformatComments(argv.Proto) + time.Sleep(5 * time.Second) protoReformat(argv.Proto) okExit("") } diff --git a/protoReformat.go b/protoReformat.go index 962e7b2..6afb93f 100644 --- a/protoReformat.go +++ b/protoReformat.go @@ -54,8 +54,6 @@ func protoReformatComments(filename string) error { } var newfile string - newfile = commentPreprocessorFull(string(data)) - saveFile(filename, newfile) log.Info("filename", filename) alltest := makeLineIter(data) @@ -64,7 +62,7 @@ func protoReformatComments(filename string) error { newfile += fmt.Sprintln(commentPreprocessor(line)) } newfile = commentPreprocessorFull(newfile) - // saveFile(filename, newfile) + saveFile(filename, newfile) return nil } @@ -476,14 +474,13 @@ func (it *LinesScanner) NextRaw() string { return it.things[it.index-1] } -// cleans out comments +// trims whitespace func (it *LinesScanner) Next() string { if it.index-1 == len(it.things) { fmt.Println("Next() error in LinesScanner", it.index) } // out := commentPreprocessor(it.things[it.index-1]) out := it.things[it.index-1] - out = commentPreprocessor(out) return strings.TrimSpace(out) // return out } @@ -527,9 +524,56 @@ func commentPreprocessor(line string) string { // thing func commentPreprocessorFull(full string) string { // Match all /* comment */ blocks - re := regexp.MustCompile(`/\*([^*]+)\*/`) + // re := regexp.MustCompile(`/\*([^*]+)\*/`) + re := regexp.MustCompile(`(?s)/\*(.*?)\*/`) return re.ReplaceAllStringFunc(full, func(s string) string { - return strings.ToUpper(s) + log.Info("FOUND:\n", s) + lines := strings.Split(s, "\n") + var cleaned []string + + for _, line := range lines { + trimmed := strings.TrimSpace(line) + + switch { + case strings.HasPrefix(trimmed, "/*"): + trimmed = trimCommentPrefix(trimmed) + case strings.HasPrefix(trimmed, "*/"): + trimmed = strings.TrimPrefix(trimmed, "*/") + case strings.HasPrefix(trimmed, "*"): + trimmed = strings.TrimPrefix(trimmed, "*") + } + trimmed = "// " + trimmed + + cleaned = append(cleaned, strings.TrimSpace(trimmed)) + } + + s = strings.Join(cleaned, "\n") + log.Info("NOW:\n", s) + return s }) } + +func trimCommentPrefix(line string) string { + trimmed := strings.TrimSpace(line) + + if strings.HasPrefix(trimmed, "/") { + i := 1 + for i < len(trimmed) && trimmed[i] == '*' { + i++ + } + if i > 1 { + return strings.TrimSpace(trimmed[i:]) + } + } + + if strings.HasPrefix(trimmed, "*") { + return strings.TrimSpace(trimmed[1:]) + } + + if trimmed == "*/" { + return "" + } + + return trimmed +}