vscode-format-code-action icon indicating copy to clipboard operation
vscode-format-code-action copied to clipboard

Configuration for non-default formatter?

Open karlhorky opened this issue 4 years ago • 4 comments

Hi @rohit-gohri 👋

I would like to run a non-default formatter on a specific file (namely the TypeScript and JavaScript Language Features, in order to format SQL in tagged template literals), and I was wondering whether you would be open to adding support for other, non-default formatters (eg. "Format Document With...")?

Not sure how the configuration would look, maybe something like:

{
  "editor.codeActionsOnSave": [
    "source.fixAll.format['vscode.typescript-language-features']"
  ]
}

Or, as an example, to format using Prettier if it's not the default:

{
  "editor.codeActionsOnSave": [
    "source.fixAll.format['esbenp.prettier-vscode']"
  ]
}

Not sure if that kind of configuration is supported though. I guess the minimal version of this feature would be to provide a single key like:

{
  "editor.codeActionsOnSave": [
    "source.fixAll.formatWithVscodeFormatter"
  ]
}

But then again, that would mean a key for any supported formatter. However, maybe it's an ok tradeoff... 🤔

karlhorky avatar Dec 03 '20 12:12 karlhorky

I don't think this would be possible. In the current implementation I'm calling VSCode's Format Document action that in turn calls the default formatter (in this case prettier). I can't directly call a specific extension AFAIK. This is my first VS Code extension, so my knowledge is very limited.

You may try changing your default formatter for typescript files or in that particular workspace:

  "[typescript]": {
    "editor.defaultFormatter": "vscode.typescript-language-features",
    "editor.formatOnSave": false,
    "editor.codeActionsOnSave": ["source.fixAll.format", "source.fixAll.eslint"]
  },

rohit-gohri avatar Dec 03 '20 14:12 rohit-gohri

Hm, yeah.. wonder if there's another API to call Format Document With... . Maybe that's something to look into...

You may try changing your default formatter for typescript files or in that particular workspace

The use case is to actually use multiple formatters in a row 😓 (Prettier first, then the built-in TS/JS thing)

karlhorky avatar Dec 03 '20 14:12 karlhorky

Oh.. Will try to look into if that's possible and expose multiple actions.

You might be interested in https://github.com/prettier/prettier-vscode/issues/1555 If prettier adds their own action, then you can use this with TS/JS Lang Features.

rohit-gohri avatar Dec 03 '20 14:12 rohit-gohri

You might be interested in prettier/prettier-vscode#1555

Oh, nice - thanks for the heads up! I've subscribed over there too.

karlhorky avatar Dec 03 '20 14:12 karlhorky