Skip to content

Instantly share code, notes, and snippets.

@pfitzseb
Created May 2, 2018 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfitzseb/b8ab0b4d5008b69e92a02df6a97d8f0c to your computer and use it in GitHub Desktop.
Save pfitzseb/b8ab0b4d5008b69e92a02df6a97d8f0c to your computer and use it in GitHub Desktop.
Toggle `#'` command
# paste into your coffee.init
atom.commands.add 'atom-text-editor', 'Toggle #\'', (e) =>
ed = atom.workspace.getActiveTextEditor()
sels = ed.getSelections()
buffer = ed.getBuffer()
lines = buffer.getLines()
for sel in sels
isOn = true
preIndents = []
postIndents = []
for ln in [sel.getBufferRowRange()[0] .. sel.getBufferRowRange()[1]]
line = lines[ln]
match = line.match(/^(\s*)#'(\s*)/)
imatch = line.match(/^(\s*)/)
preIndents.push(imatch[1].length)
if match
postIndents.push(match[2].length)
else
isOn = false
break
preInd = Math.min(preIndents...)
postInd = Math.min(postIndents...)
if not isFinite(postInd)
postInd = 0
buffer.transact () =>
for ln in [sel.getBufferRowRange()[0] .. sel.getBufferRowRange()[1]]
if isOn
buffer.delete([[ln, preInd], [ln, 2 + preInd + postInd]])
else
buffer.setTextInRange([[ln, 0], [ln, preInd]], ' '.repeat(preInd) + '#\' ')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment