Sublime Forum

JS Indentation

#1

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:

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:

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!

0 Likes

#2

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.

0 Likes

#3

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.

0 Likes