I am writing a Scala plugin for an editor I use that would highlight all unused code paths (could be unused defs
, vals
, classes
and implicits
), and give the user an option to yank them out of the .scala
file.
How can I do this? To simplify the problem, let's pretend we only have a single root level .scala
file with no external dependency on libraries or any other code files.
Ideally I would want this to be an SBT plugin that, given a single such Foo.scala
file, would spit out Foo_min.Scala
file with all unused code removed.
In IntelliJ, in a . java file, some unused code is greyed out indicating that the declared variable or function is never used. Unused imports are removed using Ctrl+Alt+O.
java file the unused code is ordinarily grayed out or features a green underline saying this code will likely (likely since of a few peculiar JNI/Reflection corner cases) be unused.
Scalafix has a rewrite for this: RemoveUnusedImports
Follow those instructions to run it: https://scalacenter.github.io/scalafix/#Installation
You need some kind of semantic API to traverse over the code and ask questions like "is this variable/import ever used" ?
As far as I know, Intellij uses Meta programming System
to achieve same goals. For scala, you can wait for scalameta
's 2.0 release, which (most probably) will support semantic API.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With