Fix for wrong indentation of JavaScript code by cindent

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Fix for wrong indentation of JavaScript code by cindent

Hari G-2
Hi,

Vim uses cindent for JS files which works in general nicely. There are
a few cases where this does not work correctly. F.e.

var a = {
    b: 1,
    c: "hello"
}

cindent would align b: 1 to col 0 and indent c: "hello" by shiftwidth.
This is because of how it treats labels in C. The attached fix avoids
this by using another 'cindentoption' value 'J' so that such lines
could be treated differently if the file is a JS file. This also sets
the 'j' option used by Java files in order to handle anonymous
functions in JS.  Further it changes how a 'case' label is determined,
to suit JS also.

I've tested this fix with JS & C source code and it seems working for me.

Review is appreciated,

/harig

--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

vim-jsindent.diff (10K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Fix for wrong indentation of JavaScript code by cindent

Bram Moolenaar

Hari Kumar wrote:

> Vim uses cindent for JS files which works in general nicely. There are
> a few cases where this does not work correctly. F.e.
>
> var a = {
>     b: 1,
>     c: "hello"
> }
>
> cindent would align b: 1 to col 0 and indent c: "hello" by shiftwidth.
> This is because of how it treats labels in C. The attached fix avoids
> this by using another 'cindentoption' value 'J' so that such lines
> could be treated differently if the file is a JS file. This also sets
> the 'j' option used by Java files in order to handle anonymous
> functions in JS.  Further it changes how a 'case' label is determined,
> to suit JS also.
>
> I've tested this fix with JS & C source code and it seems working for me.
>
> Review is appreciated,

Good to see this patch.

Since you know about this stuff, can you become the maintainer of the
javascript indent plugin?  If so then please update the header of the
file.

Instead of changing all calls to cin_iscase(), it's easier to rename
cin_iscase() to cin_iscase_js(), add the extra argument, and create a
new cin_iscase() that calls cin_iscase_js(arg, FALSE).

--
From "know your smileys":
 O:-) Saint

 /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\        download, build and distribute -- http://www.A-A-P.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

RE: Fix for wrong indentation of JavaScript code by cindent

JohnBeckett
In reply to this post by Hari G-2
Hari Kumar G wrote:

> Vim uses cindent for JS files which works in general nicely.
> There are a few cases where this does not work correctly. F.e.
>
> var a = {
>     b: 1,
>     c: "hello"
> }
>
> cindent would align b: 1 to col 0 and indent c: "hello" by
> shiftwidth. This is because of how it treats labels in C.
> The attached fix avoids this by using another 'cindentoption'
> value 'J' so that such lines could be treated differently if
> the file is a JS file.

Please review this recently-added tip:
http://vim.wikia.com/wiki/Improved_Javascript_Indent_script_for_Vim

It would be great if you were to determine what works best
(combining your script with the above) and take Bram's
suggestion to become the maintainer for the Javascript indent
file distributed with Vim. If you make a decision, whether 'yes'
or 'no', I would like to hear about it because we would not want
to keep a tip that was simply an indent script distributed with
Vim.

John

--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Fix for wrong indentation of JavaScript code by cindent

Hari G-2
In reply to this post by Bram Moolenaar
Bram Moolenaar wrote:

> Hari Kumar wrote:
>
> > Vim uses cindent for JS files which works in general nicely. There are
> > a few cases where this does not work correctly. F.e.
> >
> > var a = {
> >     b: 1,
> >     c: "hello"
> > }
> >
> > cindent would align b: 1 to col 0 and indent c: "hello" by shiftwidth.
> > This is because of how it treats labels in C. The attached fix avoids
> > this by using another 'cindentoption' value 'J' so that such lines
> > could be treated differently if the file is a JS file. This also sets
> > the 'j' option used by Java files in order to handle anonymous
> > functions in JS.  Further it changes how a 'case' label is determined,
> > to suit JS also.
> >
> > I've tested this fix with JS & C source code and it seems working for me.
> >
> > Review is appreciated,
>
> Good to see this patch.
>
Thanks for taking a look.

> Since you know about this stuff, can you become the maintainer of the
> javascript indent plugin?  If so then please update the header of the
> file.
>
My vim script skills aren't any good :) What I want to try is to fix
the indenting issues in the cindent code itself.

I found one more case where the indenting is wrong. The JS code is:

window.addEventListener("load", function()
   {
         if (1)
         {
            // do something
         }
         else
         {
            // something else
         }
   },
false);

The 'j' flag in 'cino' helps a bit but the inner 'if' block will be
incorrectly indented. I tried to find the cause but without much
success. All I could figure out was that it was using the line with
the open parenthesis as the scope and then somehow gets confused with
the brace following the if.. and would indent it on col 0. It would be
great if this issue could be fixed.

> Instead of changing all calls to cin_iscase(), it's easier to rename
> cin_iscase() to cin_iscase_js(), add the extra argument, and create a
> new cin_iscase() that calls cin_iscase_js(arg, FALSE).
>
Right, but wouldn't that create an overhead of an additional function
call? (Maybe I am not understanding the suggestion correctly)

/harig

--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php