summaryrefslogtreecommitdiff
path: root/protoReformat.go
blob: e7b757b84b6815063d0d03f570971f722ee4c176 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"fmt"
	"os"
	"strings"

	"go.wit.com/log"
)

// like 'goimport' but for .proto files

var maxVarname int
var maxVartype int

func protoReformat(filename string) error {
	// read in the .proto file
	data, err := os.ReadFile(filename)
	if err != nil {
		log.Info("file read failed", filename, err)
		return err
	}

	var inMessage bool
	var curmsg []string
	var newfile string

	// gets the max vartype and varname
	for _, line := range strings.Split(string(data), "\n") {
		if strings.HasPrefix(line, "message ") {
			inMessage = true
			continue
		}

		// find the end of the message
		if strings.HasPrefix(line, "}") {
			inMessage = false
			formatMessage(curmsg)
			curmsg = nil
			continue
		}

		// don't format or change anything when not in a "message {" section
		if !inMessage {
			continue
		}
		curmsg = append(curmsg, line)
	}

	// parse the proto file for message struct names
	for _, line := range strings.Split(string(data), "\n") {
		if strings.HasPrefix(line, "message ") {
			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)
				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 formatMessage(curmsg) {
				newfile += fmt.Sprintln(newline)
			}
			newfile += fmt.Sprintln(line)
			curmsg = nil
			continue
		}

		// don't format or change anything when not in a "message {" section
		if !inMessage {
			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
}

func formatMessage(curmsg []string) []string {
	var newmsg []string

	// find the max length of varname and vartype
	for _, line := range curmsg {
		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 {
		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
}

// returns vartype, varname, id, end
func tokenMsgVar(line string) (string, string, string, string) {
	parts := strings.Split(line, ";")
	front := parts[0]
	end := strings.Join(parts[1:], ";")

	var id string
	var varname string
	var vartype string

	parts = strings.Fields(front)
	parts, id = slicesPop(parts)
	parts, _ = slicesPop(parts) // this is the "=" sign
	parts, varname = slicesPop(parts)
	vartype = strings.Join(parts, " ")

	return vartype, varname, id, end
}

func slicesPop(parts []string) ([]string, string) {
	if len(parts) == 0 {
		return nil, ""
	}
	if len(parts) == 1 {
		return nil, parts[0]
	}
	x := len(parts)
	end := parts[x-1]
	return parts[0 : x-1], end
}