sponsor Vim development Vim logo Vim Book Ad

Simple Javascript Indenter : A simple javascript indent script, support OOP, jquery

 script karma  Rating 70/20, Downloaded by 5631  Comments, bugs, improvements  Vim wiki

created by
Miao Jiang
 
script type
indent
 
description
A simple indent plugin for javascript,

Repository: http://github.com/jiangmiao/simple-javascript-indenter
Screenshot: http://www.jiangmiao.org/blog/wp-content/uploads/2010/09/vim_simple_javascript_indenter_v1.4.1.png (with Brief Mode on)

==Options==
g:SimpleJsIndenter_BriefMode (default 0)
  set 1 to turn brief mode on.

g:SimpleJsIndenter_CaseIndentLevel (default 0)
  the case indent level in switch. the value could be -1, -0.5, 0.

===About Brief Mode===
Add 'let g:SimpleJsIndenter_BriefMode = 1' to ~/.vimrc to use brief mode.

In Brief Mode script will not indent more than one shiftwidth each line.
eg:
For a lot of people prefer set shiftwidth to 4, so in normal mode, it will indent looks like
each(function() {
........something(); // indent 2 shiftwidth
});
(((
............something(); // indent 3 shiftwidth
)));

In brief mode it will indent as
each(function() {
....something(); // indent 1 shiftwidth
});
(((
....something();
)))

===ATTENTION===
Be sure to always close the brackets with the same level as open, or the after code will not indent correctly.
eg:
function() {
....each(function() {
....} // should use }); instead of }
);    // already indented..
something();
}


== Indent Effects  ==
/*
* Comment Test
* function() {
*/
{
  /* a [{( */
  // b [{(
  comment('/* com', a /* [{( */); /* c */ // d
  ok();
  // Assign Test
  var a, /* { */
      b, // [
      c = '{'
  var a = "Hello" +
          "World"
  d = function() {
    aoeu
  }
  var k = function() {
    var k=3,
        m=4
  }

  var rurl = /{[('"/,
      r20 = /%20/g,

  a = 1,
  b = 2
  a = 1 +
      2 *
      3
}

// Function nested
;(function($) {
    $(document).ready(function() {
        $('#foo').click(function() {
            $.post(url, {
                a: 1,
                b: 2,
              }, function() {
                ok();
              }
            );
        });
    });
})(jQuery);

// Array Object Test
string_test("('",'("',"[",'{',"\"{","\'{");
{
  array_object_test: [
    1,
    2,
    {
      a: [3,4],
      b: [
        3,
        4
      ],
      c: string_test("('",'("',"[",'{',"\"{","\'{"),
      function_test: function() {
        return 0;
      },
      one_line_function_test: function() { return [1] }
    }
  ]
}

// One Line Test
{
  if( a == b &&
    c == d ||
    e == f)
  {
    ok();
  }
  if(a) {
    b;
  } else {
    e;
  }
  if(a)
    b;
  else if(k) {
    aeou
  } else
    c;
  while(true)
    foo += 1;
  try
    a;
  catch
    b;
  finally
    c;
  ok();
}
 
install details
copy indent/javascript.vim to ~/.vim/indent
Optional:
Add 'let g:SimpleJsIndenter_BriefMode = 1' to ~/.vimrc to use brief mode .
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
simple-javascript-indenter-v1.4.6.tar.gz 1.4.6 2011-10-18 7.0 Miao Jiang         New: return indent supported

                return a +
                       b +
                       c;

        Fix: failed indent for

                var a,
                    // ca
                    b,
                    /* cb */
                    c;
simple-javascript-indenter-v1.4.5.tar.gz 1.4.5 2011-10-13 7.0 Miao Jiang   Fix: float indent level is invalid in vim 7.3

  Fix: failed indent for
    var a = /reg/,
        c = 'bar'

  Optimize code
simple-javascript-indenter-v1.4.4.tar.gz 1.4.4 2011-10-13 7.0 Miao Jiang Fix: failed indent for
(a / b); // c (

Fix: failed indent for
'' + "'" + // ' (

Fix: failed indent in Brief Mode for
foo (a(b,
c)) {
aoeu
}
simple-javascript-indenter-v1.4.3.tar.gz 1.4.3 2011-07-24 7.0 Miao Jiang Fix: failed indent in brief mode, and optimize in normal mode for code
{
console.info((function() {
}).something);
next();
}
simple-javascript-indenter-v1.4.2.tar.gz 1.4.2 2011-05-17 7.0 Miao Jiang add option g:SimpleJsIndenter_CaseIndentLevel
simple-javascript-indenter-v1.4.1.tar.gz 1.4.1 2011-04-23 7.0 Miao Jiang Update: Indent current line when input } ) ]
Update: Change comment indentation style
simple-javascript-indenter-v1.4.0.tar.gz 1.4.0 2011-03-26 7.0 Miao Jiang Support switch...case .
simple-javascript-indenter-v1.3.4.tar.gz 1.3.4 2011-03-21 7.0 Miao Jiang Fix: failed indent for
function escapeattr(b) {
return b ? b.replace(/([\.#\[\]])/ig, "\\$1") : ""
}

https://github.com/jiangmiao/simple-javascript-indenter/issues#issue/1 (Thanks to whentp)
simple-javascript-indenter-v1.3.3.tar.gz 1.3.3 2011-01-08 7.0 Miao Jiang Fix Failed Indent for
test(/*a'*/b,'c') {
foo();
}
simple-javascript-indenter-v1.3.2.tar.gz 1.3.2 2011-01-07 7.0 Miao Jiang FIX: failed indent for
function a() {
$('"', {'b'}, function() {
});
}
simple-javascript-indenter-v1.3.1.tar.gz 1.3.1 2010-11-21 7.0 Miao Jiang Fix 3 format indent failed.
simple-javascript-indenter-v1.2.1.tar.gz 1.2.1 2010-09-08 7.0 Miao Jiang Support: indent for if, else, else if, while, try, catch, finally without brackets.
simple-javascript-indenter-v1.1.1.tar.gz 1.1.1 2010-09-06 7.0 Miao Jiang FIX: indent error for code
if( A ||
B &&
C)
{
}
simple-javascript-indenter-v1.0.1.tar.gz 1.0.1 2010-09-05 7.0 Miao Jiang Fix comment issue isn't solved after whole reindent if the first line is blank.
ip used for rating: 3.139.104.214

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github