Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to install Prettier PHP Plugin in Visual Studio Code #1721

Open
alexbalak21 opened this issue May 6, 2021 · 20 comments
Open

How to install Prettier PHP Plugin in Visual Studio Code #1721

alexbalak21 opened this issue May 6, 2021 · 20 comments

Comments

@alexbalak21
Copy link

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.

@czosel
Copy link
Collaborator

czosel commented May 6, 2021

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.

@NateOs
Copy link

NateOs commented May 11, 2021

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?

@alexbalak21
Copy link
Author

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.

@czosel
Copy link
Collaborator

czosel commented May 13, 2021

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)

@NateOs
Copy link

NateOs commented May 15, 2021

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

@czosel
Copy link
Collaborator

czosel commented May 15, 2021

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

@NateOs
Copy link

NateOs commented May 19, 2021

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

@czosel
Copy link
Collaborator

czosel commented May 20, 2021

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

@robertandrews
Copy link

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.

@czosel
Copy link
Collaborator

czosel commented Mar 6, 2022

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

@robertandrews
Copy link

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

Yes. test.php.

Do you have the prettier extension for vscode installed?

Yes.

@czosel
Copy link
Collaborator

czosel commented Mar 6, 2022

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

@UnderDogg
Copy link

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

@robertandrews
Copy link

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?

@UnderDogg
Copy link

UnderDogg commented Mar 28, 2022

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.

@czosel
Copy link
Collaborator

czosel commented Mar 28, 2022

@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).

@jaxramus
Copy link

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.

@webprogrammerio
Copy link

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.

@Gataquadrada
Copy link

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

@Paul-Hebert
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants