问题
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):

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

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)
回答1:
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).
- Start Eclipse
- Open Preferences/JavaScript/Code Style/Formatter
- Click 'New'
- 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'
- Close preferences and exit Eclipse
- Open following file with a text editor
workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.jsdt.core.prefs
- Find following line and replace 16 to 0
org.eclipse.wst.jsdt.core.formatter.alignment_for_selector_in_method_invocation=16
- Save it
- 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.
回答2:
The problem can be solved by accessing
Preferences > JavaScript > Code Style > Formatter
These are the steps:
- Create a new profile (since you cannot edit the builted-in one), if you haven't already, and click
Edit...
. - Open the
Line Wrapping
tab. - In the
Maximum line width
field, enter 9999. - Click
Apply
, andOk
.
The problem shows up again for code lines that have more than 9999 characters, but I can live with it.
回答3:
I've found a temporary solution:
Go to
Window -> Preferences -> JavaScript -> Code Style -> Formatter
Set the active profile to JavaScript conventions.
Click Edit
In the
Indentation
tab, setTab policy
to Spaces only.Set both
Indentation size
andTab size
to 3.As you cannot override the default profiles, change the profile name and save.
Press Ctrl+Shift+F and enjoy.
NOTE: I don't like having 3 spaces, I'd rather prefer my good old 4 spaces, but this is a temporary solution, until someone finds a better way to solve this. I just felt like sharing.
回答4:
Here's what I found: in "Line Wrapping > Function Calls" set "Indentation Policy" at the bottom to "Indent by one". This creates indentation without the wildly excessive spaces (or tabs). I haven't tried it with your exact code, but I've seen similar issues with mine.
来源:https://stackoverflow.com/questions/15157504/eclipse-javascript-formatter-crazy-i-e-too-much-padding