Open file using API and scroll to given line number


#1

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 macros.coffee:

@stackTrace = ->
  editor = atom.workspace.getActiveTextEditor()
  editor.moveToBeginningOfLine()
  editor.selectToEndOfLine()
  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 )
    atom.open({'pathsToOpen': [fileName], 'newWindow': false})
    editor2 = atom.workspace.getActiveTextEditor()
    editor2.moveToTop()
    editor2.moveDown(lineNumber - 1)
  else
    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?


#2

Instead of atom.open use atom.workspace.open and set initialLine and initialColumn


#3

Your suggestion worked like a charm. Thanks!

Paul