is there a way (now or in near feature) to automatically reformat the code
while hitting the save button or pressing ctrl-s?
no, it is not possible to do this in current version
Target milestone -> 3.3.1.
Set target milestone to TBD
Changing subcomponent to formatting.
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?
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).
> 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
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!
*** Issue 140817 has been marked as a duplicate of this issue. ***
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
> 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...
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.
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).
domak: The note about usecases was about formatter not respecting code-format rules... not about automatic reformatting...
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.
> 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 http://statistics.netbeans.org/analytics/graph/actions.jsp?ActionsIncludes=.*&ActionsExcludes=&ActionsCount=15 for
details. And no, I don't think that statistics distinguishes between reformat selection and reformat all.
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?
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.
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
(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).
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
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.
I also missed this feature, so i implemented it on my own and it works for my usecases.
http://code.google.com/p/saveactions4netbeans/ (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.
Should this issue depend on Bug 140719?
If this is implemented, I think there should be a "format on save" option in Options.
(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.
I'm currently coding in both eclipse an NB and just discovered today s.th 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.
I vote for this.
On save actions are now available in 7.3
See Tools->Options -> Editor -> On save
it supports reformatting and removing trailing whitespaces
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.
(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?
I think so.
Closing as WFM, this feature is already implemented.
Thanks for your suggestions and feadback
Sounds more like FIXED than WON'T FIX since this feature didn't exist before 7.3 and it was just implemented.
Was fixed with the following commit.
*** This bug has been marked as a duplicate of bug 140719 ***