colhountech / js2-mode

Automatically exported from code.google.com/p/js2-mode
0 stars 0 forks source link

Anonymous function body indentation. #94

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I spend most of my entire day, every day inside js2-mode, so thank you!

One big problem for me, however, is the way it indents anonymous functions.
 My preferred style is as follows:

setTimeout(function() {
  // <-- function body starts here.
  // ...
});

js2-mode, however, seems to want the function to look like:

setTimeout(function() {
             // <-- js2 indents to here.
           });

The worst thing is that even when I indent the first line to the preferred
indentation level, all subsequent enter keystrokes bring the indentation
level back to js2-mode's preferred level.  (I noticed a separate issue was
filed proposing that enter indent to the previous line's level, which I
wholeheartedly support).

I'm using js2mode version 20080616.

Aaron

Original issue reported on code.google.com by aaron...@gmail.com on 10 Nov 2008 at 2:09

GoogleCodeExporter commented 8 years ago
I just thought I'd add that this is a frequent annoyance with small page 
scripts. If
you're using jQuery you often stick everything into an anonymous function like 
this:

$(function () {
    // define a couple of functions
    function foo(...) { 
        ...
    }

    // bind functions to events
    $(".hello").click(foo);
});

Here js2-mode wants to indent to 6 instead of 4 inside the $();, so it's really
inconsistent with code written with the Karl's Javascript mode.

Original comment by olau%iol...@gtempaccount.com on 28 Sep 2009 at 2:33

GoogleCodeExporter commented 8 years ago
I 3rd this. I stopped using js2-mode because of this since most of the 
javascript I
write uses a lot of anonymous functions.

Original comment by lau.laur...@gmail.com on 28 Oct 2009 at 3:42

GoogleCodeExporter commented 8 years ago
This appears to have been fixed in the latest versions (I am using 20090723b). 
But
the fix is not complete.

The simple case reported above is fixed:

  something(function() {
    another():
  });

However if there are previous parameters to the function call and they have been
split over more than one line then the behaviour is as reported:

  something("foo",
            function() {
            indentsToHere();
  });

Note that putting all the parameters on the same line gives the requested 
behaviour:

  something("foo", function() {
    asDesired();
  });

That putting the function on the same line as an earlier parameter (but having
earlier parameters split over lines) give the undesirable indentation:

  something("foo",
            "bar", function() {
            booBoo();
  });

And that this is also true if the thing that is split over two lines is an 
expression
rather than multiple parameters (like a split string):

  something("foo and "
            + "bar", function() {
            goesHere();
  });

[And this is actually the case I care about. I would like to split Screw.Unit 
specs
with long text like so:

it("should allow me to split my very long spec descriptions so that " +
   "they are readable and indent like a single block", function() {
  expect(indentsToHere()).to(be_true);
});

]

Original comment by ben.butl...@gmail.com on 9 Nov 2009 at 3:00

GoogleCodeExporter commented 8 years ago
people like js2-mode but  not  quite content with its indentation should  
really check this out:

http://mihai.bazon.net/projects/editing-javascript-with-emacs-js2-mode

it not only fixes the problems listed above, but also fix indentation with:

var a,
      b = function(){
      },
      c;

Original comment by darren....@gmail.com on 11 Oct 2010 at 6:38

GoogleCodeExporter commented 8 years ago
Is there any fix available?

Original comment by qwigly...@gmail.com on 8 Oct 2012 at 6:59