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

Bug 355116

Summary: [formatter] Formatter and Indenter are not consistent
Product: [WebTools] JSDT Reporter: Bashir Sadjad <bashir>
Component: GeneralAssignee: Project Inbox <jsdt.javascript-inbox>
Status: NEW --- QA Contact: Chris Jaun <cmjaun>
Severity: normal    
Priority: P3 CC: bokowski, cmjaun, sam, simon_kaegi, tparker
Version: unspecifiedFlags: cmjaun: review? (cmjaun)
Target Milestone: Future   
Hardware: PC   
OS: Linux   
Whiteboard:
Attachments:
Description Flags
A patch to fix the problems listed on this bug report.
none
Same indenter fixes patch I have sent many months ago with just some white-space/unnecessary changes removed. none

Description Bashir Sadjad CLA 2011-08-18 12:20:56 EDT
In many cases the behavior of indenter and formatter are not consistent. For example with some formatter preferences tweaks, I can get 4 spaces before '[' in:

var t =
    [
      'a',
      'b'
    ];

which is what I want. However if I select this segment and just run indenter, it changes the indent to 2 spaces. I cannot find any JSDT settings that changes this indenter behavior. Debugging the code a little bit it seems that org.eclipse.wst.jsdt.internal.ui.text.JavaIndenter has a CorePrefs internal class with a prefAssignmentIndent field that determines this indent. However this indent size is simply set to prefBlockIndent() which is (eventually) hard-coded 1.

One consequence of this problem is that copy-pasting of a properly formatted block, corrupts the formatting.

I got this in JSDT core ver. 1.1.4.v201011172326 and also at the current HEAD.
Comment 1 Bashir Sadjad CLA 2011-09-08 11:42:57 EDT
Some other specific cases that indenter behavior is not consistent:

* Line breaks in array and object initializers do not honor all formatter settings.


* Line breaks while editing is different that running indenter afterwards, e.g., in

var a = {
  a1: 's',
  a2: 't'
};

hitting Enter after 't' put the cursor in wrong position.

* Line break after assignment in general, e.g.,

  var a =
  10;
Comment 2 Bashir Sadjad CLA 2011-09-08 16:39:21 EDT
Created attachment 203031 [details]
A patch to fix the problems listed on this bug report.

This patch addresses almost all of the issues that are listed on this bug report (plus some minor related other indent issues). I am not sure if there are any unit-tests for the indent functionality but if there is I would be happy to update them with regression tests for this patch.

Please note the patch is created from v201011172326 which is the plugin version that I want to eventually patch (internally in our org). However for the two files that have been modified they are exactly the same as HEAD so the patch can be applied both to branch and HEAD.
Comment 3 Boris Bokowski CLA 2011-10-04 09:51:30 EDT
Nitin, will you have time to review this patch some time soon?

Bashir, you could make it easier for Nitin to review your patch by getting rid of formatting changes. In the worst case, just go through the patch change by change and if it is a formatting or whitespace-only change, revert the change. Bonus points for contributing test cases as well :-)
Comment 4 Nitin Dahyabhai CLA 2011-10-04 16:05:07 EDT
(In reply to comment #3)
> Nitin, will you have time to review this patch some time soon?

Hopefully in time for M3.  I'm buried behind a lot of other demands for my attention.
Comment 5 Boris Bokowski CLA 2011-10-17 11:00:40 EDT
(In reply to comment #4)
> Hopefully in time for M3.  I'm buried behind a lot of other demands for my
> attention.

Thanks for responding. Are you the only one, or would it make sense to ask someone else familiar with the JSDT code base for a review?
Comment 6 Nitin Dahyabhai CLA 2011-11-29 02:43:31 EST
(In reply to comment #5)
No, no one's done extensive work on the formatter since it was initially forked from JSDT.  Will try for it in M4.
Comment 7 Bashir Sadjad CLA 2012-03-08 10:42:56 EST
Created attachment 212299 [details]
Same indenter fixes patch I have sent many months ago with just some white-space/unnecessary changes removed.

The new patch file is basically the same as the one I submitted long time ago with some white-space/unnecessary changes removed to reduce the size of the patch. It would be great if someone can review this patch and eventually commit it into JSDT code-base. It is a pain to maintain a JSDT feature patch internally for us and this patch really fixes several indenter problems, so everyone using JSDT can benefit from it.
Comment 8 Sam Hanes CLA 2012-09-12 10:11:14 EDT
I'm seeing another variation of this with array initializers. The formatter respects the value of the indentation policy (I use "Indent by one"), but the indenter always behaves as if it's set to "Indent on column". This is quite infuriating as the coding standards for my project specify a single-tab indent so I perpetually have to manually fix the indentation. 

I don't know whether Bashir's patch fixes this as I unfortunately don't have time to set up a custom build of JSDT.
Comment 9 Chris Jaun CLA 2013-10-29 17:40:31 EDT
Bashir, you need to sign the Contributor License Agreement for us to accept patches from you.

http://wiki.eclipse.org/Development_Resources/Contributing_via_Git#Eclipse_Foundation_Contributor_License_Agreement

You also need to sign off the on the Certificate of Origin by adding this line to a bugzilla comment, "This contribution complies with http://www.eclipse.org/legal/CoO.php"

More details here: http://wiki.eclipse.org/Development_Resources/Contributing_via_Git
Comment 10 Bashir Sadjad CLA 2014-01-21 10:21:15 EST
"This contribution complies with http://www.eclipse.org/legal/CoO.php"

I have also signed the CLA.