Sublime Forum

Is there an "on_project_load" event?

#1

Is there an event which is triggered each time a sublime project is opened? Opened either from restoring a previous session of our beloved text editor or from an existing one?

I was not able to find any such event in the API doc, or I misunderstood existing events like on_load.

As I like to code a plugin for python development which associates a sublime project with one virtual environment, so that every time that project is opened a terminal with that virtual environment already activated is automatically launched.

This is to replicate an important feature of PyCharm which did very well linking projects and virtual environments and will save you some repetitive tasks making python development in sublime even more joyful.

Also I’d like to know what you think of that plugin idea.

Thanks!

  • PyBen
0 Likes

#2

Unfortunately there is no such event. You can imitate the behavior by keeping track of a global dictionary yourself, where the key is the window ID, and the value is the data associated to your virtualenv.

Upon a call to EventListener.on_load(self, view), you can then get to the window via window = view.window(), and then check if it has project data associated to it with window.project_file_name().

Note that, even if a window is opened as a “folder” (i.e. subl path/to/some/folder), the dictionary returned by window.project_data() still has data in it.

Example from my sublime console where I opened a window as a folder:

>>> print(view.window().project_data())
{'folders': [{'path': '/Users/rwols/Library/Application Support/Sublime Text 3/Packages'}]}
>>> print(view.window().project_file_name())
None
2 Likes

#3

Thank you for your quick and in-depth reply. I am going to use the pieces of wisdom you kindly provided and will come back in the next days with an update and likely more questions.

0 Likes