Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 357022

Summary: [formatter] formatting nested array initializers
Product: [Eclipse Project] JDT Reporter: Steve Kelem <steve>
Component: CoreAssignee: JDT Core Triaged <jdt-core-triaged>
Status: NEW --- QA Contact: Ayushman Jain <amj87.iitr>
Severity: enhancement    
Priority: P3 CC: amj87.iitr, frederic_fusier, markus.kell.r
Version: 3.7   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
text showing initial layout of nested array initializer
none
text showing increasing indentation done by existing formatter
none
text showing nested 2nd-level initializers none

Description Steve Kelem CLA 2011-09-07 19:52:45 EDT
The Java Code Style's Formatter's settings for Array Initializers need another style to handle nested array initializers.
The current options are:
Do not wrap
Wrap where necessary
Wrap first element, others where necessary
Wrap all elements, every element on a new line
Wrap all elements, indent all but the first element
Wrap all elements, except first element if not necessary

I have lots of data for modeling aspects of a computer chip.
(These are the unit tests. The thorough testing is done elsewhere.)
The initializers are nested arrays of integers and look like:
	final int[][][] tests = {
		{ { 0, 0, 1, 0x8000 }, { 0xffff, -126, Actor.SUBNORM_STAT_FIELD } }, // 5.
									   // Smallest negative subnormal number
		{ { 0, 0, 0x0000, 0x0040 }, { 0x2000, -126, Actor.SUBNORM_STAT_FIELD } }, // 6.
									   // "Middle" positive subnormal number
		};
The formatting gets messed up every time I format the file.  I've tried all the options, and none
are the "right" one, as shown above.
With one of the options, every new line gets indented a little bit more:
	final int[][][] tests = {
		{ { 0, 0, 1, 0x8000 }, { 0xffff, -126, Actor.SUBNORM_STAT_FIELD } }, // 5.
									   // Smallest negative subnormal number
			{ { 0, 0, 0x0000, 0x0040 }, { 0x2000, -126, Actor.SUBNORM_STAT_FIELD } }, // 6.
									   // "Middle" positive subnormal number
				};
I need something that keeps each of the outer index at the same indentation, as show at the top.
If necessary, it could wrap the 2nd-level items, at which time I'd want the closing braces to line up so
you could easily see the extent of each top-level entry:
	final int[][][] tests = {
		{ { 0, 0, 1, 0x8000 },
		   { 0xffff, -126, Actor.SUBNORM_STAT_FIELD }
		}, // 5. Smallest negative subnormal number
		{ { 0, 0, 0x0000, 0x0040 },
		   { 0x2000, -126, Actor.SUBNORM_STAT_FIELD }
		}, // 6. "Middle" positive subnormal number
	};


-- Configuration Details --
Product: Eclipse 1.4.0.20110609-1120 (org.eclipse.epp.package.jee.product)
Installed Features:
 org.eclipse.jdt 3.7.0.v20110520-0800-7z8gFchFMTdFYKuLqBLqRja9B15B
Comment 1 Steve Kelem CLA 2011-09-07 19:53:29 EDT
Great.  All the formatting I did was undone!
Comment 2 Steve Kelem CLA 2011-09-07 19:57:09 EDT
Created attachment 202945 [details]
text showing initial layout of nested array initializer
Comment 3 Steve Kelem CLA 2011-09-07 19:58:02 EDT
Created attachment 202946 [details]
text showing increasing indentation done by existing formatter
Comment 4 Steve Kelem CLA 2011-09-07 19:58:41 EDT
Created attachment 202947 [details]
text showing nested 2nd-level initializers
Comment 5 Markus Keller CLA 2011-09-08 06:00:38 EDT
Moving to JDT Core to investigate. Sounds like this is more about the "Indentation policy" rather than the "Line wrapping policy".

Steve: To keep control over line breaks, you may want to enable the "Never join already wrapped lines" checkbox in the "General settings", and then do the wrapping manually.