mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 16:52:18 +01:00
Have dumbindent break after some '{' and ';'s
This commit is contained in:
1 parent
709e169955
commit
53d5f5155c
5 files changed
+140
-34
No files matched your search
@@ -24,10 +24,10 @@
|
|||||||
//
|
//
|
||||||
// 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 line breaks or otherwise re-flow lines of
|
||||||
// code, not to fit within an 80 character limit nor for any other reason) but
|
// code just to fit within an 80 character limit) but it can therefore be much,
|
||||||
// it can therefore be much, much faster at the basic task of automatically
|
// much faster at the basic task of automatically indenting nested blocks. The
|
||||||
// indenting nested blocks. The output isn't 'perfect', but it's usually
|
// output isn't 'perfect', but it's usually sufficiently readable if the input
|
||||||
// sufficiently readable if the input already has sensible line breaks.
|
// already has sensible line breaks.
|
||||||
//
|
//
|
||||||
// An example of "much, much faster", 80 times faster than clang-format:
|
// An example of "much, much faster", 80 times faster than clang-format:
|
||||||
// ----
|
// ----
|
||||||
|
|||||||
@@ -131,7 +131,6 @@ func Do(args []string) error {
|
|||||||
for _, n := range builtin.Interfaces {
|
for _, n := range builtin.Interfaces {
|
||||||
buf.printf("const char* wuffs_base__%s__vtable_name = "+
|
buf.printf("const char* wuffs_base__%s__vtable_name = "+
|
||||||
"\"{vtable}wuffs_base__%s\";\n", n, n)
|
"\"{vtable}wuffs_base__%s\";\n", n, n)
|
||||||
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
@@ -1383,10 +1382,10 @@ func (g *gen) writeInitializerImpl(b *buffer, n *a.Struct) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
b.printf("{\n")
|
b.printf("{\n")
|
||||||
b.printf("wuffs_base__status z = %s%s__initialize("+
|
b.printf("wuffs_base__status z = %s%s__initialize(\n"+
|
||||||
"&self->private_data.%s%s, sizeof(self->private_data.%s%s), WUFFS_VERSION, initialize_flags);\n",
|
"&self->private_data.%s%s, sizeof(self->private_data.%s%s), WUFFS_VERSION, initialize_flags);\n",
|
||||||
prefix, qid[1].Str(g.tm), fPrefix, f.Name().Str(g.tm), fPrefix, f.Name().Str(g.tm))
|
prefix, qid[1].Str(g.tm), fPrefix, f.Name().Str(g.tm), fPrefix, f.Name().Str(g.tm))
|
||||||
b.printf("if (z.repr) { return z; }\n")
|
b.printf("if (z.repr) {\nreturn z;\n}\n")
|
||||||
b.printf("}\n")
|
b.printf("}\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,10 +18,10 @@
|
|||||||
//
|
//
|
||||||
// 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 line breaks or otherwise re-flow lines of
|
||||||
// code, not to fit within an 80 character limit nor for any other reason) but
|
// code just to fit within an 80 character limit) but it can therefore be much,
|
||||||
// it can therefore be much, much faster at the basic task of automatically
|
// much faster at the basic task of automatically indenting nested blocks. The
|
||||||
// indenting nested blocks. The output isn't 'perfect', but it's usually
|
// output isn't 'perfect', but it's usually sufficiently readable if the input
|
||||||
// sufficiently readable if the input already has sensible line breaks.
|
// already has sensible line breaks.
|
||||||
//
|
//
|
||||||
// See `cmd/dumbindent/main.go` in this repository for an example where
|
// See `cmd/dumbindent/main.go` in this repository for an example where
|
||||||
// `dumbindent` was 80 times faster than `clang-format`.
|
// `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
|
// FormatBytes formats the C (or C-like) program in src, appending the result
|
||||||
// to dst, and returns that longer slice.
|
// to dst, and returns that longer slice.
|
||||||
//
|
//
|
||||||
// It is valid to pass a dst slice (such as nil) whose spare capacity (not
|
// It is valid to pass a dst slice (such as nil) whose unused capacity
|
||||||
// including its existing elements) is too short to hold the formatted program.
|
// (cap(dst) - len(dst)) is too short to hold the formatted program. In this
|
||||||
// In this case, a new slice will be allocated and returned.
|
// case, a new slice will be allocated and returned.
|
||||||
func FormatBytes(dst []byte, src []byte) []byte {
|
func FormatBytes(dst []byte, src []byte) []byte {
|
||||||
|
src = trimLeadingWhiteSpaceAndNewLines(src)
|
||||||
if len(src) == 0 {
|
if len(src) == 0 {
|
||||||
return dst
|
return dst
|
||||||
} else if len(dst) == 0 {
|
} 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 '\\'.
|
hanging := false // Whether the previous non-blank line ends with '=' or '\\'.
|
||||||
blankLine := false // Whether the previous line was blank.
|
blankLine := false // Whether the previous line was blank.
|
||||||
|
|
||||||
|
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
|
||||||
@@ -137,12 +139,23 @@ func FormatBytes(dst []byte, src []byte) []byte {
|
|||||||
// 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)
|
||||||
loop:
|
inner:
|
||||||
for {
|
for {
|
||||||
for i, c := range line {
|
for i, c := range line {
|
||||||
switch c {
|
switch c {
|
||||||
case '{':
|
case '{':
|
||||||
nBraces++
|
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 '}':
|
case '}':
|
||||||
nBraces--
|
nBraces--
|
||||||
case '(':
|
case '(':
|
||||||
@@ -150,6 +163,17 @@ func FormatBytes(dst []byte, src []byte) []byte {
|
|||||||
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):]
|
||||||
|
openBrace = false
|
||||||
|
hanging = false
|
||||||
|
continue outer
|
||||||
|
}
|
||||||
|
|
||||||
case '/':
|
case '/':
|
||||||
if (i + 1) >= len(line) {
|
if (i + 1) >= len(line) {
|
||||||
break
|
break
|
||||||
@@ -157,7 +181,7 @@ func FormatBytes(dst []byte, src []byte) []byte {
|
|||||||
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 loop
|
break inner
|
||||||
} 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]...)
|
||||||
@@ -165,7 +189,7 @@ func FormatBytes(dst []byte, src []byte) []byte {
|
|||||||
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 loop
|
continue inner
|
||||||
}
|
}
|
||||||
|
|
||||||
case '"', '\'':
|
case '"', '\'':
|
||||||
@@ -173,7 +197,7 @@ func FormatBytes(dst []byte, src []byte) []byte {
|
|||||||
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 loop
|
continue inner
|
||||||
|
|
||||||
case '`':
|
case '`':
|
||||||
// A raw string.
|
// A raw string.
|
||||||
@@ -182,10 +206,10 @@ func FormatBytes(dst []byte, src []byte) []byte {
|
|||||||
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 loop
|
continue inner
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break loop
|
break inner
|
||||||
}
|
}
|
||||||
openBrace = last == '{'
|
openBrace = last == '{'
|
||||||
hanging = hangingBytes[last]
|
hanging = hangingBytes[last]
|
||||||
@@ -198,7 +222,15 @@ func FormatBytes(dst []byte, src []byte) []byte {
|
|||||||
return dst
|
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 {
|
func trimLeadingWhiteSpace(s []byte) []byte {
|
||||||
for (len(s) > 0) && ((s[0] == ' ') || (s[0] == '\t')) {
|
for (len(s) > 0) && ((s[0] == ' ') || (s[0] == '\t')) {
|
||||||
s = s[1:]
|
s = s[1:]
|
||||||
@@ -206,7 +238,7 @@ func trimLeadingWhiteSpace(s []byte) []byte {
|
|||||||
return s
|
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 {
|
func trimTrailingWhiteSpace(s []byte) []byte {
|
||||||
for (len(s) > 0) && ((s[len(s)-1] == ' ') || (s[len(s)-1] == '\t')) {
|
for (len(s) > 0) && ((s[len(s)-1] == ' ') || (s[len(s)-1] == '\t')) {
|
||||||
s = s[:len(s)-1]
|
s = s[:len(s)-1]
|
||||||
@@ -257,3 +289,25 @@ func handleRaw(dst []byte, restOfSrc []byte, endQuote []byte) (retDst []byte, li
|
|||||||
}
|
}
|
||||||
return dst, line, remaining
|
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
|
||||||
|
}
|
||||||
@@ -15,6 +15,7 @@
|
|||||||
package dumbindent
|
package dumbindent
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -66,6 +67,14 @@ func TestFormatBytes(tt *testing.T) {
|
|||||||
// Nested blocks with label.
|
// Nested blocks with label.
|
||||||
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.
|
||||||
|
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 {
|
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();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
}
|
||||||
@@ -17003,8 +17003,11 @@ wuffs_gif__decoder__initialize(
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
wuffs_base__status z = wuffs_lzw__decoder__initialize(&self->private_data.f_lzw, sizeof(self->private_data.f_lzw), WUFFS_VERSION, initialize_flags);
|
wuffs_base__status z = wuffs_lzw__decoder__initialize(
|
||||||
if (z.repr) { return z; }
|
&self->private_data.f_lzw, sizeof(self->private_data.f_lzw), WUFFS_VERSION, initialize_flags);
|
||||||
|
if (z.repr) {
|
||||||
|
return z;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
self->private_impl.magic = WUFFS_BASE__MAGIC;
|
self->private_impl.magic = WUFFS_BASE__MAGIC;
|
||||||
self->private_impl.vtable_for__wuffs_base__image_decoder.vtable_name =
|
self->private_impl.vtable_for__wuffs_base__image_decoder.vtable_name =
|
||||||
@@ -21193,12 +21196,18 @@ wuffs_gzip__decoder__initialize(
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
wuffs_base__status z = wuffs_crc32__ieee_hasher__initialize(&self->private_data.f_checksum, sizeof(self->private_data.f_checksum), WUFFS_VERSION, initialize_flags);
|
wuffs_base__status z = wuffs_crc32__ieee_hasher__initialize(
|
||||||
if (z.repr) { return z; }
|
&self->private_data.f_checksum, sizeof(self->private_data.f_checksum), WUFFS_VERSION, initialize_flags);
|
||||||
|
if (z.repr) {
|
||||||
|
return z;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
wuffs_base__status z = wuffs_deflate__decoder__initialize(&self->private_data.f_flate, sizeof(self->private_data.f_flate), WUFFS_VERSION, initialize_flags);
|
wuffs_base__status z = wuffs_deflate__decoder__initialize(
|
||||||
if (z.repr) { return z; }
|
&self->private_data.f_flate, sizeof(self->private_data.f_flate), WUFFS_VERSION, initialize_flags);
|
||||||
|
if (z.repr) {
|
||||||
|
return z;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
self->private_impl.magic = WUFFS_BASE__MAGIC;
|
self->private_impl.magic = WUFFS_BASE__MAGIC;
|
||||||
self->private_impl.vtable_for__wuffs_base__io_transformer.vtable_name =
|
self->private_impl.vtable_for__wuffs_base__io_transformer.vtable_name =
|
||||||
@@ -24667,16 +24676,25 @@ wuffs_zlib__decoder__initialize(
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
wuffs_base__status z = wuffs_adler32__hasher__initialize(&self->private_data.f_checksum, sizeof(self->private_data.f_checksum), WUFFS_VERSION, initialize_flags);
|
wuffs_base__status z = wuffs_adler32__hasher__initialize(
|
||||||
if (z.repr) { return z; }
|
&self->private_data.f_checksum, sizeof(self->private_data.f_checksum), WUFFS_VERSION, initialize_flags);
|
||||||
|
if (z.repr) {
|
||||||
|
return z;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
wuffs_base__status z = wuffs_adler32__hasher__initialize(&self->private_data.f_dict_id_hasher, sizeof(self->private_data.f_dict_id_hasher), WUFFS_VERSION, initialize_flags);
|
wuffs_base__status z = wuffs_adler32__hasher__initialize(
|
||||||
if (z.repr) { return z; }
|
&self->private_data.f_dict_id_hasher, sizeof(self->private_data.f_dict_id_hasher), WUFFS_VERSION, initialize_flags);
|
||||||
|
if (z.repr) {
|
||||||
|
return z;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
wuffs_base__status z = wuffs_deflate__decoder__initialize(&self->private_data.f_flate, sizeof(self->private_data.f_flate), WUFFS_VERSION, initialize_flags);
|
wuffs_base__status z = wuffs_deflate__decoder__initialize(
|
||||||
if (z.repr) { return z; }
|
&self->private_data.f_flate, sizeof(self->private_data.f_flate), WUFFS_VERSION, initialize_flags);
|
||||||
|
if (z.repr) {
|
||||||
|
return z;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
self->private_impl.magic = WUFFS_BASE__MAGIC;
|
self->private_impl.magic = WUFFS_BASE__MAGIC;
|
||||||
self->private_impl.vtable_for__wuffs_base__io_transformer.vtable_name =
|
self->private_impl.vtable_for__wuffs_base__io_transformer.vtable_name =
|
||||||
|
|||||||
Reference in new issue
Block a user