【问题标题】:How to configure IntelliJ to use short package names on "Optimize imports" for a scala codebase?如何配置 IntelliJ 以在“优化导入”上为 scala 代码库使用短包名称?
【发布时间】:2020-01-12 09:26:36
【问题描述】:

在我们使用 IntelliJ 社区版的 scala 项目中,我们希望每个文件使用多个包,以便我们可以用更短的语法引用它们:

package com.dreamlines.dragonfly
package api
package domain
package harbours

import common.entities.EsHarbour
import common.utils.date.DLDateTime

然而 IntellJ 想要强制执行完全限定的包名称,因此在优化导入时它将代码更改为:

import com.dreamlines.dragonfly.common.entities.EsHarbour
import com.dreamlines.dragonfly.common.utils.date.DLDateTime

如何配置优化导入的行为?

应该的

  • 至少保留缩短的导入或
  • 最好是自动缩短较长的部分

我想保持优化导入的其他行为不变,例如删除未使用的导入,添加缺失的导入,排序导入。所以我不认为不使用优化导入是一个有效的选项。


我们还使用了scalafmt 格式化程序,因此它可能与我们的.scalafmt.conf 相关,在这个项目中看起来像这样:

version = 2.0.0
style = default
maxColumn = 120
continuationIndent.callSite = 2
continuationIndent.defnSite = 2
align.openParenDefnSite = false
align.openParenCallSite = false
danglingParentheses = true
indentOperator = spray
project.excludeFilters = [".*\\.sbt"]
rewrite.rules = [RedundantBraces, RedundantParens, prefercurlyfors]
unindentTopLevelOperators = true
importSelectors = singleLine
spaces.afterKeywordBeforeParen = true
lineEndings = unix
newlines.penalizeSingleSelectMultiArgList = false
newlines.alwaysBeforeElseAfterCurlyIf = false
binPack.literalArgumentLists = false
runner.optimizer.forceConfigStyleMinArgCount = 1

【问题讨论】:

    标签: scala intellij-idea code-formatting scalafmt


    【解决方案1】:

    IntelliJ 中似乎有一个代码样式选项可以强制使用完全限定的包名称。您可以禁用它,看看是否有帮助:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      • 2017-04-12
      • 1970-01-01
      • 2023-04-05
      相关资源
      最近更新 更多