Calls to window.open_file()
for files that are already opened focus the view
that represents that file and return it back to you. If it’s not already open, a new view
is created and Sublime starts loading the file.
However the load happens asynchronously while your code continues to execute, and you can’t make any modifications to a buffer while it’s still loading. So in the case that the file isn’t already open, your command is essentially rejected because it’s trying to modify the file before it’s ready.
view.is_loading()
returns True
or False
to tell you if the view is currently loading or not, which you can use to detect if this is happening. One way around it is to use an EventListener
or ViewEventListener
listening for the on_load
event to tell when the load is finished.
Alternatively, you can “defer” the operation that you would normally take until the load is done:
def post_load_action(view):
if view.is_loading():
return sublime.set_timeout(lambda: post_load_action(view), 10)
# do something with the view
view = self.window.open_file('delete')
post_load_action(view)
In this sample you put all actions that you want to take on the view
after it’s opened into the function and call it. If the file was already open, it just immediately executes and you’re done. Otherwise it schedules itself to be called at some point in the near future to try again. Eventually the file will be loaded and the action will occur.