77

Paste this JS code in the stack snippet editor:

(function() {
  console.log(foo + "=>");
})();

Select it all with ctrl+A and use shift+tab to autoindent. The code becomes

(function() {
  console.log(foo + "=>");
              })();

It works properly when using the equivalent "=\>". This makes me wonder if the autoindent uses some naive regex in order to detect arrow functions, but one can't parse JS with regex.

10
  • 37
    I hate that Shift+Tab causes auto-indent instead of reducing the indent level by one. I've grown used to certain shortcuts doing certain things and the snippet editor doesn't follow that. I always edit my code in my preferred editor before pasting into the snipped editor for this exact reason.
    – SeinopSys
    Commented Dec 18, 2016 at 22:33
  • @SeinopSys But autoindent is so handy when a newbie posts an horrendously formatted question.
    – Oriol
    Commented Dec 18, 2016 at 22:35
  • 19
    I'm not saying it's not a useful feature, but they could have bound it to a shortcut that does not already have a widely known function, or they could have made it a button on the side.
    – SeinopSys
    Commented Dec 18, 2016 at 22:37
  • 1
    Looks like the hashrocket has achieved lift-off! Commented Dec 18, 2016 at 22:45
  • Isn't it already Ctrl-k anyway?
    – k_g
    Commented Dec 19, 2016 at 3:10
  • Note that the Tidy button doesn’t cause this problem.
    – Jed Fox
    Commented Dec 19, 2016 at 15:40
  • @JF Yep, but the Tidy button does not autoindent.
    – Oriol
    Commented Dec 19, 2016 at 15:41
  • @Oriol Try dedenting the code snippet provided, then click “Tidy.”
    – Jed Fox
    Commented Dec 19, 2016 at 15:47
  • @JF Ah, you are right. But it does something weird, if you have a well indented code and add the same number of spaces before each line, then they are not removed.
    – Oriol
    Commented Dec 19, 2016 at 15:51
  • I'm looking. we use code mirror there, will have to dig into how it handles shortcuts... since it doesn't do anything when selecting multiple lines and pressing tab (one would expect it to intend by a level), I guess preventing it from handling shift+tab is also an option
    – m0sa
    Commented Feb 15, 2017 at 12:38

1 Answer 1

3

Build rev 2017.2.16.25125 has the latests code mirror release, I've also tweaked the key bindings a bit:

  • Tab adds indentation to the lines in the current selection, or jumps to the next tab stop (adding either 1 or 2 spaces) if nothing is selected. Previously it just added 2 spaces.

  • Shift-Tab removes an indentation level, from the lines in the current selection, since this is how most developers expect it to behave.

Previously it was using the CodeMirror.commands.indentAuto which was buggy in the old codemirror release.

I've also remapped that command to the tidy function for the given editor, so if anybody wants to remap it back in a user script (e.g. CodeMirror.keyMaps.default['Shift-Tab'] = 'indentAuto').

6
  • 1
    you're right, I forgot to pull in the latest mode files when updating codemirror
    – m0sa
    Commented Feb 16, 2017 at 10:11
  • Is there a new shortcut to auto-indent the code or does this functionnality is gone?
    – Veve
    Commented Feb 16, 2017 at 16:15
  • 1
    @Veve it's gone, but if you really want it, the command is still there, and works consistently with the tidy link now
    – m0sa
    Commented Feb 16, 2017 at 17:06
  • Didn't know it existed before. I think we loose a little bit with this function not directly accessible anymore outside the Snippet editor. (I won't open it just for this)
    – Veve
    Commented Feb 16, 2017 at 17:16
  • it was never accessible outside the snippet editor
    – m0sa
    Commented Feb 16, 2017 at 17:18
  • OK, I though it was also accessible directly in the normal editor.
    – Veve
    Commented Feb 16, 2017 at 17:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .