[英]Format HTML in VSCode the same way as Visual Studio
I'm searching a way to configure VSCode to format HTML (and markup files in general, such as .vue) in the same way Visual Studio is doing. 我正在寻找一种以与Visual Studio相同的方式配置VSCode来格式化HTML格式(以及一般的标记文件,例如.vue)的方法。
In Visual Studio, for instance, if I write something like: 例如,在Visual Studio中,如果我编写如下内容:
<div id="testid" class="test" style="display: block;">
<p class="p1"></p>
</div>
I can decide where to break the attribute's line, so if I insert a newline after the class I'll have: 我可以决定在哪里中断属性的行,因此,如果在类之后插入换行符,则将具有:
<div id="testid" class="test"
style="display: block;">
<p class="p1"></p>
</div>
Or I can break the line after the id: 或者,我可以在ID之后换行:
<div id="testid"
class="test" style="display: block;">
<p class="p1"></p>
</div>
Etc... 等等...
The point is that Visual Studio is aware of the newline I insert, so if I strike CTRL+K, CTRL+D it will actually format my document, but will maintain the newline as I choose, and the attributes at the newline are just aligning to the first attribute in the line of the tag. 关键是Visual Studio知道我插入的换行符,因此,如果我按CTRL + K,CTRL + D,它将实际上格式化我的文档,但是会保持我选择的换行符,并且换行符的属性只是对齐标记行中的第一个属性。
That's something I cannot emulate in VSCode, I tried the builtin formatter, Prettier and Beautify... 这是我无法在VSCode中模拟的东西,我尝试了内置格式化程序,Prettier和Beautify ...
Is there actually a way to do it? 实际上有办法吗?
EDIT 编辑
As @Mr_b194 suggested I tried HTML Format extension. 正如@ Mr_b194所建议的那样,我尝试了HTML格式扩展。 Compared to Beautify this one is actually preserving the newlines between attributes, but the first attribute in the new line is just indented (4 chars in my case), resulting not aligned with the upper line:
与Beautify相比,这实际上是保留属性之间的换行符,但是新行中的第一个属性只是缩进(在我的情况下为4个字符),结果与上一行不对齐:
<div id="testid"
class="test" style="display: block;">
<p class="p1"></p>
</div>
Visual Studio is aligning the 'class' attribute with the 'id' of the line before. Visual Studio之前将'class'属性与该行的'id'对齐。
Go to the extension in Vs code and install html format. 转到Vs代码中的扩展名并安装html格式。 Press the reload button, this would help to format all your html code
按下重新加载按钮,这将有助于格式化所有HTML代码
Ok, I found out a way! 好的,我找到了方法!
I was using the version 1.4.7 of the Beautify extension and there was a schema misalignment for the intellisense of .jsbeautifyrc file that was not showing you all the options for the property wrap_attributes
. 我使用的是Beautify扩展程序的 1.4.7版本,.jsbeautifyrc文件的智能感知存在模式未对齐的问题,并未向您显示属性
wrap_attributes
所有选项。
I opened an issue on github and the guys where super fast, they've already released the 1.4.8 that correctly shows the preserve-aligned
value I was searching. 我在github上打开了一个问题,这些家伙超级快,他们已经发布了1.4.8,可以正确显示我正在搜索的
preserve-aligned
值。
So, to make it short, you can configure the .jsbeautifyrc file for your project with: 因此,简而言之,您可以使用以下代码为您的项目配置.jsbeautifyrc文件:
{
"preserve_newlines": true,
"wrap_attributes": "preserve-aligned"
}
And is gonna work. 并且会工作。
As I was searching also for .vue support, you can configure Vetur with: 当我也在搜索.vue支持时,可以使用以下命令配置Vetur:
"vetur.format.defaultFormatterOptions": {
"js-beautify-html": {
"preserve_newlines": true,
"wrap_attributes": "preserve-aligned"
}
}
And is going to work the same way. 并且将以相同的方式工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.