Home Download Buy Blog Forum Support

JS Indentation

JS Indentation

Postby mcavage on Sun Jan 15, 2012 7:40 pm

Hello, I just grabbed the demo to try out, and I can't figure out how to tweak this annoying thing I keep seeing. If I have a block like:

Code: Select all
return foo(function(err) {
    if (err)
      return callback(err);

   return callback();
});


And I want to insert a new statement in there, the tab will bump over to align like this:

Code: Select all
return foo(function(err) {
    if (err)
      return callback(err);

       WHY_AM_I_INDENTED_HERE();
   return callback();
});


If the if(err) is wrapped with curl braces, indentation is correct. Is this a sublime internal problem or is it something I can tweak?

On a good note, I love that the keybindings are flexible enough that I was able to mimick emacs much better than other editors I've tried.

Thanks!
mcavage
 
Posts: 1
Joined: Sun Jan 15, 2012 7:36 pm

Re: JS Indentation

Postby aparajita on Tue Jan 17, 2012 5:23 pm

Maybe you didn't notice that your indentation is all over the place. Line 2 is indented 5 spaces, line 3 is indented 3 spaces, the final return is indented 4 spaces. Which should it be?

When you hit return after 'return callback(err)', the indentation level is 6 spaces, but Sublime Text rounds up to the nearest multiple of the document's tab width, which defaults to 4, so it indents the next line by 8 spaces. If your indentation was consistent everything would line up correctly. Try using the tab key instead of manually typing spaces (if that is what you did). By default tab will insert enough spaces to line up with the next multiple of the tab width.

If consistent indentation doesn't matter and don't want to use tab to indent, set the tab width to 1 space and indent manually. Otherwise you can't expect poor Sublime Text to somehow determine which indent to use out of the several you used in this one function.
aparajita
 
Posts: 177
Joined: Sun Jul 24, 2011 10:42 pm

Re: JS Indentation

Postby ajpalkovic on Tue Jan 17, 2012 5:46 pm

Actually, you can.

Textmate grammars have an indentNextLinePattern rule (or something similarly named). The purpose of this is for cases like yours. The js grammar does not make use of it, but you could look at other grammars that do and tweak it to get it right.
ajpalkovic
 
Posts: 142
Joined: Fri Jun 10, 2011 10:21 pm


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot] and 13 guests