plugin-php icon indicating copy to clipboard operation
plugin-php copied to clipboard

How to install Prettier PHP Plugin in Visual Studio Code

Open alexbalak21 opened this issue 4 years ago • 21 comments

Hello,

I am using Visual Studio Code for editing .php files. How can I set up Prettier plugin to format php in vs code. I tried to follow README.md but, I am not able to activate the code formatting on php.

Thank you for your help.

alexbalak21 avatar May 06 '21 12:05 alexbalak21

Hi @alexbalak21, can you describe the steps you took in detail? As far as I know the instructions in the Readme should still be valid.

czosel avatar May 06 '21 20:05 czosel

I have the same issue, happen to be working on a large PHP codebase and the formatting is killing me, any help adding the plugin to already installed prettier in vscode?

NateOs avatar May 11 '21 12:05 NateOs

I use Prettier to format on save on Vs Code. When I use it on php files it is not working. I installed prettier on node globally. When I use: "prettier path/to/file.php --write" it works and formats the file.

How do I add formatting php files on save ? (with Prettier PHP Plugin)

I want it to format my php files on save automatically.

alexbalak21 avatar May 13 '21 14:05 alexbalak21

For me, the plugin works as expected (tested using VScode 1.56.1 with the prettier extension v6.4.0). Try the following steps:

  1. Install the prettier extension for VScode (https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode)
  2. Install prettier and @prettier/plugin-php locally in the root your workspace: npm install --save-dev prettier @prettier/plugin-php or yarn add --dev prettier @prettier/plugin-php
  3. The prettier extension should ask if you want to run the local copy of prettier including all installed plugins: Screenshot from 2021-05-13 18-35-58 If the dialog doesn't appear, try opening the package.json file.
  4. Run "Format Document" (or enable "format on save" and save the file)

czosel avatar May 13 '21 16:05 czosel

Thanks, @czosel, I succeeded in installing the PHP plugin but now it doesn't actually format code but gives a response on the terminal that it has formatted the code but with no effect on the code that is recognisable as change

NateOs avatar May 15 '21 14:05 NateOs

Hi @NateOs, can you share the detailed steps on how to reproduce this?

czosel avatar May 15 '21 20:05 czosel

Great, I have prettier v6.4.0 installed, I downloaded the latest version of the PHP-plugin as at the time and installed it by "npm install --save-dev prettier @prettier/plugin-PHP I got the prompt to allow and i "allowed everywhere"

Before following your install process I had already tried and failed to install, therefore prettier will give an error message when I tried to format PHP, the error went away after installing your way, but now I don't get any actual formatting although prettier on save will tell me in the terminal that everything is ok, also I am not using any prettier config files, just the extension settings in vs code

NateOs avatar May 19 '21 22:05 NateOs

Are you sure the file you’re trying to format is valid PHP? Can you post a screenshot?

czosel avatar May 20 '21 07:05 czosel

I installed it npm install --save-dev prettier @prettier/plugin-php but it doesn't do anything. When I choose "Format Document" from the Command Palette, VS Code pops up to say: "There is no formatter for 'plaintext' files installed."

Screenshot 2022-03-06 at 08 41 36

It then opens up the Extensions market at "category:formatters plaintext".

This is for a doc that starts <?php and ends ?>, one that is picked up as PHP elsewhere...

Screenshot 2022-03-06 at 08 42 50

Help.

robertandrews avatar Mar 06 '22 08:03 robertandrews

Does the file you’re trying to format end with “.php”? Do you have the prettier extension for vscode installed?

czosel avatar Mar 06 '22 11:03 czosel

Does the file you’re trying to format end with “.php”?

Yes. test.php.

Do you have the prettier extension for vscode installed?

Yes.

robertandrews avatar Mar 06 '22 15:03 robertandrews

Hmm, I haven’t seen this before. Probably it would be best if you ask about this over at https://github.com/prettier/prettier-vscode

czosel avatar Mar 06 '22 20:03 czosel

I'm a bit late to the party, but ....

  • open the vscode terminal, to see if there's any output
  • there's a developer console in vscode, it should show any js exceptions when the code will not format

UnderDogg avatar Mar 27 '22 14:03 UnderDogg

I'm a bit late to the party, but ....

  • open the vscode terminal, to see if there's any output
  • there's a developer console in vscode, it should show any js exceptions when the code will not format

@UnderDogg ... I couldn't remember if this plugin was installed (doesn't appear as an extension), so (re?)-ran sudo npm install --global prettier @prettier/plugin-php...

In a PHP file, if I choose from the command palette "Format Document" (that's Prettier, right?), I still get: "There is no formatter for 'plaintext' files installed."

I have resorted to installing other PHP formatters, which correctly format the document on save. I don't know why this one won't.

Actually, I was just navigating to /usr/local/lib/node_modules/@prettier/plugin-php to delete the installed plugin and... it's not there...

@czosel The docs say to run npm install --global prettier @prettier/plugin-php. This @prettier folder is not prettier. This command creates a second folder with the @ prefix. The plugin-php is never added to prettier.

If I instead run sudo npm install --global prettier prettier/plugin-php, (no @), it still creates a folder with the @.

Is that the way this should be working?

robertandrews avatar Mar 28 '22 07:03 robertandrews

I wouldn't know how to answer this one. Focus on this error: There is no formatter for 'plaintext' files installed Check your settings, maybe .php is associated with plaintext? Check this piece:

"files.associations": {},

Or just find plaintext in your settings.json file.

UnderDogg avatar Mar 28 '22 12:03 UnderDogg

@robertandrews Yep, that's how it's supposed to be working. Inside the prettier program, all folders starting with @prettier/plugin- are searched and registered as plugins. You can verify if your installation is working by running prettier from the command line on a php file (something like yarn prettier test.php).

czosel avatar Mar 28 '22 13:03 czosel

Installing locally and opening the package.json file worked for me. I didn't get a popup asking if I wanted to allow it everywhere though.

jaxramus avatar Apr 22 '22 07:04 jaxramus

For me, the plugin works as expected (tested using VScode 1.56.1 with the prettier extension v6.4.0). Try the following steps:

  1. Install the prettier extension for VScode (https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode)
  2. Install prettier and @prettier/plugin-php locally in the root your workspace: npm install --save-dev prettier @prettier/plugin-php or yarn add --dev prettier @prettier/plugin-php

Thank you. This worked for me.

webprogrammerio avatar Apr 25 '22 03:04 webprogrammerio

Setting prettier.resolveGlobalModules to true and installing it globally (npm i -g prettier @prettier/plugin-php) fixed all my issues.

Gataquadrada avatar Aug 24 '22 22:08 Gataquadrada

I was running into a similar issue this morning.

In my case it was because my package.json and node_modules directories were not in the root directory I had open in VS Code (they were in a subdirectory.)

I opened that subdirectory directly and Prettier started working correctly.

Paul-Hebert avatar Jul 26 '23 16:07 Paul-Hebert