Skip to content

Commit

Permalink
Changes to allow phantoms to be cleanly reloaded
Browse files Browse the repository at this point in the history
Should permit package to be upgraded without necessarily requiring a restart. Extra effort has to be made to clear any existing phantoms and clear out the references to the listeners which are otherwise not properly cleared.
  • Loading branch information
ig0774 committed Nov 1, 2016
1 parent dfcaa68 commit 2af34cc
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 68 deletions.
51 changes: 50 additions & 1 deletion 01_reload_submodules.py
Expand Up @@ -5,13 +5,27 @@
#

import sublime

import sys
import traceback

if sys.version_info >= (3,):
from imp import reload


def _load_module_exports(module):
if 'exports' in module.__dict__:
for name in module.exports:
try:
# lift the export to this modules top level
globals()[name] = module.__dict__[name]
except KeyError:
print(
"Error: {0} not defined in {1}."
.format(name, module.__name__)
)


MOD_PREFIX = ''

if sublime.version() > '3000':
Expand Down Expand Up @@ -59,6 +73,22 @@
'latex_fill_all'
]

# modules which should be scanned for any exports to be hoisted to this
# module's context
EXPORT_MODULES = []
if sublime.version() > '3118':
LOAD_ORDER += [
'st_preview.preview_utils',
'st_preview.preview_threading'
]

EXPORT_MODULES += [
'st_preview.preview_math',
'st_preview.preview_image'
]

LOAD_ORDER += EXPORT_MODULES

for suffix in LOAD_ORDER:
mod = MOD_PREFIX + suffix
try:
Expand All @@ -69,9 +99,12 @@
except:
traceback.print_exc()

if suffix in EXPORT_MODULES:
_load_module_exports(sys.modules[mod])


# reload any plugins cached in memory
def plugin_loaded():
# reload any plugins cached in memory
try:
import latextools_plugin
except ImportError:
Expand All @@ -88,6 +121,22 @@ def plugin_loaded():
except:
traceback.print_exc()

for module in EXPORT_MODULES:
mod = MOD_PREFIX + module
try:
sys.modules[mod].plugin_loaded()
except AttributeError:
pass


def plugin_unloaded():
for module in EXPORT_MODULES:
mod = MOD_PREFIX + module
try:
sys.modules[mod].plugin_unloaded()
except AttributeError:
pass


if sublime.version() < '3000':
plugin_loaded()
19 changes: 19 additions & 0 deletions 03_reset_phantoms.py
@@ -0,0 +1,19 @@
#
# On reload, this module will attempt to remove any phantoms associated
# with ViewEventListeners from all active views.
#
import sublime
import sublime_plugin

if sublime.version() > '3118' and sublime_plugin.api_ready:
for w in sublime.windows():
for v in w.views():
if v.score_selector(0, 'text.tex.latex') == 0:
continue
v.erase_phantoms('preview_math')
v.erase_phantoms('preview_image')
v.settings().clear_on_change('preview_math')
v.settings().clear_on_change('preview_image')
s = sublime.load_settings('LaTeXTools.sublime-settings')
s.clear_on_change('preview_math')
s.clear_on_change('preview_image')
55 changes: 0 additions & 55 deletions loader.py

This file was deleted.

5 changes: 4 additions & 1 deletion st_preview/preview_image.py
Expand Up @@ -442,12 +442,15 @@ def on_navigate(self, href):
open_image_folder(p.image_path)

def reset_phantoms(self):
self.delete_phantoms()
self.update_phantoms()

def delete_phantoms(self):
view = self.view
with self._phantom_lock:
for p in self.phantoms:
view.erase_phantom_by_id(p.id)
self.phantoms = []
self.update_phantoms()

def update_phantom(self, p):
with self._phantom_lock:
Expand Down
11 changes: 9 additions & 2 deletions st_preview/preview_math.py
Expand Up @@ -70,6 +70,7 @@ def _on_setting_change():


def plugin_loaded():
print('plugin_loaded in preview_math called')
global _lt_settings, temp_path
_lt_settings = sublime.load_settings("LaTeXTools.sublime-settings")

Expand Down Expand Up @@ -274,6 +275,7 @@ def update_preamble_str(init=False):
self.preamble_str = self.preamble
else:
self.preamble_str = "\n".join(self.preamble)

if not init:
self.reset_phantoms()

Expand Down Expand Up @@ -316,8 +318,10 @@ def update_template_file(init=False):
"call_after": update_template_file
}
}

lt_attr = view_attr.copy()
# watch this attributes for setting changes to reset the phantoms

# watch these attributes for setting changes to reset the phantoms
watch_attr = {
"_watch_scale_quotient": {
"setting": "preview_math_scale_quotient",
Expand Down Expand Up @@ -420,13 +424,16 @@ def on_selection_modified(self):
#########

def reset_phantoms(self):
self.delete_phantoms()
self.update_phantoms()

def delete_phantoms(self):
view = self.view
_cancel_image_jobs(view.id())
with self._phantom_lock:
for p in self.phantoms:
view.erase_phantom_by_id(p.id)
self.phantoms = []
self.update_phantoms()

def update_phantoms(self):
with self._phantom_lock:
Expand Down
22 changes: 13 additions & 9 deletions st_preview/preview_utils.py
@@ -1,6 +1,7 @@
import os
import threading
import time
import traceback

import sublime

Expand All @@ -13,11 +14,6 @@
_lt_settings = {}


def plugin_loaded():
global _lt_settings
_lt_settings = sublime.load_settings("LaTeXTools.sublime-settings")


def convert_installed():
"""Return whether ImageMagick/convert is available in the PATH."""
return which('convert', path=get_texpath() or None) is not None
Expand All @@ -34,10 +30,18 @@ class SettingsListener(object):

def _init_list_add_on_change(self, key, view_attr, lt_attr):
view = self.view
_lt_settings.add_on_change(
key, lambda: self._on_setting_change(False))
self.view.settings().add_on_change(
key, lambda: self._on_setting_change(True))

# this can end up being called *before* plugin_loaded() because
# ST creates the ViewEventListeners *before* calling plugin_loaded()
global _lt_settings
if not isinstance(_lt_settings, sublime.Settings):
try:
_lt_settings = sublime.load_settings(
"LaTeXTools.sublime-settings"
)
except Exception:
traceback.print_exc()

self.v_attr_updates = view_attr
self.lt_attr_updates = lt_attr

Expand Down

0 comments on commit 2af34cc

Please sign in to comment.