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:
package a
package b
package c
and it almost like package a.b.c
except that it automatically imports everything from a
and b
you can have package object
in your file and then if you have
package a.b
package object c
this file should be in a/b/c
directory
so 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:
- match the first line with regexp (
^package (.*)
)
- translate
a.b.c
into a/b/c
(matched.split('.').map(_.trim).mkString(File.separator)
)
- compare generated location to an actual location ( I suggest resolving absolute file locations)
- move file if necessary
If there is a possibility of having more complex case than that, I could replace first step by querying scalafix/scalameta utilities.