mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 16:52:18 +01:00
Tweak how dumbindent handles #preproc lines
This commit is contained in:
1 parent
b1ea22045a
commit
f935120aa6
1 file changed
+33
-21
@@ -118,7 +118,7 @@ import (
|
|||||||
// 'Constants', but their type is []byte, not string.
|
// 'Constants', but their type is []byte, not string.
|
||||||
var (
|
var (
|
||||||
backTick = []byte("`")
|
backTick = []byte("`")
|
||||||
externC = []byte("extern \"C\" {")
|
extern = []byte("extern ")
|
||||||
namespace = []byte("namespace ")
|
namespace = []byte("namespace ")
|
||||||
starSlash = []byte("*/")
|
starSlash = []byte("*/")
|
||||||
|
|
||||||
@@ -177,6 +177,7 @@ func FormatBytes(dst []byte, src []byte, opts *Options) []byte {
|
|||||||
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.
|
||||||
hanging := false // Whether the previous non-blank line ends with '=' or '\\'.
|
hanging := false // Whether the previous non-blank line ends with '=' or '\\'.
|
||||||
|
preproc := false // Whether we're in a #preprocessor line.
|
||||||
|
|
||||||
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)
|
||||||
@@ -200,34 +201,38 @@ func FormatBytes(dst []byte, src []byte, opts *Options) []byte {
|
|||||||
nBlankLines = 0
|
nBlankLines = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Preprocessor lines (#ifdef, #pragma, etc) are never indented.
|
// Handle preprocessor lines (#ifdef, #pragma, etc).
|
||||||
//
|
if preproc || (line[0] == '#') {
|
||||||
// Also catch `extern "C" {` and `namespace foo {`.
|
if preproc {
|
||||||
if (line[0] == '#') ||
|
dst = appendRepeatedBytes(dst, indentBytes, indentCount*2)
|
||||||
((line[0] == 'e') && bytes.HasPrefix(line, externC)) ||
|
}
|
||||||
((line[0] == 'n') && bytes.HasPrefix(line, namespace)) {
|
|
||||||
line = trimTrailingWhiteSpace(line)
|
line = trimTrailingWhiteSpace(line)
|
||||||
dst = append(dst, line...)
|
dst = append(dst, line...)
|
||||||
dst = append(dst, '\n')
|
dst = append(dst, '\n')
|
||||||
hanging = lastNonWhiteSpace(line) == '\\'
|
hanging = false
|
||||||
|
preproc = lastNonWhiteSpace(line) == '\\'
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Account for leading '}'s before we print the line's indentation.
|
|
||||||
closeBraces := 0
|
closeBraces := 0
|
||||||
for ; (closeBraces < len(line)) && line[closeBraces] == '}'; closeBraces++ {
|
|
||||||
}
|
|
||||||
nBraces -= closeBraces
|
|
||||||
|
|
||||||
// The heuristics aren't perfect, and sometimes do not catch braces or
|
// Don't indent for `extern "C" {` or `namespace foo {`.
|
||||||
// parentheses in #define macros. They also don't increment nBraces for
|
if ((line[0] == 'e') && hasPrefixAndBrace(line, extern)) ||
|
||||||
// `extern "C"` or namespace lines. We work around that here, clamping
|
((line[0] == 'n') && hasPrefixAndBrace(line, namespace)) {
|
||||||
// to zero.
|
nBraces--
|
||||||
if nBraces < 0 {
|
|
||||||
nBraces = 0
|
} else {
|
||||||
}
|
// Account for leading '}'s before we print the line's indentation.
|
||||||
if nParens < 0 {
|
for ; (closeBraces < len(line)) && line[closeBraces] == '}'; closeBraces++ {
|
||||||
nParens = 0
|
}
|
||||||
|
nBraces -= closeBraces
|
||||||
|
|
||||||
|
// Because the "{" in "extern .*{" and "namespace .*{" is had no
|
||||||
|
// net effect on nBraces, the matching "}" can cause the nBraces
|
||||||
|
// count to dip below zero. Correct for that here.
|
||||||
|
if nBraces < 0 {
|
||||||
|
nBraces = 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output a certain number of spaces to roughly approximate
|
// Output a certain number of spaces to roughly approximate
|
||||||
@@ -308,6 +313,13 @@ func FormatBytes(dst []byte, src []byte, opts *Options) []byte {
|
|||||||
return dst
|
return dst
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// hasPrefixAndBrace returns whether line starts with prefix and after that
|
||||||
|
// contains a '{'.
|
||||||
|
func hasPrefixAndBrace(line []byte, prefix []byte) bool {
|
||||||
|
return bytes.HasPrefix(line, prefix) &&
|
||||||
|
bytes.IndexByte(line[len(prefix):], '{') >= 0
|
||||||
|
}
|
||||||
|
|
||||||
// trimLeadingWhiteSpaceAndNewLines converts "\t\n foo bar " to "foo bar ".
|
// trimLeadingWhiteSpaceAndNewLines converts "\t\n foo bar " to "foo bar ".
|
||||||
func trimLeadingWhiteSpaceAndNewLines(s []byte) []byte {
|
func trimLeadingWhiteSpaceAndNewLines(s []byte) []byte {
|
||||||
for (len(s) > 0) && ((s[0] == ' ') || (s[0] == '\t') || (s[0] == '\n')) {
|
for (len(s) > 0) && ((s[0] == ' ') || (s[0] == '\t') || (s[0] == '\n')) {
|
||||||
|
|||||||
Reference in new issue
Block a user