Created
May 2, 2018 12:21
-
-
Save pfitzseb/b8ab0b4d5008b69e92a02df6a97d8f0c to your computer and use it in GitHub Desktop.
Toggle `#'` command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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