jhipster/prettier-java

Integration issues with VS Code prettier extension

Closed this issue · 4 comments

MLNW commented

Prettier-Java 2.1.0

I have a mono repo with a Java and a SvelteKit application. It's structure looks like this:

.vscode/
  settings.json
code/
  java
  sveltekit
.prettierrc
.prettierignore
package.json

The package.json contains only prettier related dependencies:

{
  "scripts": {
    "lint": "prettier --plugin-search-dir . --check .",
    "format": "prettier --plugin-search-dir . --write ."
  },
  "devDependencies": {
    "prettier": "2.8.7",
    "prettier-plugin-java": "^2.1.0",
    "prettier-plugin-svelte": "^2.10.0"
  }
}

When I run pnpm run format it works as expected. Both Java and SvelteKit files are formatted correctly.

Now I want to use these plugins from VS code. I install the esbenp.prettier-vscode plugin and try to format files in VS Code. Now I get this error:
grafik

With debug mode enabled I see that the plugin is correctly found:

["INFO" - 3:17:19 PM] Detected local configuration (i.e. .prettierrc or .editorconfig), VS Code configuration will not be used
["INFO" - 3:17:19 PM] Prettier Options:
{
  "filepath": "/home/....../.vscode/settings.json",
  "parser": "json",
  "useTabs": false,
  "singleQuote": true,
  "trailingComma": "none",
  "printWidth": 100,
  "tabWidth": 2,
  "pluginSearchDirs": [
    "/home/......"
  ],
  "plugins": [
    "/home/......./node_modules/.pnpm/prettier-plugin-svelte@2.10.0_prettier@2.8.7_svelte@3.58.0/node_modules/prettier-plugin-svelte/plugin.js",
    "/home/....../node_modules/.pnpm/prettier-plugin-java@2.1.0/node_modules/prettier-plugin-java/dist/index.js"
  ]
}
["INFO" - 3:17:19 PM] Formatting completed in 268ms.

In VS Code I see this init message from prettier which does not include Java:

["DEBUG" - 3:12:29 PM] Enabling Prettier for Workspace /home/........
{
  "languageSelector": [
    {
      "pattern": "/home/......./**/*.{js,_js,bones,cjs,es,es6,frag,gs,jake,javascript,jsb,jscad,jsfl,jslib,jsm,jspre,jss,mjs,njs,pac,sjs,ssjs,xsjs,xsjslib,wxs,js.flow,jsx,ts,cts,mts,tsx,importmap,json,4DForm,4DProject,avsc,geojson,gltf,har,ice,JSON-tmLanguage,mcmeta,tfstate,tfstate.backup,topojson,webapp,webmanifest,yy,yyp,jsonc,code-snippets,sublime-build,sublime-commands,sublime-completions,sublime-keymap,sublime-macro,sublime-menu,sublime-mousemap,sublime-project,sublime-settings,sublime-theme,sublime-workspace,sublime_metrics,sublime_session,json5,css,wxss,pcss,postcss,less,scss,handlebars,hbs,graphql,gql,graphqls,md,livemd,markdown,mdown,mdwn,mkd,mkdn,mkdown,ronn,scd,workbook,mdx,component.html,html,hta,htm,html.hl,inc,xht,xhtml,mjml,vue,yml,mir,reek,rviz,sublime-syntax,syntax,yaml,yaml-tmlanguage,yaml.sed,yml.mysql}",
      "scheme": "file"
    },
    {
      "language": "javascript"
    },
    {
      "language": "mongo"
    },
    {
      "language": "javascriptreact"
    },
    {
      "language": "typescript"
    },
    {
      "language": "typescriptreact"
    },
    {
      "language": "json"
    },
    {
      "language": "jsonc"
    },
    {
      "language": "json5"
    },
    {
      "language": "css"
    },
    {
      "language": "postcss"
    },
    {
      "language": "less"
    },
    {
      "language": "scss"
    },
    {
      "language": "handlebars"
    },
    {
      "language": "graphql"
    },
    {
      "language": "markdown"
    },
    {
      "language": "mdx"
    },
    {
      "language": "html"
    },
    {
      "language": "vue"
    },
    {
      "language": "yaml"
    },
    {
      "language": "ansible"
    },
    {
      "language": "home-assistant"
    },
    {
      "language": "jsonc",
      "scheme": "vscode-userdata"
    }
  ],
  "rangeLanguageSelector": [
    {
      "language": "javascript"
    },
    {
      "language": "javascriptreact"
    },
    {
      "language": "typescript"
    },
    {
      "language": "typescriptreact"
    },
    {
      "language": "json"
    },
    {
      "language": "jsonc"
    },
    {
      "language": "graphql"
    }
  ]
}

I scoured the web but was unable to find any solutions to this issue. There is also no more documentation pointing me in the right direction. I hope you can help me figure this issue out and resolve it.

Not sure about my answer, but I think it's because prettier-java is a community plugin, and not an official one:

image

As the project prettier-vscode is maintained directly by the prettier team, I think it's normal they didn't include Java.

MLNW commented

@pascalgrimaud From their documentation I would expect it to work as the plugin is correctly loaded: https://github.com/prettier/prettier-vscode#plugins

This extension supports Prettier plugins when you are using a locally or globally resolved version of prettier. If you have Prettier and a plugin registered in your package.json, this extension will attempt to register the language and provide automatic code formatting for the built-in and plugin languages.

They also hint at it in this issue where they reference this plugin: prettier/prettier-vscode#1124

TyIsI commented

@MLNW following up just in case.

I have it running with esbenp.prettier-vscode.

Be sure to add the following to your .vscode/settings.json:

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

And be sure to restart after changing your .prettierrc too.

MLNW commented

Starting from v2.2.0 of this plugin it works in my VS code.
I guess that is due to the changes to plugin discovery made with prettier v3.0