kokororin / vscode-phpfmt

Integrates phpfmt into VS Code
https://marketplace.visualstudio.com/items?itemName=kokororin.vscode-phpfmt
BSD 3-Clause "New" or "Revised" License
130 stars 30 forks source link

Infinite indent on-save and overall issues with indentation. #122

Open HenrijsS opened 1 year ago

HenrijsS commented 1 year ago

So I wanted to switch from PHP Intelephense to phpfmt and it works on just regular PHP files (Ex., laravel controllers), but when I mix HTML in them, hell breaks loose.

See this attached video. It starts with the formatting that intelephense has, but when I save, php blocks are indented at the start and the HTML in the while is just infinitely indenting:

https://share.cleanshot.com/vlw4ftlb

Here's my phpfmt config:

{
    "phpfmt.passes": [
        "ConvertOpenTagWithEcho",
    "DoubleToSingleQuote",
    "AlignPHPCode"
    ],
    "phpfmt.psr2": false
}

EDIT:

It works perfectly fine when I use curly braces instead of endif; and enwhile;

driade commented 1 year ago

Hi @HenrijsS good morning. Could you please share the html snippet so I can reproduce the bug?

HenrijsS commented 1 year ago
<?php get_header(); ?>

<div class="container my-8 mx-auto flex flex-col">
    <?php if (have_posts()) : ?>
        <?php
        while (have_posts()) :
            the_post();
        ?>
            <div class="section-title mb-8">
                <h1><?= get_the_title(); ?></h1>
            </div>
            <?php get_template_part('template-parts/content', 'tribe_event_series'); ?>

        <?php endwhile; ?>

    <?php endif; ?>

</div>

<?php
get_footer();
driade commented 1 year ago

Thanks @HenrijsS I'd try to work on this.

driade commented 1 year ago

Hi @HenrijsS It seems the formatter "ReindentColonBlocks", which is active by default, it's causing the problem. Could you please to add it to the "phpfmt.exclude": section of your config as see if it gets better?

Thanks.

HenrijsS commented 1 year ago

Where could I find the "ReindentColonBlocks"?

CleanShot 2023-09-24 at 15 16 00@2x

EDIT: Added "phpfmt.exclude": ["ReindentColonBlocks"], to my User json file and it worked. What does this option do exactly and what do I lose when disabling it?

driade commented 1 year ago

Hmm, I'm sorry I was wrong. It seems that ReindentColonBlocks is one of the "core" transformers and can't be disabled that way.

I'd suggest you to try and directly edit the vscode-phpft file and add the "exclude", but I don't really know how to do that in VSCode. Maybe @kokororin would help.

HenrijsS commented 1 year ago

No. I disabled it and it worked 😆

I'm just wondering what do I lose when disabling it and if there's a bug overall with this.

driade commented 1 year ago

This formatter seems to scan for "endif", "endforeach", "endwhile" and "endfor" and indent its contents.

So if you use this constructions in your code, yes, you miss that. In my case I don't use that style of coding.

Yes, surely there's a bug in the code, but currently I focus on fixing the easy or the critical ones, as I've somehow "inherited" the package :) The original writers of phpfmt are not here to help!

Cheers.