Stop dumbindent adding or removing line breaks

This commit is contained in:
Nigel Tao committed 2020-06-10 21:51:25 +10:00
1 parent b82396a3c6
commit b1ea22045a
3 files changed
+54 -123

No files matched your search

+5 -5
View File
@@ -26,11 +26,11 @@
// default is 2 spaces per indent level. // default is 2 spaces per indent level.
// //
// It is similar in concept to pretty-printers like `indent` or `clang-format`. // It is similar in concept to pretty-printers like `indent` or `clang-format`.
// It is much dumber (it will not add line breaks or otherwise re-flow lines of // It is much dumber (it will not add or remove line breaks or otherwise
// code just to fit within an 80 column limit) but it can therefore be much // re-flow lines of code just to fit within an 80 column limit) but it can
// faster at the basic task of automatically indenting nested blocks. The // therefore be much faster at the basic task of automatically indenting nested
// output isn't 'perfect', but it's usually sufficiently readable if the input // blocks. The output isn't 'perfect', but it's usually sufficiently readable
// already has sensible line breaks. // if the input already has sensible line breaks.
// //
// To quantify "much faster", on this one C file, this program was 80 times // To quantify "much faster", on this one C file, this program was 80 times
// faster than `clang-format`, even without a column limit: // faster than `clang-format`, even without a column limit:
+44 -110
View File
@@ -17,11 +17,11 @@
// Package dumbindent formats C (and C-like) programs. // Package dumbindent formats C (and C-like) programs.
// //
// It is similar in concept to pretty-printers like `indent` or `clang-format`. // It is similar in concept to pretty-printers like `indent` or `clang-format`.
// It is much dumber (it will not add line breaks or otherwise re-flow lines of // It is much dumber (it will not add or remove line breaks or otherwise
// code just to fit within an 80 column limit) but it can therefore be much // re-flow lines of code just to fit within an 80 column limit) but it can
// faster at the basic task of automatically indenting nested blocks. The // therefore be much faster at the basic task of automatically indenting nested
// output isn't 'perfect', but it's usually sufficiently readable if the input // blocks. The output isn't 'perfect', but it's usually sufficiently readable
// already has sensible line breaks. // if the input already has sensible line breaks.
// //
// To quantify "much faster", on this one C file, `cmd/dumbindent` in this // To quantify "much faster", on this one C file, `cmd/dumbindent` in this
// repository was 80 times faster than `clang-format`, even without a column // repository was 80 times faster than `clang-format`, even without a column
@@ -47,15 +47,15 @@
// //
// ∙ '{' and '}' curly braces, // ∙ '{' and '}' curly braces,
// ∙ '(' and ')' round parentheses, // ∙ '(' and ')' round parentheses,
// ∙ ';' semi-colons (if they're not within parentheses), // ∙ '\n' line breaks,
// ∙ strings, comments and preprocessor directives, // ∙ ' ' spaces and '\t' tabs that start or end a line, and
// ∙ '\n' line breaks, and // ∙ strings, comments and preprocessor directives (in order to ignore any of
// ∙ ' ' spaces and '\t' tabs adjacent to any of the above. // the above special characters within them),
// //
// Everything else is an opaque byte. Consider this input: // Everything else is an opaque byte. Consider this input:
// //
// for (i = 0; i < 3; i++) { // for (i = 0; i < 3; i++) {
// j = 0; j++; // Semi-colons not within parentheses. // j = 0; // Ignore { in a comment.
// if (i < j) { foo(); } // if (i < j) { foo(); }
// u = (v + // u = (v +
// w); // w);
@@ -63,42 +63,42 @@
// //
// From the algorithm's point of view, this input is equivalent to: // From the algorithm's point of view, this input is equivalent to:
// //
// ... (.................) { // ....(.................).{
// .....; ...; //.................................... // .................................
// .. (.....) { ...(); } // ...(.....).{....()..}
// ... (... // ....(...
// .); // .);
// } // }
// //
// The formatted output (using the default of 2 spaces per indent level) is: // The formatted output (using the default of 2 spaces per indent level) is:
// //
// ... (.................) { // ....(.................).{
// .....; // .................................
// ...; //.................................... // ...(.....).{....()..}
// .. (.....) { // ....(...
// ...();
// }
// ... (...
// .); // .);
// } // }
// //
// Line breaks are generally forced after semi-colons (outside of a for loop's // Dumbindent adjusts lines horizontally (indenting) but not vertically (it
// triple expressions) and open-braces. Each output line is then indented // does not break or un-break lines, or collapse consecutive blank lines),
// according to the net number of open braces preceding it, although lines // although it will remove blank lines at the end of the input. In the example
// starting with close braces will outdent first, similar to `gofmt` style. A // above, it will not remove the "\n" between "u = (v +" and "w);", even though
// line whose start is inside a so-far-unbalanced open parenthesis, such as the // both lines are short.
// "w);" line above, gets 2 additional indent levels.
// //
// Dumbindent will remove some superfluous blank lines, but it will not remove // Each output line is indented according to the net number of open braces
// line breaks between consecutive non-empty lines, such as between "u = (v +" // preceding it, although lines starting with close braces will outdent first,
// and "w);" in the example above. // similar to `gofmt` style. A line which starts ins a so-far-unbalanced open
// parenthesis, such as the "w);" line above, gets 2 additional indent levels.
// //
// Dumbindent does not parse the input as C/C++ source code. In particular, the // Horizontal adjustment only affects a line's leading white space (and will
// algorithm has no interest in solving C++'s "most vexing parse" or // trim trailing white space). It does not affect white space within a line.
// determining whether "x*y" is a multiplication or a type definition (where // Dumbindent does not parse the input as C/C++ source code.
// "x" is a type and "y" is a pointer-typed variable, such as "int*p"). For a //
// type definition, where other formatting algorithms would re-write around the // In particular, the algorithm does not solve C++'s "most vexing parse" or
// "*" as either "x* y" or "x *y", dumbindent will not insert spaces. // otherwise determine whether "x*y" is a multiplication or a type definition
// (where y is a pointer-to-x typed variable, such as "int*p"). For a type
// definition, where other formatting algorithms would re-write around the "*"
// as either "x* y" or "x *y", dumbindent will not insert spaces.
// //
// Similarly, dumbindent will not correct this mis-indentation: // Similarly, dumbindent will not correct this mis-indentation:
// //
@@ -178,7 +178,6 @@ func FormatBytes(dst []byte, src []byte, opts *Options) []byte {
nParens := 0 // The number of unbalanced '('s. nParens := 0 // The number of unbalanced '('s.
hanging := false // Whether the previous non-blank line ends with '=' or '\\'. hanging := false // Whether the previous non-blank line ends with '=' or '\\'.
outer:
for line, remaining := src, []byte(nil); len(src) > 0; src = remaining { for line, remaining := src, []byte(nil); len(src) > 0; src = remaining {
src = trimLeadingWhiteSpace(src) src = trimLeadingWhiteSpace(src)
line, remaining = src, nil line, remaining = src, nil
@@ -231,8 +230,8 @@ outer:
nParens = 0 nParens = 0
} }
// Output a certain number of spaces to rougly approximate the // Output a certain number of spaces to roughly approximate
// "clang-format -style=Chromium" indentation style. // clang-format's default indentation style.
indent := 0 indent := 0
if nBraces > 0 { if nBraces > 0 {
indent += indentCount * nBraces indent += indentCount * nBraces
@@ -249,20 +248,12 @@ outer:
// Adjust the state according to the braces and parentheses within the // Adjust the state according to the braces and parentheses within the
// line (except for those in comments and strings). // line (except for those in comments and strings).
last := lastNonWhiteSpace(line) last := lastNonWhiteSpace(line)
inner: loop:
for { for {
for i, c := range line { for i, c := range line {
switch c { switch c {
case '{': case '{':
nBraces++ nBraces++
if breakAfterBrace(line[i+1:]) {
dst = append(dst, line[:i+1]...)
dst = append(dst, '\n')
restOfLine := line[i+1:]
remaining = src[lineLength-len(restOfLine):]
hanging = false
continue outer
}
case '}': case '}':
nBraces-- nBraces--
case '(': case '(':
@@ -270,16 +261,6 @@ outer:
case ')': case ')':
nParens-- nParens--
case ';':
if (nParens == 0) && (breakAfterSemicolon(line[i+1:])) {
dst = append(dst, line[:i+1]...)
dst = append(dst, '\n')
restOfLine := line[i+1:]
remaining = src[lineLength-len(restOfLine):]
hanging = false
continue outer
}
case '/': case '/':
if (i + 1) >= len(line) { if (i + 1) >= len(line) {
break break
@@ -287,7 +268,7 @@ outer:
if line[i+1] == '/' { if line[i+1] == '/' {
// A slash-slash comment. Skip the rest of the line. // A slash-slash comment. Skip the rest of the line.
last = lastNonWhiteSpace(line[:i]) last = lastNonWhiteSpace(line[:i])
break inner break loop
} else if line[i+1] == '*' { } else if line[i+1] == '*' {
// A slash-star comment. // A slash-star comment.
dst = append(dst, line[:i+2]...) dst = append(dst, line[:i+2]...)
@@ -295,7 +276,7 @@ outer:
restOfSrc := src[lineLength-len(restOfLine):] restOfSrc := src[lineLength-len(restOfLine):]
dst, line, remaining = handleRaw(dst, restOfSrc, starSlash) dst, line, remaining = handleRaw(dst, restOfSrc, starSlash)
last = lastNonWhiteSpace(line) last = lastNonWhiteSpace(line)
continue inner continue loop
} }
case '"', '\'': case '"', '\'':
@@ -303,7 +284,7 @@ outer:
suffix := skipCooked(line[i+1:], c) suffix := skipCooked(line[i+1:], c)
dst = append(dst, line[:len(line)-len(suffix)]...) dst = append(dst, line[:len(line)-len(suffix)]...)
line = suffix line = suffix
continue inner continue loop
case '`': case '`':
// A raw string. // A raw string.
@@ -312,10 +293,10 @@ outer:
restOfSrc := src[lineLength-len(restOfLine):] restOfSrc := src[lineLength-len(restOfLine):]
dst, line, remaining = handleRaw(dst, restOfSrc, backTick) dst, line, remaining = handleRaw(dst, restOfSrc, backTick)
last = lastNonWhiteSpace(line) last = lastNonWhiteSpace(line)
continue inner continue loop
} }
} }
break inner break loop
} }
hanging = hangingBytes[last] hanging = hangingBytes[last]
@@ -408,50 +389,3 @@ func handleRaw(dst []byte, restOfSrc []byte, endQuote []byte) (retDst []byte, li
} }
return dst, line, remaining return dst, line, remaining
} }
// breakAfterBrace returns whether s starts with a slash-slash comment or with
// the rest of a "0}" or ".a=1, .b=2}" literal, including the matching "}".
//
// This implementation isn't perfect, but it's good enough in practice.
func breakAfterBrace(s []byte) bool {
s = trimLeadingWhiteSpace(s)
if len(s) == 0 {
return false
} else if (len(s) > 1) && (s[0] == '/') && (s[1] == '/') {
return false
}
n := 1
for i, c := range s {
switch c {
case ';':
return true
case '{':
n++
case '}':
n--
if n == 0 {
return false
}
case '/':
if (i + 1) < len(s) {
switch s[i+1] {
case '/', '*':
return true
}
}
}
}
return true
}
// breakAfterSemicolon returns whether the first non-space non-tab byte of s
// (if any) does not look like a comment.
func breakAfterSemicolon(s []byte) bool {
for _, c := range s {
if (c != ' ') && (c != '\t') {
return c != '/'
}
}
return false
}
+5 -8
View File
@@ -68,9 +68,9 @@ func TestFormatBytes(tt *testing.T) {
src: "if (b) {\nlabel:\nswitch (i) {\ncase 0:\nj = k\nbreak;\n}\n}\n", src: "if (b) {\nlabel:\nswitch (i) {\ncase 0:\nj = k\nbreak;\n}\n}\n",
want: "if (b) {\n label:\n switch (i) {\n case 0:\n j = k\n break;\n }\n}\n", want: "if (b) {\n label:\n switch (i) {\n case 0:\n j = k\n break;\n }\n}\n",
}, { }, {
// Inserted line breaks. // One-liner if statement.
src: "if (x) { goto fail; }\n", src: "if (x) { goto fail; }\n",
want: "if (x) {\n goto fail;\n}\n", want: "if (x) { goto fail; }\n",
}, { }, {
// Leading blank lines. // Leading blank lines.
src: "\n\n\n x = y;", src: "\n\n\n x = y;",
@@ -108,7 +108,7 @@ func TestTabs(tt *testing.T) {
func ExampleFormatBytes() { func ExampleFormatBytes() {
const src = ` const src = `
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
j = 0; j++; // Semi-colons not within parentheses. j = 0; // Ignore { in a comment.
if (i < j) { foo(); } if (i < j) { foo(); }
u = (v + u = (v +
w); w);
@@ -119,11 +119,8 @@ w);
// Output: // Output:
// for (i = 0; i < 3; i++) { // for (i = 0; i < 3; i++) {
// j = 0; // j = 0; // Ignore { in a comment.
// j++; // Semi-colons not within parentheses. // if (i < j) { foo(); }
// if (i < j) {
// foo();
// }
// u = (v + // u = (v +
// w); // w);
// } // }