tags:

views:

252

answers:

4

I have this bit of JavaScript...

 15   $('.ajax_edit_address').each(function() {
 16     $(this).ajaxForm({
 17       target: $(this).parents('table.address').find('tr.address_header').children(':first'),
 18       success: function(response) {
 19         $('input, select, textarea', '.ajax_edit_address').removeClass('updating');
 20       }
 21     });
 22   });

That's formatted the way I like it. But let's say I had just finished typing something and I wanted to tidy it up. So I run the Vim code formatter on it...

=7j

The result is...

 15   $('.ajax_edit_address').each(function() {
 16       $(this).ajaxForm({
 17 target: $(this).parents('table.address').find('tr.address_header').children(':first'),
 18 success: function(response) {
 19 $('input, select, textarea', '.ajax_edit_address').removeClass('updating');
 20 }     
 21 }); 
 22       });

Vim seems to have trouble with functions as method arguments.

Here is what I think is the relevant part of my .vimrc...

:set cindent shiftwidth=2

" indent depends on filetype
:filetype indent on

:filetype plugin on

Is there something else that needs to be installed or configured to format JS code?

+1  A: 

Unfortunately, 'cindent' just isn't going to do the job since it's is very much tied to C syntax. Since all the default indent script for javascript does is turn on 'cindent', that's not much help. It even says so in the script!

" Maintainer: None! Wanna improve this?

I don't do anything other than really basic javascript so I've never bothered trying to find anything better. From a quick look on vim.org, this script looks like it may be worth a shot. It's newer, so it probably takes into account the more complex javascript that's used now days.

jamessan
I just tried it out; it's not perfect but it's vastly better than the default script. Thanks!
Brandon Thomson
A: 

That script is a little strange... it will turn:

var x = 1 +
  2 +
  3 +
  4 +
  5 +
  6 +
  7;

into

var x = 1 +
  2 +
    3 +
      4 +
        5 +
          6 +
            7;

There are some other bizarre cases too, so don't run it on an entire file...

Brandon Thomson
A: 

The biggest issue seems to be the cindent doesn't recognize this type of syntax:

test({
  var b = 2;
}); 
It will turn it into this:
test({
    var b = 2;
    }); 

If you handle that case I'd imagine the indent wouldn't be so awful for the jquery syntax. But this would require you writing a custom javascript indent file. Also, you'd have to edit the html indent file to not use cindent for script tags with javascript content.

I don't think anyone has successfully created a jquery/prototype compatible indent file for javascript. The existing javascript indent scripts are all flawed.

Pierre-Antoine LaFayette
The funny thing is, the script jamessan linked seems to handle this case correctly, but for the even easier code (above) it screws it all up...
Brandon Thomson
A: 

VIM plugin Jsbeautify could handle jQuery correctly. It's the vim plugin version of the online Jsbeautify.

Jichao