简体   繁体   English

更漂亮的javascript代码缩进配置

[英]Prettier config for javascript code indenting

I am using Prettier in Visual Studio Code for formatting. 我在Visual Studio Code中使用Prettier进行格式化。

Normally, it works great in my JS/TS files. 通常,它在我的JS / TS文件中效果很好。 But it insists on wrapping code like this onto single lines: 但是它坚持将这样的代码包装到单行中:

trigger('myInsertRemoveTrigger', [
  transition(':enter', [
    style({ opacity: 0 }),
    animate('5s', style({ opacity: 1 })),
  ]),
  transition(':leave', [
    animate('5s', style({ opacity: 0 }))
  ])
]),

becomes like 变得像

    trigger('fadeInOut', [
      transition(':enter', [style({ opacity: 0 }), animate('.5s', style({ opacity: 1 }))]),
      transition(':leave', [animate('.5s', style({ opacity: 0 }))])
    ])

Which I find harder to read. 我觉得很难读。 I've looked at the available options and don't see anything related to this. 我已经查看了可用的选项 ,但没有看到与此相关的任何内容。 Can I configure this somehow? 我可以以某种方式配置它吗?

Currently, my .prettierrc is 目前,我的.prettierrc

{
  "printWidth": 120,
  "singleQuote": true,
  "useTabs": false,
  "tabWidth": 2,
  "semi": true,
  "bracketSpacing": true
 }

prettier是经过优化的,因此您无法配置它重新格式化代码的方式:您只需要接受prettier制作的缩进即可:)

Just add a comment after the first element of the array. 只需在数组的第一个元素之后添加注释即可。

var a = [
  1, //
  2,
  3,
];

I'm afraid the only thing you can do is reduce printWidth . 恐怕唯一可以做的就是减少printWidth
But that will obviously affect the rest of your code as well. 但这显然也会影响其余的代码。

From what you are describing, it sounds like you are talking exactly about 'print width'. 从您所描述的内容来看,听起来您好像在谈论“打印宽度”。 Try decreasing the 'print width' to 80 or less. 尝试将“打印宽度”减小到80或更小。 Maybe 50 depending on what your preference is. 可能是50,具体取决于您的喜好。

{
  "printWidth": 80,
  "singleQuote": true,
  "useTabs": false,
  "tabWidth": 2,
  "semi": true,
  "bracketSpacing": true
 }

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

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