Defining a custom command to jump back/forward in buffer

#1

Suppose I have some code like

# %%
<some code>
# %%

and I’d like to jump around to lines with #%% . I have the following in my init.coffee using custom:jump-forward-cell and custom:jump-backward-cell :

atom.commands.add(
  "atom-text-editor"
  "custom:jump-forward-cell": ->
    editor = atom.workspace.getActiveTextEditor()
    position = editor.getCursorBufferPosition()

    editor.buffer.scanInRange(
      /^# %%/m,
      new Range(
        [position.row + 1, position.column],
        editor.buffer.getEndPosition()
      ),
      (m) -> editor.setSelectedBufferRange m.range
    )
  "custom:jump-backward-cell": ->
    editor = atom.workspace.getActiveTextEditor()
    position = editor.getCursorBufferPosition()

    editor.buffer.backwardsScanInRange(
      /^# %%/m,
      new Range([0, 0], [position.row - 1, position.column]),
      (m) -> editor.setSelectedBufferRange(m.range)
    )
)

However, this doesn’t seem to work. Any thoughts?

#2

You only get to define one command per add(), and you need commas between your function arguments. Since they’re function arguments, and not object keys, "custom:jump-forward-cell": -> is also invalid. Try this:

atom.commands.add("atom-text-editor", "custom:jump-forward-cell", ->
  editor = atom.workspace.getActiveTextEditor()
  position = editor.getCursorBufferPosition()

  editor.buffer.scanInRange(
    /^# %%/m,
    new Range(
      [position.row + 1, position.column],
      editor.buffer.getEndPosition()
    ),
    (m) -> editor.setSelectedBufferRange m.range
  )
)
#3

This snippet doesn’t seem to be working either. How might I go about debugging it?

#4

Just an idea. Would it be easier to use the TODO package where you add a snippet %% in TODO search settings?

#5

Use the developer tools console (View -> Developer -> Toggle developer tools -> Console) and add console.log() statements to your custom function. You can put them at every step and have them share the current state of the variables with you.