vscode-phpfmt icon indicating copy to clipboard operation
vscode-phpfmt copied to clipboard

Strange formatting in new line

Open PrinceOfAbyss opened this issue 11 months ago • 10 comments

I don't know how to describe the situation. So let me give you an example... So suppose this code extract:

        <?php if ( isset( $_GET['tax'] ) ) {
        global $wpdb;

        $terms = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woo_filters_mapping WHERE tax_slug = '%s';", sanitize_text_field( $_GET['tax'] ) ) ); ?>
		<div id="menu-management" style="margin-top: 2em;">
			<form id="terms-mapping" method="post" enctype="multipart/form-data">

			</form>
		</div>
        <?php }?>

Please notice the difference in the tab kind: 4 spaces for the PHP code and 1 literal tab for HTML code

So, when I format the document, it becomes like this:

        <?php if ( isset( $_GET['tax'] ) ) {
        global $wpdb;

        $terms = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woo_filters_mapping WHERE tax_slug = '%s';", sanitize_text_field( $_GET['tax'] ) ) ); ?>
		<div id="menu-management" style="margin-top: 2em;">
			<form id="terms-mapping" method="post" enctype="multipart/form-data">

			</form>
		</div>
        <?php
}?>

For some reason the closing of the PHP block, along with the closing bracket of the if() statement move to a new line.

PrinceOfAbyss avatar Jan 08 '25 23:01 PrinceOfAbyss

Yes, unfortunatelly the formatter does not prefectly work when mixing PHP and HTML. It's a known bug hard to fix, I'll try to take a look at it.

driade avatar Jan 09 '25 09:01 driade

Hello @driade

Any update on this?

PrinceOfAbyss avatar Jan 29 '25 00:01 PrinceOfAbyss

Sorry, I has no chance to look at it.

driade avatar Jan 29 '25 06:01 driade

Could you at least take a look at this very specific case, which is the most annoying one:

        <?php if ( true ) { // some condition here
        echo 'aaa';
        // more php code here	
        ?>
	<div id="menu-management">
		<span>more html code</span>
	</div>
	<?php } ?>

When formatting the above, it changes to this:

        <?php if ( true ) { // some condition here
        echo 'aaa';
        // more php code here	
        ?>
	<div id="menu-management">
		<span>more html code</span>
	</div>
	<?php
}?>

PrinceOfAbyss avatar Feb 03 '25 00:02 PrinceOfAbyss

Hi @PrinceOfAbyss, I'll do that. I've seen that the current behavior is that if the opening tag has a carriage return, the closing tag must have one too. I'll try to check why it was coded that way.

driade avatar Feb 03 '25 08:02 driade

Yeah, exactly! I was able to move the other lines (ie echo 'aaa'; and // more php code here) on another part of the code above, so that the if became an one-liner, and that was enough to make <?php }?> not braking up anymore!

PS: While you do that, could you also take another look on something else? Curly braces, unlike semi-colon ;, get their space character before the closing tag removed. So <?php if ( true ) { ?> becomes <?php if ( true ) {?>. Similarly, the closing curly brace of an if, from <?php } ?> becomes <?php }?>. So could you take care of that too?

PrinceOfAbyss avatar Feb 03 '25 08:02 PrinceOfAbyss

Hi @PrinceOfAbyss

This

Yeah, exactly! I was able to move the other lines (ie echo 'aaa'; and // more php code here) on another part of the code above, so that the if became an one-liner, and that was enough to make not braking up anymore!

Should be fixed in the latest release.

Regarding

While you do that, could you also take another look on something else? Curly braces, unlike semi-colon ;, get their space character before the closing tag removed. So becomes . Similarly, the closing curly brace of an if, from becomes . So could you take care of that too?

I'm not sure if this is the intended behavior of the program. Some of these rules were created almost 10 years ago (not by me) for a reason, and they are expected to work as they currently do.

If you could point me to sites or tools that format the code this way, I'd be happy to replicate the behavior you're asking for.

Thanks!

driade avatar Feb 03 '25 18:02 driade

I'm not sure you understood what I meant! Take a look at the screenshot below:

Image

Wouldn't it look cleaner if there was a space character where the two arrows point to?

PrinceOfAbyss avatar Feb 04 '25 02:02 PrinceOfAbyss

Hi @PrinceOfAbyss, yes, I understand your request. As I mentioned, this has been the standard for many years, and we need strong reasons to change this behavior. How do other formatters handle this, for example?

driade avatar Feb 04 '25 07:02 driade

Hello @driade TBH I don't know. I've only used your formatter as it was the only formatter where I could use PSR2 formatting style, but still be able to make some adjustments using some params!

NVM, it's a small detail, I can live with it!

PrinceOfAbyss avatar Feb 04 '25 10:02 PrinceOfAbyss