@matheusbsbs / matheus-bagliano / commits / 3e3ec2abe0
Fork 0

whitespace: add tab-in-indent error class

Some projects and languages use coding style where no tab character is used to indent the lines. This only adds support and documentation for "apply --whitespace=warn" and "diff --check"; later patches add "apply --whitespace=fix" and tests. Signed-off-by: Chris Webb <chris@arachsys.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Chris Webb committed Apr 3, 2010 at 00:37 UTC 3e3ec2abe0f275dda285e989224e4c3da19e7adb
3 files changed +25 -5
Documentation/config.txt
+2
@@ -481,6 +481,8 @@ core.whitespace::
481 error (enabled by default).
482 * `indent-with-non-tab` treats a line that is indented with 8 or more
483 space characters as an error (not enabled by default).
484 +* `tab-in-indent` treats a tab character in the initial indent part of
485 + the line as an error (not enabled by default).
486 * `blank-at-eof` treats blank lines added at the end of file as an error
487 (enabled by default).
488 * `trailing-space` is a short-hand to cover both `blank-at-eol` and
cache.h
+1
@@ -1040,6 +1040,7 @@ void shift_tree_by(const unsigned char *, const unsigned char *, unsigned char *
1040 #define WS_INDENT_WITH_NON_TAB 04
1041 #define WS_CR_AT_EOL 010
1042 #define WS_BLANK_AT_EOF 020
1043 +#define WS_TAB_IN_INDENT 040
1044 #define WS_TRAILING_SPACE (WS_BLANK_AT_EOL|WS_BLANK_AT_EOF)
1045 #define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB)
1046 extern unsigned whitespace_rule_cfg;
ws.c
+22 -5
@@ -19,6 +19,7 @@ static struct whitespace_rule {
19 { "cr-at-eol", WS_CR_AT_EOL, 1 },
20 { "blank-at-eol", WS_BLANK_AT_EOL, 0 },
21 { "blank-at-eof", WS_BLANK_AT_EOF, 0 },
22 + { "tab-in-indent", WS_TAB_IN_INDENT, 0, 1 },
23 };
24
25 unsigned parse_whitespace_rule(const char *string)
@@ -57,6 +58,9 @@ unsigned parse_whitespace_rule(const char *string)
58 }
59 string = ep;
60 }
61 +
62 + if (rule & WS_TAB_IN_INDENT && rule & WS_INDENT_WITH_NON_TAB)
63 + die("cannot enforce both tab-in-indent and indent-with-non-tab");
64 return rule;
65 }
66
@@ -127,6 +131,11 @@ char *whitespace_error_string(unsigned ws)
131 strbuf_addstr(&err, ", ");
132 strbuf_addstr(&err, "indent with spaces");
133 }
134 + if (ws & WS_TAB_IN_INDENT) {
135 + if (err.len)
136 + strbuf_addstr(&err, ", ");
137 + strbuf_addstr(&err, "tab in indent");
138 + }
139 return strbuf_detach(&err, NULL);
140 }
141
@@ -165,7 +174,7 @@ static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
174 }
175 }
176
168 - /* Check for space before tab in initial indent. */
177 + /* Check indentation */
178 for (i = 0; i < len; i++) {
179 if (line[i] == ' ')
180 continue;
@@ -177,11 +186,19 @@ static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
186 fputs(ws, stream);
187 fwrite(line + written, i - written, 1, stream);
188 fputs(reset, stream);
189 + fwrite(line + i, 1, 1, stream);
190 }
181 - } else if (stream)
182 - fwrite(line + written, i - written, 1, stream);
183 - if (stream)
184 - fwrite(line + i, 1, 1, stream);
191 + } else if (ws_rule & WS_TAB_IN_INDENT) {
192 + result |= WS_TAB_IN_INDENT;
193 + if (stream) {
194 + fwrite(line + written, i - written, 1, stream);
195 + fputs(ws, stream);
196 + fwrite(line + i, 1, 1, stream);
197 + fputs(reset, stream);
198 + }
199 + } else if (stream) {
200 + fwrite(line + written, i - written + 1, 1, stream);
201 + }
202 written = i + 1;
203 }
204