I was really confused as to why my file wasn't formatting on save. I didn't see anything in the output panel, received no errors or warnings, it just ... did nothing.

I was able to run it down to a minimal reproduction case:

failing input:

<?php
function foo(  $a){
  return <<<FOO
  $a
  FOO;
}
?>

working input:

<?php
function foo(  $a){
  return <<<FOO
  a
  FOO;
}
?>

This wouldn't be as annoying, except that even unrelated functions in the same file don't get formatted.

    Hello Kris,

    Thank you for your post.

    • I can confirm, this appears to break formatter => we will fix that ASAP
    • We have a fail-safe mechanism in place when the formatter is not able to format something correctly, the process is stopped altogether. This is to make sure we will not leave the file in a bad state, but you are right that it should say something in such a case. Will be done as well. It should not happen at all, but sometimes it does - like in this case.

    Thanks again!

      Awesome, thanks!

      I appreciate the failsafe, since I did run into exactly that extension with another plugin that didn't support private class methods :) I appreciate also the change to make it clear when formatting failed for some reason.

        Got another repro for you:

        Working:

        <script type="text/javascript">
            '<>'
            <?php echo 'hi'; ?>
        </script>

        Not working:

        <script type="text/javascript">
            '<'
            <?php echo 'hi'; ?>
        </script>

        I recognize that the mixed context problem here (php in javascript in php etc) can be particularly troublesome 😅

        It appears that a quoted < character that isn't closed is treated as the start of some html in some contexts. For example, '<'+'>' also breaks. It also breaks if it's in a javascript comment, for example. My guess is that the extension is not treating literal javascript as javascript (or a block of unknown content) and is instead just thinking the contents are HTML. However, strangely, if you comment or remove the <?php .. ?> bit, formatting also works. They both need to be present to fail. If you put the '<' after the <?php .. ?> block, it formats.

          It actually seems to fail even on balanced <>:

          <script type="text/javascript">
            '<div></div>'
            <?php     echo 'hi'; ?>
          </script>

            And maybe related:

            <?php
            
            function foo() {
              ?>
              <script type="text/javascript">
                var foo = {
                  'foo': <?='hi' ?>
                      };
              </script>
            <?php
            }

            Every time you save this, the } on line 8 "creeps" one tab stop to the right 😂

              ... I think I'll give up on formatting this repo for now. Found just a normal old PHP one too:

                <?php
                $foo = (new Builder())
                  ->bar()
                      ->baz();
                ?>

              This formats if the -> are all on the same line, but breaks (silently, this time) if they are on different lines.

                Hi Kris,

                Thank you for reporting these.

                We will go through them and fix!

                Thanks again!

                  (By the way, even though I'm sure it's a pain to implement, good autoformatting here that includes heredocs, html, js, etc. -- is extremely useful. We were previously using "format html in php" alongside another PHP extension that didn't format HTML, and that's an awkward and painful solution because it's implemented in a hacky way that interferes with undo. So, thanks so much for putting the effort into a full solution!)

                    Hi Kris,

                    Thank you for your kind words and continuous feedback! We're doing what we can to make it great.

                    Btw:

                    • The last bug you've reported is fixed in the pre-release.
                    • The one with '<' will be in the next pre-release
                    • and we are working on moving } in js code

                    Thank you!

                      \o/

                      Here's one more (perhaps different) case for the "moving indents"

                      <?php if (true): ?>
                        <div x-data="{
                                                  foo: 'bar'
                                                }">
                        <?php endif; ?>

                        Hi Kris,

                        Thank you for another case. It is actually a different case :-) will be solved as well.

                          Hi Kris,

                          All the fixes are available in the pre-release.

                          The next pre-release will also include a message when the formatting fail-safe mechanism stops the process. That should help the situation when a user is not sure why it doesn't work.

                          I'm closing this one, but please feel free to create a new one when you run into anything else.

                          Thank you!

                            Write a Reply...