Have dumbindent preserve indent on the first line

This commit is contained in:
Nigel Tao committed 2020-07-12 09:29:48 +10:00
1 parent 229a0c5af4
commit b14e3eae9f
2 files changed
+32 -10

No files matched your search

+27 -9
View File
@@ -159,13 +159,6 @@ type Options struct {
//
// Passing a nil opts is valid and equivalent to passing &Options{}.
func FormatBytes(dst []byte, src []byte, opts *Options) []byte {
src = trimLeadingWhiteSpaceAndNewLines(src)
if len(src) == 0 {
return dst
} else if len(dst) == 0 {
dst = make([]byte, 0, len(src)+(len(src)/2))
}
indentBytes := spaces
indentCount := 2
if opts != nil {
@@ -177,6 +170,18 @@ func FormatBytes(dst []byte, src []byte, opts *Options) []byte {
}
}
// Count the number of leading spaces (or tabs) on the first line. Every
// output line starts with that much indent, before further indentation
// from unbalanced braces and parentheses.
initialIndent := countInitialOccurrences(src, indentBytes[0])
src = trimLeadingWhiteSpaceAndNewLines(src)
if len(src) == 0 {
return dst
} else if len(dst) == 0 {
dst = make([]byte, 0, len(src)+(len(src)/2))
}
nBlankLines := 0 // The number of preceding blank lines.
nBraces := 0 // The number of unbalanced '{'s.
nParens := 0 // The number of unbalanced '('s.
@@ -203,10 +208,12 @@ func FormatBytes(dst []byte, src []byte, opts *Options) []byte {
// Handle preprocessor lines (#ifdef, #pragma, etc).
if preproc || (line[0] == '#') {
indent := initialIndent
if preproc {
dst = appendRepeatedBytes(dst, indentBytes, indentCount*2)
indent += indentCount * 2
}
line = trimTrailingWhiteSpace(line)
dst = appendRepeatedBytes(dst, indentBytes, indent)
dst = append(dst, line...)
dst = append(dst, '\n')
hanging = false
@@ -237,7 +244,7 @@ func FormatBytes(dst []byte, src []byte, opts *Options) []byte {
// Output indentation. Dumbindent's default, 2 spaces per indent level,
// roughly approximates clang-format's default style.
indent := 0
indent := initialIndent
if nBraces > 0 {
indent += indentCount * nBraces
}
@@ -358,6 +365,17 @@ func appendRepeatedBytes(dst []byte, repeatedBytes []byte, number int) []byte {
return dst
}
// countInitialOccurrences returns the length of the longest prefix of s that
// consists entirely of x. Passing ("aaabacdaa", 'a') returns 3.
func countInitialOccurrences(s []byte, x byte) int {
for i, c := range s {
if c != x {
return i
}
}
return len(s)
}
// lastNonWhiteSpace returns the 'z' in "abc xyz ". It returns '\x00' if s
// consists entirely of spaces or tabs.
func lastNonWhiteSpace(s []byte) byte {
+5 -1
View File
@@ -31,6 +31,10 @@ func TestFormatBytes(tt *testing.T) {
// Braces.
src: "foo{\nbar\n }\nbaz\n",
want: "foo{\n bar\n}\nbaz\n",
}, {
// Braces with initial indent, giving every output line +3 spaces.
src: " foo{\nbar\n }\nbaz\n",
want: " foo{\n bar\n }\n baz\n",
}, {
// Parentheses.
src: "i = (j +\nk)\np = q\n",
@@ -61,7 +65,7 @@ func TestFormatBytes(tt *testing.T) {
want: "a['key'] = `{\n\n\nX` ;\nb = {\n c = 0\n",
}, {
// Slash-star comment.
src: " a['key'] = /*{\n\n\nX*/ ; \nb = {\nc = 0\n",
src: "\n a['key'] = /*{\n\n\nX*/ ; \nb = {\nc = 0\n",
want: "a['key'] = /*{\n\n\nX*/ ;\nb = {\n c = 0\n",
}, {
// Nested blocks with label.