简体   繁体   English

VSCode 将多行代码自动格式化为一行代码

[英]VSCode is auto-formatting multiline to one line code

I like auto-formatting code idea very much.我非常喜欢自动格式化代码的想法。 You can use eslint rules to configure code style you like.你可以使用 eslint 规则来配置你喜欢的代码风格。 But some hidden rule makes what I don't like: inlines document destruction props or jsx template element attributes.但是一些隐藏的规则使我不喜欢:内联文档销毁道具或 jsx 模板元素属性。

Example (this is how I want it to be).示例(这就是我想要的样子)。

const {
  foo: 1,
  bar: 2,
  baz: 3,
} = require('qux')

And here is how it is reformatted by VSCode or some its plugin (not sure)这是 VSCode 或它的一些插件重新格式化的方式(不确定)

const { foo: 1, bar: 2, baz: 3 } = require('qux')

It is absolutely OK in case it is reformatted back to multiline when the line lenth exceeds some specified limit.当行长度超过某个指定的限制时,它被重新格式化回多行是绝对可以的。

JSX examples (tried with a new project generated with use of react-create-app ). JSX 示例(尝试使用react-create-app生成的新项目)。

  1. OK, it would be too long line, thus VSCode re-formats it to multiline好吧,这行太长了,因此 VSCode 将其重新格式化为多行

    <a className="App-link" href="https://reactjs.org" target="_blank" rel="noopener noreferrer" >
  2. Line length wouldn't be longer than allowed, but I still love this style线条长度不会超过允许的长度,但我仍然喜欢这种风格

    <a className="App-link" href="https://reactjs.org" >
  3. But VSCode/plugin has a different preference and reformats it to但是 VSCode/plugin 有不同的偏好并将其重新格式化为

    <a className="App-link" href="https://reactjs.org">

I tried to find a specific eslint rule which would be responsible for this behavior, but with no luck (not found).我试图找到一个特定的 eslint 规则来负责这种行为,但没有运气(未找到)。 I tried to change default vscode formatter from prettier to null - no luck.我试图将默认的 vscode 格式化程序从prettier更改为null - 不走运。

One more strange thing for me: VSCode doesn't highlights such code snippets which it is going to reformat.对我来说还有一件奇怪的事情:VSCode 没有突出显示将要重新格式化的代码片段。

So how can it be fixed?那么如何修复呢?

UPDATE更新

The only thing I want VSCode to do within such code parts, is just breaking one line to multiline in case the line exceeds eslint max-len value.我希望 VSCode 在这些代码部分中做的唯一一件事就是将一行中断到多行,以防该行超过eslint max-len值。

If you have prettier configured.如果你有prettier配置。 Did you try the prettier.printWidth property here您是否尝试过这里prettier.printWidth属性

{
  "prettier.printWidth": 80
}

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

相关问题 在Eclipse中自动格式化JavaScript代码 - Auto-formatting of JavaScript code in Eclipse jQuery 选择自动格式化 - jQuery Chosen with Auto-Formatting 有没有办法防止VSCode自动格式化在问号和单词之间添加空格 - Is there a way to prevent VSCode auto-formatting from adding a space between question marks and words 禁用 TinyMCE 自动格式化内容行为 - disabling TinyMCE auto-formatting content behavior 将输入字段自动格式化为用户类型 - Auto-formatting input fields as user types 在Aptana Studio 3中将Java数组自动格式化为新行 - Auto-formatting Javascript arrays into new lines in Aptana Studio 3 在RTF编辑器中覆盖IE电子邮件自动格式化 - Override IE email auto-formatting in rich-text editor WebStorm自动格式化为SQL模板文字添加了领先的空格 - WebStorm auto-formatting adds leading whitespace to SQL template literals 防止Atom Beautify自动格式化es6导入/对象解构(反应) - Prevent Atom Beautify from auto-formatting es6 import/object de-structuring (React) 如何使一行咖啡脚本代码变为多行? - How to make one line coffee script code to multiline?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM