New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow overriding options based on a language #5378
Comments
I think this could be actually useful as Prettier is getting support for more and more language, and even files that are parsed by multiple parsers. There is an issue to rename parser to language, so maybe this could use the same enum as language to override options for that language. |
I'm not sure if this is possible but I guess we just need to add a new |
I think this would require some work as it needs a new way to pass options to the parser. But I don't think this should be about printWidth only, but also all the other options available. What is being requested at the end is the ability to set options to a certain language, instead of doing it file based. |
Right, the title doesn't describe the issue to its full extent really. The |
Do you mind to update the issue and the title to request a new way to override the options based on a language instead of a file? I think this is what you want to do, isn't it? |
@michaeljota I updated the title and the issue description to reflect this better. |
I would prefer something like this instead: {
"singleQuote": true,
"printWidth": 100,
"overrides": [
{
"languages": ["angular", "html"],
"options": {
"printWidth": 80
}
}
]
} A property What do you think? |
I open another issue to request to ignore formatting based on languages. #5408 |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I'd like to throw my hat into the ring for this one, as it would let you mix styles across languages a lot easier. For example, I've got If I could just say "when formatting using the css parser, use double quotes" I wouldn't have to worry about it ever again (which is the point of prettier). I'm happy to take a whack at implementing this, if the team are interested in exploring supporting it, and could provide a pointer on where to look to do the actual implementation :) |
@lydell ooh that looks exciting. Would the team be against me making a PR anyway in the meantime, if I found some free time? |
Related issues merged into this one:
|
So, we wanted to have the following markdown:
But comments are not supported by JSON, only by JSON5. So, we switched to JSON5. So, I wanted to override JSON5 file format formatting to leave quotes and use double quotes inside of the markdown. Unfortunately, this is not possible, so we removed comments and replaced json5 with json. Thought I'd share our use-case. Thanks! |
@Maxim-Mazurok FWIW Prettier does support comments in JSON. playground |
Yeah, I know. I just respect the way GitHub handles it. If it's |
Consider marking the code block as |
Since the goal is to show part of packages.json file, using anything other than JSON wouldn't be correct. But JSONC is a nice hack, I'll take a note about it, thanks! |
Hi, is there any progress on this one? Linting tools like ESLint can lint code blocks in markdown for example, and now we are getting errors there because Prettier can't rewrite them based on our overrides. |
Any progress on this? Thanks! My use case is using A small example if it helps: import {html, css, LitElement} from 'lit';
export class SimpleGreeting extends LitElement {
static styles = css`
p:after {
content: " 🌎";
}
`;
render() {
return html`<p>Hello, world!</p>`;
}
}
customElements.define('simple-greeting', SimpleGreeting); |
Another one to throw into the mix - we want to be able to have Prettier treat our codeblocks as their formatted langauge in markdown. For example, I'd like to be able to have Prettier set a tabWidth of 2 for all content, except for PHP codeblocks where we use a tabWidth of 4. Any code examples in separate files and included into the markdown are fine, because we can set a file-name based override, but any inline code block is formatted with the default tabWidth instead of the php-specific variant, despite the langauge being set in the code block. |
Same issue with the Liquid prettier plugin. I can confidently predict that partners will want Proposed solution:{
printWidth: 80,
singleQuote: true,
overrides: [{
parser: "liquid-html", // as opposed to "files" globs (or in tandem with?)
options: {
printWidth: 120,
singleQuote: false,
}
}]
} That is, accept a new property in
|
In the |
Heya! Here from #15289 - here's a really common scenario where this is needed. Any Markdown documentation that involves multiple programming languages probably needs this feature. At Solana Foundation we often, for example, product documentation with both JS and Rust examples:
|
Currently it's only possible to override Prettier options based on a glob pattern, for example:
However, there are cases where multiple languages "live" in the same file, namely
.vue
files and Angular components (found in.ts
files).For brevity, I'll focus on Angular components, but the same holds true for Vue, as mentioned above.
For the above file, there can only be one
printWidth
setting, even though it actually encapsulates (up to) 3 different types of files: typescript, css and html (with theangular
parser). Similarly to how.vue
files are constructed.It sometimes makes sense to have different
printWidth
s for different file type (for example, it would make sense to have HTML files shorter in width to force each attribute to its own line, in most cases - though this is another issue on its own, that Prettier doesn't currently address in HTML/Angular/Vue templates/files). This can be done today, based on a glob pattern:I suggest adding another option to the "overrides", similar to the
files
one, to allow specifying the "type" (for lack of a better name), for which the options would apply. e.g.:The text was updated successfully, but these errors were encountered: