works again on my simple proto buf examples

This commit is contained in:
Jeff Carr 2025-03-26 09:48:03 -05:00
parent cb8d3f624c
commit f3b9d40dfd
2 changed files with 132 additions and 57 deletions

View File

@ -59,7 +59,7 @@ message Sort {
bool needAll = 5; // bool needAll = 5; //
} }
// used to format protobuf files // used to auto-format protobuf files
message FormatMsg { message FormatMsg {
repeated string lines = 1; // keys to sort on repeated string lines = 1; // keys to sort on
int64 maxVarname = 2; // max string length of var names int64 maxVarname = 2; // max string length of var names
@ -67,6 +67,9 @@ message FormatMsg {
repeated FormatMsg inceptionMsgs = 4; // messages inside messages repeated FormatMsg inceptionMsgs = 4; // messages inside messages
repeated FormatMsg enums = 5; // locally defined enums repeated FormatMsg enums = 5; // locally defined enums
repeated FormatMsg oneofs = 6; // locally defined oneofs repeated FormatMsg oneofs = 6; // locally defined oneofs
string header = 7; // the 'message {','enum {', etc line
string footer = 8; // the '}' line
repeated string notes = 9; // unknown lines or comments
} }
message Find { message Find {

View File

@ -67,84 +67,115 @@ func protoReformat(filename string) error {
fmtmsg.MaxVarname = bigName fmtmsg.MaxVarname = bigName
fmtmsg.MaxVartype = bigType fmtmsg.MaxVartype = bigType
// getMessage(fmtmsg)
// write out the messages // write out the messages
allTheLines = newLinesScanner(strings.Split(string(data), "\n")) allTheLines = newLinesScanner(strings.Split(string(data), "\n"))
for allTheLines.Scan() { for allTheLines.Scan() {
line := allTheLines.Next() line := allTheLines.Next()
if strings.HasPrefix(line, "oneof ") { if strings.HasPrefix(line, "oneof ") {
if inMessage { /*
// message inception. search for the architect. don't forget your totem if inMessage {
newmsg := new(FormatMsg) // message inception. search for the architect. don't forget your totem
newmsg.MaxVarname = bigName newmsg := new(FormatMsg)
newmsg.MaxVartype = bigType newmsg.MaxVarname = bigName
newmsg.Lines = append(newmsg.Lines, line) newmsg.MaxVartype = bigType
getInceptionMsg(newmsg) newmsg.Lines = append(newmsg.Lines, line)
newmsg.Enums = append(newmsg.Oneofs, newmsg) getInceptionEnum(newmsg)
continue fmtmsg.Enums = append(fmtmsg.Oneofs, newmsg)
} continue
}
*/
newmsg := new(FormatMsg)
newmsg.MaxVarname = bigName
newmsg.MaxVartype = bigType
newmsg.Lines = append(newmsg.Lines, line)
getInceptionEnum(newmsg)
continue
} }
if strings.HasPrefix(line, "enum ") { if strings.HasPrefix(line, "enum ") {
if inMessage { /*
// message inception. search for the architect. don't forget your totem if inMessage {
newmsg := new(FormatMsg) // message inception. search for the architect. don't forget your totem
newmsg.MaxVarname = bigName newmsg := new(FormatMsg)
newmsg.MaxVartype = bigType newmsg.MaxVarname = bigName
newmsg.Lines = append(newmsg.Lines, line) newmsg.MaxVartype = bigType
getInceptionMsg(newmsg) newmsg.Lines = append(newmsg.Lines, line)
newmsg.Enums = append(newmsg.Enums, newmsg) getInceptionEnum(newmsg)
continue fmtmsg.Enums = append(fmtmsg.Enums, newmsg)
} continue
}
*/
newmsg := new(FormatMsg)
newmsg.MaxVarname = bigName
newmsg.MaxVartype = bigType
newmsg.Header = line
getInceptionEnum(newmsg)
continue
} }
if strings.HasPrefix(line, "message ") { if strings.HasPrefix(line, "message ") {
if inMessage { newmsg := FormatMsg{
// message inception. search for the architect. don't forget your totem MaxVarname: bigName,
newmsg := new(FormatMsg) MaxVartype: bigType,
newmsg.MaxVarname = bigName Header: line,
newmsg.MaxVartype = bigType
newmsg.Lines = append(newmsg.Lines, line)
getInceptionMsg(newmsg)
newmsg.InceptionMsgs = append(newmsg.InceptionMsgs, newmsg)
continue
} }
inMessage = true getInceptionMsg(&newmsg)
parts := strings.Fields(line) for _, newline := range formatMessage2(&newmsg) {
if len(parts) > 3 {
// hack to actually indent comments on the message line itself. you're welcome
start := parts[0] + " " + parts[1] + " " + parts[2]
end := strings.Join(parts[3:], " ")
offset := int(bigName) + int(bigType) + 16 - len(start)
pad := fmt.Sprintf("%d", offset)
hmm := "%s %" + pad + "s %s"
line = fmt.Sprintf(hmm, start, " ", end)
}
newfile += fmt.Sprintln(line)
continue
}
// find the end of the message
if strings.HasPrefix(line, "}") {
inMessage = false
// format and write the last message to the file
for _, newline := range formatMessage2(fmtmsg) {
newfile += fmt.Sprintln(newline) newfile += fmt.Sprintln(newline)
} }
newfile += fmt.Sprintln(line) /*
fmtmsg = new(FormatMsg) if inMessage {
fmtmsg.MaxVarname = bigName // message inception. search for the architect. don't forget your totem
fmtmsg.MaxVartype = bigType newmsg := new(FormatMsg)
newmsg.MaxVarname = bigName
newmsg.MaxVartype = bigType
newmsg.Lines = append(newmsg.Lines, line)
getInceptionMsg(newmsg)
fmtmsg.InceptionMsgs = append(fmtmsg.InceptionMsgs, newmsg)
continue
}
inMessage = true
parts := strings.Fields(line)
if len(parts) > 3 {
// hack to actually indent comments on the message line itself. you're welcome
start := parts[0] + " " + parts[1] + " " + parts[2]
end := strings.Join(parts[3:], " ")
offset := int(bigName) + int(bigType) + 16 - len(start)
pad := fmt.Sprintf("%d", offset)
hmm := "%s %" + pad + "s %s"
line = fmt.Sprintf(hmm, start, " ", end)
}
newfile += fmt.Sprintln(line)
*/
continue continue
} }
/*
// find the end of the message
if strings.HasPrefix(line, "}") {
inMessage = false
// format and write the last message to the file
for _, newline := range formatMessage2(fmtmsg) {
newfile += fmt.Sprintln(newline)
}
newfile += fmt.Sprintln(line)
fmtmsg = new(FormatMsg)
fmtmsg.MaxVarname = bigName
fmtmsg.MaxVartype = bigType
continue
}
*/
// don't format or change anything when not in a "message {" section // don't format or change anything when not in a "message {" section
if !inMessage { if !inMessage {
newfile += fmt.Sprintln(line) newfile += fmt.Sprintln(line)
continue continue
} }
fmtmsg.Lines = append(fmtmsg.Lines, line) // fmtmsg.Lines = append(fmtmsg.Lines, line)
} }
pf, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) pf, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
@ -160,6 +191,45 @@ func protoReformat(filename string) error {
return nil return nil
} }
func getInceptionMsg(fmtmsg *FormatMsg) {
for allTheLines.Scan() {
line := allTheLines.Next()
if strings.HasPrefix(line, "oneof ") {
// message inception. search for the architect. don't forget your totem
newmsg := new(FormatMsg)
newmsg.MaxVarname = fmtmsg.MaxVarname
newmsg.MaxVartype = fmtmsg.MaxVartype
newmsg.Header = line
getInceptionEnum(newmsg)
fmtmsg.Enums = append(fmtmsg.Oneofs, newmsg)
continue
}
if strings.HasPrefix(line, "enum ") {
// message inception. search for the architect. don't forget your totem
newmsg := new(FormatMsg)
newmsg.MaxVarname = fmtmsg.MaxVarname
newmsg.MaxVartype = fmtmsg.MaxVartype
newmsg.Header = line
fmtmsg.Enums = append(fmtmsg.Enums, newmsg)
continue
}
if strings.HasPrefix(line, "message ") {
// message inception. search for the architect. don't forget your totem
newmsg := new(FormatMsg)
newmsg.MaxVarname = fmtmsg.MaxVarname
newmsg.MaxVartype = fmtmsg.MaxVartype
newmsg.Header = line
fmtmsg.InceptionMsgs = append(fmtmsg.InceptionMsgs, newmsg)
continue
}
if strings.HasPrefix(line, "}") {
fmtmsg.Footer = line
return
}
fmtmsg.Lines = append(fmtmsg.Lines, line)
}
}
// returns vartype, varname, id, end // returns vartype, varname, id, end
func tokenMsgVar(line string) (string, string, string, string) { func tokenMsgVar(line string) (string, string, string, string) {
parts := strings.Split(line, ";") parts := strings.Split(line, ";")
@ -204,7 +274,7 @@ func makeLineIter(data []byte) iter.Seq[string] {
} }
} }
func getInceptionMsg(curmsg *FormatMsg) { func getInceptionEnum(curmsg *FormatMsg) {
for allTheLines.Scan() { for allTheLines.Scan() {
line := allTheLines.Next() line := allTheLines.Next()
if strings.HasPrefix(line, "}") { if strings.HasPrefix(line, "}") {
@ -218,6 +288,7 @@ func getInceptionMsg(curmsg *FormatMsg) {
func formatMessage2(curmsg *FormatMsg) []string { func formatMessage2(curmsg *FormatMsg) []string {
var newmsg []string var newmsg []string
newmsg = append(newmsg, curmsg.Header) // +" //header")
// find the max length of varname and vartype // find the max length of varname and vartype
for _, line := range curmsg.Lines { for _, line := range curmsg.Lines {
parts := strings.Split(line, ";") parts := strings.Split(line, ";")
@ -261,6 +332,7 @@ func formatMessage2(curmsg *FormatMsg) []string {
newline = strings.TrimRight(newline, " ") newline = strings.TrimRight(newline, " ")
newmsg = append(newmsg, newline) newmsg = append(newmsg, newline)
} }
newmsg = append(newmsg, curmsg.Footer) // +" //footer")
return newmsg return newmsg
} }