How to get core class constructors?


#1

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'
    break

#2

You can get the TextEditor constructor using require 'atom'

{TextEditor} = require 'atom'

if editor instanceof TextEditor
  # ...

#3

I could have sworn i tried that . Thanks.


#4

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?


#5

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

```coffee
console.log ‘hello world’
```

becomes:

console.log 'hello world'

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


#6

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?


#7

One workaround could be this:

{TextEditorView} = require 'atom'

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

It’s ugly as hell but it should work.


#8

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.


#9

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


#10

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)


#11

I dont think it was ever available. Maybe I’m missing something: https://github.com/atom/atom/commits/master/exports/atom.coffee

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.


#12

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


#13

After talking, the plan is to export TextEditor.


#14

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)


#15

It happened a while ago:


#16

Duh. I was trying atom.TextEditor.

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