Eclipse Javascript formatter (ctrl-shift-f)

Jeff picture Jeff · Oct 23, 2012 · Viewed 26.8k times · Source

I don't like asking this question but I have honestly been looking for a way to figure this out for a very long time.

This is a simple question. I have been using NetBeans for a long time, but recently made the switch to Eclipse for a few reasons I won't get into. Anyway, I find the javascript source formatting behavior to be very odd.

Here is an example block of javascript code that I formatted myself:

function buildDatabase () {
    db.transaction(function (tx) {
        tx.executeSql('DROP TABLE IF EXISTS calendar');
        tx.executeSql('CREATE TABLE IF NOT EXISTS calendar(id UNIQUE, summary, description, location, startdate, enddate)');
    }, function (err) {
        document.querySelector('#debugLog').innerHTML += '<p><code>' + err.message + '</code></p>';
    });
}

very simple stuff. Now, if I hit Ctrl+Shift+F to format it (because frankly this feature is a massive time saver) for reasons I can't understand it will now look like this:

function buildDatabase() {
    db
            .transaction(
                    function(tx) {
                        tx.executeSql('DROP TABLE IF EXISTS calendar');
                        tx
                                .executeSql('CREATE TABLE IF NOT EXISTS calendar(id UNIQUE, summary, description, location, startdate, enddate)');
                    },
                    function(err) {
                        document.querySelector('#debugLog').innerHTML += '<p><code>'
                                + err.message + '</code></p>';
                    });
}

Why does it want to move .transaction to a new line? It doesn't get anywhere near the line width of 80. It is really messy looking. It is doing the same with the second .executeSql as well, and it still doesn't wrap properly. It's just very odd.

Now of course I am aware of Window > Preferences > JavaScript > Code Style but no matter what I seem to do in here fixes this. I am hoping someone has more experience with Eclipse to know what is going wrong.

I have the Android Dev Tools installed and PDT. However this behavior pre-exists the installation of these plugins.

Any help would be appreciated, thank you.

Answer

Luca Fagioli picture Luca Fagioli · Apr 24, 2013

The problem can be solved by accessing

Preferences > JavaScript > Code Style > Formatter

Proceed:

  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 that.