2

How can I programmatically invoke intellij's java source code formatter on an arbitrary source file on disk?

I don't mind writing a plugin to do this but if there is a way to invoke the formatter from command line that would be great!

Thanks

Ajay
  • 977
  • 2
  • 11
  • 23
  • Perhaps you can check in with IntelliJ as you *pay* for software, API help is normally provided by the company where you let your money. – Smutje Oct 04 '14 at 23:28
  • You open the file in IntelliJ, make sure that IntelliJ can identify it as a Java file, then perform the "format code" option (CTRL + ALT + L or Command + Option + L). The key here is that IntelliJ needs to be aware that it is a source file. I doubt that this functionality is exposed in an accessible API, nor would it make sense to - IntelliJ has a peculiar setup when it comes to its code style interface. – Makoto Oct 04 '14 at 23:30
  • Consider something made for the purpose, like Jalopy - https://github.com/notzippy/JALOPY2-MAIN – GreyBeardedGeek Oct 05 '14 at 01:52
  • There was a video on "Upsource" that I remember which explains the core api, it might be useful to you. – vikingsteve Oct 06 '14 at 08:47
  • Possible duplicate of [How to get the "Reformat code" function out of IntelliJ IDEA?](http://stackoverflow.com/questions/5805960/how-to-get-the-reformat-code-function-out-of-intellij-idea) – francisco Jan 19 '17 at 21:31
  • Intellij already provides that for you, check: https://confluence.jetbrains.com/display/IDEADEV/Command-Line+Source+Code+Formatter – francisco Jan 19 '17 at 20:01

1 Answers1

2

I ended up writing my own plugin which is available on github. Note that it might need some more fine tuning

Ajay
  • 977
  • 2
  • 11
  • 23