prettier / prettier-atom

An atom package for the prettier formatter.
MIT License
754 stars 95 forks source link

Editor filetype should be considered before running prettier #500

Open strindhaug opened 5 years ago

strindhaug commented 5 years ago

Prettier does not understand django templates and treats it as just html and dereby destroying the temlate syntax into something invalid. This is a prettier issue and not prettier-atom issue ofcourse: https://github.com/prettier/prettier/issues/5581

But prettier-atom should be able to tell from the current filetype the editor is using, that even though the file extension is .html, if the active filetype is something different than text.html.basic, it should not feed it through prettier and possibly destroy all the template tags.

Even though I've changed the filetype to text.html.django prettier-atom still uses prettier to destroy the template code.

The only way to edit django templates is to exclude *.html completely from being formatted as you can see from my prettier debug output below. But that completely disables formatting of html files even though I might want it for non-django html files.

Prettier: Debug output:

``` Atom version: 1.37.0 prettier-atom version: 0.56.5 prettier: bundled prettier version: 1.15.2 prettier-eslint version: 8.8.2 prettier-atom configuration: { "formatOnSaveOptions": { "enabled": true, "excludedGlobs": [ "*.html" ], "respectEslintignore": true, "showInStatusBar": false, "whitelistedGlobs": [], "isDisabledIfNotInPackageJson": false, "isDisabledIfNoConfigFile": false, "ignoreNodeModules": true }, "useEslint": true, "useStylelint": false, "prettierEslintOptions": { "prettierLast": false } } ```
robwise commented 5 years ago

We used to have the parser be inferred by prettier-atom based on the syntax scope exactly as you say. As the number of supported parsers grew, it became harder to maintain as we constantly had to keep updating the mappings of Atom-scope to Prettier-parser.

Then Prettier came out with their "plugins" feature which made it impossible for us to know which parsers your version of Prettier even supported. At this time, Prettier came out with the getFileInfo API which takes a file path as an argument and returns the inferred prettier options (including the parser).

So we switched the entire prettier-atom implementation to rely on just delegating all of the work of inferring the parser and the other options to Prettier itself.

Therefore, I think at this point we probably need to stick to that approach or else we go back to the old problem of having to manually keep the parsers up to date and dropping support for any plugins.

In your specific case, I would use my .prettierrc file to set overrides for those specific HTML files (include the folder they reside in in the glob to only hit those specific ones) and force the parser to be python (or whatever the parser you want to use is).

{
  "overrides": [
    {
      "files": ["my_django_web_app/**/*.html"],
      "options": {
        "parser": "python"
      }
    }
  ]
}
amk221 commented 4 years ago

+1 for continuing to delegate to prettier.

However, with my config:

module.exports = {
  singleQuote: true,
  overrides: [{
    files: '*.hbs',
    options: {
      singleQuote: false,
      parser: 'glimmer'
    }
  }]
};

Format on Save doesn't do anything with .hbs files.

(FWIW cmd+p > prettier format does work)