Have dumbindent break after some '{' and ';'s

This commit is contained in:
Nigel Tao committed 2020-06-07 21:52:24 +10:00
1 parent 709e169955
commit 53d5f5155c
5 files changed
+140 -34

No files matched your search

+69 -15
View File
@@ -18,10 +18,10 @@
//
// 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
// code, not to fit within an 80 character limit nor for any other reason) but
// it can therefore be much, much faster at the basic task of automatically
// indenting nested blocks. The output isn't 'perfect', but it's usually
// sufficiently readable if the input already has sensible line breaks.
// code just to fit within an 80 character limit) but it can therefore be much,
// much faster at the basic task of automatically indenting nested blocks. The
// output isn't 'perfect', but it's usually sufficiently readable if the input
// already has sensible line breaks.
//
// See `cmd/dumbindent/main.go` in this repository for an example where
// `dumbindent` was 80 times faster than `clang-format`.
@@ -50,10 +50,11 @@ var hangingBytes = [256]bool{
// FormatBytes formats the C (or C-like) program in src, appending the result
// to dst, and returns that longer slice.
//
// It is valid to pass a dst slice (such as nil) whose spare capacity (not
// including its existing elements) is too short to hold the formatted program.
// In this case, a new slice will be allocated and returned.
// It is valid to pass a dst slice (such as nil) whose unused capacity
// (cap(dst) - len(dst)) is too short to hold the formatted program. In this
// case, a new slice will be allocated and returned.
func FormatBytes(dst []byte, src []byte) []byte {
src = trimLeadingWhiteSpaceAndNewLines(src)
if len(src) == 0 {
return dst
} else if len(dst) == 0 {
@@ -66,6 +67,7 @@ func FormatBytes(dst []byte, src []byte) []byte {
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 {
src = trimLeadingWhiteSpace(src)
line, remaining = src, nil
@@ -137,12 +139,23 @@ func FormatBytes(dst []byte, src []byte) []byte {
// Adjust the state according to the braces and parentheses within the
// line (except for those in comments and strings).
last := lastNonWhiteSpace(line)
loop:
inner:
for {
for i, c := range line {
switch c {
case '{':
nBraces++
if l := lastNonWhiteSpace(line[:i]); (l != '=') && (l != ':') {
if breakAfterBrace(line[i+1:]) {
dst = append(dst, line[:i+1]...)
dst = append(dst, '\n')
restOfLine := line[i+1:]
remaining = src[lineLength-len(restOfLine):]
openBrace = true
hanging = false
continue outer
}
}
case '}':
nBraces--
case '(':
@@ -150,6 +163,17 @@ func FormatBytes(dst []byte, src []byte) []byte {
case ')':
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):]
openBrace = false
hanging = false
continue outer
}
case '/':
if (i + 1) >= len(line) {
break
@@ -157,7 +181,7 @@ func FormatBytes(dst []byte, src []byte) []byte {
if line[i+1] == '/' {
// A slash-slash comment. Skip the rest of the line.
last = lastNonWhiteSpace(line[:i])
break loop
break inner
} else if line[i+1] == '*' {
// A slash-star comment.
dst = append(dst, line[:i+2]...)
@@ -165,7 +189,7 @@ func FormatBytes(dst []byte, src []byte) []byte {
restOfSrc := src[lineLength-len(restOfLine):]
dst, line, remaining = handleRaw(dst, restOfSrc, starSlash)
last = lastNonWhiteSpace(line)
continue loop
continue inner
}
case '"', '\'':
@@ -173,7 +197,7 @@ func FormatBytes(dst []byte, src []byte) []byte {
suffix := skipCooked(line[i+1:], c)
dst = append(dst, line[:len(line)-len(suffix)]...)
line = suffix
continue loop
continue inner
case '`':
// A raw string.
@@ -182,10 +206,10 @@ func FormatBytes(dst []byte, src []byte) []byte {
restOfSrc := src[lineLength-len(restOfLine):]
dst, line, remaining = handleRaw(dst, restOfSrc, backTick)
last = lastNonWhiteSpace(line)
continue loop
continue inner
}
}
break loop
break inner
}
openBrace = last == '{'
hanging = hangingBytes[last]
@@ -198,7 +222,15 @@ func FormatBytes(dst []byte, src []byte) []byte {
return dst
}
// trimLeadingWhiteSpace converts "\t foo bar " to "foo bar ".
// trimLeadingWhiteSpaceAndNewLines converts "\t\n foo bar " to "foo bar ".
func trimLeadingWhiteSpaceAndNewLines(s []byte) []byte {
for (len(s) > 0) && ((s[0] == ' ') || (s[0] == '\t') || (s[0] == '\n')) {
s = s[1:]
}
return s
}
// trimLeadingWhiteSpace converts "\t\t foo bar " to "foo bar ".
func trimLeadingWhiteSpace(s []byte) []byte {
for (len(s) > 0) && ((s[0] == ' ') || (s[0] == '\t')) {
s = s[1:]
@@ -206,7 +238,7 @@ func trimLeadingWhiteSpace(s []byte) []byte {
return s
}
// trimTrailingWhiteSpace converts "\t foo bar " to "\t foo bar".
// trimTrailingWhiteSpace converts "\t\t foo bar " to "\t\t foo bar".
func trimTrailingWhiteSpace(s []byte) []byte {
for (len(s) > 0) && ((s[len(s)-1] == ' ') || (s[len(s)-1] == '\t')) {
s = s[:len(s)-1]
@@ -257,3 +289,25 @@ func handleRaw(dst []byte, restOfSrc []byte, endQuote []byte) (retDst []byte, li
}
return dst, line, remaining
}
// breakAfterBrace returns whether the first non-space non-tab byte of s (if
// any) does not look like a comment or another open-brace.
func breakAfterBrace(s []byte) bool {
for _, c := range s {
if (c != ' ') && (c != '\t') {
return (c != '/') && (c != '{')
}
}
return false
}
// breakAfterBrace 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
}
+35
View File
@@ -15,6 +15,7 @@
package dumbindent
import (
"os"
"testing"
)
@@ -66,6 +67,14 @@ func TestFormatBytes(tt *testing.T) {
// Nested blocks with label.
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",
}, {
// Inserted line breaks.
src: "if (x) { goto fail; }\n",
want: "if (x) {\n goto fail;\n}\n",
}, {
// Leading blank lines.
src: "\n\n\n x = y;",
want: "x = y;\n",
}}
for i, tc := range testCases {
@@ -74,3 +83,29 @@ func TestFormatBytes(tt *testing.T) {
}
}
}
func ExampleFormatBytes() {
const src = `
// Blah blah blah.
for (i = 0; i < 3; i++) {
j = 0; j < 4; j++;
if (i < j) { foo(); }
}
`
os.Stdout.Write(FormatBytes(nil, []byte(src)))
// Output:
// // Blah blah blah.
//
// for (i = 0; i < 3; i++) {
// j = 0;
// j < 4;
// j++;
// if (i < j) {
// foo();
// }
// }
}