This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 14202

Summary: automatically reformat code while saving
Product: editor Reporter: support <support>
Component: Formatting & IndentationAssignee: Dusan Balek <dbalek>
Severity: blocker CC: brooke, domak, gtzabari, jrojcek, MackSix, marcincinik, markiewb, olangr
Priority: P2    
Version: 7.1   
Hardware: All   
OS: All   
Issue Type: ENHANCEMENT Exception Reporter:
Bug Depends on: 140719    
Bug Blocks: 89036    

Description support 2001-08-04 21:01:31 UTC

is there a way (now or in near feature) to automatically reformat the code 
while hitting the save button or pressing ctrl-s?
Comment 1 David Konecny 2001-08-06 08:48:42 UTC
no, it is not possible to do this in current version
Comment 2 Jan Chalupa 2001-11-27 12:28:05 UTC
Target milestone -> 3.3.1.
Comment 3 Marek Grummich 2002-07-22 09:53:55 UTC
Set target milestone to TBD
Comment 4 Marek Grummich 2002-07-22 09:57:46 UTC
Set target milestone to TBD
Comment 5 Roman Strobl 2004-12-08 17:48:12 UTC
Changing subcomponent to formatting.
Comment 6 Ondrej Langr 2009-01-07 13:47:24 UTC
This is very desirable enhancement as it would lessen the number of shortcuts required to be remembered for basic work
with the IDE and group two frequently performed actions. Obviously it didn't get any attention for a long time, so I'm
raising priority to P2. 

Could we get this into 7.0? Any strong opinions on why not to go for it?
Comment 7 _ gtzabari 2009-01-07 13:58:13 UTC
I'd like to suggest a slight variation: automatic code-format every time the code parser is able to compile the file
without errors (if the file has changed). The reason is that I find myself invoking code-format every couple of seconds
as I insert new code and the indentation is off. This happens far more frequently than when I hit save.

Secondly, it might be worth investigating why indentation is off in the first place. I suspect the indentation engine
does not respect the code-format rules (because doing so is probably very hard to implement).
Comment 8 Vitezslav Stejskal 2009-01-08 09:06:35 UTC
> Could we get this into 7.0?


> Secondly, it might be worth investigating why indentation is off in the first place. I suspect the indentation engine
> does not respect the code-format rules (because doing so is probably very hard to implement).

Could you please list the usecases? Thanks
Comment 9 Ondrej Langr 2009-01-08 13:24:04 UTC
gtzabari: sounds reasonable. Only I'm somewhat worried of unexpected uninitiated changes under your hands in "random"
intervals. But it would be probably necessary to try if it was bothering or not. Reformatting on save is a safe backup
plan (which would make a big difference anyway), since save is involved 5 times more often than re-format!
Comment 10 Petr Dvorak 2009-01-08 14:07:37 UTC
*** Issue 140817 has been marked as a duplicate of this issue. ***
Comment 11 Jan Lahoda 2009-01-08 14:49:56 UTC
Some random notes:
-did I understand it correctly that according to statistics the reformat action is invoked quite often? Are we able to
recognize difference between "Reformat selection and "Reformat whole file"? If we are not able to recognize these two
behaviors of the reformat action, it might be true that "Reformat selection" is used (much) more often than "Reformat
whole file" (true at least for me - I hardly ever do "Reformat whole file").
-users using VCS' are quite likely not to like "reformat whole file on save", so we would still need easily accessible
reformat action.
Comment 12 Petr Dvorak 2009-01-08 15:17:00 UTC
> I hardly ever do "Reformat whole file"

Strange - I always do that (??). I just type a code and when there is "something I don't like", I press Ctrl+Alt+F...
which reformats whole file... it is faster than selecting text to reformat...

Also, "format on save" should be an option, see for example issue 156460...
Comment 13 _ gtzabari 2009-01-08 15:20:21 UTC
I suggest we do the following:

- Add a project-level property: "enforce code format", or "automatic code format". This would allow VCS users to disable
it for particular projects if need be (but personally I expect them to set a project-level formatting policy and enforce

- The reason I suggest this neutral name is that I think we should implement "reformat on save" now and look into
reformatting more often in the future. This name allows us to shift the strategy over time. If like the idea of
piggybacking code-format over a two-key shortcut such as save (CTRL+S).

- I'll try reporting individual cases where indentation falls out of place as I hit them.
Comment 14 domak 2009-01-08 15:50:16 UTC
some use cases?
As I said in issue 140817:
Reformat code and reorganize imports automatically when typing CTRL-S is a very important feature for teams (to force 
code normalization before commiting in VCS).
Comment 15 Petr Dvorak 2009-01-08 16:00:03 UTC
domak: The note about usecases was about formatter not respecting code-format rules... not about automatic reformatting...
Comment 16 _ gtzabari 2009-01-08 16:49:23 UTC
Upon further examination, I don't think the code indentation engine is at fault here. Here is an example scenario:

1) User types some expression
2) User decides to surround the expression with a for() loop

I typically add the for loop and invoke code-format right away to cause the expression to get indented inside the new loop.
Comment 17 Ondrej Langr 2009-01-08 17:52:56 UTC
> did I understand it correctly that according to statistics the reformat 
> action is invoked quite often? Are we able to recognize difference 
> between "Reformat selection and "Reformat whole file"? 

Correct. It's NO 15 action, right after Continue, in usage frequency above Undo, Run or Search.
See*&ActionsExcludes=&ActionsCount=15 for
details. And no, I don't think that statistics distinguishes between reformat selection and reformat all.

Comment 18 dolphm 2010-11-07 00:13:16 UTC
This is the critical feature preventing my team from switching to NetBeans from Eclipse.

How has this case been open for OVER 9 YEARS and not been implemented?
Comment 19 DizzyC 2011-01-25 13:43:15 UTC
Wow.. soon 10 Years since this feature request was registered.
Now.. I'm not a JAVA developer but this seems rather easy to implement. Is there no JAVA developer who sees the benefits of this feature? It should be worth the 30 minutes it probably takes a Netbeans developer to implement this.

I'm tempted to take on a few JAVA tutorial just to implement this.
Comment 20 _ gtzabari 2011-01-25 14:36:27 UTC
At the very least someone should update Target Milestone.

Ondrej, I understand that save is invoked more often than code-format but remember that save was invoked more often than compile before the compile-on-save feature was added. I would invoke code-format on every save if I could, unfortunately the keyboard shortcut is awkward and leads to wrist pains.

I foresee tying three functionalities with the SAVE action:

1. fix imports
2. code format
3. compile

(in that order)

Seriously, consider adding this functionality disabled by default. Users who want it can enable it. As the technology advances it could be enabled by default.

In my experience, code-formatting is very quick when you haven't changed the file much since the previous code-format operation and I suspect it could be made even more efficient over time (by remembering which file regions have been modified).
Comment 21 li0n12 2011-04-05 12:56:28 UTC
Comment 22 Skytake 2011-09-11 00:35:40 UTC
Omg... Really???

After 10 years, this still hasn't been fixed yet...
Can you guys just do a hot fix by adding a method call at the end of the saving procedure T_T?

I don't think anyone would complain because this is not configurable.
Make a hot fix please T_T

Comment 23 _ gtzabari 2011-09-11 01:37:29 UTC

The performance of code-format is quite a bit worse than it was when this issue was first filed (because it does a lot more than it did before). It doesn't make sense to code-format after each save until some serious optimizations are made. As I mentioned in my last comment, perhaps code-format should note which portions of the file has changed and only bother formatting those.

PS: I am not a Netbeans developer, just a user.
Comment 24 markiewb 2012-03-11 16:59:03 UTC
I also missed this feature, so i implemented it on my own and it works for my usecases. (nbm-packages also available)

@Netbeans-Team: Could you integrate the code for future releases? Most work is already done... The implementation is straight forward (Execute "fix-import"-action, then "format"-action and at last the "save"-action). An option panel is also included. But i had to use code from a non-friendly "Keymap options"-module, so there are platform-version specific modules. This is not very maintainable.
Comment 25 err 2012-05-17 21:49:03 UTC
Should this issue depend on Bug 140719?
Comment 26 MackSix 2012-05-18 13:08:46 UTC
If this is implemented, I think there should be a "format on save" option in Options.
Comment 27 markiewb 2012-05-18 18:07:21 UTC
(In reply to comment #25)
> Should this issue depend on Bug 140719?

Yes i agree, i linked this. Reformat is only one of many actions (f.e. fix import, order imports, sort members,...) which could be done on saving.
Comment 28 locked 2012-09-12 18:58:17 UTC
I'm currently coding in both eclipse an NB and just discovered today similar in eclipse:
Preferences > Java > Editor > Save Actions
- Format Source code (all or edited lines)
- Organize Imports
- Additional Actions (link to code formatted etc..)

this makes apretty advanced impression.
Comment 29 brooke 2012-09-18 11:39:11 UTC
I vote for this.
Comment 30 Jiri Prox 2012-09-18 11:55:38 UTC
On save actions are now available in 7.3

See Tools->Options -> Editor -> On save

it supports reformatting and removing trailing whitespaces
Comment 31 northar 2012-10-03 09:55:03 UTC
Hooray, finally. Thanks NB-team! Hope to see some more of the options found in  eclipse in the future, like "add final" and so on.
Comment 32 markiewb 2012-11-28 22:12:17 UTC
(In reply to comment #30)
> On save actions are now available in 7.3
> See Tools->Options -> Editor -> On save
> it supports reformatting and removing trailing whitespaces

So this issue can be set to resolved?
Comment 33 _ gtzabari 2012-11-28 22:13:39 UTC
I think so.
Comment 34 Jiri Prox 2012-11-28 22:19:54 UTC
Closing as WFM, this feature is already implemented.

Thanks for your suggestions and feadback
Comment 35 _ gtzabari 2012-11-28 22:25:15 UTC
Sounds more like FIXED than WON'T FIX since this feature didn't exist before 7.3 and it was just implemented.
Comment 36 markiewb 2012-11-28 22:43:13 UTC
Was fixed with the following commit.

*** This bug has been marked as a duplicate of bug 140719 ***