简体   繁体   English

如何在IntelliJ上配置代码格式化程序

[英]how to configure code formatter on intellij

I have to write code at another style. 我必须用另一种风格编写代码。 I wondering to how to configure intellij for this purpose? 我想知道如何为此目的配置intellij?

It is before code looking: 在查找代码之前:

@Override
public void generateTestObjectsInDb(Object... objects) {
    try {
        for (Object obj : objects) {
            DataBaseObjectService.createObjectInDB(obj);
        }
    } catch (Exception e) {
        Logger.fatal("Error occured while generating data in product Database.", e);
    }
}

And here is how it should looks: 这是它的外观:

@Override
public void generateTestObjectsInDb(Object... objects)
{
    try
    {
        for (Object obj : objects)
        {
            DataBaseObjectService.createObjectInDB(obj);
        }
    }
    catch (Exception e)
    {
        Logger.fatal("Error occured while generating data in product Database.", e);
    }
}

How to chive this new code looking after using statndard key bindings Ctrl + Alt + L ? 如何使用statndard键绑定Ctrl + Alt + L来照顾这个新代码?

You can change this in settings 您可以在设置中更改

File => Settings => Code Style => Java => Wrapping and Braces => Braces Placement . File => Settings => Code Style => Java => Wrapping and Braces => Braces Placement

IntelliJ IDEA 15 IntelliJ IDEA 15

Code Style is under Editor. 代码样式在编辑器下。

File > Settings... > Editor > Code Style > Java > Wrapping and Braces > Braces Placement > change "End of line" to "Next line" for "In class declaration", "In method declaration" and "Other" > OK 文件 > 设置... > 编辑器 > 代码样式 > Java > 包装和括号 > 括号放置 >将“在类声明”,“在方法声明”和“其他”的“行尾”更改为“下一行”>确定

大括号放置

On the right side, you will see a live preview. 在右侧,您将看到实时预览。

Apply the new formatting 应用新格式

  • For the current file: 对于当前文件:
    • click on the editor area > press Ctrl + Alt + L 单击编辑器区域>按Ctrl + Alt + L
  • For the whole project / a folder 对于整个项目/一个文件夹
    • click on a folder from the project structure (Eg: on src ) > press Ctrl + Alt + L 在项目结构中单击一个文件夹(例如:在src上 )>按Ctrl + Alt + L

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

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