diff options
| author | Jeff Carr <[email protected]> | 2025-02-01 07:32:00 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-02-01 07:32:00 -0600 |
| commit | 9a0e4a6d62d58d51d3257b588242f442bddc40da (patch) | |
| tree | 43dfc96f604cf2427e4dd6d67ad64c180d0328cb /protoReformat.go | |
| parent | cfedee740ce59638de70382652a3d637398bcab2 (diff) | |
auto formatting is working well enough
Diffstat (limited to 'protoReformat.go')
| -rw-r--r-- | protoReformat.go | 44 |
1 files changed, 31 insertions, 13 deletions
diff --git a/protoReformat.go b/protoReformat.go index 26fae1f..580d920 100644 --- a/protoReformat.go +++ b/protoReformat.go @@ -21,15 +21,9 @@ func protoReformat(filename string) error { return err } - pf, err := os.OpenFile(filename+".new", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) - if err != nil { - log.Info("file open error. permissions?", filename, err) - return err - } - defer pf.Close() - var inMessage bool var curmsg []string + var newfile string // gets the max vartype and varname for _, line := range strings.Split(string(data), "\n") { @@ -59,6 +53,7 @@ func protoReformat(filename string) error { 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 := maxVarname + maxVartype + 16 - len(start) @@ -66,29 +61,39 @@ func protoReformat(filename string) error { hmm := "%s %" + pad + "s %s" line = fmt.Sprintf(hmm, start, " ", end) } - fmt.Fprintln(pf, line) + 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 formatMessage(curmsg) { - fmt.Fprintln(pf, newline) + newfile += fmt.Sprintln(newline) } - fmt.Fprintln(pf, line) + newfile += fmt.Sprintln(line) curmsg = nil continue } // don't format or change anything when not in a "message {" section if !inMessage { - fmt.Fprintln(pf, line) + newfile += fmt.Sprintln(line) continue } curmsg = append(curmsg, line) } + pf, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + log.Info("file open error. permissions?", filename, err) + return err + } + newfile = strings.TrimSpace(newfile) + fmt.Fprintln(pf, newfile) + pf.Close() + // for i, s := range slices.Backward(pf.ToSort) { return nil } @@ -100,8 +105,8 @@ func formatMessage(curmsg []string) []string { for _, line := range curmsg { parts := strings.Split(line, ";") if len(parts) < 2 { - log.Info("parse error", line) - return curmsg + // line is blank or just a comment + continue } vartype, varname, _, _ := tokenMsgVar(line) @@ -114,6 +119,18 @@ func formatMessage(curmsg []string) []string { } for _, line := range curmsg { + 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) @@ -124,6 +141,7 @@ func formatMessage(curmsg []string) []string { id = id + ";" newline := fmt.Sprintf(hmm, vartype, varname, id, end) + newline = strings.TrimRight(newline, " ") newmsg = append(newmsg, newline) } return newmsg |
