简体   繁体   English

配置更漂亮以在新行上推大括号而不清除空行

[英]configure prettier to push curly braces on new lines & not clear empty lines

I am using prettier with VSCode, How can I configure it to format my code like this :我在 VSCode 中使用更漂亮,我如何配置它来格式化我的代码,如下所示:

function test() 
{

   if() 
      {

         MYCODE GOES HERE;

      }

}

I want the { and } on new lines, and an empty line after { and before } .我想要新行上的{} ,以及{}之前的空行。

Currently, it moves the curly brackets to same lines if condition or function name, and also remove the empty lines after/before { and } .目前,如果条件或函数名称,它会将大括号移动到相同的行,并删除{}之后/之前的空行。

Prettier is considered an " opinionated " formatter, which means it doesn't let you choose things like that. Prettier 被认为是一种“固执己见”的格式化程序,这意味着它不允许您选择类似的东西。 If you want more control over the formatting, you can use a different formatter.如果您想更好地控制格式,可以使用不同的格式化程序。

The built-in VS code formatter allows you to do what you're looking for, just search the settings for " function new line " and similar options.内置的 VS 代码格式化程序允许您执行所需的操作,只需搜索“功能新行”和类似选项的设置即可。

There are of course many other formatting extensions available in the VS code marketplace as well.当然,VS 代码市场中还有许多其他格式扩展可用。 Whichever you choose, you will have to select it has your default formatter in your VS code settings.无论您选择哪种方式,您都必须选择它在您的 VS 代码设置中具有您的默认格式化程序。

There is now a parser called Glimmer that supports Handlebars.现在有一个名为Glimmer的解析器支持 Handlebars。 Use it like this:像这样使用它:

yarn prettier '**/*.hbs' --parser glimmer

In addition, check out this VSCode extension called Prettier for Handlebars .此外,请查看这个名为 Prettier for Handlebars 的VSCode 扩展。

As mentioned in this answer , VS Code's formatter itself works quite well, but if you want this to be part of workflow, then using ESLint might make things easier.正如在这个答案中提到的,VS Code 的格式化程序本身工作得很好,但是如果您希望这成为工作流程的一部分,那么使用 ESLint 可能会使事情变得更容易。 There's a rule called brace-style .有一个规则叫做brace-style

You can then run eslint ./path/to/your/file --fix to format your code, or eslint . --fix然后你可以运行eslint ./path/to/your/file --fix来格式化你的代码,或者eslint . --fix eslint . --fix to format code in the entire project directory. eslint . --fix格式化整个项目目录中的代码。

Disclaimer: I use ESLint for code formatting most of the time and it works for me.免责声明:我大部分时间使用 ESLint 进行代码格式化,它对我有用。 I actually use it to find & fix problems too so it's like killing two birds with one stone, but note that ESLint is more about finding problems in the code and fixing them, so using ESLint just for code formatting might not be the best idea.我实际上也用它来查找和修复问题,所以这就像用一块石头杀死两只鸟,但请注意,ESLint 更多的是在代码中查找问题并修复它们,因此仅将 ESLint 用于代码格式化可能不是最好的主意。

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

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