? coder-format-13.patch ? coder_format-14.patch ? coder_format-15.patch 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.6 diff -u -r1.2.4.6 coder_format.inc --- scripts/coder_format/coder_format.inc 20 Jan 2008 21:08:23 -0000 1.2.4.6 +++ scripts/coder_format/coder_format.inc 21 Jan 2008 00:14:25 -0000 @@ -2,6 +2,7 @@ // $Id: coder_format.inc,v 1.2.4.6 2008/01/20 21:08:23 sun Exp $ + /** * Recursively process .module and .inc files in directory with coder_format_file(). * @@ -195,6 +196,14 @@ * Whether or not the current line being processed has a semicolon. * $after_case * Whether or not the current line being processed has a case/default statement. + * $after_comment + * Whether or not the current line being processed has an inline comment preceding it + * $after_initial_comment + * Whether or not the current line has a the CVS Id inline comment preceding it + * $after_visibility_modifier + * Whether or not the token is after public/protected/private/abstract + * $after_return_in_case + * Whether or not the token is after a return statement in a case * * @param $code * The source code to format. @@ -225,9 +234,17 @@ $in_multiline = array(); $after_semicolon = FALSE; $after_case = FALSE; + $after_comment = FALSE; + $after_initial_comment = FALSE; + $after_visibility_modifier = FALSE; + $after_return_in_case = FALSE; // Whether or not a function token was encountered: $in_function_declaration = FALSE; + + // The position of the last character of the last non-whitespace + // non-comment token: + $position_last_significant_token = 0; $result = ''; $lasttoken = array(0); @@ -293,6 +310,10 @@ case ';': $result = rtrim($result) . $text; + if ($after_return_in_case) { + --$_coder_indent; + $after_return_in_case = FALSE; + } if (!$parenthesis && !$in_heredoc) { coder_br($result); $after_semicolon = TRUE; @@ -342,27 +363,18 @@ case ')': if ($in_array[$parenthesis] && $in_multiline[$parenthesis]) { // Check if a comma insertion is necessary: - for ($c = strlen($result) - 1; $c >= 0; $c--) { - if ($result[$c] === "\n" || $result[$c] === " ") { - continue; - } - if ($result[$c] === ",") { - break; - } + $c = $position_last_significant_token; + if ($result[$c] !== ',') { // We need to add a comma at $c: $result = substr($result, 0, $c + 1) .','. substr($result, $c + 1); - break; } } if (!$in_quote && !$in_heredoc && (substr(rtrim($result), -1) == ',' || $in_multiline[$parenthesis])) { // Fix indent of right parenthesis in multiline structures by // increasing indent for each parenthesis and decreasing one level. - $_coder_indent = $_coder_indent + ($parenthesis - 1); $result = rtrim($result); - coder_br($result); + coder_br($result, $parenthesis - 1); $result .= $text; - // Undo temporary change. - $_coder_indent = $_coder_indent - ($parenthesis - 1); } else { $result .= $text; @@ -453,6 +465,10 @@ $result .= $text; break; } + + $position_last_significant_token = strlen(rtrim($result)) - 1; + $after_comment = FALSE; + $after_initial_comment = FALSE; } else { // If we get here, then we have found not a single char, but a token. @@ -544,21 +560,11 @@ case T_WHITESPACE: // Avoid duplicate line feeds outside arrays. - $c = $parenthesis ? 0 : 1; + $c = ($parenthesis || $after_comment) ? 0 : 1; for ($c, $cc = substr_count($text, "\n"); $c < $cc; ++$c) { // Newlines were added; not after semicolon anymore - if ($parenthesis) { - // Add extra indent for each parenthesis in multiline definitions (f.e. arrays). - $_coder_indent = $_coder_indent + $parenthesis; - $result = rtrim($result); - coder_br($result); - $_coder_indent = $_coder_indent - $parenthesis; - } - else { - // Discard any whitespace, just insert a line break. - coder_br($result); - } + coder_br($result, $parenthesis); } // If there were newlines present inside a parenthesis, @@ -574,6 +580,7 @@ } $in_variable = FALSE; + break; case T_IF: @@ -650,6 +657,11 @@ break; case T_RETURN: + if ($in_case && !$braces_in_case) { + // Defer reduction of indent for later. + ++$_coder_indent; + $after_return_in_case = true; + } case T_CONTINUE: coder_add_space($result); $result .= trim($text) .' '; @@ -668,12 +680,12 @@ // T_FUNCTION, but without line-break after the token. Because more // than one of these tokens can appear in front of a function token, // we need another white-space control variable. - coder_br($result); $result .= trim($text) .' '; + $after_visibility_modifier = TRUE; break; case T_FUNCTION: - $in_function_declaration = true; + $in_function_declaration = TRUE; // Fall through. case T_CLASS: // Write function and class to new lines. @@ -681,7 +693,12 @@ if (substr($result, -1) == '}') { coder_br($result); } - coder_br($result); + if (!$after_visibility_modifier) { + coder_br($result); + } else { + $after_visibility_modifier = FALSE; + $result .= ' '; + } $result .= trim($text) .' '; break; @@ -732,7 +749,11 @@ if (substr($text, 0, 3) == '/**') { // Prepend a new line. $result = rtrim($result); - coder_br($result); + if (!$after_initial_comment) { + coder_br($result); + } else { + $after_initial_comment = FALSE; + } coder_br($result); // Remove carriage returns. @@ -773,28 +794,31 @@ else { // Move the comment above if it's embedded. $statement = false; - if ($after_semicolon && !$after_case) { + $cc = substr_count($result, "\n", $position_last_significant_token); + if ((!$cc || $after_semicolon) && !$after_case) { $nl_position = strrpos(rtrim($result, " \n"), "\n"); $statement = substr($result, $nl_position); $result = substr($result, 0, $nl_position); $after_semicolon = false; - coder_br($result); + coder_br($result, $parenthesis); } $result .= trim($text); - if ($parenthesis) { - // Add extra indent for each parenthesis in multiline definitions (f.e. arrays). - $_coder_indent = $_coder_indent + $parenthesis; - $result = rtrim($result); - coder_br($result); - $_coder_indent = $_coder_indent - $parenthesis; - } - else { - // Discard any whitespace, just insert a line break. - coder_br($result); - } + coder_br($result, $parenthesis); if ($statement) { $result = rtrim($result, "\n "); - $result .= $statement; + $result .= rtrim($statement, "\n "); + coder_br($result, $parenthesis); + // Need to update this, as our comment trickery has just + // reshuffled the index. + $position_last_significant_token = strlen(rtrim($result, " \n")) - 1; + } else { + if (strpos($text, '$'.'Id$') === FALSE) { + $after_comment = TRUE; + } else { + // Is the number two so that our bottom code doesn't override + // our flag immediately. + $after_initial_comment = 2; + } } } break; @@ -805,14 +829,14 @@ case T_START_HEREDOC: $result .= trim($text); - coder_br($result, false); - $in_heredoc = true; + coder_br($result, FALSE, FALSE); + $in_heredoc = TRUE; break; case T_END_HEREDOC: $result .= trim($text); - coder_br($result, false); - $in_heredoc = false; + coder_br($result, FALSE, FALSE); + $in_heredoc = FALSE; break; default: @@ -822,6 +846,23 @@ // Store last token. $lasttoken = $token; + + switch ($id) { + case T_WHITESPACE: + case T_COMMENT: + case T_ML_COMMENT: + case T_DOC_COMMENT: + break; + default: + $position_last_significant_token = strlen(rtrim($result, " \n")) - 1; + break; + } + + if ($id !== T_COMMENT && $id !== T_ML_COMMENT) { + $after_comment = FALSE; + } + if ($after_initial_comment && $id !== T_WHITESPACE) $after_initial_comment--; + } } return $result; @@ -835,10 +876,12 @@ * * @param &$result * Result variable to append break and indent to, passed by reference. + * @param $parenthesis + * Optional integer of parentheses level for extra indents. * @param $add_indent * Whether to add current line indent after line feed. */ -function coder_br(&$result, $add_indent = true) { +function coder_br(&$result, $parenthesis = false, $add_indent = true) { global $_coder_indent; // Scan result backwards for whitespace. @@ -853,12 +896,20 @@ // Non-whitespace was encountered, no changes necessary. break; } - - $output = "\n"; - if ($add_indent && $_coder_indent >= 0) { - $output .= str_repeat(' ', $_coder_indent); + + if ($parenthesis) { + // Add extra indent for each parenthesis in multiline definitions (f.e. arrays). + $_coder_indent = $_coder_indent + $parenthesis; + $result = rtrim($result); + coder_br($result, false, $add_indent); + $_coder_indent = $_coder_indent - $parenthesis; + } else { + $output = "\n"; + if ($add_indent && $_coder_indent >= 0) { + $output .= str_repeat(' ', $_coder_indent); + } + $result .= $output; } - $result .= $output; } /** @@ -1027,6 +1078,21 @@ ); } +function coder_preprocessor_inline_comment() { + return array( + '#title' => 'Move inline comments above remarked line.', + '#weight' => 2, + // [\040\t] matches only a space or tab. + // (?!case) prevents matching of case statements. + // \S prevents matching of lines containing only a comment. + // [^:] prevents matching of URL protocols. + // [^;\$] prevents matching of CVS keyword Id comment and double slashes. + // in quotes (f.e. "W3C//DTD"). + '#search' => '@^([\040\t]*)(?!case)(\S.+?[;,{])[\040\t]*(?!:)//\s*([^;\$]+?)$@m', + '#replace' => "$1// $3\n$1$2", + ); +} + /** * @} End of "defgroup coder_preprocessor". */ Index: scripts/coder_format/tests/CoderTestFile.php =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/coder/scripts/coder_format/tests/Attic/CoderTestFile.php,v retrieving revision 1.1.2.2 diff -u -r1.1.2.2 CoderTestFile.php --- scripts/coder_format/tests/CoderTestFile.php 20 Jan 2008 21:08:23 -0000 1.1.2.2 +++ scripts/coder_format/tests/CoderTestFile.php 20 Jan 2008 23:11:41 -0000 @@ -23,6 +23,9 @@ /* Whether or not only[$unit]) { + break; + } $unit++; } continue; @@ -78,6 +84,10 @@ case 'EXPECT': $this->expect[$unit] .= $line ."\n"; break; + + case 'ONLY': + $this->only[$unit] = TRUE; + break; } } fclose($fh); @@ -93,6 +103,10 @@ $this->expect[$unit] = $prepend . rtrim($this->expect[$unit], "\n") ."\n\n"; } } + if (!empty($this->only[$unit])) { + $this->input = array($this->input[$unit]); + $this->expect = array($this->expect[$unit]); + } } /** @@ -182,21 +196,26 @@ } function _context($lines) { - return '
'. htmlspecialchars(implode("\n", $lines)) .'
-
'. htmlspecialchars(implode("\n", $lines)) .'
'; + return '
'. $this->_renderLines($lines) .'
+
'. $this->_renderLines($lines) .'
'; } function _added($lines) { - return ' 
'. htmlspecialchars(implode("\n", $lines)) .'
'; + return ' 
'. $this->_renderLines($lines) .'
'; } function _deleted($lines) { - return '
'. htmlspecialchars(implode("\n", $lines)) .'
 '; + return '
'. $this->_renderLines($lines) .'
 '; } function _changed($orig, $final) { - return '
'. htmlspecialchars(implode("\n", $orig)) .'
-
'. htmlspecialchars(implode("\n", $final)) .'
'; + return '
'. $this->_renderLines($orig) .'
+
'. $this->_renderLines($final) .'
'; } + + function _renderLines($lines) { + return str_replace("\n", "\n", htmlspecialchars(implode("\n", $lines)."\n")); + } + } Index: scripts/coder_format/tests/all.test =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/coder/scripts/coder_format/tests/Attic/all.test,v retrieving revision 1.1.2.2 diff -u -r1.1.2.2 all.test --- scripts/coder_format/tests/all.test 20 Jan 2008 21:08:23 -0000 1.1.2.2 +++ scripts/coder_format/tests/all.test 20 Jan 2008 23:43:20 -0000 @@ -28,6 +28,7 @@ // Order tests alphabetically, but use a weight > 0 to append them after // overall test results. $c = 10; + //$files = array('C:\Users\Edward\Webs\drupal-contributions\modules\coder\scripts\coder_format\tests\tests\comments.phpt'); foreach ($files as $file) { $expectation = new CoderTestFile(); $result = $this->assert($expectation, $file, '%s'); Index: scripts/coder_format/tests/tests/align.phpt =================================================================== RCS file: scripts/coder_format/tests/tests/align.phpt diff -N scripts/coder_format/tests/tests/align.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ scripts/coder_format/tests/tests/align.phpt 20 Jan 2008 06:38:55 -0000 @@ -0,0 +1,14 @@ + 'ad'); +$a = array(); \ No newline at end of file Index: scripts/coder_format/tests/tests/blank-nl-no-whitespace.phpt =================================================================== RCS file: scripts/coder_format/tests/tests/blank-nl-no-whitespace.phpt diff -N scripts/coder_format/tests/tests/blank-nl-no-whitespace.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ scripts/coder_format/tests/tests/blank-nl-no-whitespace.phpt 19 Jan 2008 20:25:20 -0000 @@ -0,0 +1,9 @@ +prefix)) { + $options['prefix'] = $options['language']->prefix .'/'; + } + break; + } + } +} + Index: scripts/coder_format/tests/tests/ml-array-comma.phpt =================================================================== RCS file: scripts/coder_format/tests/tests/ml-array-comma.phpt diff -N scripts/coder_format/tests/tests/ml-array-comma.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ scripts/coder_format/tests/tests/ml-array-comma.phpt 20 Jan 2008 22:15:32 -0000 @@ -0,0 +1,15 @@ + 'bar', + $bar => 'asdf' // foo +); + +-- EXPECT -- +$array = array( + 'foo' => 'bar', + // foo + $bar => 'asdf', +); Index: scripts/coder_format/tests/tests/ml-array-in-function.phpt =================================================================== RCS file: scripts/coder_format/tests/tests/ml-array-in-function.phpt diff -N scripts/coder_format/tests/tests/ml-array-in-function.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ scripts/coder_format/tests/tests/ml-array-in-function.phpt 19 Jan 2008 20:52:26 -0000 @@ -0,0 +1,9 @@ + 'a', + 'foo' => 'a', + ) +); \ No newline at end of file Index: scripts/coder_format/tests/tests/ml-array-no-leading-nl.phpt =================================================================== RCS file: scripts/coder_format/tests/tests/ml-array-no-leading-nl.phpt diff -N scripts/coder_format/tests/tests/ml-array-no-leading-nl.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ scripts/coder_format/tests/tests/ml-array-no-leading-nl.phpt 19 Jan 2008 21:10:48 -0000 @@ -0,0 +1,12 @@ + 'bar', + 2 => '23', + '32' => 'asd' +); +--EXPECT-- +$va = array('foo' => 'bar', + 2 => '23', + '32' => 'asd', +); \ No newline at end of file Index: scripts/coder_format/tests/tests/ml-array-parenthesis-nl.phpt =================================================================== RCS file: scripts/coder_format/tests/tests/ml-array-parenthesis-nl.phpt diff -N scripts/coder_format/tests/tests/ml-array-parenthesis-nl.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ scripts/coder_format/tests/tests/ml-array-parenthesis-nl.phpt 19 Jan 2008 20:33:30 -0000 @@ -0,0 +1,15 @@ + array( + 'foo' => NULL,), +); + +-- EXPECT -- +$var = array( + 'install_page' => array( + 'foo' => NULL, + ), +); Index: scripts/coder_format/tests/tests/ml-deep-array-comma.phpt =================================================================== RCS file: scripts/coder_format/tests/tests/ml-deep-array-comma.phpt diff -N scripts/coder_format/tests/tests/ml-deep-array-comma.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ scripts/coder_format/tests/tests/ml-deep-array-comma.phpt 19 Jan 2008 21:27:19 -0000 @@ -0,0 +1,26 @@ + array( + 'a' => $foo, + 23 => 'a' + ), + $bar => array( + 'd' => 'asd', + 'as' => 23 + ) +); + +-- EXPECT -- +$array = array( + 'foo' => array( + 'a' => $foo, + 23 => 'a', + ), + $bar => array( + 'd' => 'asd', + 'as' => 23, + ), +); Index: scripts/coder_format/tests/tests/parenthesis.phpt =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/coder/scripts/coder_format/tests/tests/Attic/parenthesis.phpt,v retrieving revision 1.1.2.2 diff -u -r1.1.2.2 parenthesis.phpt --- scripts/coder_format/tests/tests/parenthesis.phpt 20 Jan 2008 21:08:24 -0000 1.1.2.2 +++ scripts/coder_format/tests/tests/parenthesis.phpt 21 Jan 2008 00:07:08 -0000 @@ -29,7 +29,8 @@ case 'string': return '"'. str_replace(array("\r", "\n", "<", ">", "&"), array('\r', '\n', '\x3c', '\x3e', '\x26'), - addslashes($var)) .'"'; + addslashes($var) + ) .'"'; default: return 'null'; Index: scripts/coder_format/tests/tests/string-char-access.phpt =================================================================== RCS file: scripts/coder_format/tests/tests/string-char-access.phpt diff -N scripts/coder_format/tests/tests/string-char-access.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ scripts/coder_format/tests/tests/string-char-access.phpt 20 Jan 2008 06:16:45 -0000 @@ -0,0 +1,3 @@ +TEST: String character access with curly braces +--INPUT-- +$i{$j}; \ No newline at end of file Index: scripts/coder_format/tests/tests/variables.phpt =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/coder/scripts/coder_format/tests/tests/Attic/variables.phpt,v retrieving revision 1.1.2.2 diff -u -r1.1.2.2 variables.phpt --- scripts/coder_format/tests/tests/variables.phpt 20 Jan 2008 21:08:24 -0000 1.1.2.2 +++ scripts/coder_format/tests/tests/variables.phpt 20 Jan 2008 23:57:19 -0000 @@ -14,7 +14,7 @@ class CoderTestFile extends SimpleExpectation { private $expected; - /* Filename of test */ + // Filename of test var $filename; protected function describeException($exception) {