@@ -4826,8 +4826,18 @@ public function candidate($source, $foundTokens) {
48264826 $ tkns = token_get_all ($ source );
48274827
48284828 $ this ->tkns = [];
4829+ $ inShortEcho = false ;
48294830 foreach ($ tkns as $ i => $ token ) {
4830- if (T_WHITESPACE === $ token [0 ] && !$ this ->hasLn ($ token [1 ])) {
4831+ if (isset ($ token [0 ]) && T_OPEN_TAG_WITH_ECHO === $ token [0 ]) {
4832+ $ inShortEcho = true ;
4833+ } elseif (isset ($ token [0 ]) && T_CLOSE_TAG === $ token [0 ]) {
4834+ $ inShortEcho = false ;
4835+ }
4836+ if (T_WHITESPACE === $ token [0 ] && !$ this ->hasLn ($ token [1 ])) {
4837+ if ($ inShortEcho ) {
4838+ $ this ->tkns [] = $ token ;
4839+ continue ;
4840+ }
48314841 $ c = count ($ this ->tkns );
48324842 if (PHP_VERSION_ID >= 80000 && $ c ) {
48334843 if (isset ($ tkns [$ i + 1 ][1 ]) && $ tkns [$ i + 1 ][1 ] === '& '
@@ -5418,7 +5428,7 @@ public function format($source) {
54185428 break ;
54195429
54205430 case T_CLOSE_TAG :
5421- $ space = $ this ->getSpace (!$ hasEchoAfterOpenTag && !$ this ->hasLnBefore ());
5431+ $ space = $ this ->getSpace (!$ hasEchoAfterOpenTag && !$ this ->hasLnBefore ());
54225432 if ($ space === '' ) {
54235433 if ($ this ->leftTokenIs ([ST_SEMI_COLON ]) && !$ this ->hasLnBefore () && !$ hasOpenTagWithEcho ) {
54245434 $ space = $ this ->getSpace ();
@@ -5433,8 +5443,11 @@ public function format($source) {
54335443 case T_OPEN_TAG_WITH_ECHO :
54345444 $ hasEchoAfterOpenTag = true ;
54355445 $ hasOpenTagWithEcho = true ;
5436- $ this ->appendCode ($ text );
5437- break ;
5446+ $ this ->appendCode ($ text );
5447+ $ this ->printUntil (T_CLOSE_TAG );
5448+ $ hasEchoAfterOpenTag = false ;
5449+ $ hasOpenTagWithEcho = false ;
5450+ continue 2 ;
54385451
54395452 case T_OPEN_TAG :
54405453 $ hasEchoAfterOpenTag = true ;
@@ -7883,26 +7896,30 @@ public function format($source) {
78837896 switch ($ id ) {
78847897 case T_OPEN_TAG :
78857898
7886- $ prevText = null ;
7887- if ($ this ->ptr > 0 ) {
7888- list ($ id2 , $ prevText ) = $ this ->getToken ($ this ->leftToken ());
7889- }
7899+ $ prevText = null ;
7900+ if ($ this ->ptr > 0 ) {
7901+ list ($ id2 , $ prevText ) = $ this ->getToken ($ this ->leftToken ());
7902+ }
78907903
7891- $ prevSpace = '' ;
7892- if ($ prevText !== null ) {
7893- $ lastNewlineText = strrchr ($ prevText , $ this ->newLine );
7894- if ($ lastNewlineText !== false ) {
7895- $ prevSpace = substr ($ lastNewlineText , 1 );
7896- } elseif (preg_match ('/^\s+$/ ' , $ prevText )) {
7897- // If prevText contains only whitespace, use it as prevSpace
7898- $ prevSpace = $ prevText ;
7899- }
7900- }
7901- $ skipPadLeft = false ;
7902- if (rtrim ($ prevSpace ) == $ prevSpace ) {
7903- $ skipPadLeft = true ;
7904+ $ linePrefix = '' ;
7905+ if ($ prevText !== null ) {
7906+ $ lastNewlineText = strrchr ($ prevText , $ this ->newLine );
7907+ if ($ lastNewlineText !== false ) {
7908+ $ linePrefix = substr ($ lastNewlineText , 1 );
7909+ } else {
7910+ $ linePrefix = $ prevText ;
7911+ }
79047912 }
7905- $ prevSpace = preg_replace ('/[^\s\t]/ ' , ' ' , $ prevSpace );
7913+
7914+ // For the first line, only restore trailing whitespace before the open tag.
7915+ // For subsequent lines, align using a same-width prefix (non-whitespace becomes spaces).
7916+ $ padFirstLine = '' ;
7917+ if ($ linePrefix !== '' ) {
7918+ if (preg_match ('/[ \t]*$/ ' , $ linePrefix , $ m )) {
7919+ $ padFirstLine = $ m [0 ];
7920+ }
7921+ }
7922+ $ padOtherLines = ($ linePrefix === '' ) ? '' : preg_replace ('/[^\s\t]/ ' , ' ' , $ linePrefix );
79067923
79077924 $ placeholders = [];
79087925 $ strings = [];
@@ -7950,18 +7967,14 @@ public function format($source) {
79507967 $ tmp = explode ($ this ->newLine , $ stack );
79517968 $ lastLine = sizeof ($ tmp ) - 2 ;
79527969 foreach ($ tmp as $ idx => $ line ) {
7953- $ before = $ prevSpace ;
7970+ $ before = ( 0 === $ idx ) ? $ padFirstLine : $ padOtherLines ;
79547971 if ('' === trim ($ line )) {
79557972 continue ;
79567973 }
79577974 $ indent = '' ;
79587975 if (0 != $ idx && $ idx < $ lastLine ) {
79597976 $ indent = $ this ->indentChar ;
79607977 }
7961- if ($ skipPadLeft ) {
7962- $ before = '' ;
7963- $ skipPadLeft = false ;
7964- }
79657978 $ tmp [$ idx ] = $ before . $ indent . $ line ;
79667979 }
79677980
0 commit comments