summaryrefslogtreecommitdiff
path: root/protoReformat.go
diff options
context:
space:
mode:
Diffstat (limited to 'protoReformat.go')
-rw-r--r--protoReformat.go80
1 files changed, 77 insertions, 3 deletions
diff --git a/protoReformat.go b/protoReformat.go
index e7b757b..1739288 100644
--- a/protoReformat.go
+++ b/protoReformat.go
@@ -5,6 +5,7 @@ package main
import (
"fmt"
+ "iter"
"os"
"strings"
@@ -15,6 +16,7 @@ import (
var maxVarname int
var maxVartype int
+var linesIter iter.Seq[string]
func protoReformat(filename string) error {
// read in the .proto file
@@ -27,9 +29,13 @@ func protoReformat(filename string) error {
var inMessage bool
var curmsg []string
var newfile string
+ var fmtmsg *FormatMsg
+ fmtmsg = new(FormatMsg)
+
+ linesIter = makeLineIter(data)
// gets the max vartype and varname
- for _, line := range strings.Split(string(data), "\n") {
+ for line := range linesIter {
if strings.HasPrefix(line, "message ") {
inMessage = true
continue
@@ -39,7 +45,9 @@ func protoReformat(filename string) error {
if strings.HasPrefix(line, "}") {
inMessage = false
formatMessage(curmsg)
+ formatMessage2(fmtmsg)
curmsg = nil
+ fmtmsg = new(FormatMsg)
continue
}
@@ -48,11 +56,15 @@ func protoReformat(filename string) error {
continue
}
curmsg = append(curmsg, line)
+ fmtmsg.Lines = append(fmtmsg.Lines, line)
}
- // parse the proto file for message struct names
- for _, line := range strings.Split(string(data), "\n") {
+ // gets the max vartype and varname
+ for line := range linesIter {
if strings.HasPrefix(line, "message ") {
+ if inMessage {
+ // message inception. search for the architect. don't forget your totem
+ }
inMessage = true
parts := strings.Fields(line)
if len(parts) > 3 {
@@ -180,3 +192,65 @@ func slicesPop(parts []string) ([]string, string) {
end := parts[x-1]
return parts[0 : x-1], end
}
+
+// 'for x := range' syntax using the awesome golang 1.24 'iter'
+func makeLineIter(data []byte) iter.Seq[string] {
+ items := strings.Split(string(data), "\n")
+ // log.Println("Made All() Iter.Seq[] with length", len(items))
+ return func(yield func(string) bool) {
+ for _, v := range items {
+ if !yield(v) {
+ return
+ }
+ }
+ }
+}
+
+func formatMessage2(curmsg *FormatMsg) []string {
+ var newmsg []string
+
+ // find the max length of varname and vartype
+ 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) > maxVartype {
+ maxVartype = len(vartype)
+ }
+ if len(varname) > maxVarname {
+ maxVarname = len(varname)
+ }
+ }
+
+ for _, line := range curmsg.Lines {
+ line = strings.TrimSpace(line)
+ if line == "" {
+ newmsg = append(newmsg, line)
+ continue
+ }
+ if strings.HasPrefix(line, "//") {
+ pad := fmt.Sprintf("%d", maxVartype+maxVarname+21)
+ hmm := "%" + pad + "s %s"
+ line = fmt.Sprintf(hmm, " ", line) // todo: compute 50
+ newmsg = append(newmsg, line)
+ continue
+ }
+ mt := fmt.Sprintf("%d", maxVartype)
+ mv := fmt.Sprintf("%d", maxVarname)
+
+ hmm := " %-" + mt + "s %-" + mv + "s = %-3s %s"
+
+ vartype, varname, id, end := tokenMsgVar(line)
+ end = strings.TrimSpace(end)
+ id = id + ";"
+
+ newline := fmt.Sprintf(hmm, vartype, varname, id, end)
+ newline = strings.TrimRight(newline, " ")
+ newmsg = append(newmsg, newline)
+ }
+ return newmsg
+}