How to configure IntelliJ to use short package names on "Optimize imports" for a scala codebase?
Asked Answered
L

1

8

In a scala project where we are using IntelliJ community edition, we want to use multiple packages per file, so that we can reference them with shorter syntax:

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

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

Yet IntellJ wants to enforce the fully qualified package name and hence on optimizing the imports it changes the code to:

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

How do I configure the behavior of optimize import?

It should

  • at least keep the shortened imports or
  • at best shorten the longer ones automatically

I want to keep the other behavior of optimize import intact, e.g. removing unused imports, adding missing imports, sorting imports. So I don't consider not using optimize imports a valid option.


We are furthermore using the scalafmt formatter, so it may relate to our .scalafmt.conf that looks like this for this project:

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
Lehr answered 10/9, 2019 at 15:22 Comment(0)
L
3

It seems that there is a code style option in IntelliJ that forces fully qualified package names. You can disable that and see if that helps: enter image description here

Legroom answered 19/9, 2019 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.