简体   繁体   English

如何防止 VS Code 将 HTML 行分成多行?

[英]How to prevent VS Code from breaking up long HTML lines into multiple lines?

When my elements with attributes get long, VS Code seems to break the line up into multiple lines:当我的带有属性的元素变长时,VS Code 似乎将行分成多行:

在此处输入图像描述

(I would like three lines here instead of seven, one line per element) (这里我想要三行而不是七行,每个元素一行)

I am using prettier for formatting, and have set the printWidth option which works in javascript code, but for HTML it seems to be overridden by VS Code.我正在使用 prettier 进行格式化,并设置了在 javascript 代码中工作的printWidth选项,但对于 HTML,它似乎被 VS Code 覆盖了。

I´ve tried fiddling around with the wrapAttributes and the html.format.wrapLineLength settings, but none of those seem to have any effect.我试过摆弄wrapAttributeshtml.format.wrapLineLength设置,但这些似乎都没有任何效果。

How to deal with this matter?如何处理这件事?

UPDATE :更新

Thanks alot for your answers.非常感谢你的回答。 I havent been notified by them, so sorry for not taking action.我还没有收到他们的通知,很抱歉没有采取行动。

I´ve tried all of your suggestions, but the problem remains the same.我已经尝试了您的所有建议,但问题仍然存在。 This is my current config based on your suggestions.这是根据您的建议我当前的配置。

settings.json:设置.json:

"html.format.wrapLineLength": 0,
"html.format.enable": false,
"html.format.wrapAttributes": "auto",  
"vetur.format.defaultFormatterOptions": {
    "prettyhtml": {
        "printWidth": 300,
        "singleQuote": false,
        "wrapAttributes": false,
        "sortAttributes": false
    }
}

.prettierrc.json: .prettierrc.json:

{
  "semi": true,
  "trailingComma": "none",
  "singleQuote": true,
  "printWidth": 300,
    "tabWidth": 2,
    "useTabs": true,
    "jsxBracketSameLine": true
}

您可以在 settings.json 中添加"html.format.wrapLineLength": 0

This worked for me...这对我有用...

In your "Settings.json" file add the line在您的“Settings.json”文件中添加行

"prettier.printWidth": 300

I'm not sure if it's just the built-in HTML formatting settings but you can give this a try.我不确定这是否只是内置的 HTML 格式设置,但您可以尝试一下。

If you don't want to enable wrapping for HTML:如果您不想为 HTML 启用包装:

"html.format.wrapAttributes": "auto",  # wrap only when line length is exceeded
"html.format.wrapLineLength": 0,       # disable max chars per line

If you have other HTML formatter extensions, you can simply disable the built-in:如果您有其他 HTML 格式化程序扩展,您可以简单地禁用内置:

"html.format.enable": false,

If your HTML is still breaking-up with the *.wrap disabled or with html.format.enable false, then it's not the built-in VS code settings that's causing it.如果您的 HTML 在*.wrap禁用或html.format.enable false 的情况下仍然分解,则不是内置 VS 代码设置导致它。

Settings -> Extensions -> HTML:
Format: Wrap Line Length
Maximum amount of characters per line (0 = disable).
0

That worked for me.那对我有用。

Here is what I did.这是我所做的。 I created .prettierrc.json file inside project folder.我在项目文件夹中创建了.prettierrc.json文件。 And then added below settings to it.然后将以下设置添加到它。

{
  "html.format.wrapAttributes": "auto",
  "html.format.wrapLineLength": 0,
  "printWidth": 1000
}

The printWidth setting is important here. printWidth设置在这里很重要。 Increase/Decrease it as per your preferences as you can not disable it completely.根据您的喜好增加/减少它,因为您无法完全禁用它。

check your extension because i think VS code editor default don't have a auto break function.检查您的扩展,因为我认为 VS 代码编辑器默认没有自动中断功能。 maybe you install a extension that have a auto break line.也许你安装了一个有自动断线的扩展。 hope this help希望这有帮助

This problem was driving me crazy because I hate when my code wraps I feel like it is so much harder to read.这个问题让我发疯,因为我讨厌我的代码换行,我觉得它很难阅读。 I read a ton of stuff and couldn't get my code to stop wrapping.我读了很多东西,但无法让我的代码停止换行。 So if you tried everything already mentioned and your code still wraps then you have an extension that is causing it to wrap.因此,如果您尝试了已经提到的所有内容并且您的代码仍然包装,那么您有一个导致它包装的扩展。 So go to File>Preferences>Settings所以去文件>首选项>设置在此处输入图片说明

Then click on your extensions and go through them and find which one is causing them to wrap.然后单击您的扩展程序并浏览它们并找出导致它们换行的扩展程序。 In my case I had Vetur(a Vue extension) using Prettier which was causing my code to wrap.在我的情况下,我使用 Prettier 使用了 Vetur(一个 Vue 扩展),这导致我的代码包装。

What you could also do and what I do now is install Prettier and set the Print Width on that to the width of your screen so anything longer will break but for the most part it will keep stuff on the same line for you.您还可以做的和我现在要做的是安装 Prettier 并将其上的 Print Width 设置为屏幕的宽度,因此任何更长的东西都会中断,但在大多数情况下,它会将内容保留在同一行上。

您应该检查 Visual Studio 代码的扩展并禁用“JS-CSS-HTML Formatter”或任何其他麻烦的格式化程序

这一行可以改变它对我"vetur.format.defaultFormatter.html": "none", .. "vetur.format.defaultFormatter.html": "none",

My situation differed slightly in terms of what was being split onto new lines.在拆分到新线路上的内容方面,我的情况略有不同。 When formatting documents with large chunks of HTML it would wrap long paragraphs of text up onto multiple lines and automatically add space/tab indentation.当使用大块 HTML 格式化文档时,它会将长文本段落包装成多行并自动添加空格/制表符缩进。

I added the following into my settings.json:我将以下内容添加到我的 settings.json 中:

"html.format.unformattedContentDelimiter": "p, li",

This kept all text within <p> and <li> tags on one line without introducing whitespace characters.这将<p><li>标签内的所有文本保持在一行上,而不会引入空白字符。 You can then use VSCode native text wrapping with alt + z to visually wrap the text without introducing whitespace characters.然后,您可以使用 VSCode 原生文本换行和alt + z直观地换行文本,而不会引入空白字符。

In my situation I was using SVG in my html.在我的情况下,我在我的 html 中使用了 SVG。 And whenever I used Ctrl + Shift + I to format my HTML document, it would split the SVGs into multiple lines like this:每当我使用Ctrl + Shift + I来格式化我的 HTML 文档时,它都会将 SVG 分成多行,如下所示:

<svg id="logo-1" width="132" height="35" viewBox="0 0 132 35" fill="none" xmlns="http://www.w3.org/2000/svg">
    <path d="M38.51 11.26H41.72V24.26H38.51V11.26ZM43 19.37C43 16.15 45 14.22 48 14.22C51 14.22 53"></path>
    <path d="M32.16 12.11C32.1601 10.1197 31.5591 8.17576 30.4357 6.53282C29.3123 4.88988 27.7188 "></path>
    <path d="M82.16 12.19C32.1601 10.1197 31.5591 8.17576 30.4357 6.53282C29.3123 4.88988 27.7188 "></path>
</svg>

Whereas I wanted them to be in single line and still format my documents.而我希望它们在单行中并且仍然格式化我的文档。

My solution:我的解决方案:

  1. Select the SVG you want to convert to single line.选择要转换为单行的 SVG。 Then press F1 and type Join Lines and hit enter.然后按F1并键入Join Lines并按 Enter。

  2. After converting all SVGs into one line, Press F1 .将所有 SVG 转换为一行后,按F1 Type Open Settings (JSON) and open the settings file and add the following entry at the end:键入Open Settings (JSON)并打开设置文件并在末尾添加以下条目:

    "html.format.contentUnformatted": "svg"

Now formatting your document should not split your SVGs into multiple lines.现在格式化您的文档不应该将您的 SVG 分成多行。

PS: of course, it can be any other html tag. PS:当然可以是任何其他的html标签。

If you're using VSCode, put this in your .code-workspace file:如果您使用 VSCode,请将其放入您的 .code-workspace 文件中:

"settings": {
    "editor.defaultFormatter":"octref.vetur",
    "vetur.format.defaultFormatter.html":"prettyhtml",
    "vetur.format.defaultFormatterOptions": {
        "prettyhtml": {
            "printWidth": 500,
        }
    },
}

After a long try, I made it work while working with Vue.js , this is my config.json经过长时间的尝试,我在使用Vue.js时让它工作,这是我的配置。json

{
  "html.format.wrapAttributes": "auto",
  "html.format.wrapLineLength": 0,  
  "prettier.printWidth": 300,
  "[vue]": {
    "editor.defaultFormatter": "Vue.volar"
  },  
}

Do not move anything else, lol.不要移动其他任何东西,大声笑。 Good luck!祝你好运!

I'm a fan of prettier I wouldn't prefer another formatter over prettier,我是 prettier 的粉丝我不喜欢其他格式化程序而不是 prettier,

so the most neat solution I've found is in vscode settings.json所以我找到的最简洁的解决方案是在 vscode settings.json

{
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "html.format.wrapLineLength": 0,
  "html.format.wrapAttributes": "auto",
  "[html]": {
    "editor.defaultFormatter": "vscode.html-language-features"
  }
}

this disables prettier only for html files and uses default vscode html formatter which is in html quite similar to prettier, and using prettier anywhere else, BTW in this case the formatonsave feature is enabled and works for html files as well that's why I like it这仅对 html 文件禁用 prettier,并使用默认的 vscode html 格式化程序,它在 html 中与 prettier 非常相似,并且在其他任何地方使用 prettier,顺便说一句,在这种情况下,启用了 formatonsave 功能并且适用于 html 文件,这就是我喜欢它的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM