Open file using API and scroll to given line number


I am using the atom-macro package to define a simple macro.
The purpose of the macro is to look on the current line for a filename and line number (in a stacktrace), open the file and scroll down to the given line. I have gotten the regular expression to work and pick out the correct filename (if any) and the line number. I have also managed to open the file. But the editor does not scroll down to the correct line. It either does not move the cursor, or it moves the current line number in the original file, not the newly opened file. Here is my code that I put in the file

@stackTrace = ->
  editor = atom.workspace.getActiveTextEditor()
  selectedText = editor.getSelectedText()
  regex = /^\s*([^:]+):(\d+)/
  matches = regex.exec(selectedText)
  if (matches)
    fileName = matches[1]
    lineNumber = parseInt(matches[2])
    # alert('Follow link to ' + fileName + ' on line ' + lineNumber ){'pathsToOpen': [fileName], 'newWindow': false})
    editor2 = atom.workspace.getActiveTextEditor()
    editor2.moveDown(lineNumber - 1)
    alert('No stacktrace link found!' )

I tried it with and without moveToTop. What do I need to do to change the context to the new editor window correctly?


Instead of use and set initialLine and initialColumn


Your suggestion worked like a charm. Thanks!