Skip to content

Instantly share code, notes, and snippets.

@t9md
Last active June 4, 2017 15:18
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 t9md/e57172a18e6b75d0f9f1247a98a14922 to your computer and use it in GitHub Desktop.
Save t9md/e57172a18e6b75d0f9f1247a98a14922 to your computer and use it in GitHub Desktop.
hydrogen-helpers
hydrogenAppendCell = (editor) ->
scope = editor.getLastCursor().getScopeDescriptor()
{commentStartString} = editor.getCommentStrings(scope)
[startRow, endRow] = editor.getLastSelection().getBufferRowRange()
pattern = ///\s*#{commentStartString}\s*%%\s*$///
for row in [startRow..endRow]
scanRange = editor.bufferRangeForBufferRow(row)
replaced = false
editor.scanInBufferRange pattern, scanRange, ({range, replace}) ->
unless range.isEmpty()
replace('')
replaced = true
unless replaced
point = [row, Infinity]
editor.setTextInBufferRange([point, point], " #{commentStartString}%%")
hydrogenRestartKernelAndRunAll = (editorElement) ->
atom.commands.dispatch(editorElement, 'hydrogen:restart-kernel')
setTimeout ->
atom.commands.dispatch(editorElement, 'hydrogen:run-all')
, 1000
atom.commands.add "atom-text-editor:not([mini])",
"user:hydrogen-append-cell": ->
hydrogenAppendCell(@getModel())
"user:hydrogen-restart-kernel-and-run-all": ->
hydrogenRestartKernelAndRunAll(this)
'atom-workspace:not([mini])':
'cmd-m': 'user:hydrogen-append-cell'
'cmd-h': 'user:hydrogen-restart-kernel-and-run-all'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment