From 40fbd387126d6d5be4db0a8bd9d644e3b0b518e9 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 27 Mar 2025 17:55:56 -0500 Subject: clean comment reparser --- protoReformat.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 7 deletions(-) (limited to 'protoReformat.go') 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 +} -- cgit v1.2.3