runs again, still doing the padding wrong
This commit is contained in:
parent
11ccc557fe
commit
de7d698e19
|
@ -65,6 +65,7 @@ message FormatMsg {
|
||||||
MESSAGE = 0;
|
MESSAGE = 0;
|
||||||
ENUM = 1;
|
ENUM = 1;
|
||||||
ONEOF = 2;
|
ONEOF = 2;
|
||||||
|
VAR = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64 depth = 1; // used to indent output
|
int64 depth = 1; // used to indent output
|
||||||
|
@ -75,7 +76,7 @@ message FormatMsg {
|
||||||
repeated FormatMsg msgs = 6; // locally defined messages and enums
|
repeated FormatMsg msgs = 6; // locally defined messages and enums
|
||||||
repeated string lines = 7; // the variables
|
repeated string lines = 7; // the variables
|
||||||
string footer = 8; // the '}' line
|
string footer = 8; // the '}' line
|
||||||
Type type = 9;
|
Type type = 9; // yep. type. yep. that's what this is for
|
||||||
}
|
}
|
||||||
|
|
||||||
message Find {
|
message Find {
|
||||||
|
|
|
@ -76,13 +76,13 @@ func protoReformat(filename string) error {
|
||||||
|
|
||||||
var newfile string
|
var newfile string
|
||||||
|
|
||||||
var bigName int64
|
// var bigName int64
|
||||||
var bigType int64
|
// var bigType int64
|
||||||
|
|
||||||
var fmtmsg *FormatMsg
|
// var fmtmsg *FormatMsg
|
||||||
fmtmsg = new(FormatMsg)
|
// fmtmsg = new(FormatMsg)
|
||||||
|
|
||||||
var inMessage bool
|
/*
|
||||||
var allLinesIter iter.Seq[string]
|
var allLinesIter iter.Seq[string]
|
||||||
allLinesIter = makeLineIter(data)
|
allLinesIter = makeLineIter(data)
|
||||||
// gets the max vartype and varname
|
// gets the max vartype and varname
|
||||||
|
@ -111,23 +111,34 @@ func protoReformat(filename string) error {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
basemsg := doParse(strings.Split(string(data), "\n"))
|
||||||
|
|
||||||
|
for _, newline := range basemsg.format() {
|
||||||
|
newfile += fmt.Sprintln(newline)
|
||||||
|
}
|
||||||
|
|
||||||
|
return saveFile(filename, newfile)
|
||||||
|
}
|
||||||
|
|
||||||
|
func doParse(lines []string) *FormatMsg {
|
||||||
|
var comments string
|
||||||
|
var inMessage bool
|
||||||
|
|
||||||
var basemsg *FormatMsg
|
var basemsg *FormatMsg
|
||||||
basemsg = new(FormatMsg)
|
basemsg = new(FormatMsg)
|
||||||
basemsg.MaxVarname = bigName
|
|
||||||
basemsg.MaxVartype = bigType
|
|
||||||
inMessage = false
|
inMessage = false
|
||||||
|
|
||||||
var comments string
|
|
||||||
|
|
||||||
// write out the messages
|
// write out the messages
|
||||||
allTheLines = newLinesScanner(strings.Split(string(data), "\n"))
|
allTheLines = newLinesScanner(lines)
|
||||||
for allTheLines.Scan() {
|
for allTheLines.Scan() {
|
||||||
line := allTheLines.NextRaw()
|
line := allTheLines.NextRaw()
|
||||||
|
|
||||||
if strings.HasPrefix(line, "oneof ") {
|
if strings.HasPrefix(line, "oneof ") {
|
||||||
newmsg := basemsg.newOneofMessage(line)
|
newmsg := basemsg.newOneofMessage(line)
|
||||||
newmsg.Notes = strings.Split(comments, "\n")
|
newmsg.Notes = strings.Split(comments, "\n")
|
||||||
|
comments = ""
|
||||||
newmsg.load()
|
newmsg.load()
|
||||||
inMessage = true
|
inMessage = true
|
||||||
continue
|
continue
|
||||||
|
@ -136,6 +147,7 @@ func protoReformat(filename string) error {
|
||||||
if strings.HasPrefix(line, "enum ") {
|
if strings.HasPrefix(line, "enum ") {
|
||||||
newmsg := basemsg.newEnumMessage(line)
|
newmsg := basemsg.newEnumMessage(line)
|
||||||
newmsg.Notes = strings.Split(comments, "\n")
|
newmsg.Notes = strings.Split(comments, "\n")
|
||||||
|
comments = ""
|
||||||
newmsg.load()
|
newmsg.load()
|
||||||
inMessage = true
|
inMessage = true
|
||||||
continue
|
continue
|
||||||
|
@ -146,6 +158,7 @@ func protoReformat(filename string) error {
|
||||||
|
|
||||||
newmsg := basemsg.newStdMessage(line)
|
newmsg := basemsg.newStdMessage(line)
|
||||||
newmsg.Notes = strings.Split(comments, "\n")
|
newmsg.Notes = strings.Split(comments, "\n")
|
||||||
|
comments = ""
|
||||||
newmsg.load()
|
newmsg.load()
|
||||||
inMessage = true
|
inMessage = true
|
||||||
continue
|
continue
|
||||||
|
@ -158,11 +171,7 @@ func protoReformat(filename string) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, newline := range basemsg.format() {
|
return basemsg
|
||||||
newfile += fmt.Sprintln(newline)
|
|
||||||
}
|
|
||||||
|
|
||||||
return saveFile(filename, newfile)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveFile(filename string, data string) error {
|
func saveFile(filename string, data string) error {
|
||||||
|
@ -296,6 +305,7 @@ func makeLineIter(data []byte) iter.Seq[string] {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
// func loadEnumDefinition(newMsg *EnumMessage) *EnumMessage {
|
// func loadEnumDefinition(newMsg *EnumMessage) *EnumMessage {
|
||||||
func (newMsg *EnumMessage) load() {
|
func (newMsg *EnumMessage) load() {
|
||||||
curPB := newMsg.msgPB
|
curPB := newMsg.msgPB
|
||||||
|
@ -308,6 +318,7 @@ func (newMsg *EnumMessage) load() {
|
||||||
curPB.Lines = append(curPB.Lines, line)
|
curPB.Lines = append(curPB.Lines, line)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// find the max length of varname and vartype
|
// find the max length of varname and vartype
|
||||||
func setMaxSizes(curmsg *FormatMsg) {
|
func setMaxSizes(curmsg *FormatMsg) {
|
||||||
|
@ -361,14 +372,45 @@ func formatEnum(curmsg *FormatMsg) []string {
|
||||||
return newmsg
|
return newmsg
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg *FormatMsg) format() []string {
|
func (all *FormatMsg) format() []string {
|
||||||
|
var bigType int64
|
||||||
|
var bigName int64
|
||||||
|
|
||||||
|
// find the biggest var names and var types
|
||||||
|
for _, msg := range all.Msgs {
|
||||||
switch msg.Type {
|
switch msg.Type {
|
||||||
case FormatMsg_ENUM:
|
case FormatMsg_ENUM:
|
||||||
return formatEnum(msg)
|
|
||||||
case FormatMsg_MESSAGE:
|
case FormatMsg_MESSAGE:
|
||||||
return formatMessage(msg)
|
// find the max length of varname and vartype
|
||||||
|
setMaxSizes(msg)
|
||||||
|
if bigType < msg.MaxVartype {
|
||||||
|
bigType = msg.MaxVartype
|
||||||
}
|
}
|
||||||
return formatMessage(msg)
|
if bigName < msg.MaxVarname {
|
||||||
|
bigName = msg.MaxVarname
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// set this size in each message
|
||||||
|
for _, msg := range all.Msgs {
|
||||||
|
switch msg.Type {
|
||||||
|
case FormatMsg_ENUM:
|
||||||
|
case FormatMsg_MESSAGE:
|
||||||
|
msg.MaxVartype = bigType
|
||||||
|
msg.MaxVarname = bigName
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch all.Type {
|
||||||
|
case FormatMsg_ENUM:
|
||||||
|
return formatEnum(all)
|
||||||
|
case FormatMsg_MESSAGE:
|
||||||
|
return formatMessage(all)
|
||||||
|
}
|
||||||
|
return formatMessage(all)
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatMessage(curmsg *FormatMsg) []string {
|
func formatMessage(curmsg *FormatMsg) []string {
|
||||||
|
@ -400,9 +442,6 @@ func formatMessage(curmsg *FormatMsg) []string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// find the max length of varname and vartype
|
|
||||||
setMaxSizes(curmsg)
|
|
||||||
|
|
||||||
for _, msg := range curmsg.Msgs {
|
for _, msg := range curmsg.Msgs {
|
||||||
switch msg.Type {
|
switch msg.Type {
|
||||||
case FormatMsg_ENUM:
|
case FormatMsg_ENUM:
|
||||||
|
@ -411,7 +450,7 @@ func formatMessage(curmsg *FormatMsg) []string {
|
||||||
newmsg = append(newmsg, line)
|
newmsg = append(newmsg, line)
|
||||||
}
|
}
|
||||||
case FormatMsg_MESSAGE:
|
case FormatMsg_MESSAGE:
|
||||||
for _, line := range formatMessage(msg) {
|
for _, line := range msg.format() {
|
||||||
line = fmt.Sprintf("%s%s", curmsg.pad(), line)
|
line = fmt.Sprintf("%s%s", curmsg.pad(), line)
|
||||||
newmsg = append(newmsg, line)
|
newmsg = append(newmsg, line)
|
||||||
}
|
}
|
||||||
|
@ -445,7 +484,11 @@ func formatMessage(curmsg *FormatMsg) []string {
|
||||||
newline = strings.TrimRight(newline, " ")
|
newline = strings.TrimRight(newline, " ")
|
||||||
newmsg = append(newmsg, newline)
|
newmsg = append(newmsg, newline)
|
||||||
}
|
}
|
||||||
|
if curmsg.Footer == "" {
|
||||||
|
newmsg = append(newmsg, "// footer was empty")
|
||||||
|
} else {
|
||||||
newmsg = append(newmsg, curmsg.Footer) // +" //footer")
|
newmsg = append(newmsg, curmsg.Footer) // +" //footer")
|
||||||
|
}
|
||||||
return newmsg
|
return newmsg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue