How to get core class constructors?


I need to find out if an object is an instance of TextEditor, as opposed to something like a SettingsView. How can I do this? Ideally I could use if obj instanceof TextEditor but I don’t know how to get the constructor TextEditor.

I could have sworn I’ve done this before but I can’t find it anywhere in my code. Any ideas?

Edit: After digging through core I was able to find TextEditor with this code. But it is really ugly.

for provider in atom.views.providers
  if (TextEditor = provider.modelConstructor).name is 'TextEditor'


You can get the TextEditor constructor using require 'atom'

{TextEditor} = require 'atom'

if editor instanceof TextEditor
  # ...


I could have sworn i tried that . Thanks.


I did try this and it didn’t (doesn’t) work. It returns undefined.

{TextEditor} = require 'atom'

Edit: BTW, how did you get your source code colored in your post?


When you wrap your code in a block using triple backticks, add a language identifier, for example:

console.log ‘hello world’


console.log 'hello world'

You can do all the fancy stuff supported in GitHub Flavored Markdown, in particular code highlighting is explained here.


Oh you’re right, seems it’s no longer available. I bet this is going to break a lot of packages.

@kevinsawicki, @nathansobo: Is there a workaround to access the TextEditor class?


One workaround could be this:

{TextEditorView} = require 'atom'

TextEditor = new TextEditorView({}).getEditor().constructor 

It’s ugly as hell but it should work.


You could also do:

path = require 'path'

require(path.join(atom.packages.resourcePath, 'src', 'text-editor'))

But this should really be exported from the atom module. Both Workspace and WorkspaceView are exported, so it makes sense.


It’s not as ugly as what I using now. See earlier post.


atom.workspaceView is also gone

Edit: It’s a timing problem. If I put it in a process.nextTick it works.

Edit: Do they test any packages before releasing? I’ve got 14 they can test. Although I have no specs. (grin)


I dont think it was ever available. Maybe I’m missing something:

It will be deprecated, but it is not gone.

I dont think providing it solely for instanceof checks is the best thing. We should have a nice way to do what you want to do in the original post.


Ugh, I was pretty sure I had wrote that before but I must have been really tired…


After talking, the plan is to export TextEditor.


I see this hasn’t happened yet. I’ll continue to use the hack but I’ll bug you every time I need to. (grin)


It happened a while ago:


Duh. I was trying atom.TextEditor.

What is the rule for the contents of require 'atom' versus atom.x?