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

Bug 369785

Summary: [formatter] Space after opening bracket in for loop
Product: [WebTools] JSDT Reporter: Victor Homyakov <vkhomyackov>
Component: GeneralAssignee: Michael Rennie <Michael_Rennie>
Status: RESOLVED FIXED QA Contact: Chris Jaun <cmjaun>
Severity: normal    
Priority: P3 CC: bugs.eclipse.org, D.Wittner, jpadilla, kovuthehusky, lucioj, lupo.bugzilla, mesko.balazs, skyh, starwhisperer
Version: unspecifiedFlags: cmjaun: review+
Target Milestone: 3.5.1   
Hardware: PC   
OS: Linux   
Whiteboard:
Attachments:
Description Flags
Patch to fix bug 369785
none
alternate patch none

Description Victor Homyakov CLA 2012-01-26 05:53:00 EST
Example:

for (var i = 0;; ++i) {
  // empty
}

After formatting:

for ( var i = 0;; ++i) {
  // empty
}

- formatter inserts extra space after opening bracket before "var".
Comment 1 Nitin Dahyabhai CLA 2012-01-28 13:57:13 EST
There's a formatter option controlling this, you're certain it's not set to do this intentionally?
Comment 2 Victor Homyakov CLA 2012-01-31 08:33:15 EST
(In reply to comment #1)
JS formatter profile - tab "White Space":

when "Sort options by Syntax element" is selected - "After opening parenthesis" - everything is unchecked;

when "Sort options by Java element" is selected - "Control statements" - 'for' - checked options are: before opening parenthesis, after comma in initialization, after comma in increments, after semicolon.
Comment 3 Andrey Ivlev CLA 2012-04-04 03:59:14 EDT
I have same problem. Also formatter works fine without var keyword

for(i=0;i<10;i++) -> for (i = 0; i < 10; i++)

but!

for(var i=0;i<10;i++) -> for ( var i = 0; i < 10; i++)

It's very annoying
Comment 4 Lupo L CLA 2012-05-06 08:37:58 EDT
Created attachment 215140 [details]
Patch to fix bug 369785

The second parameter to printNextToken(), named considerSpaceIfAny, does _not_ mean "add a space if needed", but "add a space if none has been inserted yet".
Comment 5 Victor Homyakov CLA 2012-07-06 04:36:58 EDT
Hey, will someone fix it? Bug is still not fixed in Web Tools 3.4.0 (Eclipse Java EE IDE for Web Developers. Version: Juno Release Build id: 20120322-1740).
Comment 6 Victor Homyakov CLA 2013-04-05 07:00:54 EDT
Is any eclipse.org developer "eating his own dog food" (i.e. using JSDT)? It has annoying bugs in source formatter, and they are not fixed for years.
Comment 7 Andrey Ivlev CLA 2013-04-05 07:29:22 EDT
One year and three months of silence. Thank you, Lupo! But I can't apply your patch. I dont know how to do it.
Comment 8 Kevin Breslin CLA 2013-04-30 19:32:28 EDT
When will this fix be added to production? It's quite bothersome.
Comment 9 Michael Rennie CLA 2013-05-02 14:58:08 EDT
Created attachment 230424 [details]
alternate patch

Here is an alternate patch.

The other patch does fix the issue, but in the case of the first statement following the opening parenthesis of the for loop we should be honouring the "insert_space_after_opening_paren_in_for" formatter setting and not just hard coding what to do for var declarations in general.

Its too late to get this in for 3.5, but we can definitely target this for 3.6 / 3.5.1.
Comment 10 Michael Rennie CLA 2013-05-02 15:08:33 EDT
*** Bug 357457 has been marked as a duplicate of this bug. ***
Comment 11 Wang Jialiang CLA 2013-05-02 15:18:06 EDT
This is really good news!
I'm glad we have a timeline for the fix.

Hi Michael, I wonder if you guys have any plans for the following issue:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=353546

which seems to be more serious than this one as most of the complex JavaScript source files in our project are messed up after reformatting.

This issue was initially spotted by us since 2008, it would be really helpful if it could catch some attention.

Thanks a lot.
Comment 12 Jorge Padilla CLA 2013-06-19 14:47:03 EDT
Chris, I have reviewed this patch and validated it resolves this problem.  In my opinion the "alternate patch" is a more completed patch, so I recommend it to be included in 3.5.1.  Could you officially complete this review?
Comment 13 Chris Jaun CLA 2013-08-14 15:50:50 EDT
Pushed to 3.5.1 and master.
Comment 14 James Fry CLA 2013-10-14 10:25:32 EDT
Should this be in WTP 3.5.1 (and hence Kepler SR1 / 4.3.1)?
Comment 15 Wang Jialiang CLA 2013-10-18 17:16:44 EDT
It seems this issue is only fixed for the regular for loop.
In case of for each loop, there's still an extra space in front of the var keyword.
Comment 16 Balazs Mesko CLA 2019-03-12 08:51:46 EDT
Comment #15 is still correct, the enhanced for loop is still bugged.

for (var i = 0; i < list.length; i++) {
    // ...
}

works fine, but for in loop is still formatted as such:

for ( var elem in list) {
    // ...
}

Tested with Eclipse Photon (4.8.0), Build id: 20180619-1200.