From b14e3eae9faefdf3568f60148f3a6dc49e8be176 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 12 Jul 2020 09:29:48 +1000 Subject: [PATCH] Have dumbindent preserve indent on the first line --- lib/dumbindent/dumbindent.go | 36 +++++++++++++++++++++++-------- lib/dumbindent/dumbindent_test.go | 6 +++++- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/lib/dumbindent/dumbindent.go b/lib/dumbindent/dumbindent.go index cda2c355..c73f7577 100644 --- a/lib/dumbindent/dumbindent.go +++ b/lib/dumbindent/dumbindent.go @@ -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 { diff --git a/lib/dumbindent/dumbindent_test.go b/lib/dumbindent/dumbindent_test.go index 2db988b1..a93f1a79 100644 --- a/lib/dumbindent/dumbindent_test.go +++ b/lib/dumbindent/dumbindent_test.go @@ -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.