Index: 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
--- coder_format.inc	20 Jan 2008 21:08:23 -0000	1.2.4.6
+++ coder_format.inc	20 Jan 2008 22:54:29 -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().
  *
@@ -225,9 +226,14 @@
   $in_multiline    = array();
   $after_semicolon = FALSE;
   $after_case      = FALSE;
+  $after_comment   = 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);
@@ -342,27 +348,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 +450,9 @@
           $result .= $text;
           break;
       }
+      
+      $position_last_significant_token = strlen(rtrim($result)) - 1;
+      $after_comment = FALSE;
     }
     else {
       // If we get here, then we have found not a single char, but a token.
@@ -544,21 +544,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 +564,7 @@
           }
           
           $in_variable = FALSE;
+          
           break;
 
         case T_IF:
@@ -773,28 +764,26 @@
           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;
+              // 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;
+              }
             }
           }
           break;
@@ -805,14 +794,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 +811,22 @@
 
       // 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;
+      }
+      
     }
   }
   return $result;
@@ -838,7 +843,7 @@
  * @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 +858,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;
 }
 
 /**
