Skip to content

add separator for long source lines #376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions transpiler/gofmt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package transpiler

import (
"strings"
)

// Examples:
// from:
// var di [][][]byte = [][][]byte{[][]byte{[]byte("cq\x00"), []byte(";\x00")}, [][]byte{[]byte("pl\x00"), []byte("+\x00")}, [][]byte{[]byte("hy\x00"), []byte("-\x00")}, [][]byte{[]byte("sl\x00"), []byte("/\x00")}}
// to:
// var di [][][]byte = [][][]byte{
// [][]byte{[]byte("cq\x00"), []byte(";\x00")},
// [][]byte{[]byte("pl\x00"), []byte("+\x00")},
// [][]byte{[]byte("hy\x00"), []byte("-\x00")},
// [][]byte{[]byte("sl\x00"), []byte("/\x00")},
// }
//
// from:
// noarch.Printf([]byte("%d not ok - %s:%d: \x00"), current_test, []byte("/home/konstantin/go/src/github.com/Konstantin8105/c4go/tests/init.c\x00"), 13)
// to:
// noarch.Printf(
// []byte("%d not ok - %s:%d: \x00"),
// current_test,
// []byte("/home/konstantin/go/src/github.com/Konstantin8105/c4go/tests/init.c\x00"),
// 13,
// )

const maxLineSymbol int = 100

func addBreaklines(src string) string {
// separate long lines by comma
lines := strings.Split(src, "\n")
for i := range lines {
line := lines[i]
if len(line) > maxLineSymbol {
line = formatting(line)
}
}

return strings.Join(lines, "\n")
}

func formatting(line string) string {
// parens
// * ()
// * []
// * {}
// * ""
// separator :
// * ,

// check last byte
if line[len(line)-1] != '}' && line[len(line)-1] != ')' {
return line
}
if !strings.Contains(line, "=") {
return line
}

// initialization levels
levels := make([]int, len(line))
for i := 0; i < len(line); i++ {
levels[i] = -1
}

// add levels for parens - "
for i, isInside := 0, false; i < len(line); i++ {
if isInside {
levels[i] = 0
}
if line[i] == '"' {
isInside = !isInside
}
}

_ = levels

return line
}
2 changes: 2 additions & 0 deletions transpiler/transpiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ func TranspileAST(fileName, packageName string, withOutsideStructs bool,
source += getVaListStruct()
}

source = addBreaklines(source)

return
}

Expand Down