Prettier-vscode: Is there a way to tell specify the files to format (not the ones to ignore!)

Created on 18 Dec 2017  路  7Comments  路  Source: prettier/prettier-vscode

I know of prettier.ignorePath, prettier.disableLanguages and the .prettierignore file. But they all exclude some files.

I want to be able to tell the Prettier extension to only format .ts and .json files. Is this possible?

locked

Most helpful comment

["javascript","javascriptreact","typescript","typescriptreact","json","jsonc","css","postcss","less","scss","graphql","markdown","vue"] these are all supported languages.

You will have to add every languages you don't want in prettier.disableLanguages

In @electrotype 's example: prettier.disableLanguages: ["javascript","javascriptreact","typescriptreact","css","postcss","less","scss","graphql","markdown","vue"] ie everything but the ones you want : "typescript","json","jsonc"

It's not possible to add a language. HTML is not supported by prettier yet

All 7 comments

Looking for same solution, because i want to format .EJS files, which also have HTML syntax.

["javascript","javascriptreact","typescript","typescriptreact","json","jsonc","css","postcss","less","scss","graphql","markdown","vue"] these are all supported languages.

You will have to add every languages you don't want in prettier.disableLanguages

In @electrotype 's example: prettier.disableLanguages: ["javascript","javascriptreact","typescriptreact","css","postcss","less","scss","graphql","markdown","vue"] ie everything but the ones you want : "typescript","json","jsonc"

It's not possible to add a language. HTML is not supported by prettier yet

I am going to throw my weight into this issue too. What brought me here was the automatic formatting that this extension was applying to my project's README.md file. Specifically, I was not a big fan of the 3 spaces that were automatically being added in-between bullet list points and the first character of that list item.

I had set editor.formatOnSave = true so that Prettier would be applied to all files where I wish for it to be applied. In my React project, I imagined that to be just files ending in .js and .jsx. I didn't expect Markdown files to also be formatted. I suppose that the root cause of this problem is that there is no universal way to map the glob of files that we apply Prettier to (in my case, through the args of the prettier command run in the "lint" script of package.json) to this extension. I interpret Prettier on its own as being an opt-in tool, as it won't actually format anything unless you pass it a specific glob. Whereas, this extension is acting like an opt-out tool, where you have to specifically exclude things you don't want to be touched. As such, my current workaround is to set the configuration ""prettier.disableLanguages": ["markdown"].

I also want to note that there may be a relationship between this issue and https://github.com/prettier/prettier-vscode/issues/402, where the author is looking for more support for automatic Prettier formatting on Markdown documents. Any solution that is devised should be sufficient to handle both issues' concerns.

馃憤 to this idea. My reasoning: on several projects now I have had to duplicate config between the Prettier CLI (globs) and the VSCode workspace settings for the Prettier extension (formatOnSave for specific languages).

Ideally, I could define an include glob once (like you can for excludes), and have both my IDE and the CLI honour that.

You can also do this in workspace settings.json:

{
    "editor.formatOnPaste": true,
    "editor.formatOnSave": true,
    "editor.formatOnType": true,
    "[markdown]": {
        "editor.formatOnPaste": false,
        "editor.formatOnSave": false,
        "editor.formatOnType": false
    }
}

This issue appears to be resolved as such I am closing it. If you are still having problems please feel free to reopen.

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings