mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 16:52:18 +01:00
Have dumbindent preserve indent on the first line
This commit is contained in:
1 parent
229a0c5af4
commit
b14e3eae9f
2 files changed
+32
-10
No files matched your search
@@ -159,13 +159,6 @@ type Options struct {
|
|||||||
//
|
//
|
||||||
// Passing a nil opts is valid and equivalent to passing &Options{}.
|
// Passing a nil opts is valid and equivalent to passing &Options{}.
|
||||||
func FormatBytes(dst []byte, src []byte, opts *Options) []byte {
|
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
|
indentBytes := spaces
|
||||||
indentCount := 2
|
indentCount := 2
|
||||||
if opts != nil {
|
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.
|
nBlankLines := 0 // The number of preceding blank lines.
|
||||||
nBraces := 0 // The number of unbalanced '{'s.
|
nBraces := 0 // The number of unbalanced '{'s.
|
||||||
nParens := 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).
|
// Handle preprocessor lines (#ifdef, #pragma, etc).
|
||||||
if preproc || (line[0] == '#') {
|
if preproc || (line[0] == '#') {
|
||||||
|
indent := initialIndent
|
||||||
if preproc {
|
if preproc {
|
||||||
dst = appendRepeatedBytes(dst, indentBytes, indentCount*2)
|
indent += indentCount * 2
|
||||||
}
|
}
|
||||||
line = trimTrailingWhiteSpace(line)
|
line = trimTrailingWhiteSpace(line)
|
||||||
|
dst = appendRepeatedBytes(dst, indentBytes, indent)
|
||||||
dst = append(dst, line...)
|
dst = append(dst, line...)
|
||||||
dst = append(dst, '\n')
|
dst = append(dst, '\n')
|
||||||
hanging = false
|
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,
|
// Output indentation. Dumbindent's default, 2 spaces per indent level,
|
||||||
// roughly approximates clang-format's default style.
|
// roughly approximates clang-format's default style.
|
||||||
indent := 0
|
indent := initialIndent
|
||||||
if nBraces > 0 {
|
if nBraces > 0 {
|
||||||
indent += indentCount * nBraces
|
indent += indentCount * nBraces
|
||||||
}
|
}
|
||||||
@@ -358,6 +365,17 @@ func appendRepeatedBytes(dst []byte, repeatedBytes []byte, number int) []byte {
|
|||||||
return dst
|
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
|
// lastNonWhiteSpace returns the 'z' in "abc xyz ". It returns '\x00' if s
|
||||||
// consists entirely of spaces or tabs.
|
// consists entirely of spaces or tabs.
|
||||||
func lastNonWhiteSpace(s []byte) byte {
|
func lastNonWhiteSpace(s []byte) byte {
|
||||||
|
|||||||
@@ -31,6 +31,10 @@ func TestFormatBytes(tt *testing.T) {
|
|||||||
// Braces.
|
// Braces.
|
||||||
src: "foo{\nbar\n }\nbaz\n",
|
src: "foo{\nbar\n }\nbaz\n",
|
||||||
want: "foo{\n bar\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.
|
// Parentheses.
|
||||||
src: "i = (j +\nk)\np = q\n",
|
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",
|
want: "a['key'] = `{\n\n\nX` ;\nb = {\n c = 0\n",
|
||||||
}, {
|
}, {
|
||||||
// Slash-star comment.
|
// 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",
|
want: "a['key'] = /*{\n\n\nX*/ ;\nb = {\n c = 0\n",
|
||||||
}, {
|
}, {
|
||||||
// Nested blocks with label.
|
// Nested blocks with label.
|
||||||
|
|||||||
Reference in new issue
Block a user