Different comment styles via keyboard shortcuts?


#1

I’d like to set up keyboard shortcuts for both // and /// comment styles in SCSS documents (/// is for SassDoc definitions).

I’d like to set CMD+/ to toggle // (current atom default) and something else to trigger /// in the same fashion. Alas, my atom fu is not yet strong so I’m not sure how to accomplish this via snippets (without always going to the beginning of the line).

Is it possible via snippets? Do I need to write or find a package?


#2

It’s not possible via snippets because the original function isn’t a snippet. The shortcut actually calls the editor:toggle-line-comments function. You would have to make a package to add a new feature that does the same but with three slashes.


#3

You can add a new binding in the keymap.cson file. Let’s say, you want the ctrl-alt-/ combination to add three slashes into your buffer. So you could:

  • edit your keymap.cson file:
'atom-workspace atom-text-editor:not([mini])':
  'ctrl-alt-/': 'comments:add-three-slashes'
  • add the code to your init.cson file:
atom.commands.add 'atom-text-editor', 'comments:add-three-slashes', ->
  editor = atom.workspace.getActiveTextEditor()
  cursor = editor.cursors[0]
  marker = cursor.marker

  startingRow = marker.getStartBufferPosition().row
  endingRow   = marker.getEndBufferPosition().row

  for row in [startingRow..endingRow]
    editor.setCursorBufferPosition([row, 0])
    editor.insertText '///'

You should check the keybinding you want to use is not taken by any other command.

Edit: the code I wrote you just works if you have a single cursor in the editor. You can play around with it to tweak it as you need!

Hope this helps!


#4

Oh, thanks for that. I forgot how malleable Atom was.


#5

Wow. That snippet is a full on, yet magically concise, lesson in Atom hacking. Thank you so much!