How to correctly use addOpener (not registering function)


#1

I’m trying to use addOpener to just open a particular filetype with a view I’ve written, and it doesn’t appear to be registering, either when I open a file manually with ctrl-o, or when I call atom.workspace.open(filepath).

Here is the code:

atom.workspace.addOpener (uri) ->
  console.log "hey"
  atom.workspace.activePane.activateItem new MyView uri

As you’ll note, the opener is registered for every possible filetype. However, when atom.workspace.open is called, the view is not displaying, and the console.log is not showing up at all. I’ve tried limiting it by filetype, and when that didn’t work, I registered it for every possible uri. The console merely shows the result of an atom.workspace.open. Why isn’t the opener registering?

Also, is opening with ctrl-o the same as an atom.workspace.open, or do the two call different functions?


#2

You need to return an item opener object that has specific methods. Check out the example package html-tab.


#3

I’ve actually created a package based off of html-tab already which does implement getTitle, getViewClass, etc, and opens a new tab as desired. The issue is that the activateItem call isn’t being fired, as evidenced by the lack of the console.log.

Calling atom.workspace.addOpener and giving it a function from the console seemed to work, but for some reason this part isn’t activating. It’s within the “activate” method of the package I’m developing, from where many other functions are being successfully called.

EDIT: the code is viewable at https://github.com/cosmicexplorer/red-october, if you’re interested.


#4

Unfortunately I don’t remember details of how it works. I just always use html-tab as a template. I’m sorry I don’t have time to be more help right now. I’ll keep this bookmarked and try to help later today or tomorrow.


#5

No worries, I appreciate the willingness to help.


#6

So it was a result of the package not activating until an activation event was called. I found this thread explaining how no packages should activate until triggered by an activation event. Noticing this resolved the issue. I have, however, created a new thread asking about addOpeners and package activation.


#7

Just so you know, you don’t have to activate the item yourself, just return your view and Atom will make it the active item automatically.


#8

Huh! Good to know. I’ll fix that immediately.