summaryrefslogtreecommitdiff
path: root/protoReformat.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-04-02 23:26:49 -0500
committerJeff Carr <[email protected]>2025-04-02 23:26:49 -0500
commit8471b3e683a46acdd038a529656d71093963b315 (patch)
tree488c5778d9acd72c8b825b5c0f3855bd3253e009 /protoReformat.go
parentb4df5436ec87711fd548c3106968fbcf0cdd75b4 (diff)
more complete format() functions
Diffstat (limited to 'protoReformat.go')
-rw-r--r--protoReformat.go135
1 files changed, 88 insertions, 47 deletions
diff --git a/protoReformat.go b/protoReformat.go
index bafdea6..ed69d20 100644
--- a/protoReformat.go
+++ b/protoReformat.go
@@ -312,51 +312,59 @@ func formatEnum(curmsg *FormatMsg) []string {
return newmsg
}
-func (all *FormatMsg) format() []string {
+// set all children to have the same max sizes
+func (parent *FormatMsg) formatStandardSizes() {
var bigType int64
var bigName int64
// find the biggest var names and var types
- for _, msg := range all.Msgs {
- switch msg.Type {
+ for _, child := range parent.Msgs {
+ switch child.Type {
case FormatMsg_ENUM:
case FormatMsg_MESSAGE:
// find the max length of varname and vartype
- setMaxSizes(msg)
- if bigType < msg.MaxVartype {
- bigType = msg.MaxVartype
+ setMaxSizes(child)
+ if bigType < child.MaxVartype {
+ bigType = child.MaxVartype
}
- if bigName < msg.MaxVarname {
- bigName = msg.MaxVarname
+ if bigName < child.MaxVarname {
+ bigName = child.MaxVarname
}
default:
}
}
// set this size in each message
- for _, msg := range all.Msgs {
- switch msg.Type {
+ for _, child := range parent.Msgs {
+ switch child.Type {
case FormatMsg_ENUM:
case FormatMsg_MESSAGE:
- msg.MaxVartype = bigType
- msg.MaxVarname = bigName
+ child.MaxVartype = bigType
+ child.MaxVarname = bigName
default:
}
}
+}
+
+func (parent *FormatMsg) format() []string {
+ parent.formatStandardSizes()
- switch all.Type {
+ switch parent.Type {
case FormatMsg_ENUM:
- return formatEnum(all)
+ return formatEnum(parent)
+ case FormatMsg_ONEOF:
+ return formatEnum(parent)
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 {
line = strings.TrimSpace(line)
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)
}
@@ -364,9 +372,40 @@ func (msg *FormatMsg) formatLineBase(line string, dbg string) string {
func (msg *FormatMsg) formatLine(line string, dbg string) string {
line = strings.TrimSpace(line)
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 {
@@ -415,15 +454,10 @@ func formatMessage(curmsg *FormatMsg) []string {
case FormatMsg_ENUM:
for _, line := range formatEnum(msg) {
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:
for _, line := range msg.format() {
@@ -438,33 +472,40 @@ func formatMessage(curmsg *FormatMsg) []string {
line = strings.TrimSpace(line)
if line == "" {
// newmsg = append(newmsg, line)
- newmsg = append(newmsg, curmsg.formatLine(line, "lines"))
+ newmsg = append(newmsg, "\n")
continue
}
if strings.HasPrefix(line, "//") {
- pad := fmt.Sprintf("%d", curmsg.MaxVartype+curmsg.MaxVarname+21)
- hmm := "%" + pad + "s %s"
- line = fmt.Sprintf(hmm, " ", line) // todo: compute 50
- newmsg = append(newmsg, line)
+ /*
+ pad := fmt.Sprintf("%d", curmsg.MaxVartype+curmsg.MaxVarname+21)
+ hmm := "%" + pad + "s %s"
+ line = fmt.Sprintf(hmm, " ", line) // todo: compute 50
+ newmsg = append(newmsg, line)
+ */
+ newmsg = append(newmsg, curmsg.formatComment(line, "comment"))
continue
}
- mt := fmt.Sprintf("%d", curmsg.MaxVartype)
- mv := fmt.Sprintf("%d", curmsg.MaxVarname)
+ newmsg = append(newmsg, curmsg.formatVarLine(line, "var"))
+ continue
+ /*
+ mt := fmt.Sprintf("%d", curmsg.MaxVartype)
+ mv := fmt.Sprintf("%d", curmsg.MaxVarname)
- hmm := "%s%-" + mt + "s %-" + mv + "s = %-3s %s"
+ hmm := "%s%-" + mt + "s %-" + mv + "s = %-3s %s"
- vartype, varname, id, end := tokenMsgVar(line)
- end = strings.TrimSpace(end)
- id = id + ";"
+ vartype, varname, id, end := tokenMsgVar(line)
+ end = strings.TrimSpace(end)
+ id = id + ";"
- var newline string
- if argv.Debug {
- newline = fmt.Sprintf(hmm+" //depth=%d", curmsg.padding(0), vartype, varname, id, end, curmsg.Depth)
- } else {
- newline = fmt.Sprintf(hmm, curmsg.padding(0), vartype, varname, id, end)
- }
- newline = strings.TrimRight(newline, " ")
- newmsg = append(newmsg, newline)
+ var newline string
+ if argv.Debug {
+ newline = fmt.Sprintf(hmm+" //depth=%d", curmsg.padding(0), vartype, varname, id, end, curmsg.Depth)
+ } else {
+ newline = fmt.Sprintf(hmm, curmsg.padding(0), vartype, varname, id, end)
+ }
+ newline = strings.TrimRight(newline, " ")
+ newmsg = append(newmsg, newline)
+ */
}
if curmsg.Footer == "" {