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?
The biggest issue seems to be the cindent doesn’t recognize this type of syntax:
It will turn it into this:
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.