Stop dumbindent collapsing consecutive blank lines

This commit is contained in:
Nigel Tao committed 2020-06-10 12:22:41 +10:00
1 parent ed48514f30
commit 58c0862c50
4 files changed
+39 -20

No files matched your search

+10 -2
View File
@@ -679,13 +679,17 @@ func (g *gen) genHeader(b *buffer) error {
b.writes("// ---------------- Status Codes\n\n")
wroteStatus := false
for _, z := range g.statusList {
if !z.fromThisPkg || !z.public {
continue
}
b.printf("extern const char* %s;\n", z.cName)
wroteStatus = true
}
if wroteStatus {
b.writes("\n")
}
b.writes("\n")
b.writes("// ---------------- Public Consts\n\n")
if err := g.forEachConst(b, pubOnly, (*gen).writeConst); err != nil {
@@ -782,13 +786,17 @@ func (g *gen) genImpl(b *buffer) error {
b.writes("// ---------------- Status Codes Implementations\n\n")
wroteStatus := false
for _, z := range g.statusList {
if !z.fromThisPkg || z.msg == "" {
continue
}
b.printf("const char* %s = \"%s%s: %s\";\n", z.cName, z.msg[:1], g.pkgName, z.msg[1:])
wroteStatus = true
}
if wroteStatus {
b.writes("\n")
}
b.writes("\n")
b.writes("// ---------------- Private Consts\n\n")
if err := g.forEachConst(b, priOnly, (*gen).writeConst); err != nil {
+4 -1
View File
@@ -368,10 +368,13 @@ func (g *gen) writeFuncImplPrologue(b *buffer) error {
b.writes("\n")
// Generate the local variables.
oldLenB := len(*b)
if err := g.writeVars(b, &g.currFunk, false); err != nil {
return err
}
b.writes("\n")
if oldLenB != len(*b) {
b.writes("\n")
}
if g.currFunk.derivedVars != nil {
for _, o := range g.currFunk.astFunc.In().Fields() {
+23 -15
View File
@@ -122,8 +122,9 @@ var (
namespace = []byte("namespace ")
starSlash = []byte("*/")
spaces = []byte(" ")
tabs = []byte("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t")
newLines = []byte("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
spaces = []byte(" ")
tabs = []byte("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t")
)
// hangingBytes is a look-up table for updating the hanging variable.
@@ -172,11 +173,11 @@ func FormatBytes(dst []byte, src []byte, opts *Options) []byte {
}
}
nBlankLines := 0 // The number of preceding blank lines.
nBraces := 0 // The number of unbalanced '{'s.
nParens := 0 // The number of unbalanced '('s.
openBrace := false // Whether the previous non-blank line ends with '{'.
hanging := false // Whether the previous non-blank line ends with '=' or '\\'.
blankLine := false // Whether the previous line was blank.
outer:
for line, remaining := src, []byte(nil); len(src) > 0; src = remaining {
@@ -193,14 +194,14 @@ outer:
// - immediately before a '}',
// - at the end of file.
if len(line) == 0 {
blankLine = true
nBlankLines++
continue
}
if blankLine {
blankLine = false
if nBlankLines > 0 {
if !openBrace && (line[0] != '}') {
dst = append(dst, '\n')
dst = appendRepeatedBytes(dst, newLines, nBlankLines)
}
nBlankLines = 0
}
// Preprocessor lines (#ifdef, #pragma, etc) are never indented.
@@ -243,14 +244,7 @@ outer:
if (nParens > 0) || hanging {
indent += indentCount * 2
}
for indent > 0 {
n := indent
if n > len(indentBytes) {
n = len(indentBytes)
}
dst = append(dst, indentBytes[:n]...)
indent -= n
}
dst = appendRepeatedBytes(dst, indentBytes, indent)
// Output the leading '}'s.
dst = append(dst, line[:closeBraces]...)
@@ -364,6 +358,20 @@ func trimTrailingWhiteSpace(s []byte) []byte {
return s
}
// appendRepeatedBytes appends number copies of a byte, assuming that
// repeatedBytes' elements are all the same byte.
func appendRepeatedBytes(dst []byte, repeatedBytes []byte, number int) []byte {
for number > 0 {
n := number
if n > len(repeatedBytes) {
n = len(repeatedBytes)
}
dst = append(dst, repeatedBytes[:n]...)
number -= n
}
return dst
}
// lastNonWhiteSpace returns the 'z' in "abc xyz ". It returns '\x00' if s
// consists entirely of spaces or tabs.
func lastNonWhiteSpace(s []byte) byte {
+2 -2
View File
@@ -44,9 +44,9 @@ func TestFormatBytes(tt *testing.T) {
src: "int x = // comm.\ny;\n",
want: "int x = // comm.\n y;\n",
}, {
// Most blank lines should be dropped.
// Consecutive blank lines.
src: "f {\n\n\ng;\n\n\nh;\n\n\n}\n\n",
want: "f {\n g;\n\n h;\n}\n",
want: "f {\n g;\n\n\n h;\n}\n",
}, {
// Single-quote string.
src: "a = '{'\nb = {\nc = 0\n",