Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse JavaScript formatter crazy (i.e: too much padding)

I'm having trouble to get eclipse format my JavaScript well. In this first example, it behaves as expected (not the length of the second url):

Behaving good

Whenever i have a longer url, eclipse goes crazy formatting my code and I get extra padding everywhere, like the example below:

Behaving bad

Why does the JavaScript formatter behaving like this, and how do I fix it?

(Just in case: I have not installed any formatter plugin, I just use the default that comes with Eclipse Juno)

like image 227
Twinone Avatar asked Mar 01 '13 12:03

Twinone


2 Answers

The problem can be solved by accessing

Preferences > JavaScript > Code Style > Formatter

These are the steps:

  1. Create a new profile (since you cannot edit the builted-in one), if you haven't already, and click Edit....
  2. Open the Line Wrapping tab.
  3. In the Maximum line width field, enter 9999.
  4. Click Apply, and Ok.

The problem shows up again for code lines that have more than 9999 characters, but I can live with it.

like image 162
Luca Fagioli Avatar answered Sep 21 '22 09:09

Luca Fagioli


The long url in the example affects "$.ajax(..." and "$(function()...".

As for "$(function()...", the setting is in Preferences/JavaScript/Code Style/Formatter/Line Wrapping/Function Calls/Arguments. 'Do not wrap' is available.

As for "$.ajax(...", it is similar to 'Qualified invocation' in the Java formatter.(Preferences/Java/Code Style/Formatter/Line Wrapping/Function Calls) Unfortunately, JSDT has no UI for this.(Preferences/JavaScript/Code Style/Formatter/Line Wrapping/Function Calls)

This is a trick for JSDT 1.4.1(WTP R3.4.1).

  1. Start Eclipse
  2. Open Preferences/JavaScript/Code Style/Formatter
  3. Click 'New'
  4. Input profile name as you like, select 'Eclipse [built-in]'(or JavaScript Conventions) in listbox, uncheck 'Open the edit dialog now'(no need to edit), and click 'OK'
  5. Close preferences and exit Eclipse
  6. Open following file with a text editor
    workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.jsdt.core.prefs
  7. Find following line and replace 16 to 0
    org.eclipse.wst.jsdt.core.formatter.alignment_for_selector_in_method_invocation=16
  8. Save it
  9. Start Eclipse

(since you have your own profile, maybe 1-5 are unnecessary)

This hack will be reverted when you edited formatter settings in preferences. - (To prevent overwriting, all you need to do is export you formatter profile, edit the XML and change the 'org.eclipse.wst.jsdt.core.formatter.alignment_for_selector_in_method_invocation' to 0. Save and reimport again now. This change is permanent.

I don't think this satisfies your needs, it's better to use some other formatter(3rd party plugin), I guess.

like image 21
atlanto Avatar answered Sep 25 '22 09:09

atlanto