first part done

This commit is contained in:
Jeff Carr 2025-03-26 03:36:05 -05:00
parent 2775e36aa4
commit 7a68c6247a
1 changed files with 23 additions and 12 deletions

View File

@ -27,13 +27,15 @@ func protoReformat(filename string) error {
} }
var inMessage bool var inMessage bool
var curmsg []string
var newfile string var newfile string
var fmtmsg *FormatMsg var fmtmsg *FormatMsg
fmtmsg = new(FormatMsg) fmtmsg = new(FormatMsg)
linesIter = makeLineIter(data) linesIter = makeLineIter(data)
var bigName int64
var bigType int64
// gets the max vartype and varname // gets the max vartype and varname
for line := range linesIter { for line := range linesIter {
if strings.HasPrefix(line, "message ") { if strings.HasPrefix(line, "message ") {
@ -44,9 +46,14 @@ func protoReformat(filename string) error {
// find the end of the message // find the end of the message
if strings.HasPrefix(line, "}") { if strings.HasPrefix(line, "}") {
inMessage = false inMessage = false
formatMessage(curmsg) // formatMessage(curmsg)
formatMessage2(fmtmsg) formatMessage2(fmtmsg)
curmsg = nil if bigName < fmtmsg.MaxVarname {
bigName = fmtmsg.MaxVarname
}
if bigType < fmtmsg.MaxVartype {
bigType = fmtmsg.MaxVartype
}
fmtmsg = new(FormatMsg) fmtmsg = new(FormatMsg)
continue continue
} }
@ -55,10 +62,14 @@ func protoReformat(filename string) error {
if !inMessage { if !inMessage {
continue continue
} }
curmsg = append(curmsg, line)
fmtmsg.Lines = append(fmtmsg.Lines, line) fmtmsg.Lines = append(fmtmsg.Lines, line)
} }
maxVarname = int(bigName)
maxVartype = int(bigType)
var curmsg []string
// gets the max vartype and varname // gets the max vartype and varname
for line := range linesIter { for line := range linesIter {
if strings.HasPrefix(line, "message ") { if strings.HasPrefix(line, "message ") {
@ -71,7 +82,7 @@ func protoReformat(filename string) error {
// hack to actually indent comments on the message line itself. you're welcome // hack to actually indent comments on the message line itself. you're welcome
start := parts[0] + " " + parts[1] + " " + parts[2] start := parts[0] + " " + parts[1] + " " + parts[2]
end := strings.Join(parts[3:], " ") end := strings.Join(parts[3:], " ")
offset := maxVarname + maxVartype + 16 - len(start) offset := int(bigName) + int(bigType) + 16 - len(start)
pad := fmt.Sprintf("%d", offset) pad := fmt.Sprintf("%d", offset)
hmm := "%s %" + pad + "s %s" hmm := "%s %" + pad + "s %s"
line = fmt.Sprintf(hmm, start, " ", end) line = fmt.Sprintf(hmm, start, " ", end)
@ -218,11 +229,11 @@ func formatMessage2(curmsg *FormatMsg) []string {
} }
vartype, varname, _, _ := tokenMsgVar(line) vartype, varname, _, _ := tokenMsgVar(line)
if len(vartype) > maxVartype { if len(vartype) > int(curmsg.MaxVartype) {
maxVartype = len(vartype) curmsg.MaxVartype = int64(len(vartype))
} }
if len(varname) > maxVarname { if len(varname) > int(curmsg.MaxVarname) {
maxVarname = len(varname) curmsg.MaxVarname = int64(len(varname))
} }
} }
@ -233,14 +244,14 @@ func formatMessage2(curmsg *FormatMsg) []string {
continue continue
} }
if strings.HasPrefix(line, "//") { if strings.HasPrefix(line, "//") {
pad := fmt.Sprintf("%d", maxVartype+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)
continue continue
} }
mt := fmt.Sprintf("%d", maxVartype) mt := fmt.Sprintf("%d", curmsg.MaxVartype)
mv := fmt.Sprintf("%d", maxVarname) mv := fmt.Sprintf("%d", curmsg.MaxVarname)
hmm := " %-" + mt + "s %-" + mv + "s = %-3s %s" hmm := " %-" + mt + "s %-" + mv + "s = %-3s %s"