简体   繁体   English

如何在 VS 代码中阻止 Prettier 将属性拆分为多行?

[英]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来格式化代码。

禁用或启用格式保存

You can visually check the setting here您可以在此处目视检查设置

After Trying A lot, I came up with below solution.在尝试了很多之后,我想出了以下解决方案。

  1. Disable prettier for html files and use the default formatter provided by VS code.禁用 prettier for html 文件并使用 VS code 提供的默认格式化程序。
  2. Set the line length as per the requirements.根据要求设置线长。 (I set it to 100) (我设置为100)

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.

相关问题 在 VS 代码中更漂亮,将属性拆分为多行(打印宽度不适用于 html)Angular - Prettier in VS code splitting attributes onto multiple lines ( print width doesn't work for html ) Angular 如何在CSS下拉菜单中阻止链接包装到多行? - How do I stop links from wrapping onto multiple lines in a CSS drop down menu? 如何停止更漂亮以自动添加新行? - How to stop prettier to keep adding new lines automatically? 如何阻止 prettier 格式化 HTML 文件? - How do I stop prettier from formatting HTML files? 如何在 vs 代码中启用更漂亮的自动关闭标签? - How to enable auto close tag with prettier in vs code? 如何在VS Code / Prettier中为不同的文件指定不同的格式 - How to specify different formatting for different files in VS code/Prettier VS Code - 如何添加/删除/编辑多个标签的属性 - VS Code - How to add/remove/edit multiple tags' attributes <label><span>当<span>有时会进行多</span>行时,</span></label>如何将a <label>与a</label>对齐<label><span><span>?</span></span></label> - How do I align a <label> with a <span> when the <span> sometimes goes onto multiple lines? 将单个长单词分割成多行而不会破坏链接或html标签 - splitting long single words onto multiple lines without breaking links or html tags 如何让 lorem ipsum 在 VS Code 中实例化多行而不是 1? - How to get lorem ipsum to instantiate on multiple lines instead of 1 in VS Code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM