Bug 166962 - Formatter introduced spaces instead of tabs
Summary: Formatter introduced spaces instead of tabs
Status: RESOLVED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2.1   Edit
Hardware: PC Windows 2000
: P3 minor (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: needinfo
Depends on:
Blocks:
 
Reported: 2006-12-06 10:05 EST by Osvaldo Pinali Doederlein CLA
Modified: 2009-08-30 02:09 EDT (History)
1 user (show)

See Also:


Attachments
Test project and formatter settings (5.85 KB, application/octet-stream)
2006-12-14 08:51 EST, Osvaldo Pinali Doederlein CLA
no flags Details
screenshot (8.19 KB, image/pjpeg)
2006-12-22 11:23 EST, Martin Aeschlimann CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Osvaldo Pinali Doederlein CLA 2006-12-06 10:05:07 EST
Build ID: M20060921-0945

Steps To Reproduce:
Process this source code, for example:

public class Test
{
	public static void main (String[] args)
	{
		int x = 0, y = 0;
		
		if (0 == 0)
		{
			if (1 == 1)
				x = y;
			else
				y = x;
		}
	}
}

I used physical tabs for indentation and I have this configured in my formatting settings: Tab policy = tabs only, Use tabs only for leading indentations = checked, Tab size = 4. In the Cleanup wizard, I asked to remove braces for single-line statements, so I got this code:

public class Test
{
	public static void main (final String[] args)
	{
		int x = 0, y = 0;
		
		if (0 == 0) if (1 == 1)
        	x = y;
        else
        	y = x;
	}
}

Notice that in three of the four rewritten lines, physical tabs were replaced by the equivalent number of spaces (4). (Here in Bugzilla the indentation should be appear wrong because the browser's tab is 8 spaces.) This is pretty annoying, I have to reformat after cleaning up. In short, the wizard is not honoring this formatting option at all.
Comment 1 Olivier Thomann CLA 2006-12-06 10:10:35 EST
Move to JDT/UI
Comment 2 Martin Aeschlimann CLA 2006-12-14 06:43:27 EST
I'm afraid I can't reproduce this. I think this must be a combination of formatter settings, for example 'Keep simple ten tstaements on one line'.

Could you export you formatter profile and attcj it here. I'd be also happy if you could attach your code example here. It is essential that you really have tabs before the first if.
Comment 3 Osvaldo Pinali Doederlein CLA 2006-12-14 08:51:10 EST
Created attachment 55667 [details]
Test project and formatter settings
Comment 4 Osvaldo Pinali Doederlein CLA 2006-12-14 08:57:59 EST
Ok, there goes the test project. It includes an XML with the exported Formatter Profile. Since Eclipse 3.2.1 can't export the CleanUp setting, they are the following (only the checked options):

- Code Style:
Use blocks in  if etc. / Always
Use modifier final where possible / Parameters and Locals
- Member Accesses:
Use declaring class as qualifier / Change all accesses through instances
- Unnecessary code:
Remove unused imports
Remove unused local variables
Remove unnecessary casts
Remove unnecessary NON-NLS tags
- Missing code: (nothing)

In the Preview window you will already see the replacement of most hard tabs (not all though) by chars, inside the outer if() block.
Comment 5 Osvaldo Pinali Doederlein CLA 2006-12-14 09:32:56 EST
I performed additional tests. First, I loaded the project into Rational Application Developer 7.0, which is based on Eclipse 3.2.1. Guess what, the bug doesn't happen!! But it surely happens consistently in my "pure" Eclipse 3.2.1 setup. I installed RAD with its own Eclipse runtime, didn't use the option to hook on an existing Eclipse base. My RAD installation is brand new (I'm just evaluating it), so it's less polluted by plugins and updates.

Next, I installed a new minimal Eclipse setup (just Platform + JDT), created a brand new workspace, imported the project and the formatter settings (not changing any other setting), and fired the Cleanup Wizard with the listed options. The bug is there.
Comment 6 Osvaldo Pinali Doederlein CLA 2006-12-14 09:45:48 EST
One last bit of information.

I run Eclipse on top of Sun JDK. Right now I'm using 6.0 (final release), before I was on 6.0 betas and before that, 5.0. (I noticed this bug since Eclipse 3.2.0 milestones, IIRC.) But RAD 7.0 obviously runs on top of IBM's JVM, so a VM compatibility issue is not impossible. Perhaps the bug is related to some bug that exists in Sun JDK but not in IBM's.
Comment 7 Martin Aeschlimann CLA 2006-12-22 11:22:36 EST
Thanks for the good reproducable steps. I now see what you mean. You actually don't need your profile, but the bug also shows up with the default settings

Simplified steps:
- create a new project and make sure its formatter settings are set to 'tab only'
- create the following file (only uses tabs)

package test;
public class Test
{
	public static void main (String[] args)
	{
		if (1 == 1)
			x = y;
		else
			y = x;
	}
}

- on 'if' invoke quick fix and choose 'change if statement to block'
- see resulting screenshot

- I debugged the problem down to the formatter
   - set a breakpoint in ASTRewriteFormatter.formatString()
   - verify that
    this.options.get(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR)
     still returns 'TAB'
   - result introduces spaces
Comment 8 Martin Aeschlimann CLA 2006-12-22 11:23:38 EST
Created attachment 56099 [details]
screenshot
Comment 9 Olivier Thomann CLA 2006-12-22 15:20:22 EST
What happens if you unchecked using tab only for leading indentations?
Comment 10 Olivier Thomann CLA 2006-12-22 15:52:45 EST
So far I got only tabs using the code formatter only.
I'll try using the cleanup wizard.
Comment 11 Olivier Thomann CLA 2006-12-22 16:02:32 EST
Added regression test org.eclipse.jdt.core.tests.formatter.FormatterRegressionTests#test642.
Comment 12 Olivier Thomann CLA 2007-02-02 10:33:28 EST
(In reply to comment #7)
> Simplified steps:
> - create a new project and make sure its formatter settings are set to 'tab
> only'
> - create the following file (only uses tabs)
> ....
> - on 'if' invoke quick fix and choose 'change if statement to block'
> - see resulting screenshot
I followed your steps using I20070130-0800 and I could not reproduce.
Does your file contain spaces after its creation ?
Comment 13 Olivier Thomann CLA 2007-02-14 18:59:21 EST
Closing as REMIND.
Comment 14 Denis Roy CLA 2009-08-30 02:09:23 EDT
As of now 'LATER' and 'REMIND' resolutions are no longer supported.
Please reopen this bug if it is still valid for you.