Eclipse indents new line with tabs instead of spaces
Asked Answered
N

2

8

I've followed all the suggestions here.

When I press return, I get a new line that is indented with tabs instead of spaces.

If I backspace to clear the tabs, and then press TAB a series of times, it correctly indents with spaces.

I'm pretty sure I have all my settings set up correctly. I created a new Code Style > Formatter policy for every language in the project, and specified to always use spaces. It seems as though these settings are partially active (ex: when I press tab), but inactive when I use return. I tried restarting Eclipse. I'll try restarting the computer now...

I'm using Mac OS X 10.9.2 and a Liferay Developer Studio (1.6.3.v201312111844) version of Eclipse (not sure which Eclipse build its based on though).

Can anyone think of another setting/solution to ensure that newlines are created with spaces instead of tabs? I recently saw http://editorconfig.org/, and I'm wondering if there's some interference.

Thanks for any suggestions

Nomadic answered 9/5, 2014 at 17:43 Comment(0)
S
5

If the file has existing lines that are using tabs, then Enter will respect that and try to create new lines in a similar way (see this comment by topchef for a solution). Also, it could be something in Liferay Studio's proprietary settings is overriding Eclipse standard preferences (as suggested by user John).

Keep in mind that each type of editor in Eclipse can have its own preferences and perhaps that's what you're running into here. You can try to find them all by opening Preferences and searching for "indent" in the search field. That will show all the preferences pages where indentation can be configured.

enter image description here

Also note that the Formatter settings don't have any affect on as-you-type formatting; that's for when you select a file or group of files or part of a file and choose Source > Format from the menu.

Suspicious answered 9/5, 2014 at 18:14 Comment(5)
Which settings control the 'as-you-type' settings? I've changed everything I've found, maybe I missed something. I did Source > Format the whole file, saved it, restarted, etc. It's still not working..Nomadic
See the additions I made to my answer.Suspicious
Thanks for the screen. I found those "Typing" settings, and they're set appropriately. I reformatted my code and there are no tabs. It still doesn't work - but I'll give up for nowNomadic
Whew! Got it. Just as I was about to give up, here's a potential tip for anyone else who finds this: Go to General > Editors > File Associations to figure out which editors are enabled for various file types. I had an Aptana editor active, which had its own Tabs/Spaces setting. I changed literally 5-6 different Editor > Formatter settings before I found the right one.Nomadic
@Suspicious It looks like the Formatter settings do affects as-you-type formatting. Eclipse was auto indenting a new line when I hit return using a four-space tab. Once I modified the settings in the Formatter, Eclipse started indenting by two spaces. This is on Eclipse Neon. This should be rechecked as I was messing with a number of settings.Chitterlings
C
0

I've been struggling with this with my editing. I found out an interesting thing:

Eclipse editor is taking indentation character cues from the previous line!

If you go to the end of a line indented with the tab character and press enter to add code, it uses the tab to indent the new line. If the existing line was indented with spaces, it uses spaces. I have wasted man-hours of time every time I see times and check everywhere in IDE settings and preferences for a setting that can change this behavior without luck. I just happened to notice the behavior I described above. So the trick is, make sure the line above is indented with spaces!

(Edit:) Wow. I just noticed @E-riz posted an answer here about 10 years ago that says the same thing I said here. I'm going to go follow that link in his answer!

Cariole answered 25/1 at 21:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.