summaryrefslogtreecommitdiff
path: root/protoReformat.go
diff options
context:
space:
mode:
Diffstat (limited to 'protoReformat.go')
-rw-r--r--protoReformat.go146
1 files changed, 83 insertions, 63 deletions
diff --git a/protoReformat.go b/protoReformat.go
index 07baf76..5cd685d 100644
--- a/protoReformat.go
+++ b/protoReformat.go
@@ -40,13 +40,12 @@ func (msg *StdMessage) name() string {
}
type Message interface {
- format() []string
name() string
load()
addMsg(Message)
}
-func protoReformat(filename string) error {
+func protoReformatComments(filename string) error {
// read in the .proto file
data, err := os.ReadFile(filename)
if err != nil {
@@ -56,7 +55,6 @@ func protoReformat(filename string) error {
var newfile string
- /* check the comment preprocessor
log.Info("filename", filename)
alltest := makeLineIter(data)
// gets the max vartype and varname
@@ -64,8 +62,18 @@ func protoReformat(filename string) error {
newfile += fmt.Sprintln(commentPreprocessor(line))
}
saveFile(filename, newfile)
- os.Exit(-1)
- */
+ return nil
+}
+
+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 newfile string
var fmtmsg *FormatMsg
fmtmsg = new(FormatMsg)
@@ -109,11 +117,12 @@ func protoReformat(filename string) error {
// write out the messages
allTheLines = newLinesScanner(strings.Split(string(data), "\n"))
for allTheLines.Scan() {
- line := allTheLines.Next()
+ line := allTheLines.NextRaw()
+
if strings.HasPrefix(line, "oneof ") {
newmsg := fmtmsg.newOneofMessage(line)
newmsg.load()
- for _, newline := range newmsg.format() {
+ for _, newline := range newmsg.msgPB.format() {
newfile += fmt.Sprintln(newline)
}
continue
@@ -123,7 +132,7 @@ func protoReformat(filename string) error {
newmsg := fmtmsg.newEnumMessage(line)
newmsg.load()
// loadEnumDefinition(newmsg)
- for _, newline := range newmsg.format() {
+ for _, newline := range newmsg.msgPB.format() {
newfile += fmt.Sprintln(newline)
}
continue
@@ -133,7 +142,7 @@ func protoReformat(filename string) error {
newmsg := fmtmsg.newStdMessage(line)
newmsg.load()
log.Info("got to message", line)
- for _, newline := range newmsg.format() {
+ for _, newline := range newmsg.msgPB.format() {
newfile += fmt.Sprintln(newline)
}
continue
@@ -215,13 +224,13 @@ func (msg *StdMessage) load() {
if strings.HasPrefix(line, "oneof ") {
newmsg := msg.msgPB.newOneofMessage(line)
newmsg.load()
- curPB = newmsg.msgPB
+ // curPB = newmsg.msgPB
continue
}
if strings.HasPrefix(line, "enum ") {
newmsg := msg.msgPB.newEnumMessage(line)
newmsg.load()
- curPB = newmsg.msgPB
+ // curPB = newmsg.msgPB
// loadEnumDefinition(newmsg)
continue
}
@@ -229,14 +238,14 @@ func (msg *StdMessage) load() {
// message inception. search for the architect. don't forget your totem
newmsg := msg.msgPB.newStdMessage(line)
newmsg.load()
- curPB = newmsg.msgPB
+ // curPB = newmsg.msgPB
continue
}
if strings.HasPrefix(line, "}") {
msg.msgPB.Footer = line
return
}
- curPB.Notes = append(curPB.Notes, line)
+ curPB.Lines = append(curPB.Lines, line)
// fmtmsg.Lines = append(fmtmsg.Lines, line)
}
@@ -319,36 +328,54 @@ func setMaxSizes(curmsg *FormatMsg) {
}
}
-func (curmsg *FormatMsg) format() []string {
- return formatEnum(curmsg)
+// use this for header and footer lines
+func (msg *FormatMsg) padBase() string {
+ var pad string
+ for i := 1; i < int(msg.Depth); i += 1 {
+ pad += fmt.Sprintf("%8s", " ")
+ }
+ return pad
}
-func (curmsg *EnumMessage) format() []string {
- return formatEnum(curmsg.msgPB)
+// use this for lines inside the message
+func (msg *FormatMsg) pad() string {
+ var pad string
+ for i := 0; i < int(msg.Depth); i += 1 {
+ pad += fmt.Sprintf("%8s", " ")
+ }
+ return pad
}
func formatEnum(curmsg *FormatMsg) []string {
var newmsg []string
- newmsg = append(newmsg, curmsg.Header) // +" //header")
+ header := fmt.Sprintf("%s%s // enum depth=%d", curmsg.padBase(), curmsg.Header, curmsg.Depth)
+ newmsg = append(newmsg, header)
for _, line := range curmsg.Lines {
- line = " " + strings.TrimSpace(line)
+ line = fmt.Sprintf("%s%s", curmsg.pad(), line)
newmsg = append(newmsg, line)
}
- newmsg = append(newmsg, curmsg.Footer) // +" //footer")
+ footer := fmt.Sprintf("%s%s // enum footer depth=%d", curmsg.padBase(), curmsg.Footer, curmsg.Depth)
+ newmsg = append(newmsg, footer)
return newmsg
}
-func (curmsg *StdMessage) format() []string {
- return formatMessage(curmsg.msgPB)
+func (msg *FormatMsg) format() []string {
+ switch msg.Type {
+ case FormatMsg_ENUM:
+ return formatEnum(msg)
+ case FormatMsg_MESSAGE:
+ return formatMessage(msg)
+ }
+ return formatMessage(msg)
}
func formatMessage(curmsg *FormatMsg) []string {
var newmsg []string
if curmsg.Header != "" {
- line := curmsg.Header
+ line := fmt.Sprintf("%s%s // msg depth=%d", curmsg.padBase(), curmsg.Header, curmsg.Depth)
parts := strings.Fields(line)
if len(parts) > 3 {
// hack to actually indent comments on the message line itself. you're welcome
@@ -356,51 +383,34 @@ func formatMessage(curmsg *FormatMsg) []string {
end := strings.Join(parts[3:], " ")
offset := int(curmsg.MaxVarname) + int(curmsg.MaxVartype) + 16 - len(start)
pad := fmt.Sprintf("%d", offset)
- hmm := "%s %" + pad + "s %s"
- line = fmt.Sprintf(hmm, start, " ", end)
+ hmm := "%s %" + pad + "s %s // depth=%d"
+ line = fmt.Sprintf(hmm, start, " ", end, curmsg.Depth)
+ } else {
+ line = fmt.Sprintf("%s // len(parts)=%d depth=%d", line, len(parts), curmsg.Depth)
}
- newmsg = append(newmsg, line) // +" //header")
+ newmsg = append(newmsg, line) // " //header")
+ } else {
+ newmsg = append(newmsg, "// ERROR: header was blank") // +" //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 _, msg := range curmsg.Enums {
- for _, newline := range formatEnum(msg) {
- newmsg = append(newmsg, newline)
- }
- }
- for _, msg := range curmsg.Oneofs {
- for _, newline := range formatEnum(msg) {
- newmsg = append(newmsg, newline)
+ for _, msg := range curmsg.Msgs {
+ switch msg.Type {
+ case FormatMsg_ENUM:
+ for _, line := range formatEnum(msg) {
+ line = fmt.Sprintf("%s%s", curmsg.pad(), line)
+ newmsg = append(newmsg, line)
}
- }
-
- for _, msg := range curmsg.Msgs {
- for _, newline := range msg.format() {
- newmsg = append(newmsg, newline)
+ case FormatMsg_MESSAGE:
+ for _, line := range formatMessage(msg) {
+ line = fmt.Sprintf("%s%s", curmsg.pad(), line)
+ newmsg = append(newmsg, line)
}
+ default:
}
- */
+ }
for _, line := range curmsg.Lines {
line = strings.TrimSpace(line)
@@ -455,7 +465,15 @@ func (it *LinesScanner) Scan() bool {
return true
}
-// Next() returns the next thing in the array
+// does no cleaning of the data
+func (it *LinesScanner) NextRaw() string {
+ if it.index-1 == len(it.things) {
+ fmt.Println("Next() error in LinesScanner", it.index)
+ }
+ return it.things[it.index-1]
+}
+
+// cleans out comments
func (it *LinesScanner) Next() string {
if it.index-1 == len(it.things) {
fmt.Println("Next() error in LinesScanner", it.index)
@@ -463,8 +481,8 @@ func (it *LinesScanner) Next() string {
// out := commentPreprocessor(it.things[it.index-1])
out := it.things[it.index-1]
out = commentPreprocessor(out)
- // return strings.TrimSpace(out)
- return out
+ return strings.TrimSpace(out)
+ // return out
}
// END DEFINE THE ITERATOR
@@ -481,11 +499,13 @@ func commentPreprocessor(line string) string {
var comments []string
for _, match := range matches {
comments = append(comments, strings.TrimSpace(match[1]))
+ // comments = append(comments, match[1])
}
// Remove the block comments from the original line
line = re.ReplaceAllString(line, "")
- line = strings.TrimSpace(line)
+ // line = strings.TrimSpace(line)
+ line = strings.TrimSuffix(line, " ")
// Append comments at the end with //
for _, comment := range comments {