def on_post_save(self, view): # Assume both paths are already absolute, only normalize the case. packages_path = split_path(os.path.normcase(get_packages_path())) file_name = split_path(os.path.normcase(view.file_name())) # Check if the file is inside packages directory. if file_name[:len(packages_path)] != packages_path: return # Ignore files placed directly in packages directory, as Sublime # will reload them automatically. if len(file_name) < len(packages_path) + 3: return # Path to the package containing saved file. package_path = file_name[:len(packages_path) + 1] # Translate file name to dot-separated module name and reload the # module if needed. relative_name = file_name[len(packages_path) + 1:] relative_name[-1] = os.path.splitext(relative_name[-1])[0] module = sys.modules.get('.'.join(relative_name)) if module is not None: original_dir = os.getcwd() os.chdir(os.path.join(*package_path)) try: reload(module) finally: os.chdir(original_dir) # The module is indirectly loaded from some package top-level # module, so reload them too, to see the changes in Sublime. package_path.append('*.py') for file_name in glob(os.path.join(*package_path)): reload_plugin(file_name)
def reload_plugin(self, plugin_name): if gte_st3: plugin_name = '.'.join([HIVE_NAME, plugin_name]) else: plugin_name = path.join(self.hive_dir(), plugin_name + '.py') sublime_plugin.reload_plugin(plugin_name)
def reload_plugin(pkg_name): pkg_path = os.path.join(os.path.realpath( sublime.packages_path()), pkg_name) plugins = [pkg_name + "." + os.path.splitext(f)[0] for f in os.listdir(pkg_path) if f.endswith(".py")] for plugin in plugins: sublime_plugin.reload_plugin(plugin)
def run(self, package_name, source, items): # Reload current file first if not in root dir if os.path.dirname(source): if sublime.version()[0] == "3": modulename = package_name + "." + (source.replace(os.sep, ".")[:-3] if source[-11:] != "__init__.py" else source.replace(os.sep, ".")[:-12]) else: modulename = os.path.join(package_dir, source) # Reload the file sublime_plugin.reload_plugin(modulename) print("Package Reloader - Reloading %s" % package_name) # Load modules for item in items: if sublime.version()[0] == "3": modulename = package_name + "." + (item.replace("/", ".")[:-3] if item[-11:] != "__init__.py" else item.replace("/", ".")[:-12]) else: modulename = os.path.join(package_dir, item) sublime_plugin.reload_plugin(modulename) # Clean up multiple callbacks for key, value in sublime_plugin.all_callbacks.items(): items = {} for item in value: name = item.__module__ + '.' + item.__class__.__name__ # Save item connected with the name if name in items: items[name] += [item] else: items[name] = [item] sublime_plugin.all_callbacks[key] = [value[0] for key, value in items.items()]
def run(self, edit): sublime.load_settings("TabNine.sublime-settings").set( "native_auto_complete", True ) sublime.save_settings("TabNine.sublime-settings") sublime_plugin.unload_plugin(__name__) sublime_plugin.reload_plugin(__name__)
def reload_plugin(pkg_name): plugins = [ pkg_name + '.' + posixpath.basename(posixpath.splitext(path)[0]) for path in sublime.find_resources("*.py") if posixpath.dirname(path) == 'Packages/'+pkg_name ] for plugin in plugins: sublime_plugin.reload_plugin(plugin)
def reload_plugin(pkg_name): pkg_path = os.path.join(os.path.realpath(sublime.packages_path()), pkg_name) plugins = [ pkg_name + "." + os.path.splitext(file_path)[0] for file_path in os.listdir(pkg_path) if file_path.endswith(".py") ] for plugin in plugins: sublime_plugin.reload_plugin(plugin)
def reload_plugin(prefix): """Reload Sublime 'plugins' using official API.""" toplevel = [] for name, module in sys.modules.items(): if name.startswith(prefix): depth = len(name.split('.')) if depth == 2: toplevel.append(name) for name in sorted(toplevel): sublime_plugin.reload_plugin(name)
def reload_package(pkg_name, dummy=True, verbose=True, then=None): if pkg_name not in sys.modules: dprint("error:", pkg_name, "is not loaded.") return if is_dependency(pkg_name): dependencies, packages = resolve_dependencies(pkg_name) else: dependencies = set() packages = {pkg_name} if verbose: dprint("begin", fill='=') all_modules = { module_name: module for pkg_name in dependencies | packages for module_name, module in get_package_modules(pkg_name).items() } # Tell Sublime to unload plugins for pkg_name in packages: for plugin in package_plugins(pkg_name): module = sys.modules.get(plugin) if module: sublime_plugin.unload_module(module) # Unload modules for module_name in all_modules: sys.modules.pop(module_name) # Reload packages try: with intercepting_imports( all_modules, verbose), importing_fromlist_aggresively(all_modules): for pkg_name in packages: for plugin in package_plugins(pkg_name): sublime_plugin.reload_plugin(plugin) except Exception: dprint("reload failed.", fill='-') reload_missing(all_modules, verbose) raise if dummy: load_dummy(verbose) if verbose: dprint("end", fill='-') if then: then() sublime.active_window().status_message('GitSavvy has 🙌 reloaded.')
def plugin_unloaded(): PACKAGE_NAME = __name__.split('.')[0] from package_control import events if events.pre_upgrade(PACKAGE_NAME): print('Upgrading from %s!' % events.pre_upgrade(PACKAGE_NAME)) elif events.remove(PACKAGE_NAME): # set_language("EN", True) cleanup() sublime_plugin.reload_plugin('Default') print('Removing %s!' % events.remove(PACKAGE_NAME))
def reload_plugins(prefix): """Reload Sublime 'plugins' using official API.""" toplevel = [] for name, module in sys.modules.items(): if name.startswith(prefix): depth = len(name.split(".")) if depth == 2: toplevel.append(name) for name in sorted(toplevel): sublime_plugin.reload_plugin(name)
def ensure_loaded(main, modules): # More simple (comparing to reload_modules(perform_reload=False)) and dumb # approach to ensure all modules are back. Quite useful when debugging the # "reload" module itself, i.e. for cases when reloading might fail due to # bugs in reload_modules(). missing_modules = {name: module for name, module in modules.items() if name not in sys.modules} if missing_modules: for name, module in missing_modules: sys.modules[name] = modules print("GS [reload] BUG!", "restored", name) sublime_plugin.reload_plugin(git_savvy.__name__)
def reload_package(package, dependencies=[], dummy=True, verbose=True): if verbose: dprint("begin", fill='=') if dummy: load_dummy(verbose) packages = [package] + dependencies parents = set() for package in packages: for parent in resolve_parents(package): parents.add(parent) parents = list(parents) modules = sorted(list(set(get_package_modules(packages + parents))), key=lambda x: x[0].split('.')) plugins = [m for m, is_plugin in modules if is_plugin] # Tell Sublime to unload plugin_modules for plugin in plugins: if plugin in sys.modules: sublime_plugin.unload_module(sys.modules[plugin]) # these are modules marked to be reloaded, they are not necessarily reloaded modules_to_reload = [ sys.modules[m] for m, is_plugin in modules if m in sys.modules ] with ReloadingImporter(modules_to_reload, verbose) as importer: if plugins: # we only reload top level plugin_modules to mimic Sublime Text natural order for plugin in plugins: if plugin in sys.modules: module = sys.modules[plugin] importer.reload(module) for plugin in plugins: if plugin in sys.modules: module = sys.modules[plugin] sublime_plugin.load_module(module) else: # in case we missed something sublime_plugin.reload_plugin(plugin) else: # it is possibly a dependency but no packages use it for module in modules_to_reload: importer.reload(module) if dummy: load_dummy(verbose) if verbose: dprint("end", fill='-')
def ensure_loaded(main, modules): # More simple (comparing to reload_modules(perform_reload=False)) and dumb # approach to ensure all modules are back. Quite useful when debugging the # "reload" module itself, i.e. for cases when reloading might fail due to # bugs in reload_modules(). missing_modules = {name: module for name, module in modules.items() if name not in sys.modules} if missing_modules: for name, module in missing_modules: sys.modules[name] = modules print("restored", name, "XXX RELOAD BUG!") sublime_plugin.reload_plugin(git_savvy.__name__)
def ensure_loaded(main, modules): """Ensure all modules are loaded again. More simple (comparing to reload_modules(perform_reload=False)) and dumb approach to ensure all modules are back. Quite useful when debugging the "reload" module itself, i.e. for cases when reloading might fail due to bugs in reload_modules(). """ missing_modules = {name: module for name, module in modules.items() if name not in sys.modules} if missing_modules: for name, _ in missing_modules: sys.modules[name] = modules print("SL [reload] BUG!", "restored", name) sublime_plugin.reload_plugin(main.__name__)
def ensure_loaded(main, modules): # More simple (comparing to reload_modules(perform_reload=False)) and dumb # approach to ensure all modules are back. Quite useful when debugging the # "reload" module itself, i.e. for cases when reloading might fail due to # bugs in reload_modules(). missing_modules = {name: module for name, module in modules.items() if name not in sys.modules} if missing_modules: for name, module in missing_modules: sys.modules[name] = modules dump("bug! ", "restored ", name) main = sys.modules[settings.PACKAGE_NAME + "." + settings.PACKAGE_MAIN] sublime_plugin.reload_plugin(main.__name__)
def reload_package(pkg_name, dummy=True, verbose=True): if pkg_name not in sys.modules: dprint("error:", pkg_name, "is not loaded.") return if is_dependency(pkg_name): dependencies, packages = resolve_dependencies(pkg_name) else: dependencies = set() packages = {pkg_name} if verbose: dprint("begin", fill='=') all_modules = { module_name: module for pkg_name in dependencies | packages for module_name, module in get_package_modules(pkg_name).items() } # Tell Sublime to unload plugins for pkg_name in packages: for plugin in package_plugins(pkg_name): module = sys.modules.get(plugin) if module: sublime_plugin.unload_module(module) # Unload modules for module_name in all_modules: sys.modules.pop(module_name) # Reload packages try: with intercepting_imports(all_modules, verbose), importing_fromlist_aggresively(all_modules): for pkg_name in packages: for plugin in package_plugins(pkg_name): sublime_plugin.reload_plugin(plugin) except Exception: dprint("reload failed.", fill='-') reload_missing(all_modules, verbose) raise if dummy: load_dummy(verbose) if verbose: dprint("end", fill='-')
def ensure_loaded(main, modules): # More simple (comparing to reload_modules(perform_reload=False)) and dumb # approach to ensure all modules are back. Quite useful when debugging the # "reload" module itself, i.e. for cases when reloading might fail due to # bugs in reload_modules(). missing_modules = { name: module for name, module in modules.items() if name not in sys.modules } if missing_modules: for name, module in missing_modules: sys.modules[name] = modules dump("bug! ", "restored ", name) main = sys.modules[settings.PACKAGE_NAME + "." + settings.PACKAGE_MAIN] sublime_plugin.reload_plugin(main.__name__)
def plugin_loaded(): """Apply patches.""" # Force a reload of the plugin and do the patching here. The # package_resources module reloads the package_resource_viewer module on # the first start which would otherwise mess with the patching. sublime_plugin.reload_plugin( "PackageResourceViewer.package_resource_viewer") from PackageResourceViewer import package_resource_viewer @patch(package_resource_viewer.PackageResourceViewerBase) def insert_text(patch, self, content, view): """Call the original method and detect the indentation.""" patch.original(self, content, view) if not view.is_loading(): view.run_command("detect_indentation") apply_patches(__name__)
def reload(self, force=False): if(force): def _reload(): sublime.active_window().active_view().run_command('save') sublime.active_window().run_command('close') sublime.active_window().open_file(__file__) sublime.set_timeout(_reload, 200) else: sublime.set_timeout(lambda:sublime_plugin.reload_plugin('caniuse_local.v8'),200)
def on_post_save(self, view): """tests if one of the files of plugin was saved ! """ in_dev_step = 'CommentCalls' in view.settings().get('development') # in current user's preferences # print("Before test...") # the catalog of the plugin's modules plugin_files = [ "modules/xili_mod_calls_settings.py", "modules/xili_mod_comment_anonym.py", "modules/xili_mod_comment_apply.py", "modules/xili_mod_comment_do.py", "modules/xili_mod_comment_class.py", "modules/xili_mod_data.py", "modules/xili_mod_calls_select.py" ] if in_dev_step and view.file_name().endswith(".py"): for plugin_file in plugin_files: if view.file_name().endswith(plugin_file): print("After saving this file (view): ", view.file_name()) sublime_plugin.reload_plugin('CommentCalls.xili_comment_calls') # the root of this plugin
def run(self, package_name, source, items): # Reload current file first if not in root dir if os.path.dirname(source): if sublime.version()[0] == "3": modulename = package_name + "." + ( source.replace(os.sep, ".")[:-3] if source[-11:] != "__init__.py" else source.replace(os.sep, ".")[:-12]) else: modulename = os.path.join(package_dir, source) # Reload the file sublime_plugin.reload_plugin(modulename) print("Package Reloader - Reloading %s" % package_name) # Load modules for item in items: if sublime.version()[0] == "3": modulename = package_name + "." + ( item.replace("/", ".")[:-3] if item[-11:] != "__init__.py" else item.replace("/", ".")[:-12]) else: modulename = os.path.join(package_dir, item) sublime_plugin.reload_plugin(modulename) # Clean up multiple callbacks for key, value in sublime_plugin.all_callbacks.items(): items = {} for item in value: name = item.__module__ + '.' + item.__class__.__name__ # Save item connected with the name if name in items: items[name] += [item] else: items[name] = [item] sublime_plugin.all_callbacks[key] = [ value[0] for key, value in items.items() ]
def reload(self, main, scripts, folders, times): if int(sublime.version()) > 3: main = sublime.expand_variables(main, sublime.active_window() \ .extract_variables()) base_path = os.path.dirname(main) pck_name = os.path.basename(base_path) for folder in folders: sys.path.append(os.path.join(base_path, folder)) for item in os.listdir(os.path.join(base_path, folder)): root, ext = os.path.splitext(item) if (os.path.isfile(os.path.join(base_path, folder, item)) and ext == '.py' and root != '__init__'): module = '.'.join( [pck_name, folder, os.path.splitext(item)[0]]) sublime_plugin.reload_plugin(module) sys.path.pop() for script in scripts: module = pck_name + '.' + \ (script[:-3] if script.endswith('.py') else script) sublime_plugin.reload_plugin(module) module = sys.modules[pck_name + '.' + os.path.splitext(os.path.basename(main))[0]] sublime_plugin.reload_plugin(module.__name__) if times > 1: return self.reload(main, scripts, folders, times - 1)
def plugin_loaded(): # The user needs to be able to set the location of their XML catalog files # in the .sublime-settings file of this plugin. # # Unfortunately, the XML_CATALOG_FILES environment variable must be set # *before* lxml is imported. The sublime.load_settings() method in turn # works only *after* plugins have finished loading. # # As a workaround, we'll wait until this plugin has finished loading and # only then load the files that import and use lxml. sublime_plugin.reload_plugin("%s.impl.plugin" % constants.PLUGIN_NAME) sublime_plugin.reload_plugin("%s.impl.validator" % constants.PLUGIN_NAME) sublime_plugin.reload_plugin("%s.impl.formatter" % constants.PLUGIN_NAME)
def reload(self): sublime_plugin.reload_plugin("SublimeJS.v8")
import os import re import sys import pprint import textwrap import unittest import sublime_plugin from AmxxEditor.AmxxEditor import PawnParse from AmxxEditor.AmxxEditor import Node # Import and reload the debugger sublime_plugin.reload_plugin("AmxxEditor.AmxxEditor") from debug_tools import getLogger log = getLogger(__name__.split('.')[-1], 127) def get_relative_path(relative_path, script_file): """ Computes a relative path for a file on the same folder as this class file declaration. https://stackoverflow.com/questions/4381569/python-os-module-open-file-above-current-directory-with-relative-path """ basepath = os.path.dirname(script_file) filepath = os.path.abspath(os.path.join(basepath, relative_path)) return filepath
def enable_plugins(): """Reenable disabled plugins by anaconda """ for plugin in DISABLED_PLUGINS: sublime_plugin.reload_plugin(plugin)
from sublime_plugin import reload_plugin # This needs to be done to initialise sublime plugin # commands like TextCommand and WindowCommand because # sublime only loads .py files from the root package. reload_plugin('User.tests.commands')
def run(self, name): sublime_plugin.reload_plugin(name)
import sublime import sublime_plugin import unittest import textwrap import OverrideCommitCompletion.tests.testing.utilities sublime_plugin.reload_plugin( "OverrideCommitCompletion.tests.testing.utilities") def wrap_text(text): return textwrap.dedent(text).strip(" ").strip("\n") class OverrideCommitCompletionUnitTests(unittest.TestCase, sublime_plugin.EventListener): is_running_unit_tests = False @classmethod def setUp(self): self.maxDiff = None OverrideCommitCompletion.tests.testing.utilities.OverrideCommitCompletionUnitTestsEventListener.is_running_unit_tests = True # Create a new Sublime Text view to perform the Unit Tests self.view = sublime.active_window().new_file() self.view.set_syntax_file("Packages/Text/Plain text.tmLanguage") # make sure we have a window to work with settings = sublime.load_settings("Preferences.sublime-settings") settings.set("close_windows_when_empty", False)
import os import io import sys import unittest try: import sublime_plugin import debug_tools.logger from debug_tools import utilities from debug_tools import testing_utilities # Import and reload the debugger sublime_plugin.reload_plugin("debug_tools.logger") sublime_plugin.reload_plugin("debug_tools.utilities") sublime_plugin.reload_plugin("debug_tools.testing_utilities") except ImportError: def assert_path(module): if module not in sys.path: sys.path.append(module) # Import the debug tools assert_path( os.path.join( os.path.dirname( os.path.dirname(os.path.dirname(os.path.realpath(__file__)))),
def _plugin_loaded(): if Reload_Plugins: logger.info('reloading packages that reference MTFocusCommon') for plugin in plugins_load_order: if plugin in sys.modules: sublime_plugin.reload_plugin(plugin)
def run(self, edit): # C:\Users\vova\AppData\Roaming\Sublime Text 2\Packages\Chains\ sublime_plugin.reload_plugin( 'C:\\Users\\vova\\AppData\\Roaming\\Sublime Text 2\\Packages\\Chains\\Chains.py' )
def run(self, edit): sublime_plugin.reload_plugin("CompleteMagic.CompleteMagic")
def reload_modules(main, modules, perform_reload=True): """Implements the machinery for reloading a given plugin module.""" # # Here's the approach in general: # # - Hide package modules from the sys.modules temporarily; # # - Install a special import hook onto sys.meta_path; # # - Call sublime_plugin.reload_plugin(), which imports the main # module under the hood, triggering the hook; # # - The hook, instead of creating a new module object, peeks the saved # one and reloads it. Once the module encounters an import statement # requesting another module, not yet reloaded, the hook reenters and # processes that new module recursively, then get back to the previous # one, and so on. # # This makes the modules reload in the very same order as they were loaded # initially, as if they were imported from scratch. # if perform_reload: sublime_plugin.unload_module(main) # Insert the main module at the beginning to make the reload # order be as close to the order of the "natural" import as possible. module_names = [main.__name__] + sorted( name for name in modules if name != main.__name__) # First, remove all the loaded modules from the sys.modules cache, # otherwise the reloading hook won't be called. loaded_modules = dict(sys.modules) for name in loaded_modules: if name in modules: del sys.modules[name] stack_meter = StackMeter() @FilteringImportHook.when(condition=lambda name: name in modules) def module_reloader(name): module = modules[name] sys.modules[name] = module # restore the module back if perform_reload: with stack_meter as depth: dump("reloading ", ("| " * depth) + "|-- ", name) try: return module.__loader__.load_module(name) except: if name in sys.modules: del sys.modules[name] # to indicate an error raise else: if name not in loaded_modules: dump("no reload ", "-- ", name) return module with intercepting_imports(module_reloader), \ importing_fromlist_aggresively(modules): # Now, import all the modules back, in order, starting with the main # module. This will reload all the modules directly or indirectly # referenced by the main one, i.e. usually most of our modules. log(sep="", end="") sublime_plugin.reload_plugin(main.__name__) # Be sure to bring back *all* the modules that used to be loaded, not # only these imported through the main one. Otherwise, some of them # might end up being created from scratch as new module objects in # case of being imported after detaching the hook. In general, most of # the imports below (if not all) are no-ops though. for name in module_names: importlib.import_module(name)
def run(self): sublime_plugin.reload_plugin("Zorgmode") self.window.run_command("unit_testing", {"package": "Zorgmode"})
def run(self, edit): # C:\Users\vova\AppData\Roaming\Sublime Text 2\Packages\Chains\ sublime_plugin.reload_plugin('C:\\Users\\vova\\AppData\\Roaming\\Sublime Text 2\\Packages\\Chains\\Chains.py')
def reload_modules(main, modules, perform_reload=True): """Implements the machinery for reloading a given plugin module.""" # # Here's the approach in general: # # - Hide package modules from the sys.modules temporarily; # # - Install a special import hook onto sys.meta_path; # # - Call sublime_plugin.reload_plugin(), which imports the main # module under the hood, triggering the hook; # # - The hook, instead of creating a new module object, peeks the saved # one and reloads it. Once the module encounters an import statement # requesting another module, not yet reloaded, the hook reenters and # processes that new module recursively, then get back to the previous # one, and so on. # # This makes the modules reload in the very same order as they were loaded # initially, as if they were imported from scratch. # if perform_reload: sublime_plugin.unload_module(main) # Insert the main module at the beginning to make the reload # order be as close to the order of the "natural" import as possible. module_names = [main.__name__] + sorted(name for name in modules if name != main.__name__) # First, remove all the loaded modules from the sys.modules cache, # otherwise the reloading hook won't be called. loaded_modules = dict(sys.modules) for name in loaded_modules: if name in modules: del sys.modules[name] stack_meter = StackMeter() @FilteringImportHook.when(condition=lambda name: name in modules) def module_reloader(name): module = modules[name] sys.modules[name] = module # restore the module back if perform_reload: with stack_meter as depth: dump("reloading ", ("| " * depth) + "|-- ", name) try: return module.__loader__.load_module(name) except: if name in sys.modules: del sys.modules[name] # to indicate an error raise else: if name not in loaded_modules: dump("no reload ", "-- ", name) return module with intercepting_imports(module_reloader), \ importing_fromlist_aggresively(modules): # Now, import all the modules back, in order, starting with the main # module. This will reload all the modules directly or indirectly # referenced by the main one, i.e. usually most of our modules. log(sep="", end="") sublime_plugin.reload_plugin(main.__name__) # Be sure to bring back *all* the modules that used to be loaded, not # only these imported through the main one. Otherwise, some of them # might end up being created from scratch as new module objects in # case of being imported after detaching the hook. In general, most of # the imports below (if not all) are no-ops though. for name in module_names: importlib.import_module(name)
def reload_package(pkg_name, dummy=True, verbose=True, then=None): if pkg_name not in sys.modules: dprint("error:", pkg_name, "is not loaded.") return if is_dependency(pkg_name): dependencies, packages = resolve_dependencies(pkg_name) else: dependencies = set() packages = {pkg_name} if verbose: dprint("begin", fill='=') all_modules = { module_name: module for pkg_name in dependencies | packages for module_name, module in get_package_modules(pkg_name).items() } plugins = [ plugin for pkg_name in packages for plugin in package_plugins(pkg_name) ] # Tell Sublime to unload plugins for plugin in plugins: module = sys.modules.get(plugin) if module: sublime_plugin.unload_module(module) # Unload modules for module_name in all_modules: sys.modules.pop(module_name) # Reload packages try: with intercepting_imports( all_modules, verbose), importing_fromlist_aggressively(all_modules): for plugin in plugins: sublime_plugin.reload_plugin(plugin) except Exception: dprint("reload failed.", fill='-') # Rollback modules for name, module in all_modules.items(): sys.modules[name] = module # Try reloading again to get the commands back. Here esp. the # reload command itself. for plugin in plugins: sublime_plugin.reload_plugin(plugin) traceback.print_exc() print( '--- Reloading GitSavvy failed. Restarting Sublime is highly recommended. ---' ) sublime.active_window().status_message( 'GitSavvy reloading 💣ed. 😒.') return if dummy: load_dummy(verbose) if verbose: dprint("end", fill='-') if then: then() sublime.active_window().status_message('GitSavvy has 🙌 reloaded.')
def plugin_loaded(): for mod in submodules: sublime_plugin.reload_plugin('Golang.go.' + mod) Golang.go.log.DEBUG = '*' Golang.go.log.TRACE = True