How can I get Eclipse to insert tabs instead of spaces for Java content assist?
Asked Answered
M

9

26

Is there any way to persuade Eclipse to use tabs instead of spaces for indenting its built-in Java content assist proposals (such as when creating an empty method which overrides a method in the parent class)?

The only setting I could find that looked relevant is in Windows/Preferences/General/Editors/Text Editors/Insert spaces for tabs, and this setting is unchecked.

(EDIT: Several posters have pointed out Java / Code Style / Formatter / Indentation / Tab policy, which is set to "Tabs only")

At the moment I have to remember to select them and re-format with <Ctrl>+<Shift>+<F>.

Mcgough answered 14/4, 2010 at 10:29 Comment(5)
But do you have an active Formatter with in its General settings the "Tab policy" set to "Spaces only" ? Check in Preferences / Java / Code Style / Formatter / button Edit.Rayraya
@Simon never mind: if the reformat works, your Formatter is good. I have added an answer referencing a bug which might be relevant to your issue.Rayraya
@VonC: Yes, and the "Tab policy" setting is set to "Tabs only"Mcgough
our comments have crossed. I have found a second bug still in progress. (see my updated answer)Rayraya
related: How do I change Eclipse to use spaces instead of tabs?.Mobilize
R
9

It seems to be currently a bug (not fixed in the 3.5 train):

bug 44755 (for comments, but also valid for other refactoring JDT operations)

Also linked to bug 231832, involving a bug in the AST rewrite...

I believe this is a problem with ASTRewrite, not with the client of ASTRewrite. We have to make sure that code formatter settings are available for the rewrite part.
Moving to JDT/Core for investigation.

Detected in late 3.4, still not fixed in 3.5

Rayraya answered 14/4, 2010 at 10:43 Comment(0)
B
26

I found the solution, thanks to Simon Nickerson's comment above, which is what the original poster was referring to in his edit. After having found the setting, I see what they are talking about. However, the way to it was given a few steps from the beginning of the path, so it fails to give a clear answer to people who are new to Eclipse or have been away from it for a few years, like me. so I'll give the full path here:

  1. Go to Project->Properties.
  2. In the resulting box, go to "Java Code Style"->Formatter.
  3. Click the "Configure Workspace Settings" link in the upper right of the box.
  4. Click the Edit button next to the name.
  5. Change "Tab policy:" drop down box to "Spaces only" because tabs are evil and inevitably result in poorly formatted code. >:-( This is also where you can actually affect the indentation size and tab size in your files.
  6. You will need to change the "Profile name" at the top of the box to save your changes.
  7. Thankfully, once you hit "OK", Eclipse will use your profile for your current project, so you're done after this step.

Now, what's really broken is the fact that these settings override the more up-front interface for changing similar-sounding settings in "Windows/Preferences/General/Editors/Text Editors/Insert spaces for tabs". To someone relatively new to the editor, this would seem like the place to set the default tabs = spaces setting. After finding the real answer, it's clear that this is just for general text editing, and language-specific settings need to be set up in the profiles for the languages at hand. This should be made a bit clearer for noobs, though. ;-)

Bib answered 13/10, 2011 at 3:18 Comment(3)
How is this an answer to the question? The OP wants to use tabs, but this answer details how to use spaces. And even if you choose "Tabs only" in point 5, this doesn't work anyway (see comment by OP: #2637089).Harve
@PeterJaric Do not ask bigots. Bigots are there only to probote their bigotry.Contusion
This is the answer I am looking for!Roanne
R
9

It seems to be currently a bug (not fixed in the 3.5 train):

bug 44755 (for comments, but also valid for other refactoring JDT operations)

Also linked to bug 231832, involving a bug in the AST rewrite...

I believe this is a problem with ASTRewrite, not with the client of ASTRewrite. We have to make sure that code formatter settings are available for the rewrite part.
Moving to JDT/Core for investigation.

Detected in late 3.4, still not fixed in 3.5

Rayraya answered 14/4, 2010 at 10:43 Comment(0)
S
3

Have a look in the preferences at Java / Code Style / Formatter
Create your own profile and set "Indentation" / "Tab policy" to "Tabs only"

Shaunta answered 14/4, 2010 at 10:33 Comment(0)
P
2

Preferences > Java > Code Style > Formatter > Edit > Indentation > Use tabs only for leading indentation checkbox

Paugh answered 14/4, 2010 at 10:34 Comment(2)
I don't see this exact setting, but the "Tab policy" setting in the same dialogue box is set to "Tabs only".Mcgough
I found that on Windows Eclipse 3.5.1Paugh
M
2

The way I have it setup is to enable the "Format source code" in the Editor->Save Actions preferences.

That way it reformats the source code on save.

Madaih answered 14/4, 2010 at 10:35 Comment(4)
Clever, but I don't want to do this as it would introduce too much noise into the source control diffs.Mcgough
OK here's another brainwave ( though it may be a bit tedious ) Goto java->Editor->Templates, edit each of the templates you use and fix them by pasting to an editor that can reformat tabs to spaces, and then paste back the fixed versions.Madaih
The template I'm most interested appears to be defined elsewhere (in Java / Code Style / Code Templates / Code / Method body), but these just give the body of the methods, and there is no indentation at all!Mcgough
@Simon: not surprising: as indicated by bug 231832 (mentioned in my answer), ASTRewrite has no access to Formatting preferences.Rayraya
P
2

I made sure that Edit>Smart Insert Mode was NOT checked, and that seemed to help me when configuration the indentation and formatting settings did not work. Running Eclipse 3.7.0.

Periodate answered 24/8, 2011 at 22:2 Comment(0)
M
1

I also had this problem. The issue turned out to be some configuration that was buried somewhere in my workspace. I created a new workspace and reimported my project and my tabs were not converted to spaces any more.

Mickelson answered 1/9, 2010 at 14:48 Comment(0)
G
1

Even after setting Insert spaces for tabs option in Text Editor preferences AND setting my Formatter profile Tab policy to Spaces only Eclipse was still inserting tab characters. Only after removing all the tabs from the file did it properly insert spaces for tabs. After discovering this, I determined the Insert spaces for tabs option in Text Editor preferences doesn't seem to have any effect, and it's the change to the Formatter profile Tab policy that achieves the desired results.

Gati answered 11/12, 2011 at 0:41 Comment(0)
A
0

Yet another solution that "worked for me" even if the other solutions didn't work (Version: 3.8.1):

Preferences > Java > Code Style > Formatter > Edit > Indentation > Use spaces to indent wrapped lines - Set unchecked

This is essentially the "smart tabs" option. It works for the formatter, but it incorrectly sets code generation into using spaces only. Annoyingly, the solution breaks the formatter.

Arlie answered 16/3, 2015 at 19:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.