diff --git a/MarkdownLivePreview.py b/MarkdownLivePreview.py index e7307ca..2db9245 100644 --- a/MarkdownLivePreview.py +++ b/MarkdownLivePreview.py @@ -138,18 +138,15 @@ class OpenMarkdownPreviewCommand(sublime_plugin.TextCommand): # This is needed because the listener isn't easily accessible directly from the command instance def trigger_listener_setup(self, md_view_id, pv_view_id): print("--- MarkdownLivePreview: trigger_listener_setup running for md_view: {}, pv_view: {} ---".format(md_view_id, pv_view_id)) - # Find the listener instance (this is a bit indirect, but common in ST plugins) - listener_instance = None - for listener in sublime_plugin.event_listeners: - if isinstance(listener, MarkdownLivePreviewListener): - listener_instance = listener - break + # Access the listener instance directly via its class variable + listener_instance = MarkdownLivePreviewListener.instance if listener_instance: - print("--- MarkdownLivePreview: Found listener instance, calling setup_and_update_preview ---") + print("--- MarkdownLivePreview: Found listener instance via class variable, calling setup_and_update_preview ---") listener_instance.setup_and_update_preview(md_view_id, pv_view_id) else: - print("--- MarkdownLivePreview: ERROR: Could not find MarkdownLivePreviewListener instance to trigger setup! ---") + # This should ideally not happen if the listener loaded correctly before the command ran + print("--- MarkdownLivePreview: ERROR: MarkdownLivePreviewListener.instance is None! Cannot trigger setup. ---") def is_enabled(self): # FIXME: is this the best way there is to check if the current syntax is markdown? @@ -160,6 +157,7 @@ class OpenMarkdownPreviewCommand(sublime_plugin.TextCommand): class MarkdownLivePreviewListener(sublime_plugin.EventListener): + instance = None # Class variable to hold the single instance phantom_sets = { # markdown_view.id(): phantom set @@ -169,6 +167,11 @@ class MarkdownLivePreviewListener(sublime_plugin.EventListener): # then, we update only if now() - last_update > DELAY last_update = 0 + def __init__(self): + super().__init__() # Good practice to call super + MarkdownLivePreviewListener.instance = self + print("--- MarkdownLivePreview: Listener instance created and registered. ---") + # FIXME: maybe we shouldn't restore the file in the original window... def on_pre_close(self, markdown_view):