Atom Editorで使えるちょっとした関数を定義する

以前、あるキーの入力回数に応じて表示する内容を変更する([' => ', ' -> ', '=>', '->'] を順番に表示) ものが欲しくて、試行錯誤した結果 fat-arrow という Atomプラグインを作る ことになったのですが、 そんなことしなくても、ちょっとした関数などを書ける init.coffee が用意されていました。
3.2 Hacking Atom : The Init File

書いてみる

packageから移行しただけですが、これだけで実装できます(APIはクセがありますが)。
ちょっとした関数にkeymapを追加するというのがやりたかった。

// init.coffee
CANDIDATES = [' => ', ' -> ', '=>', '->']
atom.commands.add 'atom-text-editor', 'fat-arrow:show', ->
  _nextIndex = (selectedText) ->
    return 0 unless selectedText
    index = CANDIDATES.indexOf(selectedText)
    return 0 if (index >= CANDIDATES.length - 1)
    return index + 1

  return unless editor = atom.workspace.getActiveTextEditor()
  editor = atom.workspace.getActivePaneItem()
  selectedText = editor.getSelectedText()
  nextIndex = _nextIndex(selectedText)
  editor.insertText(CANDIDATES[nextIndex], {select: true, undo: 'skip'})
// keymap.json
'atom-text-editor:not([mini])':
  'ctrl-=': 'fat-arrow:show'

満足。