Removing Trailing Spaces

Last week, when I was at the IPC 2009 conference, a some user ask me whether NetBeans removes trailing spaces. It was one question from many and I would like to clarify how it works in NetBeans 6.0 - NetBeans 6.8.

Basically it works automatically on save. So when you invoke Save action then the trailing spaces should be removed from the file. There are two exceptions, when the spaces are not removed from a line:

  • 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.

If you want remove all trailing spaces in the file, you have to invoke action Remove Trailing Spaces from Source menu by hand.

Comments:

Hi Petr,

i was one of these guys. And i think this behaviour isn't very useful. I think it is very very necessary to give users a chance to configure those options. NetBeans as IDE has very less configuration options which is in my eyes not acceptable for a good IDE. Each developer handles a lot of things different to others and cannot modify the behaviour of an IDE is worse.

I would like to decide this for my own. If i know forget to move my cursor to the first position in a line an save this the merging problem will only be moved forward away from NetBeans. If some other guys with other IDE's will know edit and save those files, they will have a change, because the will correctly drop the trailing white spaces which i have not. Which is a much bigger Problem, because this users have to check, what they have changed in a file in lines they never edited.

And one more thing about this. If i edit a file which is NOT in svn/cvs, the behaviour stays the same. If i decide to strip trailing white spaces, the IDE should not try to decided when this should be done.

Other example. We uses pre commit hooks in svn which checks the coding styles. If i try to commit a file with trailing white spaces, it will be blocked and i have to handle it by my own to fix this.

Sorry, but the missing configuration options in NetBeans are a big big minus to the IDE. These are all stuff other IDE's like eclipse handles it much much better and convenient for the users.

I although think, that an IDE is not a dummy tool like a WYSIWIG text editor. It is OK to have configuration options. Guys are in trouble with this should perhaps do not software developing :-)

Timo

Posted by Timo on November 24, 2009 at 07:34 AM CET #

Would really be happy if it would be possible to just add removing trailing spaces on ALL lines as an action on save, not only when changed something in that specific line.

Meanwhile I have to use an ant-job which uses sed in my project which does it for me on ALL php/phtml-files:

http://www.robo47.net/codeschnipsel/29-Remove-Trailing-Spaces-sed-basierter-Ant-Task

Posted by robo47 on November 24, 2009 at 08:02 AM CET #

Hi Timo, robo47

I agree with you that there has to be a possibility to configure the behavior. The functionality is not covered by PHP team, it's a part of basic editor functionality, so we have to push guys from editor core to fix it. I have talked with them and they know about this problem. I hope that they will have enough time to do it.

Regards,
Petr

Posted by Petr Pisl on November 24, 2009 at 08:27 AM CET #

I agree with the posts above.

Just have a look at simple text editors like "ConTEXT". Even in such a simple editor I may configure the following behavior:

- trim trailing spaces on save
- allow cursor after end of line
- backup file on save
- remember editing positions on save
- detect external file changes
- automatically update on external file changes
- auto-indent
- enable drag-drop editing
- trigger smart home-key positioning
- automatically copy text at cursor to search dialog
- enable smart tabs
- enable line numbers
- group undo
- undo after save
- automatically hide mouse when typing
- enable word-wrap by default
- show word-wrap glyph
- default text font and size
- show gutter
- gutter width
- default highlighter
- indention settings
- word-wrap width

So: which of those above do I have in NetBeans?
It's "indention settings". That's all.

Posted by Tom on November 24, 2009 at 08:43 AM CET #

Some features from your list Netbeans already have.

Posted by Honza Marek on November 24, 2009 at 09:02 AM CET #

@honza Yes, we now it has some of those. It's just that you can't configure those editor settings.

We ask for a button to turn them on or off. ;-)

Posted by Tom on November 24, 2009 at 10:41 AM CET #

Trailing spaces and other options to cleanup files (whitespace/formatting wise) should be configurable as well.

Please make new line characters configurable instead of OS-dependant. E.g. I'm forced to work using windows, while all source code I develop uses unix style endings, which means I c&p files around when I need a new one...sux.

Sensible defaults are nice things, but they often collide with presets and defaults used in companies or personal and open source projects.

Posted by agreed on November 24, 2009 at 12:10 PM CET #

BTW: did you know that SVN has a property to enforce Unix-style endings on commit? Just set svn:eol-style to LF. This might be away around this issue for now.

Only problem is: you need to set it by hand for each file. Plus it won't kick in for newly added files either.

Posted by Tom on November 25, 2009 at 01:31 AM CET #

The posters above are crazy! The existing method of removing trailing space only from the lines that you have edited is ingenious. You can still remove them all via the menu if you like.

Petr Pisi if your VCS has a pre-commit hook that blocks commits when there's trailing whitespace then the default netbeans behaviour is perfect for you. Any lines that have whitespace that you didn't edit were there before you got there and thus caused by someone else. If your team is that concerned about trailing whitespace then write a script to cleanup your whole repository.

Please don't add unnecessary options to Netbeans. If you want an option for every insignificant thing then you might prefer Eclipse. Netbeans' simplicity is a breath of fresh air allowing me to focus on the important things: the code.

Posted by dalin on November 25, 2009 at 04:37 AM CET #

to Tom: You are right, some features are not implemented. It doesn't mean that we don't want to do it. As every project, the NetBeans project has limited resources and we have to choose, which features we can do. For example word wrapping. It looks like simple feature, but it is not. Yes, almost every simple text editor has it, but there is "just" word wrapping. In NetBeans you have to be careful about much more things. So when we decide to implement it, it means that you have to change internal line representation and all other features like debugging, parsing, hints etc. should be aware of this.

I agree that can the trailing spaces can be configurable. I talked this week with editor guys, and they agree with me as well. The current behavior is a result of evolution, which fits most case in Java world. To be honest, I haven't heard a complain about this until we published the PHP support.

to agreed: I can be wrong, but I think that the current behavior is that NetBeans save the file with the same new line character that is at the first line, when the file is open. The internal representation is always in java way, but saving is done with the char which was found at the first line.

As dalin wrote, every VCS system has an option to convert during commits the new line characters to unify them through repository. At least it works in all VCS that I have used so far(CVS, Subversion, Git and Mercurial).

to dalin: Exactly, the main phylosophy behing the first exception (when the trailing spaces are not deleted) is that the diffs should contain only relevant changes to be easy readable. And it's good practice to do changes in separate commit like change formatting of the file or remove trailing spaces.

On the other hand it's true that NetBeans usually know, whether the file is or isn't in a VCS, but it can be configurable as well. We are still trying to keep Options "clean" from the UI point of view. I hope that we will keep this trend in the feature, although we will add new options.:)

Posted by Petr Pisl on November 25, 2009 at 07:41 AM CET #

@Petr I wouldn't change word-wrap at all. It might not be perfect, but it's okay the way it is.

To the trailing spaces issue: I agree that the line-ending should not be changed. However: I feel that a default line-ending style for new files would be okay.

Perhaps it is more of a SVN-issue? Setting the eol-style property automatically when adding a new file (or even saving an existing file?), according to some project settings, should do the job, I suppose. This behaviour should also ensure that these changes don't appear in the SVN-diffs at all.

Posted by Tom on November 25, 2009 at 12:02 PM CET #

I don't like the current behavior in NetBeans. My fellow developers at this company use Zend Studio (the more recent versions based on Eclipse). They use a strip-trailing-whitespace-on-save option.

I've gotten complaints that my commits to our Subversion repository have trailing tabs in them. This tends to happen, I believe, when I add several lines, and hit return to leave a blank line between my new code insert and the next block of code. Hitting return causes NetBeans to tab out to the correct indent point. I then hit save, and that blank line with one or more tabs on it is preserved.

It's difficult to remember to go to the Source menu and select "Remove Trailing Spaces" before each save and commit.

Is there an open ticket in the Netbeans bug tracker referencing this issue?

Posted by Nick on December 08, 2009 at 03:58 PM CET #

Hi Nick,

the issue is https://www.netbeans.org/bugzilla/show_bug.cgi?id=157561 . You can vote for this. The good news is that is on the plan for NetBeans 6.8.

You mentioned that you use subversion. I'm sure that there is a subversion hook, which can solve it.

Regards,
Petr

Posted by Petr Pisl on December 09, 2009 at 06:21 AM CET #

Hi Petr,

Thanks for your reply. Unfortunately, that bug seems to be asking for the reverse of what I want -- the commenters seem to be requesting a UI feature to disable NetBeans from stripping trailing spaces on a save.

What I want is for NetBeans to _more aggressively_ strip spaces on save -- at the moment, it skips spaces before the caret on an otherwise empty line. I want it to strip those spaces too.

If I have the following code:

if (i > 2)
{
<tab>echo "Some string.";
<tab><-- caret is here after pressing return on previous line.

and save the file, I end up with a tab on the line after the echo. Instead, I want the tab or spaces on that line to be removed on save. This only happens if I manually move the cursor to the beginning of the line, or to another line, before saving. I will not always remember to do this.

The functionality I'm looking for would be equivalent with Zend Studio's PHP -> Editor -> Save Actions -> Remove trailing whitespace option, with the All lines radio button selected.

Does that make sense?

Best,
Nick

Posted by Nick on December 09, 2009 at 11:51 AM CET #

This is bad form to reply to myself, but my post above was a long winded way of saying that I want exactly the behavior of NetBeans' existing "Remove Trailing Spaces from Source" feature to automatically occur when the file is saved. I do not want either of the two exceptions that are mentioned in the original blog post.

Do you still think there's an open bug addressing this need? I've added a note with my request to Bug 157561, anyway.

In the meantime -- thank you for the tip on the Subversion pre-commit hook to prevent a commit containing whitespace. This would be an acceptable workaround for the time being. Do you know of a script I can incorporate into pre-commit to do this?

Thanks,
Nick

Posted by Nick on December 09, 2009 at 01:17 PM CET #

Hi Nick,

Tom mentioned in his comment

"BTW: did you know that SVN has a property to enforce Unix-style endings on commit? Just set svn:eol-style to LF. This might be away around this issue for now"

I think that the issue #157561 converts into options that will allow to control the triling spaces behavior. It's on the editor team plan for NB 6.9.

Posted by Petr Pisl on December 10, 2009 at 03:05 AM CET #

Tom wrote: "Please don't add unnecessary options to Netbeans. If you want an option for every insignificant thing then you might prefer Eclipse. Netbeans' simplicity is a breath of fresh air allowing me to focus on the important things: the code."

I think that already this trailing space removal when something is set(or not) but caret is(or not) there or some other condition like the moon was full 3 days ago or anything... is exactly not simple!!!
there should be an advanced option if I want this behaviour or I do not want Netbeans to remove the trailing spaces
for some people it is important to add trailing space

Posted by Pakozdi Tibor on February 15, 2010 at 06:27 PM CET #

This is I think the only feature stopping me from moving to NetBeans, so I'm stuck with bloated Eclipse (I code in PHP). There really should be an option to enable this, do some "advanced settings" or something...

Posted by Marko on February 25, 2010 at 01:29 PM CET #

I really liked what this tutorial has to offer. thanks so much for sharing it! in my turn I'd like to recommenend you the best music search engine http://www.mp3hunting.com enjoy!

Posted by Mike Hunter on March 10, 2010 at 01:17 AM CET #

did you know that SVN has a property to enforce Unix-style endings on commit?
Just set svn:eol-style to LF. This might be away around this issue for now

Posted by Dollar Items on March 23, 2010 at 03:22 AM CET #

Just set svn:eol-style to LF. This might be away around this issue for now

Posted by labatterie on April 26, 2010 at 03:51 AM CEST #

I'm forced to work using windows, while all source code I develop uses unix style endings, which means I c&p files around when I need a new one...sux.

Posted by error fix on August 26, 2010 at 07:35 AM CEST #

I'm forced to work using windows, while all source code I develop

Posted by yiwu on September 11, 2010 at 02:31 AM CEST #

Strip trailing spaces on save is essential for every serious programmer. I am deeply disappointed and could almost not believe my eyes that there is no such option as of 6.9.1 yet.

Posted by Backup on September 24, 2010 at 10:04 AM CEST #

Strip trailing spaces on save is essential for every serious programmer. I

Posted by yiwu on October 06, 2010 at 06:09 AM CEST #

Simply brilliant! What a time saver this is. Thanks. I am going to recommend it over to the guys at http://speedupmypctips.com.

Posted by Rich Murphy on October 07, 2010 at 10:53 AM CEST #

Use your browser's back button and you won't lose what you've written. If the Captcha image there isn't clear, click Preview to get a new one.

Posted by Pakozdi on November 03, 2010 at 07:47 PM CET #

hey I need the NetBeans for a new proyect, where can I get it??? thanks!

Posted by florida cpa on December 19, 2010 at 02:48 PM CET #

Strip trailing spaces on save is essential for every serious programmer. I am deeply disappointed and could almost not believe my eyes that there is no such option as of 6.9.1 yet.
http://www.areshop.com/Watches-Breitling.html
http://www.areshop.com/Watches-Cartier.html

Posted by jacky on January 11, 2011 at 08:30 PM CET #

I'm forced to work using windows, while all source code I develop uses unix style endings, which means I c&p files around when I need a new one...sux. http://www.areshop.com/Watches-Omega.html
http://www.areshop.com/Watches-Rolex.html

Posted by steve on January 11, 2011 at 08:31 PM CET #

I think I need more configuration options, because It wuld make my life easier!!

I like NetBeans 6.8., it easier but it could be a improvement piont in the future!

Posted by Costa Rica Doctors on January 15, 2011 at 01:21 PM CET #

I dont't understand why NetBeans doesn't have the option to remove trailing spaces of the line where the caret is on save. Eclipse has that option a long time.

Posted by guest on March 28, 2014 at 04:47 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
About

This blogs is written by NetBeans developers who contribute to the PHP support mainly.

Search

Archives
« May 2015
SunMonTueWedThuFriSat
     
1
2
3
4
5
6
7
8
9
10
11
12
13
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
      
Today