summaryrefslogtreecommitdiff
path: root/protoReformat.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-03-26 10:07:29 -0500
committerJeff Carr <[email protected]>2025-03-26 20:44:06 -0500
commit255f08f81f35750f6ab5975175b5ee1fda8d027b (patch)
treea5d7b135d41e0b2b91f7206f9e0af580bf5e67f3 /protoReformat.go
parentf3b9d40dfdbadb3255b7e06ab63ab268d09ae316 (diff)
still not right
Diffstat (limited to 'protoReformat.go')
-rw-r--r--protoReformat.go93
1 files changed, 56 insertions, 37 deletions
diff --git a/protoReformat.go b/protoReformat.go
index ee3d2fa..87eaf1a 100644
--- a/protoReformat.go
+++ b/protoReformat.go
@@ -25,7 +25,6 @@ func protoReformat(filename string) error {
return err
}
- var inMessage bool
var newfile string
var fmtmsg *FormatMsg
fmtmsg = new(FormatMsg)
@@ -33,6 +32,7 @@ func protoReformat(filename string) error {
var bigName int64
var bigType int64
+ var inMessage bool
var allLinesIter iter.Seq[string]
allLinesIter = makeLineIter(data)
// gets the max vartype and varname
@@ -45,7 +45,7 @@ func protoReformat(filename string) error {
// find the end of the message
if strings.HasPrefix(line, "}") {
inMessage = false
- formatMessage2(fmtmsg)
+ setMaxSizes(fmtmsg)
if bigName < fmtmsg.MaxVarname {
bigName = fmtmsg.MaxVarname
}
@@ -91,6 +91,9 @@ func protoReformat(filename string) error {
newmsg.MaxVartype = bigType
newmsg.Lines = append(newmsg.Lines, line)
getInceptionEnum(newmsg)
+ for _, newline := range formatMessage(newmsg) {
+ newfile += fmt.Sprintln(newline)
+ }
continue
}
@@ -112,17 +115,20 @@ func protoReformat(filename string) error {
newmsg.MaxVartype = bigType
newmsg.Header = line
getInceptionEnum(newmsg)
+ for _, newline := range formatEnum(newmsg) {
+ newfile += fmt.Sprintln(newline)
+ }
continue
}
if strings.HasPrefix(line, "message ") {
- newmsg := FormatMsg{
- MaxVarname: bigName,
- MaxVartype: bigType,
- Header: line,
- }
- getInceptionMsg(&newmsg)
- for _, newline := range formatMessage2(&newmsg) {
+ newmsg := new(FormatMsg)
+ newmsg.MaxVarname = bigName
+ newmsg.MaxVartype = bigType
+ newmsg.Header = line
+
+ getInceptionMsg(newmsg)
+ for _, newline := range formatMessage(newmsg) {
newfile += fmt.Sprintln(newline)
}
/*
@@ -153,29 +159,7 @@ func protoReformat(filename string) error {
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
- if !inMessage {
- newfile += fmt.Sprintln(line)
- continue
- }
-
- // fmtmsg.Lines = append(fmtmsg.Lines, line)
+ newfile += fmt.Sprintln(line)
}
pf, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
@@ -285,11 +269,8 @@ func getInceptionEnum(curmsg *FormatMsg) {
}
}
-func formatMessage2(curmsg *FormatMsg) []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
+func setMaxSizes(curmsg *FormatMsg) {
for _, line := range curmsg.Lines {
parts := strings.Split(line, ";")
if len(parts) < 2 {
@@ -305,6 +286,44 @@ func formatMessage2(curmsg *FormatMsg) []string {
curmsg.MaxVarname = int64(len(varname))
}
}
+}
+
+func formatEnum(curmsg *FormatMsg) []string {
+ var newmsg []string
+ newmsg = append(newmsg, curmsg.Header) // +" //header")
+
+ for _, line := range curmsg.Lines {
+ line = strings.TrimSpace(line)
+ newmsg = append(newmsg, line)
+ }
+
+ newmsg = append(newmsg, curmsg.Footer) // +" //footer")
+ return newmsg
+}
+
+func formatMessage(curmsg *FormatMsg) []string {
+ var newmsg []string
+ newmsg = append(newmsg, curmsg.Header) // +" //header")
+
+ // find the max length of varname and vartype
+ setMaxSizes(curmsg)
+ /*
+ for _, line := range curmsg.Lines {
+ parts := strings.Split(line, ";")
+ if len(parts) < 2 {
+ // line is blank or just a comment
+ continue
+ }
+
+ vartype, varname, _, _ := tokenMsgVar(line)
+ if len(vartype) > int(curmsg.MaxVartype) {
+ curmsg.MaxVartype = int64(len(vartype))
+ }
+ if len(varname) > int(curmsg.MaxVarname) {
+ curmsg.MaxVarname = int64(len(varname))
+ }
+ }
+ */
for _, line := range curmsg.Lines {
line = strings.TrimSpace(line)