How to activate a Text Editor?


#1

I can get a active TextEditor by this.

editor = atom.workspace.getActiveTextEditor()

But how to determine whether the Text Edtior is active?
And how to activate a Text Editor?
(use this? -> atom.workspace.open(editor.getPath()) ???)


#2

Which TextEditor are you checking to see if it’s active?

And how to activate a Text Editor?
(use this? -> atom.workspace.open(editor.getPath()) ???)

Perhaps. If you’re trying to activate an existing editor, you would use the Pane methods to activate a specific item, but you haven’t provided any information about your use case.


#3

thx your replay.

This TextEditor :

editor = atom.workspace.getActiveTextEditor()

And, sometimes, I need to whether the Text Edtior is active.


#4

You don’t need to activate that editor, because it’s already active by definition.


#5
sFilePath = "./test.coffee"
oldEditor = atom.workspace.getActiveTextEditor() # path isnt sFilePath

atom.workspace.open(sFilePath, { changeFocus:true }).then (oNewEditor) =>
  console.log "after editor open", oNewEditor

Like this, how can I activate the oldEditor?


#6

Not sure this is exactly what you need, but found some related code in my package jumpy:

    @disposables.add atom.workspace.observeTextEditors (currentEditor) =>
        ...
        pane = atom.workspace.paneForItem(currentEditor)
        pane.activate()
        ...

currentEditor = getActiveTextEditor() should give you enough to pass into paneForItem()

see https://github.com/DavidLGoldberg/jumpy/blob/master/lib/jumpy-view.coffee#L266


#7

@DavidLGoldberg has the right idea. Assign a variable to getActiveTextEditor(), assign a variable to the paneForItem() for that editor, and then activate the pane when you need to get back to the first editor.


#8

Thx for your idea, I will try it soon.:thumbsup:


#9

Thx for your help! :thumbsup: