• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Auto-format code

 
Marshal
Posts: 6974
471
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers,

I'm looking for the way to auto-format code in IntelliJ, specifically during the maven project build. So I was thinking about the maven plugin, to which I could supply a custom codestyle configuration file.

One of closest plugins what I found is reference to project:

An issue is, that it works with Eclipse IDE only, while I need to work with IntelliJ IDEA.

So I was up for new searches, so I found this, works with IntelliJ, it does format the code, but it formats based on google style guidelines, and the documentation explicitly states, that "The format cannot be configured by design".

So I ran out of ideas. Did you face any similar problem, or maybe discovered anything like that, or maybe you have some other ideas/suggestions?

Thanks.
 
Saloon Keeper
Posts: 10428
223
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I shudder by the idea of automatic code formatting. It never does exactly what you want, and it may even mess up code that is really nicely and clearly formatted by hand, just because it doesn't conform to some dumb style rule.

Automatic formatting in your IDE is a tool that you should trigger by hand, to help you do most of it. In the end though, it's the programmer's responsibility to make it look good, not some tool.

Is there a particular reason you want the code to be formatted by Maven?
 
Bartender
Posts: 20938
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a product called Jalopy that allows very precise control over pretty-printing of source code. It used to be available in a free edition, but last time I looked you had to pay for it.
 
Liutauras Vilda
Marshal
Posts: 6974
471
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Automatic formatting in your IDE is a tool that you should trigger by hand, to help you do most of it. In the end though, it's the programmer's responsibility to make it look good, not some tool.

Is there a particular reason you want the code to be formatted by Maven?


That is what we started come up with as for a conclusion.

Well, the idea was to upload such code style file to resources of the project and invoke upon build, so the users wouldn't need to worry whether they got right (if at all) code style set up on their machines and so we'd have a consistent codebase when committing.

I did more research over the night though, and discovered an IntelliJ plugin called "Save Actions". And it looks quite promising. So it applies current IDE set style upon file save + offers other features (see attached pic). So building maven project locally having some changes it saves automatically file, which in turn triggers this plug-in.

I think I'll play with it more to see how it feels a bit in a longer run, so we potentially could use it. But with this approach we need to ensure we have consistent code style set-up on all IDE's. But that is something we can probably make so.


Tim Holloway wrote:There's a product called Jalopy that allows very precise control over pretty-printing of source code. It used to be available in a free edition, but last time I looked you had to pay for it.



Thanks. Will have a look at that one.

In general I was a bit surprised that such maven plugins don't exist for IntelliJ so you could supply config for formatter. But might be there are reasons what Stephan mentioned and such thing don't have much use in projects.
Screenshot-2019-03-29-at-13.23.48.png
[Thumbnail for Screenshot-2019-03-29-at-13.23.48.png]
 
Tim Holloway
Bartender
Posts: 20938
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, Maven isn't supposed to be that closely coupled with any IDE. In fact, one of the reasons I use Maven is that I can do builds without any IDE whatever. On machines that aren't running windowing systems.

However, some people do include source code check-in as part of what they expect Maven to do and it's not uncommon to enforce shop standards for source code formatting. As for my example, my own rabid insistence that all conditional statements, no matter how trivial contain their predicates in blocks.

You can argue that Maven isn't the ideal place to do that, that in fact, it should be done by the source code manager as a commit hook. It's a matter of taste, I suppose, but there is the advantage that each and every POM  doesn't have to be manually configured for source reformatting.
 
Sheriff
Posts: 13562
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Automatic formatting in your IDE is a tool that you should trigger by hand, to help you do most of it. In the end though, it's the programmer's responsibility to make it look good, not some tool.


Completely agree.

I've always told my teams to format their code before they commit. If we come across poorly formatted code, we do the formatting as a single refactoring/commit. We never want to mix both reformatting and new/changed code in the same commit since that makes it difficult to tell the real code changes apart from changes just to the code formatting.
 
Liutauras Vilda
Marshal
Posts: 6974
471
Mac OS X VI Editor BSD Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:Actually, Maven isn't supposed to be that closely coupled with any IDE. In fact, one of the reasons I use Maven is that I can do builds without any IDE whatever. On machines that aren't running windowing systems.


Completely agree. This question actually came up today, because some of us build from terminal, some from IDE, so basically IDE's own pluggins (i.e. Save Actions) or bounded to IDE maven plugins simply don't work in both situations, which in turn in neither situation well.

Really researching further feels like spending resources not where we should do. Will have to stick to classical approaches.


Junilu Lacar wrote:I've always told my teams to format their code before they commit. If we come across poorly formatted code, we do the formatting as a single refactoring/commit.


Sounds good, really it is a refactoring after all. And takes very little effort actually.

It was good to hear confirmations that no need to over-engineer things here.

Thanks all.
 
Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
create a simple IDEA theme and earn!
https://coderanch.com/wiki/708971/intellij-idea/ide/IntelliJ-Themes-Contest-Deadline
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!