Skip to content

Commit 7adde96

Browse files
DeathAxedeathaxe
authored andcommitted
Improve submodule reloading
This commit provides a change to clear the sys.modules cache before importing any local package/module. This causes python to reload all modules as soon as the FileManager.py is reloaded/imported by ST. It is to ensure fluent plugin updates even if global variables or module names change. Nothing else is needed to instantly update all submodules during development but saving the FileManager.py This strategy is used by `A File Icon` and `GitGutter` for instance.
1 parent 5bab357 commit 7adde96

1 file changed

Lines changed: 10 additions & 30 deletions

File tree

FileManager.py

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
# -*- encoding: utf-8 -*-
22
import os
3-
import imp
43
import sys
54

65
import sublime
76
import sublime_plugin
87

8+
# Clear module cache to force reloading all modules of this package.
9+
prefix = __package__ + "." # don't clear the base package
10+
for module_name in [
11+
module_name
12+
for module_name in sys.modules
13+
if module_name.startswith(prefix) and module_name != __name__
14+
]:
15+
del sys.modules[module_name]
16+
prefix = None
17+
918
from .libs.sublimefunctions import *
1019
from .commands.copy import FmCopyCommand
1120
from .commands.create import FmCreaterCommand, FmCreateCommand
@@ -21,20 +30,6 @@
2130
from .commands.open_terminal import FmOpenTerminalCommand
2231
from .commands.rename import FmRenameCommand, FmRenamePathCommand
2332

24-
BASE_NAME = os.path.dirname(__file__)
25-
26-
27-
def _reload(file):
28-
if file.endswith(".pyc"):
29-
file = file[:-1]
30-
file = file[:-3]
31-
module = sys.modules.get(file.replace(os.path.sep, "."))
32-
if module:
33-
imp.reload(module)
34-
35-
36-
# auto reload sub files - for dev
37-
3833

3934
def plugin_loaded():
4035
settings = get_settings()
@@ -58,21 +53,6 @@ def plugin_loaded():
5853
)
5954

6055

61-
class FmDevListener(sublime_plugin.EventListener):
62-
def on_post_save(self, view):
63-
"""Reload FileManager
64-
To use this, you need to have this plugin:
65-
https://github.com/math2001/sublime-plugin-reloader"""
66-
if not (
67-
os.path.dirname(__file__) in view.file_name()
68-
and view.file_name().endswith(".py")
69-
):
70-
return
71-
sublime.run_command(
72-
"reload_plugin", {"main": __file__, "folders": ["commands", "libs"],},
73-
)
74-
75-
7656
class FmEditReplace(sublime_plugin.TextCommand):
7757
def run(self, edit, **kwargs):
7858
kwargs.get("view", self.view).replace(

0 commit comments

Comments
 (0)