From 58c0862c500485f4457ac81798db897bd78f0cf6 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Wed, 10 Jun 2020 12:22:41 +1000 Subject: [PATCH] Stop dumbindent collapsing consecutive blank lines --- internal/cgen/cgen.go | 12 ++++++++-- internal/cgen/func.go | 5 +++- lib/dumbindent/dumbindent.go | 38 +++++++++++++++++++------------ lib/dumbindent/dumbindent_test.go | 4 ++-- 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/internal/cgen/cgen.go b/internal/cgen/cgen.go index c5204ba5..a642ffe7 100644 --- a/internal/cgen/cgen.go +++ b/internal/cgen/cgen.go @@ -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 { diff --git a/internal/cgen/func.go b/internal/cgen/func.go index 8a7e6e79..bba27d6e 100644 --- a/internal/cgen/func.go +++ b/internal/cgen/func.go @@ -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() { diff --git a/lib/dumbindent/dumbindent.go b/lib/dumbindent/dumbindent.go index 4ac68f10..c7c93091 100644 --- a/lib/dumbindent/dumbindent.go +++ b/lib/dumbindent/dumbindent.go @@ -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 { diff --git a/lib/dumbindent/dumbindent_test.go b/lib/dumbindent/dumbindent_test.go index c512c261..1357edbb 100644 --- a/lib/dumbindent/dumbindent_test.go +++ b/lib/dumbindent/dumbindent_test.go @@ -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",