I was wondering whether intellij has a feature that would allow it to scan through files and perform things such as organising imports, safely deleting unused functions and fields etc. This feature was very helpful in .NET in Resharper plugin. Thanks for any pointers.
You can also run code cleanup across your entire project or solution. Right-click on the project or solution name in Solution Explorer, select Analyze and Code Cleanup, and then select Run Code Cleanup.
Code cleanup refers to the act of writing code so that it cleans up leftover data structures and other unwanted materials from memory and the filesystem. It is sometimes treated as a synonym of refactoring code, which involves making the source code itself easier to understand, maintain, and modify.
Go to Settings/Preferences | Editor | Code Style, select your programming language, and open the Wrapping and Braces tab. In the Keep when reformatting section, select the formatting rules which you want to ignore and deselect those which should be applied. Reformat your code ( Ctrl+Alt+L ).
Use Visual Studio's Undo action (Edit | Undo menu item) to undo the cleanup after its completion. This action is available when a document is open.
Use Analyze
| Inspect Code with the corresponding options enabled to find unused code, in the results you can apply the fix to all the occurrences at once removing all the unused methods and fields.
Use Code
| Reformat Code or Optimize Imports for the rest. Reformat Code has an option to optimize imports and you can run it on the whole project to do both operations globally in a single step.
It's also possible to optimize imports on the fly (enabled in Settings) and reformat/optimize imports when committing to the version control. Code inspections can be also executed before the commit to notify you about possible problems.
There is a plugin for IDEA - FindBugs-IDEA. It scans for cases you listed and a lot more.
You can try it out using the trial.
As to the imports, yes, you can Organize Imports ctrl-alt-o
, and you can choose to do so on the current file or on every file in the project.
Not sure if you can delete all unused methods in a project, but you do get warnings regarding methods/variables that aren't used and can be safely deleted (and IntelliJ does offer the option to safely delete).
Also, in the Preferences, you can setup a ton of things that you choose to be considered as warnings, depending on your coding style, conventions, etc.
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