more complete format() functions

This commit is contained in:
Jeff Carr 2025-04-02 23:26:49 -05:00
parent b4df5436ec
commit 8471b3e683
3 changed files with 95 additions and 47 deletions

View File

@ -118,3 +118,7 @@ reformat-signal.proto-full-debug: goimports vet build
reformat-signal.proto-fruit: goimports vet build reformat-signal.proto-fruit: goimports vet build
git checkout example/*.proto git checkout example/*.proto
make -C example proto-reformat-fruit make -C example proto-reformat-fruit
reformat-signal.proto-fruit-debug: goimports vet build
git checkout example/*.proto
make -C example proto-reformat-fruit-debug

View File

@ -117,3 +117,6 @@ proto-reformat-full-debug:
proto-reformat-fruit: proto-reformat-fruit:
../autogenpb --proto fruit.proto --format ../autogenpb --proto fruit.proto --format
proto-reformat-fruit-debug:
../autogenpb --proto fruit.proto --format --debug

View File

@ -312,51 +312,59 @@ func formatEnum(curmsg *FormatMsg) []string {
return newmsg return newmsg
} }
func (all *FormatMsg) format() []string { // set all children to have the same max sizes
func (parent *FormatMsg) formatStandardSizes() {
var bigType int64 var bigType int64
var bigName int64 var bigName int64
// find the biggest var names and var types // find the biggest var names and var types
for _, msg := range all.Msgs { for _, child := range parent.Msgs {
switch msg.Type { switch child.Type {
case FormatMsg_ENUM: case FormatMsg_ENUM:
case FormatMsg_MESSAGE: case FormatMsg_MESSAGE:
// find the max length of varname and vartype // find the max length of varname and vartype
setMaxSizes(msg) setMaxSizes(child)
if bigType < msg.MaxVartype { if bigType < child.MaxVartype {
bigType = msg.MaxVartype bigType = child.MaxVartype
} }
if bigName < msg.MaxVarname { if bigName < child.MaxVarname {
bigName = msg.MaxVarname bigName = child.MaxVarname
} }
default: default:
} }
} }
// set this size in each message // set this size in each message
for _, msg := range all.Msgs { for _, child := range parent.Msgs {
switch msg.Type { switch child.Type {
case FormatMsg_ENUM: case FormatMsg_ENUM:
case FormatMsg_MESSAGE: case FormatMsg_MESSAGE:
msg.MaxVartype = bigType child.MaxVartype = bigType
msg.MaxVarname = bigName child.MaxVarname = bigName
default: default:
} }
} }
}
switch all.Type { func (parent *FormatMsg) format() []string {
parent.formatStandardSizes()
switch parent.Type {
case FormatMsg_ENUM: case FormatMsg_ENUM:
return formatEnum(all) return formatEnum(parent)
case FormatMsg_ONEOF:
return formatEnum(parent)
case FormatMsg_MESSAGE: case FormatMsg_MESSAGE:
return formatMessage(all) return formatMessage(parent)
default:
return formatMessage(parent)
} }
return formatMessage(all)
} }
func (msg *FormatMsg) formatLineBase(line string, dbg string) string { func (msg *FormatMsg) formatLineBase(line string, dbg string) string {
line = strings.TrimSpace(line) line = strings.TrimSpace(line)
if argv.Debug { if argv.Debug {
return fmt.Sprintf("/*a*/ %s%s // %s depth=%d", msg.padBase(), line, dbg, msg.Depth) return fmt.Sprintf("/*a*/%s/*b*/%s // %s depth=%d", msg.padBase(), line, dbg, msg.Depth)
} }
return fmt.Sprintf("%s%s", msg.padBase(), line) return fmt.Sprintf("%s%s", msg.padBase(), line)
} }
@ -364,9 +372,40 @@ func (msg *FormatMsg) formatLineBase(line string, dbg string) string {
func (msg *FormatMsg) formatLine(line string, dbg string) string { func (msg *FormatMsg) formatLine(line string, dbg string) string {
line = strings.TrimSpace(line) line = strings.TrimSpace(line)
if argv.Debug { if argv.Debug {
return fmt.Sprintf("/*a*/ %s%s // %s depth=%d", msg.pad(), line, dbg, msg.Depth) return fmt.Sprintf("/*a*/%s/*b*/%s // %s depth=%d", msg.pad(), line, dbg, msg.Depth)
} }
return fmt.Sprintf("%s%s", msg.padBase(), line) return fmt.Sprintf("%s%s", msg.pad(), line)
}
func (msg *FormatMsg) formatComment(line string, dbg string) string {
line = strings.TrimSpace(line)
pad := fmt.Sprintf("%d", msg.MaxVartype+msg.MaxVarname+13) // 21 is correct?
hmm := "%" + pad + "s %s"
comment := fmt.Sprintf(hmm, " ", line) // todo: compute 50
if argv.Debug {
return fmt.Sprintf("/*a*/%s/*b*/%s // %s depth=%d", msg.pad(), comment, dbg, msg.Depth)
}
return fmt.Sprintf("%s%s", msg.pad(), comment)
}
func (msg *FormatMsg) formatVarLine(line string, dbg string) string {
line = strings.TrimSpace(line)
mt := fmt.Sprintf("%d", msg.MaxVartype)
mv := fmt.Sprintf("%d", msg.MaxVarname)
hmm := "%-" + mt + "s %-" + mv + "s = %-3s %s"
vartype, varname, id, end := tokenMsgVar(line)
end = strings.TrimSpace(end)
id = id + ";"
newline := fmt.Sprintf(hmm, vartype, varname, id, end)
newline = strings.TrimRight(newline, " ")
if argv.Debug {
return fmt.Sprintf("/*a*/%s/*b*/%s // %s depth=%d", msg.pad(), newline, dbg, msg.Depth)
}
return fmt.Sprintf("%s%s", msg.pad(), newline)
} }
func trimLines(lines []string) []string { func trimLines(lines []string) []string {
@ -415,15 +454,10 @@ func formatMessage(curmsg *FormatMsg) []string {
case FormatMsg_ENUM: case FormatMsg_ENUM:
for _, line := range formatEnum(msg) { for _, line := range formatEnum(msg) {
newmsg = append(newmsg, 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_ONEOF:
for _, line := range formatEnum(msg) {
newmsg = append(newmsg, line)
} }
case FormatMsg_MESSAGE: case FormatMsg_MESSAGE:
for _, line := range msg.format() { for _, line := range msg.format() {
@ -438,16 +472,22 @@ func formatMessage(curmsg *FormatMsg) []string {
line = strings.TrimSpace(line) line = strings.TrimSpace(line)
if line == "" { if line == "" {
// newmsg = append(newmsg, line) // newmsg = append(newmsg, line)
newmsg = append(newmsg, curmsg.formatLine(line, "lines")) newmsg = append(newmsg, "\n")
continue continue
} }
if strings.HasPrefix(line, "//") { if strings.HasPrefix(line, "//") {
/*
pad := fmt.Sprintf("%d", curmsg.MaxVartype+curmsg.MaxVarname+21) pad := fmt.Sprintf("%d", curmsg.MaxVartype+curmsg.MaxVarname+21)
hmm := "%" + pad + "s %s" hmm := "%" + pad + "s %s"
line = fmt.Sprintf(hmm, " ", line) // todo: compute 50 line = fmt.Sprintf(hmm, " ", line) // todo: compute 50
newmsg = append(newmsg, line) newmsg = append(newmsg, line)
*/
newmsg = append(newmsg, curmsg.formatComment(line, "comment"))
continue continue
} }
newmsg = append(newmsg, curmsg.formatVarLine(line, "var"))
continue
/*
mt := fmt.Sprintf("%d", curmsg.MaxVartype) mt := fmt.Sprintf("%d", curmsg.MaxVartype)
mv := fmt.Sprintf("%d", curmsg.MaxVarname) mv := fmt.Sprintf("%d", curmsg.MaxVarname)
@ -465,6 +505,7 @@ func formatMessage(curmsg *FormatMsg) []string {
} }
newline = strings.TrimRight(newline, " ") newline = strings.TrimRight(newline, " ")
newmsg = append(newmsg, newline) newmsg = append(newmsg, newline)
*/
} }
if curmsg.Footer == "" { if curmsg.Footer == "" {