works again on my simple proto buf examples
This commit is contained in:
parent
cb8d3f624c
commit
f3b9d40dfd
|
@ -59,7 +59,7 @@ message Sort {
|
|||
bool needAll = 5; //
|
||||
}
|
||||
|
||||
// used to format protobuf files
|
||||
// used to auto-format protobuf files
|
||||
message FormatMsg {
|
||||
repeated string lines = 1; // keys to sort on
|
||||
int64 maxVarname = 2; // max string length of var names
|
||||
|
@ -67,6 +67,9 @@ message FormatMsg {
|
|||
repeated FormatMsg inceptionMsgs = 4; // messages inside messages
|
||||
repeated FormatMsg enums = 5; // locally defined enums
|
||||
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 {
|
||||
|
|
|
@ -67,37 +67,65 @@ func protoReformat(filename string) error {
|
|||
fmtmsg.MaxVarname = bigName
|
||||
fmtmsg.MaxVartype = bigType
|
||||
|
||||
// getMessage(fmtmsg)
|
||||
|
||||
// write out the messages
|
||||
allTheLines = newLinesScanner(strings.Split(string(data), "\n"))
|
||||
for allTheLines.Scan() {
|
||||
line := allTheLines.Next()
|
||||
if strings.HasPrefix(line, "oneof ") {
|
||||
/*
|
||||
if inMessage {
|
||||
// message inception. search for the architect. don't forget your totem
|
||||
newmsg := new(FormatMsg)
|
||||
newmsg.MaxVarname = bigName
|
||||
newmsg.MaxVartype = bigType
|
||||
newmsg.Lines = append(newmsg.Lines, line)
|
||||
getInceptionMsg(newmsg)
|
||||
newmsg.Enums = append(newmsg.Oneofs, newmsg)
|
||||
getInceptionEnum(newmsg)
|
||||
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 inMessage {
|
||||
// message inception. search for the architect. don't forget your totem
|
||||
newmsg := new(FormatMsg)
|
||||
newmsg.MaxVarname = bigName
|
||||
newmsg.MaxVartype = bigType
|
||||
newmsg.Lines = append(newmsg.Lines, line)
|
||||
getInceptionMsg(newmsg)
|
||||
newmsg.Enums = append(newmsg.Enums, newmsg)
|
||||
getInceptionEnum(newmsg)
|
||||
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 ") {
|
||||
newmsg := FormatMsg{
|
||||
MaxVarname: bigName,
|
||||
MaxVartype: bigType,
|
||||
Header: line,
|
||||
}
|
||||
getInceptionMsg(&newmsg)
|
||||
for _, newline := range formatMessage2(&newmsg) {
|
||||
newfile += fmt.Sprintln(newline)
|
||||
}
|
||||
/*
|
||||
if inMessage {
|
||||
// message inception. search for the architect. don't forget your totem
|
||||
newmsg := new(FormatMsg)
|
||||
|
@ -105,7 +133,7 @@ func protoReformat(filename string) error {
|
|||
newmsg.MaxVartype = bigType
|
||||
newmsg.Lines = append(newmsg.Lines, line)
|
||||
getInceptionMsg(newmsg)
|
||||
newmsg.InceptionMsgs = append(newmsg.InceptionMsgs, newmsg)
|
||||
fmtmsg.InceptionMsgs = append(fmtmsg.InceptionMsgs, newmsg)
|
||||
continue
|
||||
|
||||
}
|
||||
|
@ -121,9 +149,11 @@ func protoReformat(filename string) error {
|
|||
line = fmt.Sprintf(hmm, start, " ", end)
|
||||
}
|
||||
newfile += fmt.Sprintln(line)
|
||||
*/
|
||||
continue
|
||||
}
|
||||
|
||||
/*
|
||||
// find the end of the message
|
||||
if strings.HasPrefix(line, "}") {
|
||||
inMessage = false
|
||||
|
@ -137,6 +167,7 @@ func protoReformat(filename string) error {
|
|||
fmtmsg.MaxVartype = bigType
|
||||
continue
|
||||
}
|
||||
*/
|
||||
|
||||
// don't format or change anything when not in a "message {" section
|
||||
if !inMessage {
|
||||
|
@ -144,7 +175,7 @@ func protoReformat(filename string) error {
|
|||
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)
|
||||
|
@ -160,6 +191,45 @@ func protoReformat(filename string) error {
|
|||
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
|
||||
func tokenMsgVar(line string) (string, string, string, string) {
|
||||
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() {
|
||||
line := allTheLines.Next()
|
||||
if strings.HasPrefix(line, "}") {
|
||||
|
@ -218,6 +288,7 @@ func getInceptionMsg(curmsg *FormatMsg) {
|
|||
func formatMessage2(curmsg *FormatMsg) []string {
|
||||
var newmsg []string
|
||||
|
||||
newmsg = append(newmsg, curmsg.Header) // +" //header")
|
||||
// find the max length of varname and vartype
|
||||
for _, line := range curmsg.Lines {
|
||||
parts := strings.Split(line, ";")
|
||||
|
@ -261,6 +332,7 @@ func formatMessage2(curmsg *FormatMsg) []string {
|
|||
newline = strings.TrimRight(newline, " ")
|
||||
newmsg = append(newmsg, newline)
|
||||
}
|
||||
newmsg = append(newmsg, curmsg.Footer) // +" //footer")
|
||||
return newmsg
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue