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 157561

Summary: Add option to control if trailing whitespaces are removed on save
Product: editor Reporter: mslama <mslama>
Component: OptionsAssignee: Vitezslav Stejskal <vstejskal>
Status: RESOLVED FIXED    
Severity: blocker CC: anebuzelsky, Feuermurmel, gregkotsaftis, hmichel, jkovalsky, k776, koba, markeb, mmirilovic, mpetras, olangr, pjiricka, ppisl, robertgonzalez
Priority: P3    
Version: 6.x   
Hardware: All   
OS: All   
Issue Type: DEFECT Exception Reporter:
Attachments: Patch
Tools-Options -> Editor -> General dialog screenshot
The patch implementing the new setting and its GUI

Description mslama 2009-01-28 16:25:47 UTC
When editing java source in editor I add space to empty line so that number of spaces is the same as indentation level.
When I save document spaces I added are gone. This was problem some time ago. I do not remember issue number. Now it is
back again. Please do NOT STRIP ANYTHING on save action.

Simple steps to reproduce:
I have following code
        String AU_PREF_NODE = "org/netbeans/modules/autoupdate"; // NOI18N
        Preferences auPref = NbPreferences.root().node(AU_PREF_NODE);

        int period = auPref.getInt("period", 2);
        if (period == 5) {
            return;
        }

Go to empty line, press TAB, it adds spaces so that cursor is at the same column a 'P' above. Go one line step down or
up, save document and check spaces on empty line. They are away....
Comment 1 mslama 2009-01-28 16:38:09 UTC
It is on my own NB build 090128
  Product Version         = NetBeans IDE Dev (Build 090128)
  Operating System        = Linux version 2.6.27-11-generic running on amd64
  Java; VM; Vendor        = 1.6.0_11; Java HotSpot(TM) 64-Bit Server VM 11.0-b16; Sun Microsystems Inc.
  Runtime                 = Java(TM) SE Runtime Environment 1.6.0_11-b03
Comment 2 Vitezslav Stejskal 2009-02-04 11:05:23 UTC
Trailing whitespace on lines that were edited is removed when a document is saved. This was requested in issue #13063 by
many users. At the present there is no way how to turn this feature off in the editor options. 
Comment 3 Petr Dvorak 2009-02-04 11:18:09 UTC
Vita, is there at least some support for turning this off in the editor's infrastructure, so that the only problem is
missing GUI? I can imagine someone doesn't want this feature...
Comment 4 mslama 2009-02-04 11:26:43 UTC
In other words you say: Use another editor. Ok. I will.

There is difference between line with any text ie. not only white chars on line and line containing only white spaces.
Usually empty lines are used to separate parts of code blocks ie. to make code more readable. If I scroll using cursor
up/down and there is empty line without any indentation it can cause jumping visible area to right and back as cursor
skips to first column on empty line.

IMO it is MUST to have this as option. Automatic removal of anything on saving without option to turn it off is
unacceptable. Reopening. You can make it enhancement to add it as editor option if you wish but please handle it somehow.
Comment 5 Petr Dvorak 2009-02-04 12:09:07 UTC
I think P2 is too high. It is a very commonly requested feature, it negatively affects a minimal number of external
users, I believe. Lowering priority to P3, please feel free to raise it back...
Comment 6 mslama 2009-02-04 12:48:03 UTC
Summary updated.
Comment 7 Jiri Kovalsky 2009-03-20 16:38:45 UTC
This issue is a candidate for NetFIX [1] team.

[1] http://wiki.netbeans.org/NetFIX
Comment 8 _ theanuradha 2009-03-23 13:42:32 UTC
I'll give this try (netfix )
Comment 9 _ theanuradha 2009-03-27 04:38:47 UTC
After looking at source IMO I found best place to disable trailing whitespace remove is on

TrailingWhitespaceRemove.run(CompoundEdit compoundEdit)

As first step I have patch with cli option to disable trailing whitespace remove.
if you apply attached patch and run netbeans with "-J-Deditor.lib.disable.trailing.whitespace.remove=true"

Now Where netbeans ui team suggest put gui option ?
Let me know I really happy to work on that too 


Comment 10 _ theanuradha 2009-03-27 04:39:15 UTC
Created attachment 78957 [details]
Patch
Comment 11 _ theanuradha 2009-03-27 04:41:56 UTC
Ah sorry option is 
"-J-Deditor.lib.TrailingWhitespaceRemove.disable=true"
 not 
"-J-Deditor.lib.disable.trailing.whitespace.remove=true"

Comment 12 Michel Graciano 2009-03-27 11:34:10 UTC
I think the property should be named something like this 'editor.lib.disable.trailing.whitespace.remove' as first
comment. All in lower case as usual.
The option could be located, imo, Options > Editor > Formating and had this option for each language if possible. If it
is not the idea, if one option for whole IDE, Options > Editor > General, create a new category named 'Trailing
Whitespace Behavior' with one option 'Use Trailing $Whitespace'. What do you think?

Regards
Comment 13 _ theanuradha 2009-03-27 12:06:15 UTC
>>I think the property should be named something like this 'editor.lib.disable.trailing.whitespace.remove' as first
>>comment. All in lower case as usual.

Well it depends and shorter is the better see on netbeans.conf "-J-Dapple.laf.useScreenMenuBar=true "

>>The option could be located, imo, Options > Editor > Formating and had this option for each language if possible. If >>it
>>is not the idea, if one option for whole IDE, Options > Editor > General, create a new category named 'Trailing
>>Whitespace Behavior' with one option 'Use Trailing $Whitespace'. What do you think?

We have wait from official recommendation from netbeans ui team  (to maintain ui guide line they fellow ).

Comment 14 Jiri Kovalsky 2009-03-30 15:24:14 UTC
Vito, can you please review Anuradha's patch and integrate it if you have no comments? In the other case please speak
up. Thanks!
Comment 15 Vitezslav Stejskal 2009-03-30 16:43:50 UTC
Well, the patch is ok, but it does not fix the problem. The whole point of this issue was to allow users control
whitespace removal from the IDE. In other words we need the UI. IMO there is only a little point in applying this patch.
Sorry.

Ondro, what would be a good place for the 'Remove Trailing Whitespace' option? Thanks
Comment 16 Ondrej Langr 2009-03-30 16:45:02 UTC
I've been asked to give a location for the "Remove trailing whitespaces" option. 

Editor -> Formatting -> Remove trailing whitespaces right below "Expand tabs to spaces" seems to be the only right
location (language specific, as someone already mentioned). 

As for default settings, I'd say on. 

Just please make sure that it works during re-format as this will certainly be expected from users, given the location
of the option.
Comment 17 Vitezslav Stejskal 2009-03-31 14:25:02 UTC
> Just please make sure that it works during re-format as this will certainly be expected from users,
> given the location of the option.

It works for lines that were edited and the trailing spaces are removed from these lines when a file is saved. That said
it won't work during reformat as you would expect. Eg. consider a line which formatting is correct -> reformatting that
line won't change the line -> no edits on the line -> no trailing whitespace removal when saving the file.

Making it work the logical way, that is to remove trailing whitespace on every line that is subject of reformatting (eg
all lines in a file or all selected lines), is going to be harder than just adding the option to the UI.
Comment 18 Ondrej Langr 2009-03-31 15:29:56 UTC
It seems that code format does remove trailing spaces (tested by editing the file out-of-the IDE while it was turned off). 

If we do not make code format to reflect this option, we will almost certainly be getting user complaints like "i've
told netbeans not to remove trailing spaces and it does so anyway" or "remove trailing spaces option does not work". 

IMHO, adding the option anyway is short-sighted .. I'd vote for either making it respected by both file save and code
reformat actions or not adding it at all.
  
Comment 19 gerbenvv 2009-04-04 09:12:36 UTC
Sorry, I made this issue P4 accidentally.

Note that if you have your cursor on an indented line (with only whitespace), the whitespace on this line won't be
removed. Is this a bug, or is it just that users want their cursor to be in exactly the same place?

I also found out that sometimes edited empty lines aren't removed, it seems to be very random to me, I can't reproduce
it every time.
Comment 20 Jiri Prox 2009-07-27 10:21:56 UTC
*** Issue 169173 has been marked as a duplicate of this issue. ***
Comment 21 markeb 2009-07-27 17:36:04 UTC
Any movement on this issue? This is literally one of the major usability problems with NetBeans that is keeping me 
from dropping Eclipse.
Comment 22 Jiri Kovalsky 2009-10-14 13:43:36 UTC
Anuradha, are you going to incorporate recent comments anytime soon? Thanks!
Comment 23 robertgonzalez 2009-11-12 11:57:33 UTC
Has there been a determination on whether this will be fixed? I'm only asking because I would love to use Netbeans more but I just can't if it is going to be editing my files for me when I save them. I have been waiting for this to be fixed for a few releases now and, while the IDE has improved greatly over the last few releases, I just can't use it if it edits my documents on its own without my input.

For what its worth, no editor should edit your documents on its own, in my opinion. I know it has been said that some users have asked for whitespace to be stripped or removed, but seriously, this should be an "off" option that a user has to turn on. Really, no other editor makes changes to your documents without your consent. Why should NetBeans?
</rant>
Comment 24 Vitezslav Stejskal 2009-11-13 05:53:44 UTC
> Has there been a determination on whether this will be fixed?

Highly unlikely in 6.8, sorry. We are about to enter the high resistance mode before the final stabilization.
Comment 25 Vitezslav Stejskal 2009-12-04 01:48:07 UTC
*** Bug 166451 has been marked as a duplicate of this bug. ***
Comment 26 dreldouay 2009-12-09 12:26:31 UTC
This may belong as a new bug, but I would like the "trailing whitespaces are removed on save" feature, when enabled, to use the same behavior as the "Remove Trailing Spaces" option on the Source menu. Currently, there are two exceptions/differences where trailing spaces aren't removed on a save (these do not apply when choosing the Remove Trailing Spaces operation manually):

    * The line wasn't changed. The main reason for such behavior is that usually you don't want to have unnecessary changes in the diff, if the files comes from a VCS. So unchanged lines are not touched and if there is a space at the end of such line, then is not removed. When you edit the line then the spaces are removed on save as well.
    * The line where the caret is located. When you invoke Save action, then the position of the caret is not changed. But all trailing spaces after caret are removed.

(credit to http://blogs.sun.com/netbeansphp/entry/removing_trailing_spaces).

What I'm asking for is, we add an option to toggle whether whitespaces are removed on save, we should allow for the toggling of the above exceptions too.

I would personally like the feature to work like Zend Studio's preference in PHP -> Editor -> Save Actions -> Remove trailing whitespace (All lines).  I recognize that this may not match everyone's requirements, however.
Comment 27 Petr Pisl 2009-12-10 03:20:41 UTC
The possibility a configuration of trailing spaces behavior is discussed very often in recent days (I'm talking mainly about PHP users). If we want to solve the issue properly, there has to be provided a full control over the behavior. There has to be possibility to switch on removing all trailing spaces on save, even at the lines that were not edited.
Comment 28 hmblprogrammer 2010-01-12 10:42:00 UTC
I think this should be P2. It would be a simple enough change and it is in my opinion a major issue, enough so that I won't use NetBeans until it is resolved. My editor shouldn't change my code unless I want it to!
Comment 29 Vitezslav Stejskal 2010-02-22 03:20:55 UTC
*** Bug 181013 has been marked as a duplicate of this bug. ***
Comment 30 Feuermurmel 2010-02-22 12:53:37 UTC
After Vitezslav correctly labeled my report as duplicate of this one, I found out that this problem is existing since over one and a half years and I wanted to give my sincere voice for resolving this issue as fast as possible.

As far as I can comprehend, stripping empty lines of their content, even though it has been requested and implemented as a feature, is an inexcusable malpractice. I do see that there are people who want their text editor to do this, I am unable to understand the reasoning behind it. And changing the one thing I can't see without telling me is something I conceive as pure betrayal. My text editor is the last piece of software on my computer that I must be able to trust and it must never ever ever ever ever misuse that trust and change what I typed behind my back. It is exactly like if I couldn't trust my mains tester if I was an electrician.

I'm in charge of my source code, for heavens sake!

Even though I almost found, with NetBeans, what I was looking for, I will be evaluating and using other Java IDEs until this has been fixed/enhanced.

Michael
Comment 31 Marian Mirilovic 2010-03-17 09:34:30 UTC
*** Bug 182173 has been marked as a duplicate of this bug. ***
Comment 32 gregkotsaftis 2010-03-18 10:33:01 UTC
In my opinion "Remove Trailing Spaces" should be a configurable property for specified file types. For example it is desired to trim spaces on source files *.java but it is not on manifest files, because of the blank spaces that are required for "Class-Path:" e.g consider the following manifest.mf, every line in Class-Path section must end with a training space. Since Netbeans trims these spaces I have to edit my manifest files with notepad!

Manifest-Version: 1.0
Main-Class: test.MainDesktop
Class-Path: 
 lib\. 
 lib\hibernate\antlr-2.7.6.jar 
 lib\hibernate\c3p0-0.9.1.jar 
 lib\hibernate\cglib-2.2.jar 
 lib\hibernate\commons-collections-3.1.jar 
 lib\hibernate\commons-logging-1.1.1.jar 
 lib\hibernate\dom4j-1.6.1.jar 
 lib\hibernate\ehcache-1.2.3.jar 
 lib\hibernate\hibernate3.jar 
 lib\hibernate\javassist-3.9.0.GA.jar 
 lib\hibernate\jta-1.1.jar 
 lib\hibernate\log4j-1.2.15.jar 
 lib\hibernate\slf4j-api-1.5.8.jar 
 lib\hibernate\slf4j-log4j12-1.5.8.jar 
 lib\jasperreports\commons-beanutils-1.7.jar 
 lib\jasperreports\commons-digester-1.7.jar 
 lib\jasperreports\commons-javaflow-20060411.jar 
 lib\jasperreports\itext-1.3.1.jar 
 lib\jasperreports\jasperreports-3.0.0-modified.jar 
 lib\jasperreports\jdt-compiler-3.1.1.jar 
 lib\jasperreports\poi-3.0.1-final-20070705.jar
Comment 33 Vitezslav Stejskal 2010-06-08 12:22:20 UTC
I'm taking a stab on this.
Comment 34 Vitezslav Stejskal 2010-06-08 12:24:41 UTC
Created attachment 99895 [details]
Tools-Options -> Editor -> General dialog screenshot

An initial GUI - the available options in the combobox are: Never, Always, From Modified Lines Only.
Comment 35 Vitezslav Stejskal 2010-06-08 12:27:58 UTC
Created attachment 99896 [details]
The patch implementing the new setting and its GUI

The 'Always' option does not work yet. I need to update TrailingWhitespaceRemove and its ModsProcessor to support that. I think I'll have to discuss it with Mila. Also the API change in SimpleValueNames is not properly documented in apichanges.
Comment 36 Vitezslav Stejskal 2010-06-10 14:05:58 UTC
http://hg.netbeans.org/jet-main/rev/64f8d745b985 - the setting works for all languages, but each language may (if they need) to override this in its own module layer. Currently text/x-manifest language overrides this setting to 'never'.
Comment 37 Quality Engineering 2010-06-12 06:51:09 UTC
Integrated into 'main-golden', will be available in build *201006120001* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main/rev/64f8d745b985
User: Vita Stejskal <vstejskal@netbeans.org>
Log: #157561: adding option to control how trailing whitespace is removed when saving files
Comment 38 gregkotsaftis 2013-02-26 20:48:14 UTC
Thanks for resolving this issue (NB 7.3)!
But how about adding an option for manifest files?

We need a new "Language" option on "Options>Editor>OnSave", to control "*.mf" files.

Plain text does not work, so as a workaround, we have to set "All Languages" to "none" (for trailing whitespaces) and all other languages to e.g. all lines
This is not so productive.
Please give a manifest language option as well in a new NB version.
Comment 39 Marian Mirilovic 2013-02-26 20:56:01 UTC
(In reply to comment #38)
> Thanks for resolving this issue (NB 7.3)!
> But how about adding an option for manifest files?
> 
> We need a new "Language" option on "Options>Editor>OnSave", to control "*.mf"
> files.
> 
> Plain text does not work, so as a workaround, we have to set "All Languages" to
> "none" (for trailing whitespaces) and all other languages to e.g. all lines
> This is not so productive.
> Please give a manifest language option as well in a new NB version.

Could you please be so kind and create new issue ? This is almost 3 years old bug, please do not reopen such old bugs, report new one and add link to the old one at comment. Thanks in advance.