whitespace: more accurate initial-indent highlighting
Instead of highlighting the entire initial indent, highlight only the problematic spaces. In the case of an indent like ' \t \t' there may be multiple problematic ranges, so it's easiest to emit the highlighting as we go instead of trying rember disjoint ranges and do it all at the end. Signed-off-by: J. Bruce Fields <bfields@citi.umich.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
J. Bruce Fields committed
Dec 16, 2007 at 11:31 UTC
ffe568859ba89b4283afc36251561a9be3173bf8
1 file changed
+16
-8
ws.c
+16
-8
@@ -150,24 +150,32 @@ unsigned check_and_emit_line(const char *line, int len, unsigned ws_rule,
150
continue;
151
if (line[i] != '\t')
152
break;
153
- if ((ws_rule & WS_SPACE_BEFORE_TAB) && written < i)
153
+ if ((ws_rule & WS_SPACE_BEFORE_TAB) && written < i) {
154
result |= WS_SPACE_BEFORE_TAB;
155
+ if (stream) {
156
+ fputs(ws, stream);
157
+ fwrite(line + written, i - written, 1, stream);
158
+ fputs(reset, stream);
159
+ }
160
+ } else if (stream)
161
+ fwrite(line + written, i - written, 1, stream);
162
+ if (stream)
163
+ fwrite(line + i, 1, 1, stream);
164
written = i + 1;
165
}
166
167
/* Check for indent using non-tab. */
159
- if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= 8)
168
+ if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= 8) {
169
result |= WS_INDENT_WITH_NON_TAB;
161
-
162
- if (stream) {
163
- /* Highlight errors in leading whitespace. */
164
- if ((result & WS_SPACE_BEFORE_TAB) ||
165
- (result & WS_INDENT_WITH_NON_TAB)) {
170
+ if (stream) {
171
fputs(ws, stream);
167
- fwrite(line, written, 1, stream);
172
+ fwrite(line + written, i - written, 1, stream);
173
fputs(reset, stream);
174
}
175
+ written = i;
176
+ }
177
178
+ if (stream) {
179
/* Now the rest of the line starts at written.
180
* The non-highlighted part ends at trailing_whitespace. */
181
if (trailing_whitespace == -1)