? scripts/coder_format/tests
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.5
diff -u -r1.2.4.5 coder_format.inc
--- scripts/coder_format/coder_format.inc	16 Jan 2008 22:26:33 -0000	1.2.4.5
+++ scripts/coder_format/coder_format.inc	18 Jan 2008 21:27:12 -0000
@@ -166,6 +166,10 @@
  *   $inline_if bool
  *      Controls formatting of ? and : for inline ifs until a ; (semicolon) is
  *      processed.
+ *   $in_function_declaration
+ *      Prevents whitespace after & for function declarations, e.g.
+ *      function &foo(). Is true after function token but before first
+ *      parenthesis.
  *
  * @param $code
  *      The source code to format.
@@ -187,11 +191,14 @@
   $in_do_while    = false;
   
   // Whitespace controls:
-  $in_object   = false;
-  $in_at       = false;
-  $in_php      = false;
-  $in_quote    = false;
-  $inline_if   = false;
+  $in_object        = false;
+  $in_at            = false;
+  $in_php           = false;
+  $in_quote         = false;
+  $inline_if        = false;
+  
+  // Whether or not a function token was encountered:
+  $in_function_declaration = false;
   
   $result      = '';
   $lasttoken   = array(0);
@@ -220,7 +227,8 @@
               ++$braces_in_case;
             }
             ++$_coder_indent;
-            $result = rtrim($result) .' '. $text . coder_br();
+            $result = rtrim($result) .' '. $text;
+            coder_br($result);
           }
           else {
             $in_brace = true;
@@ -243,9 +251,10 @@
             $result = rtrim($result);
             if (substr($result, -1) != '{') {
               // Avoid line break in empty curly braces.
-              $result .= coder_br();
+              coder_br($result);
             }
-            $result .= $text . coder_br();
+            $result .= $text;
+            coder_br($result);
           }
           else {
             $in_brace = false;
@@ -256,7 +265,7 @@
         case ';':
           $result = rtrim($result) . $text;
           if (!$parenthesis && !$in_heredoc) {
-            $result .= coder_br();
+            coder_br($result);
           }
           else {
             $result .= ' ';
@@ -279,13 +288,17 @@
             if ($in_case) {
               ++$_coder_indent;
             }
-            $result = rtrim($result) . $text . coder_br();
+            $result = rtrim($result) . $text;
+            coder_br($result);
           }
           break;
         
         case '(':
           $result .= $text;
           ++$parenthesis;
+          // Terminate function declaration, as a parenthesis indicates
+          // the beginning of the arguments.
+          $in_function_declaration = false;
           break;
         
         case ')':
@@ -293,7 +306,9 @@
             // Fix indent of right parenthesis in multiline arrays by
             // increasing indent for each parenthesis and decreasing one level.
             $_coder_indent = $_coder_indent + $parenthesis - 1;
-            $result = rtrim($result) . coder_br() . $text;
+            $result = rtrim($result);
+            coder_br($result);
+            $result .= $text;
             $_coder_indent = $_coder_indent - $parenthesis + 1;
           }
           else {
@@ -340,14 +355,23 @@
             $result .= $text;
           }
           else {
-            $result = rtrim($result) .' '. $text .' ';
+            $result = rtrim($result) .' '. $text;
+            // Ampersands used to declare reference return value for
+            // functions should not have trailing space.
+            if (!$in_function_declaration) {
+              $result .= ' ';
+            }
           }
           break;
 
         case '-':
           $result = rtrim($result);
           // Do not add a space before negative numbers or variables.
-          if (substr($result, -1) == '>' || substr($result, -1) == '=' || substr($result, -1) == ',' || substr($result, -1) == ':') {
+          $c = substr($result, -1);
+          if ($c == '(') {
+            $result .= ltrim($text);
+          }
+          elseif ($c == '>' || $c == '=' || $c == ',' || $c == ':' || $c == '?') {
             $result .= ' '. $text;
           }
           else {
@@ -397,11 +421,11 @@
           $in_php = true;
           // Add a line break between two PHP tags.
           if (substr(rtrim($result), -2) == '?>') {
-            $result .= coder_br();
+            coder_br($result);
           }
           $result .= trim($text);
           if ($first_php_tag) {
-            $result .= coder_br();
+            coder_br($result);
             $first_php_tag = false;
           }
           else {
@@ -458,12 +482,13 @@
             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 = rtrim($result);
+              coder_br($result);
               $_coder_indent = $_coder_indent - $parenthesis;
             }
             else {
               // Discard any whitespace, just insert a line break.
-              $result .= coder_br();
+              coder_br($result);
             }
           }
           break;
@@ -504,7 +529,9 @@
         case T_ELSE:
         case T_ELSEIF:
           // Write else and else if to a new line.
-          $result = rtrim($result) . coder_br() . trim($text) .' ';
+          $result = rtrim($result);
+          coder_br($result);
+          $result .= trim($text) .' ';
           break;
         
         case T_CASE:
@@ -515,19 +542,22 @@
             $in_case = true;
             // Add a line break between cases.
             if (substr($result, -1) != '{') {
-              $result .= coder_br();
+              coder_br($result);
             }
           }
           else {
             // Decrease current indent to align multiple cases.
             --$_coder_indent;
           }
-          $result .= coder_br() . trim($text) .' ';
+          coder_br($result);
+          $result .= trim($text) .' ';
           break;
         
         case T_BREAK:
           // Write break to a new line.
-          $result = rtrim($result) . coder_br() . trim($text);
+          $result = rtrim($result);
+          coder_br($result);
+          $result .= trim($text);
           if ($in_case && !$braces_in_case) {
             --$_coder_indent;
             $in_case = false;
@@ -546,13 +576,16 @@
           break;
         
         case T_FUNCTION:
+          $in_function_declaration = true;
+          // Fall through.
         case T_CLASS:
           // Write function and class to new lines.
           $result = rtrim($result);
           if (substr($result, -1) == '}') {
-            $result .= coder_br();
+            coder_br($result);
           }
-          $result .= coder_br() . trim($text) .' ';
+          coder_br($result);
+          $result .= trim($text) .' ';
           break;
         
         case T_EXTENDS:
@@ -600,7 +633,9 @@
         case T_DOC_COMMENT:
           if (substr($text, 0, 3) == '/**') {
             // Prepend a new line.
-            $result = rtrim($result) . coder_br() . coder_br();
+            $result = rtrim($result);
+            coder_br($result);
+            coder_br($result);
             
             // Remove carriage returns.
             $text = str_replace("\r", '', $text);
@@ -612,7 +647,8 @@
               
               // Add a new line between function description and first parameter description.
               if (!$params_fixed && substr($lines[$l], 0, 8) == '* @param' && $lines[$l - 1] != '*') {
-                $result .= ' *'. coder_br();
+                $result .= ' *';
+                coder_br($result);
                 $params_fixed = true;
               }
               else if (!$params_fixed && substr($lines[$l], 0, 8) == '* @param') {
@@ -622,7 +658,8 @@
               
               // Add a new line between function params and return.
               if (substr($lines[$l], 0, 9) == '* @return' && $lines[$l - 1] != '*') {
-                $result .= ' *'. coder_br();
+                $result .= ' *';
+                coder_br($result);
               }
               
               // Add one space indent to get ' *[...]'.
@@ -631,7 +668,7 @@
               }
               $result .= $lines[$l];
               if ($l < count($lines)) {
-                $result .= coder_br();
+                coder_br($result);
               }
             }
           }
@@ -640,12 +677,13 @@
             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 = rtrim($result);
+              coder_br($result);
               $_coder_indent = $_coder_indent - $parenthesis;
             }
             else {
               // Discard any whitespace, just insert a line break.
-              $result .= coder_br();
+              coder_br($result);
             }
           }
           break;
@@ -655,12 +693,14 @@
           break;
         
         case T_START_HEREDOC:
-          $result .= trim($text) . coder_br(false);
+          $result .= trim($text);
+          coder_br($result, false);
           $in_heredoc = true;
           break;
         
         case T_END_HEREDOC:
-          $result .= trim($text) . coder_br(false);
+          $result .= trim($text);
+          coder_br($result, false);
           $in_heredoc = false;
           break;
         
@@ -678,20 +718,36 @@
 
 /**
  * Generate a line feed including current line indent.
- *
+ * 
+ * This function will also remove all line indentation from the
+ * previous line if no text was added.
+ * 
+ * @param $result
+ *   Result variable to append break and indent to.
  * @param $add_indent
  *   Whether to add current line indent after line feed.
- * @return
- *   The resulting string.
  */
-function coder_br($add_indent = true) {
+function coder_br(&$result, $add_indent = true) {
   global $_coder_indent;
   
+  // Scan result backwards for whitespace.
+  for ($i = strlen($result) - 1; $i >= 0; $i--) {
+    if ($result[$i] == ' ') {
+      continue;
+    }
+    if ($result[$i] == "\n") {
+      $result = rtrim($result, ' ');
+      break;
+    }
+    // Non-whitespace was encountered, no changes necessary.
+    break;
+  }
+  
   $output = "\n";
   if ($add_indent && $_coder_indent >= 0) {
     $output .= str_repeat('  ', $_coder_indent);
   }
-  return $output;
+  $result .= $output;
 }
 
 /**
@@ -864,11 +920,13 @@
     '#weight' => 2,
     // [\040\t] matches only a space or tab.
     // (?!case) prevents matching of case statements.
+    // (?!*) prevents matching of docblock statements.
+    // (?!//) prevents matching of comment 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',
+    // [^;,\$] prevents matching of CVS keyword Id comment and double slashes
+    //   in quotes (f.e. "W3C//DTD") or in multiline arrays (f.e. "//",)
+    '#search' => '@^([\040\t]*)(?!case)(?!\*)(?!//)(\S.+?)[\040\t]*[^:]//\s*([^;,\$]+?)$@m',
     '#replace' => "$1// $3\n$1$2",
   );
 }
