more complete format() functions
This commit is contained in:
parent
b4df5436ec
commit
8471b3e683
4
Makefile
4
Makefile
|
@ -118,3 +118,7 @@ reformat-signal.proto-full-debug: goimports vet build
|
|||
reformat-signal.proto-fruit: goimports vet build
|
||||
git checkout example/*.proto
|
||||
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
|
||||
|
|
|
@ -117,3 +117,6 @@ proto-reformat-full-debug:
|
|||
|
||||
proto-reformat-fruit:
|
||||
../autogenpb --proto fruit.proto --format
|
||||
|
||||
proto-reformat-fruit-debug:
|
||||
../autogenpb --proto fruit.proto --format --debug
|
||||
|
|
103
protoReformat.go
103
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:
|
||||
}
|
||||
}
|
||||
|
||||
switch all.Type {
|
||||
case FormatMsg_ENUM:
|
||||
return formatEnum(all)
|
||||
case FormatMsg_MESSAGE:
|
||||
return formatMessage(all)
|
||||
}
|
||||
return formatMessage(all)
|
||||
|
||||
func (parent *FormatMsg) format() []string {
|
||||
parent.formatStandardSizes()
|
||||
|
||||
switch parent.Type {
|
||||
case FormatMsg_ENUM:
|
||||
return formatEnum(parent)
|
||||
case FormatMsg_ONEOF:
|
||||
return formatEnum(parent)
|
||||
case FormatMsg_MESSAGE:
|
||||
return formatMessage(parent)
|
||||
default:
|
||||
return formatMessage(parent)
|
||||
}
|
||||
}
|
||||
|
||||
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,16 +472,22 @@ 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)
|
||||
*/
|
||||
newmsg = append(newmsg, curmsg.formatComment(line, "comment"))
|
||||
continue
|
||||
}
|
||||
newmsg = append(newmsg, curmsg.formatVarLine(line, "var"))
|
||||
continue
|
||||
/*
|
||||
mt := fmt.Sprintf("%d", curmsg.MaxVartype)
|
||||
mv := fmt.Sprintf("%d", curmsg.MaxVarname)
|
||||
|
||||
|
@ -465,6 +505,7 @@ func formatMessage(curmsg *FormatMsg) []string {
|
|||
}
|
||||
newline = strings.TrimRight(newline, " ")
|
||||
newmsg = append(newmsg, newline)
|
||||
*/
|
||||
}
|
||||
|
||||
if curmsg.Footer == "" {
|
||||
|
|
Loading…
Reference in New Issue