[PATCH 7/7] output.c: Fix indentation of statements within if/for/while statements

Tim Hentenaar <[email protected]> Sun, 5 Jul 2015 15:10:54 +0200
Newsgroups gmane.comp.gnu.indent.bugs
Message-ID <[email protected]>
For -as, if we're in an if/for/while statement, and we're breaking the
line somewhere within the parenthesized expression, use the same indent
level as the parent statement.
---
 ChangeLog                               |  6 ++++--
 regression/input/align-with-spaces.c    | 16 ++++++++++++++++
 regression/standard/align-with-spaces.c | 16 ++++++++++++++++
 src/output.c                            | 28 +++++++++++++++++++++++++---
 4 files changed, 61 insertions(+), 5 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 00bd32a..8b8a9d6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,7 +1,9 @@
 2015-07-04 Tim Hentenaar <[email protected]>
 	* handletoken.c: Detect C99 compound literals.
 	* Makefile.am: Add -Wextra to AM_CFLAGS.
-
+	* output.c: For -as, if we're in an if/for/while statement, and
+	  we're breaking the line somewhere within the parenthesized
+	  expression, use the same indent level as the parent statement.
 2015-06-30 Tim Hentenaar <[email protected]>
 	* Tighten-up the CFLAGS with additional warning flags.
 	* Ensure indent builds with clang.
@@ -51,7 +53,7 @@
 	* Fix indentation of block comments following braces.
 
 2015-06-13 Tim Hentenaar <[email protected]>
-    * Fix regression introduced in hg revision 25c27d429590
+	* Fix regression introduced in hg revision 25c27d429590
 	* Add an option (-slc/--single-line-conditionals) to allow
 	  conditionals and their inner statement to be on the same line if
 	  they're not braced. This should fix GNU bug #42357.
diff --git a/regression/input/align-with-spaces.c b/regression/input/align-with-spaces.c
index 5ab9ffc..53eab5a 100644
--- a/regression/input/align-with-spaces.c
+++ b/regression/input/align-with-spaces.c
@@ -4,4 +4,20 @@ static void really_long_function_decl(const char *string_here, int integer_here,
 		ERROR("unable to allocate memory for a new xxx "
 			  "because %s", strerror(errno));
 	}
+
+	if (sbuf_add_str(delete_state, SBUF_TSPACE, 0) ||
+		sbuf_add_snum(states[states_allocated - 1].timestamp,
+		              SBUF_SCOLON)) {
+		i++;
+	}
+
+	switch (x) {
+	case XXX:
+		if (1) {
+			if (modify_mlist(delta->old_file.path,
+			                 delta->new_file.path))
+				goto err;
+		}
+	break;
+	}
 }
diff --git a/regression/standard/align-with-spaces.c b/regression/standard/align-with-spaces.c
index 65b8753..cb7e655 100644
--- a/regression/standard/align-with-spaces.c
+++ b/regression/standard/align-with-spaces.c
@@ -6,4 +6,20 @@ really_long_function_decl (const char *string_here, int integer_here,
 		ERROR ("unable to allocate memory for a new xxx "
 		       "because %s", strerror (errno));
 	}
+
+	if (sbuf_add_str (delete_state, SBUF_TSPACE, 0) ||
+	    sbuf_add_snum (states[states_allocated - 1].timestamp,
+	                   SBUF_SCOLON)) {
+		i++;
+	}
+
+	switch (x) {
+	case XXX:
+		if (1) {
+			if (modify_mlist (delta->old_file.path,
+			                  delta->new_file.path))
+				goto err;
+		}
+		break;
+	}
 }
diff --git a/src/output.c b/src/output.c
index 71049ce..ee01bcc 100644
--- a/src/output.c
+++ b/src/output.c
@@ -592,6 +592,7 @@ static int pad_output(int cur_col, int target_column)
 {
     int offset = 0;
     int align_target = target_column;
+    int tos = parser_state_tos->tos;
 
     if (cur_col < target_column)
     {
@@ -600,9 +601,30 @@ static int pad_output(int cur_col, int target_column)
             if (settings.align_with_spaces)
             {
                 if (align_target >= parser_state_tos->ind_level)
-	                align_target = parser_state_tos->ind_level;
-	            offset = (align_target - cur_col + 1) / settings.tabsize;
-	            align_target = cur_col + (offset * settings.tabsize);
+                    align_target = parser_state_tos->ind_level;
+
+                /* If we're within an if/for/while, only use tabs
+                 * to indent to the same level as the parent
+                 * statement.
+                 */
+                if (parser_state_tos->last_rw == rw_sp_paren &&
+                    parser_state_tos->p_stack[tos] == stmt   &&
+                    *s_code && tos > 0)
+                {
+                    do {
+                        if (parser_state_tos->p_stack[tos] == ifstmt ||
+                            parser_state_tos->p_stack[tos] == forstmt ||
+                            parser_state_tos->p_stack[tos] == whilestmt)
+                            break;
+                    } while (--tos);
+                    if (tos) align_target = parser_state_tos->il[tos];
+                } else if (parser_state_tos->p_stack[tos] == ifstmt ||
+                           parser_state_tos->p_stack[tos] == forstmt ||
+                           parser_state_tos->p_stack[tos] == whilestmt)
+                    align_target = parser_state_tos->il[tos];
+
+                offset = (align_target - cur_col + 1) / settings.tabsize;
+                align_target = cur_col + (offset * settings.tabsize);
             }
 
             offset = settings.tabsize - (cur_col - 1) % settings.tabsize;
-- 
2.3.6