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

Formatting doesn't work in VScode #1730

Closed
davidlindercodes opened this issue May 19, 2021 · 11 comments
Closed

Formatting doesn't work in VScode #1730

davidlindercodes opened this issue May 19, 2021 · 11 comments

Comments

@davidlindercodes
Copy link

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.

@czosel
Copy link
Collaborator

czosel commented May 20, 2021

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.

@czosel czosel changed the title No functionality Formatting doesn't work in VScode May 20, 2021
@davidlindercodes
Copy link
Author

davidlindercodes commented May 21, 2021

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"
  }

@D1360-64RC14
Copy link

D1360-64RC14 commented Jan 24, 2022

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
    },
    // ...
}

@tm1000
Copy link

tm1000 commented Feb 3, 2022

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

@kjoedion
Copy link

kjoedion commented Dec 4, 2022

I was also having trouble with the global installation.

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

"prettier.resolveGlobalModules": true

@tm1000
Copy link

tm1000 commented Dec 4, 2022

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

@WazzaJB
Copy link
Contributor

WazzaJB commented Jun 13, 2023

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
Copy link
Collaborator

czosel commented Jun 13, 2023

@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 👍

@WazzaJB
Copy link
Contributor

WazzaJB commented Jun 13, 2023

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

@ClaudiusAyadi
Copy link

.prettierrc

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

@czosel czosel closed this as completed Sep 17, 2023
@Rados51
Copy link

Rados51 commented Dec 13, 2023

Thanks, @WazzaJB !

prettier.documentSelectors did the work for me.

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

8 participants