【问题标题】:Code style settings in IntelliJ (indentation)IntelliJ 中的代码样式设置(缩进)
【发布时间】:2013-10-29 22:22:43
【问题描述】:

我已将一个项目从 Eclipse 导入 IntelliJ,我想配置代码样式设置。但是我在声明数组时遇到了一个问题。

eclipse格式化程序将代码格式化如下:

@Before
public void prepareData() throws Exception //NOPMD
{
    LOG.info("Preparing setup data");
    new CoreBasicDataCreator().createEssentialData(null, null);
    CatalogManager.getInstance().createEssentialData(Collections.EMPTY_MAP, null);
    serviceLayerDataSetup.createJobPerformables();
    impExSystemSetup.createAutoImpexEssentialData(new SystemSetupContext(Collections.EMPTY_MAP, Type.ESSENTIAL,
            CuppyConstants.EXTENSIONNAME));
    cuppySystemSetup.importBasics(new SystemSetupContext(Collections.singletonMap(CuppyConstants.PARAM_BASICS, new String[]
    { CuppyConstants.PARAM_BASICS_PLAYERS }), Type.NOTDEFINED, CuppyConstants.EXTENSIONNAME));
    cuppySystemSetup.importWC2002(new SystemSetupContext(Collections.singletonMap(CuppyConstants.PARAM_WC2002, new String[]
    { CuppyConstants.PARAM_WC2002_SETUP, CuppyConstants.PARAM_WC2002_RESULTS_PRELIMINARIES,
            CuppyConstants.PARAM_WC2002_RESULTS_FINALS, CuppyConstants.PARAM_WC2002_BETS_PRELIMINARIES,
            CuppyConstants.PARAM_WC2002_BETS_FINALS }), Type.NOTDEFINED, CuppyConstants.EXTENSIONNAME));

    LOG.info("Finished preparation of setup data");

    LOG.info("Preparing session");
    JaloSession.getCurrentSession().setUser(UserManager.getInstance().getUserByLogin("sternthaler"));
    LOG.info("Finished preparation of session");
}

IntelliJ 将代码重新格式化为:

@Before
public void prepareData() throws Exception //NOPMD
{
    LOG.info("Preparing setup data");
    new CoreBasicDataCreator().createEssentialData(null, null);
    CatalogManager.getInstance().createEssentialData(Collections.EMPTY_MAP, null);
    serviceLayerDataSetup.createJobPerformables();
    impExSystemSetup.createAutoImpexEssentialData(new SystemSetupContext(Collections.EMPTY_MAP, Type.ESSENTIAL,
            CuppyConstants.EXTENSIONNAME));
    cuppySystemSetup.importBasics(new SystemSetupContext(Collections.singletonMap(CuppyConstants.PARAM_BASICS, new String[]
            { CuppyConstants.PARAM_BASICS_PLAYERS }), Type.NOTDEFINED, CuppyConstants.EXTENSIONNAME));
    cuppySystemSetup.importWC2002(new SystemSetupContext(Collections.singletonMap(CuppyConstants.PARAM_WC2002, new String[]
            { CuppyConstants.PARAM_WC2002_SETUP, CuppyConstants.PARAM_WC2002_RESULTS_PRELIMINARIES,
                    CuppyConstants.PARAM_WC2002_RESULTS_FINALS, CuppyConstants.PARAM_WC2002_BETS_PRELIMINARIES,
                    CuppyConstants.PARAM_WC2002_BETS_FINALS }), Type.NOTDEFINED, CuppyConstants.EXTENSIONNAME));

    LOG.info("Finished preparation of setup data");

    LOG.info("Preparing session");
    JaloSession.getCurrentSession().setUser(UserManager.getInstance().getUserByLogin("sternthaler"));
    LOG.info("Finished preparation of session");
}

如果您是 IntelliJ 用户,您可以在其代码样式编辑器中使用此代码示例。 希望你们中的任何人都可以解决这个问题:)

【问题讨论】:

  • 你能描述一下 intellij 格式有什么问题吗?将代码放在这里很好,但最好完整地描述它而不是“完成工作”的问题......
  • 我不太清楚如何描述它。唯一的问题是这个新的行数组标识(从那时的例子中可以看出)。我在 IntelliJ 中找不到相应的参数来设置这个

标签: java intellij-idea


【解决方案1】:

你可以在 Idea 中使用这个插件:Eclipse Code Formatter

Preferences -> Plugins -> Browse Repositories(button)中搜索安装即可

【讨论】:

  • 这是一个很好的建议,但我不认为它回答了 OP 的终极问题。他正在寻找在 IDEA 中为代码格式化进行特定配置。如果 Eclipse 格式化文件是项目本身的一部分(在源代码控制中),那么您的建议很好。但是,如果不存在这样的文件并且 OP 希望我们在 IDEA 中处理的所有项目都使用这种类型的格式,则无济于事。
【解决方案2】:

如果我对你的理解正确,你想要这个:

cuppySystemSetup.importBasics(new SystemSetupContext(Collections.singletonMap(CuppyConstants.PARAM_BASICS, new String[]
    {CuppyConstants.PARAM_BASICS_PLAYERS}), Type.NOTDEFINED, CuppyConstants.EXTENSIONNAME));

这样格式化

cuppySystemSetup.importBasics(new SystemSetupContext(Collections.singletonMap(CuppyConstants.PARAM_BASICS, new String[]
{CuppyConstants.PARAM_BASICS_PLAYERS}), Type.NOTDEFINED, CuppyConstants.EXTENSIONNAME));

为此,您需要将 Settings > Code Style > Java > Tabs and Indents > "Continuation indent" 设置为 0。但是请注意,这将使所有换行的行刷新。我不确定这是否是你想要的。或者,如果您只希望它用于特定代码。如果是这样,请您澄清一下。

另请注意,在 IDEA 13(目前在 beta/EAP)中,您可以通过 cmets 对代码段使用关闭格式。 (此功能是通过IDEA-56995 添加的)。标签是可配置的,默认为如下所示的值

// @formatter:off
cuppySystemSetup.importBasics(new SystemSetupContext(Collections.singletonMap(CuppyConstants.PARAM_BASICS, new String[]
{CuppyConstants.PARAM_BASICS_PLAYERS}), Type.NOTDEFINED, CuppyConstants.EXTENSIONNAME));
// @formatter:on

您可以使用实时模板轻松创建 环绕,以将这些标签添加到代码块中。

【讨论】:

  • 嗨,Continuation ident 没有帮助,因为那时所有这些新行都将处于同一标识级别。您会看到它们已在我的展示代码中标识。
  • 那么,您是否希望任何数组声明的左括号(即使定义为方法调用的参数)在下一行并与上一行齐平?如果是这样,那在 IDEA 中是不可能的。如果设置存在,它将位于 Java > Wrapping and Braces > Array Initializer。我建议你打开一个feature request有时 代码风格的请求可以很快实现。这完全取决于Denis(开发代码样式组件的开发人员)在他的盘子里有什么。
【解决方案3】:

也许您应该只在代码样式 java 中使用“重新格式化时保持”“换行符”。

不是很漂亮,但它有时是处理不良遗留代码的唯一方法(带有空格和所有......)。

在您的示例中,您可以从方法调用中提取数组(和其他变量)的声明,这样您可能会得到与 Eclipse 相同的结果。此外,恕我直言,它可以使您的代码更易于阅读,因为实际上很难快速弄清楚这段代码在做什么。

【讨论】:

  • 嗨,代码本身并不重要。它只是来自平台本身的内部测试的复制/粘贴 :) 我会试试你写的内容并回复你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 2016-07-25
  • 2013-08-22
  • 1970-01-01
  • 2018-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多