+ Start a Discussion
louisa barrett 7louisa barrett 7 

Visual Studio Code and Prettier not respecting print width field

Hi all,

I have decided to swap from the dev console to visual studio code.I read there was no built in support for formatting apex and it was suggested to install Prettier, which I have done.
When the code is formatted (Shift, Alt + F) parameters in methods are being dropped to the next line. This means that only 1/4 of the actual line space is being used. Below is a screen shot of what it looks like after formatting and what I would like it to look like. After some research it was suggested to change the Prettier:Print Width property from the default value or 80 to 120, which I've done. This made no difference, I've tried increasing it 250 and it the behaviour is still the same. It has also been mentioned that the print width property doesn't work if tsLint is enabled, but I don't have that installed, so I don't think that is the issue.
I've attached some screen shots and code snippets for clarity.

Code formatting example:
//Desired formatting
 public void OnAfterUpdate(Asset[] oldAssets, Map<ID, Asset> oldAssetsMap, Asset[] updatedAssets,Map<ID, Asset> assetsMap){
    fetchAssetCounts(updatedAssets, assetsMap);

//After formatting in VS Code using Prettier
 public void OnAfterUpdate(
    Asset[] oldAssets,
    Map<ID, Asset> oldAssetsMap,
    Asset[] updatedAssets,
    Map<ID, Asset> assetsMap
  ) {
    fetchAssetCounts(updatedAssets, assetsMap);
Screen shot of VS Code:
There is more than enough room on the same line without the parameters dropping to the next line
User-added image

Print width property
User-added image

.prettierrc file
  "trailingComma": "none",
  "overrides": [
      "files": "**/lwc/**/*.html",
      "options": { "parser": "lwc"}
      "files": "*.{cmp,page,component}",
      "options": { "parser": "html" }

settings.json file;
    "workbench.colorTheme": "Visual Studio Dark",
    "salesforcedx-vscode-apex.java.home": "C:\\Program Files\\Java\\jdk-11.0.12",
    "editor.defaultFormatter": "esbenp.prettier-vscode",
    "prettier.requireConfig": true,
    "prettier.arrowParens": "avoid",
    "prettier.useTabs": true,
    "prettier.printWidth": 120,
    "salesforcedx-vscode-core.retrieve-test-code-coverage": true,
    "prettier.proseWrap": "never",
    "prettier.tabWidth": 4,
    "eslint.runtime": ""
Does anyone have any suggestions?

Many thanks
SwethaSwetha (Salesforce Developers) 
HI Louisa,
Came across a similar post from past:https://github.com/prettier/prettier-vscode/issues/371

Your question is best suited to be posted as an issue on https://github.com/prettier/prettier-vscode/issues than Salesforce Forum.

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you