How can you observe what files are opened?


#1

I would like to assign an event handler to when I open files and get passed the path of the file opened when it’s opened.


#2

The atom.workspace.eachEditor() function registers a callback that is given an instance of each Editor object, current and future. From there, you should be able to get the path to the file.


#3

excellent. thanks man. But how do you observe when the editor closes? with an interval that checks for the existence of editors? Anything built-in??


#4

By subscribing to the destroyed event on the Editor model you receive in eachEditor.


#5

whats the exact code for it? i can’t do it with a callback to destroyed() or via something like subscribe(‘destroyed’, callback)


#6

Just like you would have done with any jquery object:

editor.on 'destroyed', -> # ...

To use the subscribe method you have to be either in a view or have your class include the Subscriber mixin from emissary.


#7

that works, but it’s unfortunate i dont get the editor that was destroyed. i can use _.difference() from underscore or manually compare getEditors() to an older array, but is there a specialized way to do just this?

…also, how can i destroy an editor programmatically? Calling .destroy() on an editor produces this error:

TypeError: Cannot call method 'getScreenRow' of undefined

#8

What do you need the editor that was destroyed for? Can’t you save whatever you need off the editor in the closure of the callback?

editor.on 'destroyed', ->
  console.log("Editor for #{editor.path} was destroyed")

#9

oh, you’re 100% right. wasn’t thinking properly. …ok, so how do you destroy an editor. The .destroy() method on an Editor doesn’t work–though I think I got it to work once.

Here’s what I can get reliably to work:

pane = atom.workspace.getPanes()[0];
pane.destroyItem(editor);

The problem with that though is what if you have multiple panes–how do you know which pane the editor belongs to? Try/catch execute the same destroyItem() call in every pane until it works??


#10

Perhaps if you tell us what it is you’re trying to do, we can help better. In my experience, you don’t want to destroy an Editor directly. You might want to close a tab, that contains an EditorView … which has an Editor associated with it … which has a TextBuffer to hold the actual text, etc. So if you tell us what it is you’re trying to accomplish, then we may be able to tell you the right way to do it.


#11

close a tab sir. i get that there is a lot of things associated with it.


#12

Ok, then you can always look at how Atom does it. The command to close the active tab is core:close which you can find the implementation here:

If all you want to do is close the active tab, then you can just use the following code:

atom.workspaceView.trigger 'core:close'

#13

thanks for that. i’m finally looking into the core files now, rather than just the API docs. …my question though is basically this: i’m storying a list of open editors, and i’m not necessarily in it the editor I want to close right now. So I can’t just use the methods that deal with the “active” editor (because I’m doing that collaborative thing I mentioned on chat–I’m closing an editor that another Atom user across the world closed, which has the same relative path). So I want to use the editor object or information it has like the path/uri to close it. Here’s how I found I can do it:

atom.workspace.paneForUri(someEditor.getPath())._view.detach() 

…obviously that’s not ideal because it’s using _view. In general,it seems very hard to get views from models. I get that there’s multiple EditorViews per Editor–so why not return an array of them? I’ll close em all for example.