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

Inline php in HTML gets wrongly indented

Open BasvanH opened this issue 6 years ago • 6 comments

If I add php code inline which is not sticked against the httml code, it will be wrongfully indented.

This gets indented far away from it's original position. <input type="checkbox" id="winrm_status" name="winrm_status" <?php echo ($thisChecked ? 'checked' : ''); ?> >

If done like this so the php is attached to the html code, it sticks on it's place <input type="checkbox" id="winrm_status" name="winrm_status"<?php echo ($thisChecked ? ' checked' : ''); ?> >

Is there an existing option to disable indent on inline html & php?

BasvanH avatar Apr 30 '19 09:04 BasvanH

Maybe due to not having the closing > on the <input> tag is causing the it to bug out?

Kennethjk3 avatar May 09 '19 16:05 Kennethjk3

Maybe due to not having the closing > on the <input> tag is causing the it to bug out?

Well spotted, but this is unfortunately a typo in the created issue. In code we do have the closing >. Have changed the original post. Thanks!

BasvanH avatar May 10 '19 06:05 BasvanH

Looked into it real briefly

maybe adding this line to settings.json "phpfmt.passes": ["AlignPHPCode",]

This is the full settings I found on the interwebz, hopefull it helps

//phpfmt
"phpfmt.php_bin": "php",
"phpfmt.passes": [
    "AlignPHPCode",
    "AlignTypeHint",
    "AddMissingParentheses",
    "ConvertOpenTagWithEcho",
    "DocBlockToComment",
    "IndentTernaryConditions",
    "JoinToImplode",
    "PSR2KeywordsLowerCase",
    "PSR2LnAfterNamespace",
    "PSR2CurlyOpenNextLine",
    "PSR2ModifierVisibilityStaticOrder",
    "PSR2SingleEmptyLineAndStripClosingTag",
    "ReindentSwitchBlocks",
    "RemoveUseLeadingSlash",
    "StripExtraCommaInArray",
    "SpaceBetweenMethods",
],
"phpfmt.exclude": [
    "ReindentComments",
    "StripNewlineWithinClassBody"
],
"phpfmt.psr2": false,

I use beautify with these in my settings and i dont have to many issues with it

  "beautify.language": {
    "js": {
      "type": [
        "javascript",
        "json",
        "jsonc"
      ],
      "filename": [
        ".jshintrc",
        ".jsbeautifyrc"
      ]
    },
    "css": [
      "css",
      "less",
      "scss"
    ],
    "html": [
      "htm",
      "html",
      "php"
    ]
  },

Good luck!

Kennethjk3 avatar May 10 '19 20:05 Kennethjk3

Thanks! I had already set the "AlignPHPCode". I finally got a solution. Apparently it's not PHPfmt whose doing the wrong indent, It's Code own formatting.

I have set these options, and now it works as expected.

"html.format.wrapAttributes": "auto",
"html.format.wrapLineLength": 0

BasvanH avatar May 14 '19 08:05 BasvanH

Well, seems like in some circumstancesthe code still gets wrongly indented. When I remove AlignPHPCode from the passes, the problem is gone. So AlignPHPCode needs to be adjusted so it excludes lines where are on the same row.

BasvanH avatar May 15 '19 11:05 BasvanH

try "Format HTML in PHP " after "phpfmt". then wrong spaces removed.

rexsolus2021 avatar Aug 29 '22 03:08 rexsolus2021

Adding another case; take this input

<ol class="product-items <?php echo /* @noEscape */$type?>">

It will get formatted to

<ol class="product-items<?php echo /* @noEscape */$type ?>">

Note the space after product-items disappeared

vandijkstef avatar Jan 04 '23 10:01 vandijkstef

Updated to the latest version, but my use case is not resolved. It's partially the reason I wanted ignores in the first place.

vandijkstef avatar Apr 28 '23 07:04 vandijkstef