Interesting questions. Here’s some supposition for you which may or may not muddy the waters and/or may not already be things you know/have tried/have guessed. I’m definitely interested in what the gurus have to say on this one.
The fact that the view
class has a method is_valid()
seems to indicate that it is expected that a view object may hang around for an arbitrary period of time after the underlying buffer goes away. The view definitely would have to hang around until at least after the on_close
callback is invoked, which happens right when the close command is executed.
Since the on_close_async
handler would also need to have a view and presumably do something with it If I had to guess I would say that the standard garbage collection mechanism would be in control of when such an object goes away, which means that as long as you’re in a position to check, it’s going to still be there.
Based on the following code, the view object remains around (with it’s unique ID) but the buffer object goes away at some point, and when it does, the attempt to access the settings doesn’t throw an error but just returns None
.
def run(self):
active_window = sublime.active_window()
active_view = active_window.active_view()
temp_buffer = active_window.open_file("c:/test.txt", sublime.TRANSIENT)
active_window.run_command("close")
active_window.focus_view(active_view)
def check_valid():
print("---")
syntax = temp_buffer.settings().get("syntax") # returns correctly
print(syntax)
print("view ID", temp_buffer.id())
print("Buffer ID", temp_buffer.buffer_id())
print("Is valid", temp_buffer.is_valid())
check_valid()
sublime.set_timeout(check_valid, 1000)
Based on this and the principles of thread safety I would assume that is_valid
would not just spontaneously start returning false while a command is still running and as long as its still reporting as valid the settings are still available.
This sort of glosses over how settings are specific to a view and not to a buffer though. Possibly whatever internal mechanism makes the buffer go away also tells the view and it’s specific settings are no longer needed because it’s not representing anything anymore?
Evidence seems to suggest that this is the case; in this case it can not only get the syntax, but also syntax specific settings (including custom settings that have nothing to do with the core).
Again, if I had guess I would say that the loading process would want to know at least the syntax being used so that it could process the syntax as the file was loading, and to do that it would make sense to apply all settings and not just the one.