Root package declaration to reduce folder clutter

#1

Since (thankfully) a package declaration doesn’t need to match the directory structure, like in Java, it would be great if there could be a way in IDEA to define, say, a root package which would be picked up by all package declarations. This would mean no “package directive does not match file” warnings and when automatically generating package declarations would take into account the root package. This would mean a lot less directory clutter.

E.g. my package is com.foo.bar.baz.mycompany.api. All the directories until api only exist to define the package structure. If I could put somewhere basePackage = “com.foo.bar.baz.mycompany” then use the “api” folder as the base folder, as it were, it would help with folder clutter. Then when I defined a new class under “api”, instead of making the package “api”, it would prepend the base package value.

It would also solve the problem referenced here: https://youtrack.jetbrains.com/issue/SCL-6875

5 Likes

#2

Adding context information may confuse readers, in my opinion should be useful define a base package directive to reduce directory path.

I explain it.

Define basePackage = "org.example"
Create a file in source root directory using package declaration “org.example” -> OK
Create a file in source root directory without package declaration -> warning, package should be "org.example"
Create a file in “api” directory using package declaration “org.example.api” -> OK
Create a file in “api” directory using package declaration “api” -> warning, package should be “org.example.api”

Pay attention: must be possible associate the basePackage to a specified source set.

Using “src/main/kotlin/org/example/api” directory path is excessive long, but this may be a gradle/maven problem.

2 Likes

#3

It would be nice to have support on this in IntelliJ. Not only the annoying warning saying Package directive doesn't match file location the real problem comes when refactoring folders/file structures using the IDE, because IntelliJ changes the packages in a wrong way when you move them around.

[Not sure if this forum is the right place to report this, because it is related with support on IntelliJ, its not about Kotlin exactlty]

0 Likes

#4

With the current version of IntelliJ (2018.3.4) this seems to somewhat work as expected:

  1. Create a file in source root with package org.example -> OK
  2. Create a file in api directory with package org.example.api -> OK

It breaks down when you only have directories in your source root:

  1. Create a file in api directory with package org.example.api -> Warning
  2. Create a file in db directory with package org.example.db -> Still warning on both files

My current workaround is creating a dummy.kt in the source root with only the package declaration org.example. The package resolution as well as refactoring work properly.

EDIT: I noticed that refactoring doesn’t actually work properly, but the erroneous warnings go away, and (bonus) you actually get a warning when the package is actually not correct, eg: dir=[root]/api, package=org.example (should be org.example.api)

0 Likes