git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Chris Webb <chris@arachsys.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org
Subject: [PATCH 5/6] whitespace: add tab-in-indent support for --whitespace=fix
Date: Sat, 3 Apr 2010 00:37:30 +0100	[thread overview]
Message-ID: <3edb43ab524b6b2ee883c25cfa80bc6012a4abd4.1270250437.git.chris@arachsys.com> (raw)
In-Reply-To: <cover.1270250437.git.chris@arachsys.com>

If tab-in-indent is set, --whitespace=fix will ensure that any stray tabs in
the initial indent are expanded to the correct number of space characters.

Signed-off-by: Chris Webb <chris@arachsys.com>
---
 ws.c |   21 ++++++++++++++++++---
 1 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/ws.c b/ws.c
index 897ff56..e581535 100644
--- a/ws.c
+++ b/ws.c
@@ -313,8 +313,9 @@ void ws_fix_copy(struct strbuf *dst, const char *src, int len, unsigned ws_rule,
 		char ch = src[i];
 		if (ch == '\t') {
 			last_tab_in_indent = i;
-			if ((ws_rule & WS_SPACE_BEFORE_TAB) &&
-			    0 <= last_space_in_indent)
+			if ((ws_rule & WS_TAB_IN_INDENT) ||
+			    ((ws_rule & WS_SPACE_BEFORE_TAB) &&
+			     0 <= last_space_in_indent))
 			    need_fix_leading_space = 1;
 		} else if (ch == ' ') {
 			last_space_in_indent = i;
@@ -325,7 +326,21 @@ void ws_fix_copy(struct strbuf *dst, const char *src, int len, unsigned ws_rule,
 			break;
 	}
 
-	if (need_fix_leading_space) {
+	if ((ws_rule & WS_TAB_IN_INDENT) && last_tab_in_indent >= 0) {
+		/* Expand tabs into spaces */
+		int last = last_tab_in_indent + 1;
+		for (i = 0; i < last; i++) {
+			if (src[i] == '\t')
+				do {
+					strbuf_addch(dst, ' ');
+				} while (dst->len % 8);
+			else
+				strbuf_addch(dst, src[i]);
+		}
+		len -= last;
+		src += last;
+		fixed = 1;
+	} else if (need_fix_leading_space) {
 		/* Process indent ourselves */
 		int consecutive_spaces = 0;
 		int last = last_tab_in_indent + 1;
-- 
1.7.0.3

  parent reply	other threads:[~2010-04-02 23:37 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-02 23:36 [PATCH 0/6] Add tab-in-indent whitespace rule Chris Webb
2010-04-02 23:37 ` [PATCH 1/6] whitespace: we cannot "catch all errors known to git" anymore Chris Webb
2010-04-02 23:37 ` [PATCH 2/6] whitespace: add tab-in-indent error class Chris Webb
2010-04-03 10:02   ` Martin Mares
2010-04-02 23:37 ` [PATCH 3/6] whitespace: tests for git-diff --check with " Chris Webb
2010-04-02 23:37 ` [PATCH 4/6] whitespace: replumb ws_fix_copy to take a strbuf *dst instead of char *dst Chris Webb
2010-04-02 23:37 ` Chris Webb [this message]
2010-04-02 23:37 ` [PATCH 6/6] whitespace: tests for git-apply --whitespace=fix with tab-in-indent Chris Webb

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3edb43ab524b6b2ee883c25cfa80bc6012a4abd4.1270250437.git.chris@arachsys.com \
    --to=chris@arachsys.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).