简体   繁体   English

Android Studio 自动格式化注释

[英]Android studio auto formatting annotations

How can I set auto formatting like this:如何像这样设置自动格式:

    @Mock MyInterface myInterface;

    //  instead

    @Mock
    MyInterface myInterface;

simple, go to... ( Jetbrains IDE's like a Webstorm, Android studio, pycharm, etc )简单,去...( Jetbrains IDE 就像 Webstorm、Android studio、pycharm 等

File -> Settings -> Code Style -> Java -> Wrapping and Braces -> Method annotations and set to Do not wrap and also Field Annotations . File -> Settings -> Code Style -> Java -> Wrapping and Braces -> Method annotations 并设置为Do not wrap and Field Annotations 。

Note: Changing the setting will not actually reformat all existing ones.注意:更改设置实际上不会重新格式化所有现有设置。 It just won't be wrapped when inserted automatically by the IDE, as you want in your question.正如您在问题中所希望的那样,当 IDE 自动插入时,它不会被包装。 This applies to all annotations for methods.这适用于方法的所有注释。

Just press enter.只需按回车键。 It is as easy as that.就是这么简单。 Write the annotation and press enter.编写注释并按回车键。

For new fields writing everything on the same line automatically drops it down (so it goes over two lines).对于在同一行上写下所有内容的新字段,它会自动将其放下(因此它会超过两行)。 (I'm using the default settings on that so if you have changed anything that isn't what will happen for you). (我正在使用默认设置,因此如果您更改了任何不会发生在您身上的事情)。

That assumes the annotation is first (lombok annotations used for the example. Use a different annotation or get lombok to test this case):假设注释是第一个(用于示例的 lombok 注释。使用不同的注释或获取 lombok 来测试这种情况):

@Data
public class SomeClass

and not:并不是:

public @Data class SomeClass 

Writing:写作:

 @Data public class testClass{

and pressing enter after adding the clamp automatically formatted it.添加夹子后按回车键自动格式化它。 If it doesn't just get the cursor after the annotation and press enter.如果它不只是在注释后获取光标,然后按回车键。 Force the formatting if it for whatever reason doesn't cooperate.如果出于某种原因不合作,则强制格式化。

you can't use the auto-indent shortcut (for me CTRL+I) doesn't add a new line for the annotation and declaration because both are valid ways to write it and both are "prefered".您不能使用自动缩进快捷方式(对我来说 CTRL+I)不会为注释和声明添加新行,因为两者都是有效的编写方式,并且都是“首选”。 Writing it on the same line is just the same as writing it on its own line.写在同一行和写在自己的行上是一样的。 Auto-indent doesn't correct it自动缩进不会纠正它

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

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