Atom.workspace.isTextEditor


#1

I recently developed a package and used atom.workspace.isTextEditor function. The package works fine in ubuntu but for some reason it gives error in windows due to the above function. How should I solve it?

Also is there any way to check if current pane is a text editor.


#2

i run into the same problem. Try this :

{TextEditor} = require 'atom'

//Then anywhere in your code

my_pane instanceof TextEditor

Also, the function you are talking about in not present in the doc, meaning it’s not a public function so you shouldn’t rely on it.


#3

You can use atom.workspace.getActiveTextEditor. If the active pane item isn’t a text editor, it will return undefined.


#4

@lary that function you showed above is pretty new, so for backward compatibility you’ll have to do something like

function isTextEditor(textEditor) {
  if (atom.workspace.isTextEditor) {
    return atom.workspace.isTextEditor(textEditor)
  } else return typeof textEditor.getText === 'function'
}

#5

I still feel that using the getActiveTextEditor function is still the way to go on this. It covers backwards compatibility and everything!


#6

@leedohm what if the text editor is inactive?


#7

Then you can compare it to the list of getTextEditors. Undocumented functions have a tendency to:

  1. Not work in all cases that people might expect them to
  2. Change or disappear without notice

For these reasons, I don’t recommend using undocumented functions.


#8

@leedohm The function I provided above fallsback to the getText method, it has been available since the very early days. Also this method was recommended by as-cii (the person who deprecated the API himself). IMO comparing it with all text editors is also inefficient


#9

Sorry, I won’t agree with recommending something that isn’t part of the published API.


#10

@leedohm well this uses the published API :wink:

Atom = require('atom')
function isTextEditor(textEditor) {
  if (atom.workspace.isTextEditor) {
    return atom.workspace.isTextEditor(textEditor)
  } else return textEditor instanceof Atom.TextEditor
}

#11

I’m not sure why you care so much about backwards compatibility but you should know that Atom.TextEditor is pretty new. I’m the one who complained that it wasn’t there.