clean comment reparser

This commit is contained in:
Jeff Carr 2025-03-27 17:55:56 -05:00
parent bdc2e4fadf
commit 40fbd38712
5 changed files with 72 additions and 17 deletions

View File

@ -92,6 +92,12 @@ clean-more:
ls -l autogenpb autogenpb.last ls -l autogenpb autogenpb.last
-rm -f autogenpb.2* -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-restore
make -C example proto-reformat-comments 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

View File

@ -108,4 +108,6 @@ proto-reformat-restore:
proto-reformat-comments: proto-reformat-comments:
../autogenpb --proto signal.proto --format-comments ../autogenpb --proto signal.proto --format-comments
# autogenpb --proto SignalService.proto --format
proto-reformat-full:
autogenpb --proto signal.proto --format

View File

@ -321,14 +321,14 @@ message Chat {
uint32 expireTimerVersion = 10; uint32 expireTimerVersion = 10;
} }
/** //
* Call Links have some associated data including a call, but unlike other recipients // 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. // are not tied to threads because they do not have messages associated with them.
* //
* note: // note:
* - room id can be derived from the root key // - room id can be derived from the root key
* - the presence of an admin key means this user is a call admin // - the presence of an admin key means this user is a call admin
*/ //
message CallLink { message CallLink {
enum Restrictions { enum Restrictions {
UNKNOWN = 0; // Interpret as "Admin Approval" UNKNOWN = 0; // Interpret as "Admin Approval"

View File

@ -18,6 +18,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
"github.com/alexflint/go-arg" "github.com/alexflint/go-arg"
"github.com/go-cmd/cmd" "github.com/go-cmd/cmd"
@ -63,6 +64,8 @@ func main() {
} }
if argv.Format { if argv.Format {
protoReformatComments(argv.Proto)
time.Sleep(5 * time.Second)
protoReformat(argv.Proto) protoReformat(argv.Proto)
okExit("") okExit("")
} }

View File

@ -54,8 +54,6 @@ func protoReformatComments(filename string) error {
} }
var newfile string var newfile string
newfile = commentPreprocessorFull(string(data))
saveFile(filename, newfile)
log.Info("filename", filename) log.Info("filename", filename)
alltest := makeLineIter(data) alltest := makeLineIter(data)
@ -64,7 +62,7 @@ func protoReformatComments(filename string) error {
newfile += fmt.Sprintln(commentPreprocessor(line)) newfile += fmt.Sprintln(commentPreprocessor(line))
} }
newfile = commentPreprocessorFull(newfile) newfile = commentPreprocessorFull(newfile)
// saveFile(filename, newfile) saveFile(filename, newfile)
return nil return nil
} }
@ -476,14 +474,13 @@ func (it *LinesScanner) NextRaw() string {
return it.things[it.index-1] return it.things[it.index-1]
} }
// cleans out comments // trims whitespace
func (it *LinesScanner) Next() string { func (it *LinesScanner) Next() string {
if it.index-1 == len(it.things) { if it.index-1 == len(it.things) {
fmt.Println("Next() error in LinesScanner", it.index) fmt.Println("Next() error in LinesScanner", it.index)
} }
// out := commentPreprocessor(it.things[it.index-1]) // out := commentPreprocessor(it.things[it.index-1])
out := it.things[it.index-1] out := it.things[it.index-1]
out = commentPreprocessor(out)
return strings.TrimSpace(out) return strings.TrimSpace(out)
// return out // return out
} }
@ -527,9 +524,56 @@ func commentPreprocessor(line string) string {
// thing // thing
func commentPreprocessorFull(full string) string { func commentPreprocessorFull(full string) string {
// Match all /* comment */ blocks // Match all /* comment */ blocks
re := regexp.MustCompile(`/\*([^*]+)\*/`) // re := regexp.MustCompile(`/\*([^*]+)\*/`)
re := regexp.MustCompile(`(?s)/\*(.*?)\*/`)
return re.ReplaceAllStringFunc(full, func(s string) string { 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
}