prettier / prettier-vscode

Visual Studio Code extension for Prettier
https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode
MIT License
5.08k stars 451 forks source link

Allow formatting unreleased languages #1747

Closed Alonski closed 3 years ago

Alonski commented 3 years ago

Is your feature request related to a problem? Please describe. The Handlebars language is currently unreleased in Prettier (https://github.com/prettier/prettier/blob/main/src/language-handlebars/index.js). Because of this Prettier-VSCode doesn't currently format hbs files. The Ember.js community created this extension for using Prettier to format Handlebars: https://github.com/mfeckie/handlebars-formatter This extension hardcodes the Prettier version it supports and doesn't use the installed version in the project. Personally I wanted to use a different version of Prettier, so I forked the extension and released my own: https://github.com/ember-tooling/prettier-for-handlebars-vscode/

The issue with this is that this still hardcodes the version. Yesterday I played around with the notion of adding support to Pretter-VSCode for Handlebars and the outcome works, without too many changes.

You can see the changes I made here: https://github.com/prettier/prettier-vscode/compare/main...ember-tooling:main

Describe the solution you'd like I would like Pretter-VSCode to add a new configuration which allows passing in a prettier.SupportLanguage object that will be added to the supportedLanguages for the extension.

Describe alternatives you've considered Another option is to add a configuration for allowing getSupportLanguage to also return unreleased languages. The issue with this is that from what I understand this would require a change to Prettier itself as the exported getSupportInfo function from Prettier doesn't accept arguments. https://github.com/prettier/prettier/blob/main/src/index.js#L62

Finally, if none of these are approved I can go ahead and release a new version of my extension Prettier For Handlebars VSCode with the new code from https://github.com/ember-tooling/prettier-vscode but this seems like a waste of maintaining effort and having support built into the official extension will allow much easier access to users.

Additional context Open to chat about this :) Thanks!

dcyriller commented 3 years ago

Another option is to add a configuration for allowing getSupportLanguage to also return unreleased languages

If you you'd like to add Handlebars to the list of the supported languages, I wonder if you could not change https://github.com/prettier/prettier-vscode/blob/d4505c8cb3aa51ad255c7e19db752bc2afc6bd23/src/LanguageResolver.ts#L72 to

    return prettierInstance.getSupportInfo({ showUnreleased: true }).languages;

(getSupportInfo source)

Alonski commented 3 years ago

@dcyriller That is what I wrote in alternatives :) The exported function doesn't accept arguments: https://github.com/prettier/prettier/blob/4d58724016186d0a3eff235db5459d1e966b1d3f/src/index.js#L62

stephancasas commented 3 years ago

On this same note, it would be nice to pass in an array of file extensions that Prettier should parse. For example, I've setup my local prettier.json to treat Adonis Edge templates as HTML because they are effectively standard markup with mustache syntax:

"overrides": [
    {
        "files": "*.edge",
        "options": {
            "parser": "html"
        }
    }
]

However, despite including the extension in the overrides array, Prettier does not load support for those files.

As usual, you can set a files.associations object in VSCode's config, but then you lose the ability to make use of other extensions which are targeted for Edge templates.

For now, I've just modified the method within the installed extension, in-place:

getSupportedFileExtensions(e){
    const t=[];
    return (this.getSupportLanguages(e).forEach((e=> {
        e&&e.extensions&&t.push(...e.extensions)
    })),t.filter(((e,t,r)=>r.indexOf(e)===t))).concat('.edge');
}

Sure, it'll get over-written on an update, but it serves the purpose for now.

Alonski commented 3 years ago

Closing this. It is possible to do this today :) https://dev.to/jelhan/format-glimmer-templates-with-prettier-ipa

github-actions[bot] commented 3 years ago

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.