Index: scripts/coder_format/coder_format.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/coder/scripts/coder_format/coder_format.inc,v retrieving revision 1.2.4.10 diff -u -r1.2.4.10 coder_format.inc --- scripts/coder_format/coder_format.inc 21 Jan 2008 21:41:12 -0000 1.2.4.10 +++ scripts/coder_format/coder_format.inc 22 Jan 2008 01:46:51 -0000 @@ -144,9 +144,12 @@ * $in_case bool * Is true after case and default. Is false after break and return, if * $braces_in_case is not greater than 0. - * $braces_in_case int Count of braces + * $switches int Switch level + * Nested switches need to have extra indents added to them. + * $braces_in_case array Count of braces * The number of currently opened curly braces in a case. This is needed * to support arbitrary function exits inside of a switch control strucure. + * This is an array to allow for nested switches. * $parenthesis int Parenthesis level * The number of currently opened parenthesis. This * - prevents line feeds in brackets (f.e. in arguments of for()). @@ -238,8 +241,9 @@ // Indent controls: $_coder_indent = 0; $in_case = false; + $switches = 0; $parenthesis = 0; - $braces_in_case = 0; + $braces_in_case = array(); $in_brace = false; $in_heredoc = false; $first_php_tag = true; @@ -294,9 +298,13 @@ // Write curly braces at the end of lines followed by a line break if // not in quotes (""), object ($foo->{$bar}) or in variables (${foo}). // (T_DOLLAR_OPEN_CURLY_BRACES exists but is never assigned.) - if (!$in_variable && !$in_quote && !$in_object && substr(rtrim($result), -1) != '$' || substr(rtrim($result), -1) == ')') { + if ($after_php) { + $text = " $text "; + } + if (!$after_php && (!$in_variable && !$in_quote && !$in_object && substr(rtrim($result), -1) != '$' || substr(rtrim($result), -1) == ')')) { if ($in_case) { - ++$braces_in_case; + ++$braces_in_case[$switches]; + $_coder_indent += $switches - 1; } ++$_coder_indent; $result = rtrim($result) .' '. $text; @@ -310,23 +318,39 @@ case '}': if (!$in_quote && !$in_brace && !$in_heredoc) { - if ($in_case) { - --$braces_in_case; + if ($switches) { + --$braces_in_case[$switches]; } --$_coder_indent; - if ($braces_in_case < 0) { + if ($braces_in_case[$switches] < 0 && $in_case) { // Decrease indent if last case in a switch is not terminated. --$_coder_indent; - $in_case = false; - $braces_in_case = 0; + $in_case = FALSE; } - $result = rtrim($result); - if (substr($result, -1) != '{') { - // Avoid line break in empty curly braces. + if ($braces_in_case[$switches] < 0) { + $braces_in_case[$switches] = 0; + $switches--; + } + if ($switches > 0) { + $in_case = TRUE; + } + + if (!$after_php) { + $result = rtrim($result); + if (substr($result, -1) != '{') { + // Avoid line break in empty curly braces. + coder_br($result); + } + $result .= $text; coder_br($result); } - $result .= $text; - coder_br($result); + else { + $result = rtrim($result, ' '); + if (substr($result, -1) !== "\n") { + $result .= ' '; + } + $result .= $text; + } } else { $in_brace = false; @@ -363,9 +387,12 @@ if ($inline_if) { $result .= ' '. $text .' '; } + elseif ($after_php) { + $result .= $text; + } else { if ($in_case) { - ++$_coder_indent; + $_coder_indent++; } $result = rtrim($result) . $text; coder_br($result); @@ -531,11 +558,11 @@ case T_OPEN_TAG: case T_OPEN_TAG_WITH_ECHO: $in_php = true; - $after_php = true; // Add a line break between two PHP tags. - if (substr(rtrim($result), -2) == '?>') { + if (substr(rtrim($result), -2) == '?>' && !$after_php) { coder_br($result); } + $after_php = true; $nl = substr_count($text, "\n"); $result .= trim($text); if ($first_php_tag) { @@ -554,6 +581,10 @@ case T_CLOSE_TAG: $in_php = false; + if ($after_php) { + $result = rtrim($result, ' ') . ' '; + $text = ltrim($text, ' '); + } // Do not alter a closing PHP tag ($text includes trailing white-space) // at all. Should allow to apply coder_format on phptemplate files. $result .= $text; @@ -623,10 +654,12 @@ $in_variable = FALSE; break; + case T_SWITCH: + $switches++; + // Purposely fall through. case T_IF: case T_FOR: case T_FOREACH: - case T_SWITCH: case T_GLOBAL: case T_STATIC: case T_ECHO: @@ -667,7 +700,7 @@ case T_CASE: case T_DEFAULT: - $braces_in_case = 0; + $braces_in_case[$switches] = 0; $result = rtrim($result); $after_case = true; if (!$in_case) { @@ -690,14 +723,14 @@ $result = rtrim($result); coder_br($result); $result .= trim($text); - if ($in_case && !$braces_in_case) { + if ($in_case && !$braces_in_case[$switches]) { --$_coder_indent; - $in_case = false; + $in_case = FALSE; } break; case T_RETURN: - if ($in_case && !$braces_in_case) { + if ($in_case && !$braces_in_case[$switches]) { // Defer reduction of indent for later. ++$_coder_indent; $after_return_in_case = true; @@ -706,7 +739,7 @@ coder_add_space($result); $result .= trim($text) .' '; // Decrease indent only if we're not in a control structure inside a case. - if ($in_case && !$braces_in_case) { + if ($in_case && !$braces_in_case[$switches]) { --$_coder_indent; $in_case = false; } Index: scripts/coder_format/tests/tests/control.phpt =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/coder/scripts/coder_format/tests/tests/Attic/control.phpt,v retrieving revision 1.1.2.5 diff -u -r1.1.2.5 control.phpt --- scripts/coder_format/tests/tests/control.phpt 21 Jan 2008 23:46:39 -0000 1.1.2.5 +++ scripts/coder_format/tests/tests/control.phpt 22 Jan 2008 01:38:03 -0000 @@ -69,10 +69,12 @@ switch ($long) { case 0: return t('Lines and paragraphs break automatically.'); + case 1: - return t('Lines and paragraphs are automatically recognized. The <br /> line break, <p> paragraph and </p> close paragraph tags are inserted automatically. If paragraphs are not recognized simply add a couple blank lines.'); + return t('Lines and paragraphs are automatically recognized.'); } break; + case 2: return t('Web page addresses and e-mail addresses turn into links automatically.'); } @@ -102,3 +104,35 @@ $foo; } +--INPUT-- +switch ($foo) { + case 'foo': + if ($foo) { + doSomethingElse(); + } + else { + doSomething(); + } + break; +} + +--INPUT-- +switch ($foo) { + case 'foo': + if ($foo) { + doSomething(); + } +} +--INPUT-- +function foo() { + if ($foo) { + switch ($foo) { + case 'foo': + foo(); + break; + } + } + else { + foo(); + } +} \ No newline at end of file Index: scripts/coder_format/tests/tests/tags.phpt =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/coder/scripts/coder_format/tests/tests/Attic/tags.phpt,v retrieving revision 1.1.2.5 diff -u -r1.1.2.5 tags.phpt --- scripts/coder_format/tests/tests/tags.phpt 21 Jan 2008 23:46:39 -0000 1.1.2.5 +++ scripts/coder_format/tests/tests/tags.phpt 22 Jan 2008 01:15:28 -0000 @@ -40,6 +40,5 @@