Search code examples
javascriptkeymappingatom-editor

Changing JS Indenting in Atom Keymap.cson


I'm trying to update my keymap.cson file so that JavaScript source is indented slightly differently. I do not want it to de-indent case and default statements in a switch.

By default, Atom will format this way:

switch(x) {
case 1:
    //stuff
case 2:
    //stuff
default:
    //stuff
}

I like my case statements to be indented once (and //stuff to be indented once more).

So I'm trying to edit my keymap to make it format things this way:

switch(x) {
    case 1:
        //stuff
    case 2:
        //stuff
    default:
        //stuff
}

Unfortunately, whatever I try, I can't get it to stop decreasing the indentation as soon as I hit the spacebar after "case".

From the Keymaps Documentation it looks like putting the following coffeescript in my keymap.cson file should disable the default behavior and add my new behavior (which omits |case|default from the second line of the regular expression) should do the trick but I'm not sure why it's not working:

'.source.js':
    'editor':
        'decreaseIndentPattern': 'unset!'

'.source.js':
  'editor':
    'decreaseIndentPattern': '(?x)
        ^(.*\\*/)?\\s*(\\}|\\))
      | ^\\s* else \\s*$
      '

I think the original keymap is in the language-javascript package here.


Solution

  • I determined that this probably isn't technically a Keymap so it can't be overridden in keymap.cson. What I ended up doing was forking the language-javascript repo, making my change, uninstalling the original language-javascript package, and installing my custom fork instead.

    Would still like to know if there is a way to update this at runtime instead; with this solution I will have to manually keep my fork up to date with any upstream changes.

    Edit: Just an update for this particular change, it was actually accepted into the official language-javascript package via PR #36.