[英]How do you stop Prettier in VS code splitting attributes onto multiple lines?
I am using prettier to format my code in VS Code but I really dislike how it does it.我在 VS Code 中使用 prettier 来格式化我的代码,但我真的不喜欢它的方式。
My main beef is it splitting attributes over multiple lines.我的主要观点是将属性拆分为多行。
<input
type="checkbox"
name="asiaNews"
id="asiaNews"
value="asiaNews"
/>
I'd much prefer it to look like this我更喜欢它看起来像这样
<input type="checkbox" name="asiaNews" id="asiaNews" value="asiaNews" />
I can't find anything in the docs or on SO我在文档或 SO 上找不到任何内容
How to prevent VS Code from breaking up long HTML lines into multiple lines? 如何防止 VS Code 将长 HTML 行分成多行?
Is there a way to do it or a different tool that I can use so I can have my own custom formatting rules that suits my sensibilities?有没有办法做到这一点,或者我可以使用不同的工具,这样我就可以拥有适合自己的自定义格式规则?
A quick fix is to go to Prettier Extension Settings (ctrl + shift + X) and in Prettier Extension Settings search for "Print Width" set it to 250 or anything that works for you.快速解决方法是转到更漂亮的扩展设置(ctrl + shift + X)并在更漂亮的扩展设置中搜索“打印宽度”将其设置为 250 或任何适合您的值。
1: Go to Extention Settings: 1:进入扩展设置:
2: Change the value of Print Width to your liking. 2:根据自己的喜好更改打印宽度的值。
To disable format code on save.在保存时禁用格式代码。 Turn off the "Format On Save" and use
Alt+Shift+F
to format the code when ever you want.关闭“保存时格式化”并在需要时使用
Alt+Shift+F
来格式化代码。
After Trying A lot, I came up with below solution.在尝试了很多之后,我想出了以下解决方案。
Here is my settings.json looks like after above changes.这是我的 settings.json 经过上述更改后的样子。
{
"editor.formatOnSave": true,
"html.format.wrapLineLength": 100,
"prettier.disableLanguages": ["html"]
}
This will try to wrap the HTML attributes only if the length is greater than 100. The good thing is that even if the length exceeds 100, it won't wrap each attribute to new line.这将仅在长度大于 100 时尝试包装 HTML 属性。好处是即使长度超过 100,它也不会将每个属性包装到新行。
将此行添加到设置 JSON 以增加最大线宽,
"prettier.printWidth": 160,
add new rule in .prettierrc.json
that located in your project:在位于项目中的
.prettierrc.json
中添加新规则:
{
printWidth": 160,
}
{"printWidth": 100}
在prettierrc.json
是不够好。
my solution was to uninstall prettier !!我的解决方案是卸载 prettier !! and install ESLint & typeScript extension.
并安装 ESLint 和 typeScript 扩展。 it comes with a basic formatter which does exactly what I wanted.
它带有一个基本的格式化程序,它完全符合我的要求。 you can disable and enable them by search for formatter in settings
您可以通过在设置中搜索格式化程序来禁用和启用它们
This happens because prettier assume that you want your code width to be in 80 characters only because of their default setting.发生这种情况是因为更漂亮地假设您希望代码宽度为 80 个字符,只是因为它们的默认设置。
So You should tell prettier to I have too much space.所以你应该告诉prettier 我有太多的空间。
To do this simply create .prettierrc.json file in the root folder and add为此,只需在根文件夹中创建.prettierrc.json文件并添加
{
"printWidth": 600
}
And Save your file.并保存您的文件。 This will clear your issue.
这将清除您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.