@@ -4222,6 +4222,9 @@ private function manageAttribute($text) {
42224222 }
42234223
42244224 final class ReindentColonBlocks extends FormatterPass {
4225+ private $ lastCloseTagHadNewline = false ;
4226+ private $ inTemplateGap = false ;
4227+
42254228 public function candidate ($ source , $ foundTokens ) {
42264229 if (isset ($ foundTokens [T_ENDIF ]) || isset ($ foundTokens [T_ENDWHILE ]) || isset ($ foundTokens [T_ENDFOREACH ]) || isset ($ foundTokens [T_ENDFOR ])) {
42274230 return true ;
@@ -4233,6 +4236,8 @@ public function candidate($source, $foundTokens) {
42334236 public function format ($ source ) {
42344237 $ this ->tkns = token_get_all ($ source );
42354238 $ this ->code = '' ;
4239+ $ this ->lastCloseTagHadNewline = false ;
4240+ $ this ->inTemplateGap = false ;
42364241
42374242 while (list ($ index , $ token ) = $ this ->each ($ this ->tkns )) {
42384243 list ($ id , $ text ) = $ this ->getToken ($ token );
@@ -4274,15 +4279,51 @@ public function format($source) {
42744279 $ this ->printUntil (T_END_HEREDOC );
42754280 break ;
42764281
4282+ case T_CLOSE_TAG :
4283+ $ this ->appendCode ($ text );
4284+ $ this ->lastCloseTagHadNewline = $ this ->hasLn ($ text );
4285+ $ this ->inTemplateGap = $ this ->lastCloseTagHadNewline ;
4286+ break ;
4287+
4288+ case T_OPEN_TAG :
4289+ case T_OPEN_TAG_WITH_ECHO :
4290+ $ this ->inTemplateGap = false ;
4291+ if ($ this ->hasLn ($ text ) && $ this ->indent > 0 && !$ this ->lastCloseTagHadNewline ) {
4292+ $ indent = $ this ->getIndent ();
4293+ $ text = preg_replace ("/ \n$/ " , "\n" . $ indent , $ text );
4294+ }
4295+ $ this ->appendCode ($ text );
4296+ break ;
4297+
42774298 default :
42784299 $ hasLn = $ this ->hasLn ($ text );
4279- if ($ hasLn ) {
4280- if ($ this ->rightTokenIs ([T_ENDIF , T_ELSE , T_ELSEIF , T_ENDFOR , T_ENDFOREACH , T_ENDWHILE ])) {
4281- $ this ->setIndent (-1 );
4282- $ text = str_replace ($ this ->newLine , $ this ->newLine . $ this ->getIndent (), $ text );
4283- $ this ->setIndent (+1 );
4284- } else {
4285- $ text = str_replace ($ this ->newLine , $ this ->newLine . $ this ->getIndent (), $ text );
4300+ if ($ hasLn && !$ this ->inTemplateGap ) {
4301+ $ skipIndent = false ;
4302+ if ($ this ->rightTokenIs (T_CLOSE_TAG )) {
4303+ list (, $ closeTagText ) = $ this ->inspectToken (+1 );
4304+ if ($ this ->hasLn ($ closeTagText )) {
4305+ $ skipIndent = true ;
4306+ }
4307+ }
4308+ if (!$ skipIndent ) {
4309+ $ indent = $ this ->getIndent ();
4310+ if ($ this ->rightUsefulTokenIs ([T_ENDIF , T_ELSE , T_ELSEIF , T_ENDFOR , T_ENDFOREACH , T_ENDWHILE ])) {
4311+ $ this ->setIndent (-1 );
4312+ $ indent = $ this ->getIndent ();
4313+ $ this ->setIndent (+1 );
4314+ }
4315+ // For T_INLINE_HTML, only add indentation after newlines if the content doesn't already start with whitespace
4316+ if ($ id === T_INLINE_HTML ) {
4317+ $ text = preg_replace_callback ('/\n([^\n])/ ' , function ($ m ) use ($ indent ) {
4318+ // If the char after newline is already whitespace, don't add more
4319+ if ($ m [1 ] === "\t" || $ m [1 ] === " " ) {
4320+ return "\n" . $ m [1 ];
4321+ }
4322+ return "\n" . $ indent . $ m [1 ];
4323+ }, $ text );
4324+ } else {
4325+ $ text = str_replace ($ this ->newLine , $ this ->newLine . $ indent , $ text );
4326+ }
42864327 }
42874328 }
42884329 $ this ->appendCode ($ text );
@@ -7975,6 +8016,18 @@ public function format($source) {
79758016 if (0 != $ idx && $ idx < $ lastLine ) {
79768017 $ indent = $ this ->indentChar ;
79778018 }
8019+ // Don't add padding if the line already has sufficient leading whitespace
8020+ $ expectedPrefix = $ before . $ indent ;
8021+ $ expectedLen = strlen ($ expectedPrefix );
8022+ if ($ expectedLen > 0 ) {
8023+ // Get leading whitespace from the line
8024+ preg_match ('/^[\t ]*/ ' , $ line , $ m );
8025+ $ lineLeadingWs = isset ($ m [0 ]) ? $ m [0 ] : '' ;
8026+ if (strlen ($ lineLeadingWs ) >= $ expectedLen ) {
8027+ // Line already has enough indentation, don't add more
8028+ continue ;
8029+ }
8030+ }
79788031 $ tmp [$ idx ] = $ before . $ indent . $ line ;
79798032 }
79808033
@@ -9292,6 +9345,11 @@ public function format($source) {
92929345
92939346 if (T_OPEN_TAG_WITH_ECHO == $ id ) {
92949347 $ text = '<?php echo ' ;
9348+ // Skip following whitespace to avoid double space
9349+ $ nextToken = isset ($ this ->tkns [$ index + 1 ]) ? $ this ->tkns [$ index + 1 ] : null ;
9350+ if ($ nextToken && is_array ($ nextToken ) && T_WHITESPACE == $ nextToken [0 ]) {
9351+ $ this ->each ($ this ->tkns );
9352+ }
92959353 }
92969354
92979355 $ this ->appendCode ($ text );
0 commit comments