Get new TextEditor synchronously


Hello everybody!
I’m currently working on a mechanism to print stuff in a TextEditor. I would like Atom to reuse the same Editor if still open, otherwise a new Editor should be created. So, my code looks pretty much like this:

printStuff: (stuff) ->
  @printMessageOn 'console-output', stuff

printMessageOn: (editorName, message) ->
  editor = @getEditorByName editorName
  editor.insertText message

getEditorByName: (name) ->
  editor = atom.workspace.getTextEditors().find (e) -> e.getTitle() is name
  return editor

In the getEditorByName method I would like to create a new TextEditor with the required name when it does not exist. Problem is, returns a Promise, and I would like to avoid handling promises outside the method. I found the buildTextEditor method, but when I use it the new TextEditor is not displayed.

Any suggestions on how to solve this? Cheerio!


I got a temporary fix checking if the provided editor actually exists, then creating a new one if it doesn’t.

printMessageOn: (editorName, message) ->
  print = (edtr, msg) ->
    edtr.insertText msg

  editor = @getEditorByName editorName
  if editor
    print editor, message
  else (e) ->
      print e, data, error

I don’t really like this solution, code is duplicated and the if-else part really annoys me.

I’ll keep working on this if anyone may be interested!


There is a atom.workspace.openSync here, but it’s not mentioned in the docs because it blocks the UI.
I don’t think you can really avoid a Promise here.

The only good news is that you don’t need the if-else part and @getEditorByName because checks if an editor for the given URI already exists (make sure that searchAllPanes is enabled).