prettier/plugin-php

Formatting doesn't work in VScode

davidlindercodes opened this issue · 11 comments

My setup looks like this:
VS Code (latest version - no updates available)
Prettier code formatter (6.4.0) - Installed via VS Code extensions
Prettier/plugin-php - installed globally using:
npm install --global prettier @prettier/plugin-php

My VS code setting.json file looks like this:

    {
    "editor.codeActionsOnSave": null,
    "editor.defaultFormatter": "esbenp.prettier-vscode",
    "editor.formatOnSave": true
    }

Yes when I try to format a .php file, using either format on save or smd-shift-p nothing happens, it has zero functionality. What am I doing wrong here? Is there an installation set that I missed?

Prettier is currently working as expected for .js files, it is only plug-php that I am having issues with.

Did you already try installing the plugin locally in your project? (npm install prettier @prettier/plugin-php) It seems that making the prettier-vscode plugin work with global installations of prettier and plugins requires some configuration: https://github.com/prettier/prettier-vscode#prettier-resolution

Also see #1721 (comment) for step-by-step instructions.

Hi Czosel,

Yes, I tried installing the plugin locally, but the same issue still exists.

  "dependencies": {
    "@prettier/plugin-php": "^0.16.3",
    "gulp-cache": "^1.1.3",
    "node-sass": "^5.0.0",
    "prettier": "^2.3.0"
  }

Same problem.

On terminal Prettier formats without problems, but VSCode's extension doesn't recognize the plugin. Even specifying the global or local node_modules in pluginSearchDirs. Prettier is locally and globally installed.

image

My local .prettierrc:

{
  "phpVersion": "7.0",
  "tabWidth": 4,
  "useTabs": false,
  "singleQuote": true,
  "trailingCommaPHP": true,
  "pluginSearchDirs": [
    "/HDExterno/Langs/PHP/plugin/html/wp-content/themes/api/node_modules",
    "./node_modules",
    "node_modules"
  ],
  "plugins": [
    "@prettier/plugin-php"
  ],
  "parser": "php"
}

My settings.json:

{
    // ...
    "editor.defaultFormatter": "esbenp.prettier-vscode",
    "[php]": {
        "editor.formatOnSave": true
    },
    // ...
}

@D1360-64RC14 setting pluginSearchDirs, plugins and parser is not necessary if prettierrc is in the same path as your node_modules folder.

I was also having trouble with the global installation.

Adding this to my vscode user settings.json made it work!:

"prettier.resolveGlobalModules": true
tm1000 commented

@kjoedion makes sense. For me I install prettier php for every project individually

The solutions I found across the various tickets here didn't work for me unfortunately, but appreciate everyones efforts to come up with a solution.

I had to extend the prettier document selectors within VS Code to get it to run on PHP files:

.vscode/settings.json

"prettier.documentSelectors": [
    "**/*.{js,jsx,ts,tsx,vue,html,css,scss,less,json,md,mdx,graphql,yaml,yml,php}"
 ]

I also added parser to the prettier config and ended up with this:
.prettierrc

{
 "parser": "php",
 "plugins": ["@prettier/plugin-php"],
 "phpVersion": "8.2"
}

All works fine in VS Code for me now. Hopefully this helps someone else. Thanks for this plugin, big fan of prettier and so I'm really grateful I can bring some of that experience over to the small amount of PHP work I am doing.

Related tickets are: #1721, #1640, prettier/prettier-vscode#2564

czosel commented

@WazzaJB thanks for the write-up! If this is reproducible in a fresh VScode installation, I'd be happy to add this to our documentation 👍

In that case I shall raise a PR and try to prepare a codespace you can trial this in :)

.prettierrc

Thanks a lot, @WazzaJB. Your solution works perfectly. Thanks for sharing.

Thanks, @WazzaJB !

prettier.documentSelectors did the work for me.