diff --git a/protoReformat.go b/protoReformat.go index ed69d20..01f0023 100644 --- a/protoReformat.go +++ b/protoReformat.go @@ -312,6 +312,21 @@ func formatEnum(curmsg *FormatMsg) []string { return newmsg } +func formatOneof(curmsg *FormatMsg) []string { + // curmsg.formatStandardSizes() + + var newmsg []string + newmsg = append(newmsg, curmsg.formatLineBase(curmsg.Header, "oneof header")) + + for _, line := range curmsg.Lines { + // func (msg *FormatMsg) formatMsgLine(line string, dbg string) string { + newmsg = append(newmsg, curmsg.formatMsgLine(line, "oneof")) + } + + newmsg = append(newmsg, curmsg.formatLineBase(curmsg.Footer, "oneof header")) + return newmsg +} + // set all children to have the same max sizes func (parent *FormatMsg) formatStandardSizes() { var bigType int64 @@ -321,6 +336,15 @@ func (parent *FormatMsg) formatStandardSizes() { for _, child := range parent.Msgs { switch child.Type { case FormatMsg_ENUM: + case FormatMsg_ONEOF: + // find the max length of varname and vartype + setMaxSizes(child) + if bigType < child.MaxVartype { + bigType = child.MaxVartype + } + if bigName < child.MaxVarname { + bigName = child.MaxVarname + } case FormatMsg_MESSAGE: // find the max length of varname and vartype setMaxSizes(child) @@ -338,6 +362,9 @@ func (parent *FormatMsg) formatStandardSizes() { for _, child := range parent.Msgs { switch child.Type { case FormatMsg_ENUM: + case FormatMsg_ONEOF: + child.MaxVartype = bigType + child.MaxVarname = bigName case FormatMsg_MESSAGE: child.MaxVartype = bigType child.MaxVarname = bigName @@ -353,7 +380,7 @@ func (parent *FormatMsg) format() []string { case FormatMsg_ENUM: return formatEnum(parent) case FormatMsg_ONEOF: - return formatEnum(parent) + return formatOneof(parent) case FormatMsg_MESSAGE: return formatMessage(parent) default: @@ -403,11 +430,26 @@ func (msg *FormatMsg) formatVarLine(line string, dbg string) string { 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("/*a*/%s/*b*/%s // %s depth=%d (%d,%d)", msg.pad(), newline, dbg, msg.Depth, msg.MaxVartype, msg.MaxVarname) } return fmt.Sprintf("%s%s", msg.pad(), newline) } +func (msg *FormatMsg) formatMsgLine(line string, dbg string) string { + line = strings.TrimSpace(line) + if line == "" { + if argv.Debug { + return "// empty line " + msg.Header + " empty line end" + } else { + return line + } + } + if strings.HasPrefix(line, "//") { + return msg.formatComment(line, "comment") + } + return msg.formatVarLine(line, "var "+dbg) +} + func trimLines(lines []string) []string { return strings.Split(strings.TrimSuffix(strings.Join(lines, "\n"), "\n"), "\n") } @@ -439,9 +481,9 @@ func formatMessage(curmsg *FormatMsg) []string { hmm := "%s %" + pad + "s %s" line = fmt.Sprintf(hmm, start, " ", end) } - newmsg = append(newmsg, line) // " //header") + newmsg = append(newmsg, line) // +"// header") } else { - newmsg = append(newmsg, line) // " //header") + newmsg = append(newmsg, line) // +"// header") } } else { if curmsg.Depth != 0 { @@ -456,7 +498,7 @@ func formatMessage(curmsg *FormatMsg) []string { newmsg = append(newmsg, line) } case FormatMsg_ONEOF: - for _, line := range formatEnum(msg) { + for _, line := range formatOneof(msg) { newmsg = append(newmsg, line) } case FormatMsg_MESSAGE: @@ -468,44 +510,40 @@ func formatMessage(curmsg *FormatMsg) []string { } } - for _, line := range curmsg.Lines { - line = strings.TrimSpace(line) - if line == "" { - // newmsg = append(newmsg, line) - 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) + // trim curmsg.Lines + // if curmsg.Lines is empty, don't do anything + dump := strings.Join(curmsg.Lines, "\n") + dump = strings.TrimSpace(dump) - hmm := "%s%-" + mt + "s %-" + mv + "s = %-3s %s" + if dump == "" { + // do nothing + } else { + // print the lines + curmsg.Lines = strings.Split(dump, "\n") + // newmsg = append(newmsg, "// dump "+dump+"dump end\n") - 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) + for _, line := range curmsg.Lines { + line = strings.TrimSpace(line) + if line == "" { + if argv.Debug { + newmsg = append(newmsg, "// empty line "+curmsg.Header+" empty line end") + } else { + newmsg = append(newmsg, line) + } + continue } - newline = strings.TrimRight(newline, " ") - newmsg = append(newmsg, newline) - */ + 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")) + } } if curmsg.Footer == "" {