I have a file src/main/scala/foo.scala
which needs to be inside package bar
. Ideally the file should be inside src/main/scala/bar/foo.scala
.
// src/main/scala/foo.scala
package bar
// ...
How can I auto-fix this issue throughout my project such that the folder structure matches the package structure?
Is there any SBT plugin etc that can help me fix this issue?
As far as I am aware there are not such tools, though AFAIR IntelliJ can warn about package-directory mismatch.
Best I can think if is custom scalafix (https://scalacenter.github.io/scalafix/) rule - scalafix/scalameta would be used to check file's actual package, translate it to an expected directory and if they differ, move file.
I suggest scalafix/scalameta because there are corner cases like:
you are allowed to write your packages like:
and it almost like
package a.b.c
except that it automatically imports everything froma
andb
you can have
package object
in your file and then if you havethis file should be in
a/b/c
directoryso I would prefer to check if file didn't fall under any of those using some existing tooling.
If you are certain that you don't have such cases (I wouldn't without checking) you could:
^package (.*)
)a.b.c
intoa/b/c
(matched.split('.').map(_.trim).mkString(File.separator)
)If there is a possibility of having more complex case than that, I could replace first step by querying scalafix/scalameta utilities.