views:

110

answers:

4

I'm trying to get VIM to indent Javascript with the '=' and related commands. When I try to auto indent the following, for example:

   new function($) {
     $.fn.setCursorPosition = function(pos) { 
       if ($(this).setSelectionRange) {
         $(this).setSelectionRange(pos, pos);
       } else if ($(this).createTextRange) {
         var range = $(this).createTextRange();
         range.collapse(true);
         range.moveEnd('character', pos);
         range.moveStart('character', pos);
         range.select();
       }

The result is the rather absurd:

       new function($) {
       $.fn.setCursorPosition = function(pos) {
       if ($(this).setSelectionRange) {
       $(this).setSelectionRange(pos, pos);
       } else if ($(this).createTextRange) {
       var range = $(this).createTextRange();
       range.collapse(true);
       range.moveEnd('character', pos);
       range.moveStart('character', pos);
       range.select();
       }

I've set set syntax=javascript, and I've set filetype to:

filetype detection:ON  plugin:ON  indent:ON

Though I've tried every permutation of this. I've tried every permutation of smartindent, autoindent, and cindent, but nothing seems to have the correct effect of giving Vim the expected indentation. I've set tabstop=4.

I've installed javascript.vim, and IndentAnything, though they don't seem to have any effect.

I'd be very grateful for any suggestions as to how to get Vim indenting properly in Javascript.

Thank you.

Brian

+1  A: 

Adding the two closing braces and selecting the entire block with vi{ provided proper automatic indentation for me in gvim 7.2 with no plugins. You may want to see if an errant plugin is messing it up by starting vim with the --noplugins flag on the command line. and try again.

sleepynate
+1  A: 

I hate to say something unhelpful like "It works for me", but it does. Even with nothing in my .vimrc and all plugins off, I get the correct indentation.

new function($) {
    $.fn.setCursorPosition = function(pos) { 
        if ($(this).setSelectionRange) {
            $(this).setSelectionRange(pos, pos);
        } else if ($(this).createTextRange) {
            var range = $(this).createTextRange();
            range.collapse(true);
            range.moveEnd('character', pos);
            range.moveStart('character', pos);
            range.select();
        }

Have you tried loading vim with the --noplugins switch and temporarily moving/renaming your .vimrc to see if it still doesn't work? I suspect another setting in your .vimrc or another plugin may be causing the conflict.

michaelmichael
+1  A: 

For me it works (not very helpful statement, I know ;-) ). I suppose that the filetype is not detected correctly.

What does

 :set filetype

say? It should report "javascript".

[EDIT] Hint: Please note that there is an option called 'filetype' and a command called :filetype. To get help for the option do :help 'filetype' for the command do :help :filetype.

Habi
@Habi: Thanks. It'd be a shame to have missed that, but alas `:set filetype` is `javascript`. :)
Brian M. Hunt
@Brian M. Hunt: Hmmm, maybe the indentation is 0 (I don't know if this is possible at all, but if it is possible it could be a reason for your problem). What does ":set shiftwidth" and ":set cinoptions" say?
Habi
@Habi: `:set shiftwidth` is `shiftwidth=4` and `set cinoptions` is `cinoptions=`. hrmm.
Brian M. Hunt
@Brian M. Hunt: Same here. Strange. Does the '=' work for other languages, for example C?
Habi
@Brian M. Hunt: Another idea: What does ':set equalprg" say? Its value should be "" by default.
Habi
Aye, it's `:set equalprg` returns `equalprg=`.
Brian M. Hunt
+1  A: 

I was having problems the other day with MacVim 7.2 and a Lua file that wouldn't indent correctly -- even after using set syntax, set filetype and filetype indent on, it wasn't indenting the file correctly.

I discovered adding:

filetype plugin indent on

to my .gvimrc file solved the issue, at least for me. YMMV.

Blair Holloway
Brian M. Hunt