简体   繁体   English

VSCode Prettier 不再在 JS 中换行

[英]VSCode Prettier not wrapping lines anymore in JS

I was using basic JS and HTML for coding practice and have installed Prettier extension.我使用基本的 JS 和 HTML 进行编码练习,并安装了 Prettier 扩展。 I assigned Ctrl+Shift+L to use auto formatting.我指定了 Ctrl+Shift+L 以使用自动格式化。 I used this extension before and I can clearly remember that it was wrapping long code lines and object into multiple logical lines, for example:我以前用过这个扩展,我清楚地记得它把长代码行和 object 包装成多个逻辑行,例如:

const someObj = {name: 'MyName', surname: 'MySurname'};

To this:对此:

const someObj = {
  name: 'MyName',
  surname: 'MySurname'
};

Any suggestions what options should I play with to get this formatting?有什么建议我应该使用哪些选项来获得这种格式?

That is the default (one line) formatting, but if you press enter immediately after your opening curly brace it should be formatted like in your second snippet.这是默认的(单行)格式,但是如果您在打开花括号后立即按 Enter 键,则它应该像第二个片段一样格式化。

Per Prettier's documentation:根据 Prettier 的文档:

By default, Prettier's printing algorithm prints expressions on a single line if they fit.默认情况下,Prettier 的打印算法在适合的情况下将表达式打印在一行上。 Objects are used for a lot of different things in JavaScript, though, and sometimes it really helps readability if they stay multiline.不过,在 JavaScript 中,对象被用于许多不同的事情,有时如果它们保持多行,它确实有助于提高可读性。 See object lists, nested configs, stylesheets and keyed methods, for example.例如,参见 object 列表、嵌套配置、样式表和键控方法。 We haven't been able to find a good rule for all those cases, so Prettier instead keeps objects multiline if there's a newline between the { and the first key in the original source code.我们无法为所有这些情况找到一个好的规则,所以如果在原始源代码中 { 和第一个键之间有换行符,Prettier 会保持对象多行。 A consequence of this is that long singleline objects are automatically expanded, but short multiline objects are never collapsed.这样做的结果是长的单行对象会自动展开,但短的多行对象永远不会折叠。

You can find some workarounds in their docs here: https://prettier.io/docs/en/rationale.html#multi-line-objects您可以在他们的文档中找到一些解决方法: https://prettier.io/docs/en/rationale.html#multi-line-objects

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

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